Skip to content

Commit ff94fa7

Browse files
committed
online chat
1 parent a53e06f commit ff94fa7

16 files changed

Lines changed: 693 additions & 294 deletions

Zero-K.info/Controllers/LobbyController.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using System.Web;
@@ -152,5 +153,98 @@ public ActionResult ChatHistory(ChatHistoryModel model) {
152153
return View("LobbyChatHistory", model);
153154
}
154155

156+
public class ChatModel
157+
{
158+
public string Channel { get; set; }
159+
public string User { get; set; }
160+
public string Message { get; set; }
161+
public IQueryable<LobbyChatHistory> Data = new List<LobbyChatHistory>().AsQueryable();
162+
}
163+
164+
[Auth]
165+
public ActionResult ChatNotification(ChatModel model)
166+
{
167+
model = model ?? new ChatModel();
168+
169+
var db = new ZkDataContext();
170+
var acc = db.Accounts.Where(x => x.AccountID == Global.AccountID).First();
171+
var ret = db.LobbyChatHistories.AsQueryable();
172+
ret = ret.Where(x => x.Target == Global.Account.Name && x.SayPlace == SayPlace.User && x.Time > acc.LastChatRead);
173+
model.Data = ret.OrderByDescending(x => x.Time).ToList().AsQueryable();
174+
model.Channel = "";
175+
acc.LastChatRead = DateTime.UtcNow;
176+
db.SaveChanges();
177+
178+
return PartialView("ChatNotification", model);
179+
}
180+
[Auth]
181+
public async Task<ActionResult> ChatMessages(ChatModel model)
182+
{
183+
model = model ?? new ChatModel();
184+
185+
var db = new ZkDataContext();
186+
var ret = db.LobbyChatHistories.AsQueryable();
187+
bool isMuted = Punishment.GetActivePunishment(Global.AccountID, Request.UserHostAddress, 0, null, x => x.BanMute) != null;
188+
if (!string.IsNullOrEmpty(model.Channel))
189+
{
190+
// only show allowed channels
191+
if (!Global.Server.ChannelManager.CanJoin(Global.Account, model.Channel)) return PartialView("LobbyChatMessages", model);
192+
if (!String.IsNullOrEmpty(model.Message) && !isMuted)
193+
{
194+
await Global.Server.GhostSay(new Say()
195+
{
196+
IsEmote = false,
197+
Place = SayPlace.Channel,
198+
Ring = false,
199+
Source = SaySource.Zk,
200+
Target = model.Channel,
201+
Text = model.Message,
202+
Time = DateTime.UtcNow,
203+
User = Global.Account.Name,
204+
});
205+
}
206+
ret = ret
207+
.Where(x => x.Target == model.Channel && x.SayPlace == SayPlace.Channel)
208+
.OrderByDescending(x => x.Time).Take(200);
209+
}
210+
else if (!string.IsNullOrEmpty(model.User))
211+
{
212+
if (!String.IsNullOrEmpty(model.Message) && !isMuted)
213+
{
214+
await Global.Server.GhostSay(new Say()
215+
{
216+
IsEmote = false,
217+
Place = SayPlace.User,
218+
Ring = false,
219+
Source = SaySource.Zk,
220+
Target = model.User,
221+
Text = model.Message,
222+
Time = DateTime.UtcNow,
223+
User = Global.Account.Name,
224+
});
225+
}
226+
//Users can abuse rename to gain access to other users PMs, it's a feature
227+
ret = ret
228+
.Where(x => (x.User == model.User && x.Target == Global.Account.Name || x.User == Global.Account.Name && x.Target == model.User) && x.SayPlace == SayPlace.User)
229+
.OrderByDescending(x => x.Time);
230+
}
231+
else
232+
{
233+
return PartialView("LobbyChatMessages", model);
234+
}
235+
236+
model.Data = ret;
237+
model.Message = "";
238+
239+
return PartialView("LobbyChatMessages", model);
240+
}
241+
[Auth]
242+
public ActionResult Chat(ChatModel model)
243+
{
244+
model = model ?? new ChatModel();
245+
246+
return View("LobbyChat", model);
247+
}
248+
155249
}
156250
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@using ZeroKWeb
2+
@using ZkData
3+
@model ZeroKWeb.Controllers.LobbyController.ChatModel
4+
5+
@{
6+
Page.Title = "Chat";
7+
}
8+
9+
<h1>@Page.Title</h1>
10+
<br />
11+
@{
12+
var ajaxOptions = Global.GetAjaxOptions("grid");
13+
ajaxOptions.OnSuccess += ";document.getElementById('chatbox').value = ''";
14+
}
15+
@using (Ajax.BeginForm("ChatMessages", "Lobby", null, ajaxOptions, new { id = "form" }))
16+
{
17+
<table>
18+
<tr>
19+
<td>Channel</td>
20+
<td>@Html.TextBoxFor(x => x.Channel)</td>
21+
</tr>
22+
<tr><td>User</td><td>@Html.TextBoxFor(x => x.User, new { data_autocomplete = Url.Action("Users", "Autocomplete"), data_autocomplete_action = "submit" })</td></tr>
23+
</table>
24+
<input type="submit" />
25+
26+
<div id="grid">
27+
Loading chat messages...
28+
</div>
29+
<table class="width-100">
30+
<tr>
31+
<td>
32+
@Html.TextBoxFor(x => x.Message, new { Class = "width-100", Id = "chatbox", Style = "box-sizing: border-box;padding: 5px;" })
33+
</td>
34+
</tr>
35+
</table>
36+
<br />
37+
}
38+
39+
<script type="text/javascript">
40+
function updateChat() {
41+
if (document.getElementById('chatbox').value == '') {
42+
$('form#form').trigger('submit');
43+
}
44+
}
45+
setInterval(updateChat, 5000);
46+
updateChat();
47+
</script>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@using ZeroKWeb
2+
@using ZkData
3+
@model ZeroKWeb.Controllers.LobbyController.ChatModel
4+
5+
@{
6+
string gridTitle = "Private Messages";
7+
if (!String.IsNullOrEmpty(Model.User))
8+
{
9+
gridTitle = "@" + Model.User;
10+
}
11+
if (!String.IsNullOrEmpty(Model.Channel))
12+
{
13+
gridTitle = "#" + Model.Channel;
14+
}
15+
var chatGrid = new UniGrid<LobbyChatHistory>(Model.Data, gridTitle, "chatgrid");
16+
chatGrid.AddCol("Time", x => Html.PrintDate(x.Time)).SetSort(x => x.Time).SetWidth("120px");
17+
chatGrid.AddCol("User", UserChatCol).SetSort(x => x.User).SetWidth("200px");
18+
chatGrid.AddCol("Text", TxtChatCol).SetSort(x => x.Text);
19+
}
20+
21+
@helper TxtChatCol(LobbyChatHistory x)
22+
{
23+
if (x.IsEmote)
24+
{
25+
<span style="color: violet">@x.Text</span>
26+
}
27+
else
28+
{
29+
<span>@x.Text</span>
30+
}
31+
}
32+
33+
@helper UserChatCol(LobbyChatHistory x)
34+
{
35+
var acc = Account.AccountByName(new ZkDataContext(), x.User);
36+
if (acc != null)
37+
{
38+
@Html.PrintAccount(acc)
39+
}
40+
else
41+
{
42+
<span>@x.User</span>
43+
}
44+
}
45+
@GridHelpers.RenderTable(chatGrid)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@using ZeroKWeb
2+
@using ZkData
3+
@model ZeroKWeb.Controllers.LobbyController.ChatModel
4+
@if (Model.Data.Count() > 0)
5+
{
6+
<div class="js_dialog" title="You have unread messages">
7+
@Html.Partial("LobbyChatMessages")
8+
</div>
9+
}

0 commit comments

Comments
 (0)