Skip to content

Commit

Permalink
alternative performance fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
DeinFreund committed May 17, 2020
1 parent 95affe3 commit 5d70725
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Zero-K.info/Controllers/LobbyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public async Task<ActionResult> ChatMessages(ChatModel model)
model = model ?? new ChatModel();

var db = new ZkDataContext();
var ret = db.LobbyChatHistories.AsQueryable();
bool isMuted = Punishment.GetActivePunishment(Global.AccountID, Request.UserHostAddress, 0, null, x => x.BanMute) != null;
if (!string.IsNullOrEmpty(model.Channel))
{
Expand All @@ -203,9 +202,11 @@ await Global.Server.GhostSay(new Say()
User = Global.Account.Name,
});
}
ret = ret
.Where(x => x.Target == model.Channel && x.SayPlace == SayPlace.Channel)
.OrderByDescending(x => x.LobbyChatHistoryID).Take(200);
string channelName = model.Channel;
model.Data = db.LobbyChatHistories
.SqlQuery("SELECT TOP 30 * FROM [dbo].[LobbyChatHistories] WHERE [Target] = {0} AND [SayPlace] = {1} AND [Time] > {2} ORDER BY [Time] DESC", channelName, SayPlace.Channel, DateTime.UtcNow.AddDays(-30))
.ToList().OrderBy(x => x.Time).AsQueryable();
//Note if using Take(), it will be slow for uncommon channels like zktourney when ordering by Time and slow for common channels like zk if ordering by ID
}
else if (!string.IsNullOrEmpty(model.User))
{
Expand All @@ -223,17 +224,18 @@ await Global.Server.GhostSay(new Say()
User = Global.Account.Name,
});
}
string otherName = model.User;
string myName = Global.Account.Name;
//Users can abuse rename to gain access to other users PMs, it's a feature
ret = ret
.Where(x => (x.User == model.User && x.Target == Global.Account.Name || x.User == Global.Account.Name && x.Target == model.User) && x.SayPlace == SayPlace.User)
.OrderByDescending(x => x.LobbyChatHistoryID);
model.Data = db.LobbyChatHistories
.Where(x => (x.User == otherName && x.Target == myName || x.User == myName && x.Target == otherName) && x.SayPlace == SayPlace.User)
.OrderByDescending(x => x.Time);
}
else
{
return PartialView("LobbyChatMessages", model);
}

model.Data = ret;
model.Message = "";

return PartialView("LobbyChatMessages", model);
Expand Down

0 comments on commit 5d70725

Please sign in to comment.