From 3e03e121e63ccaedc0b24d5254613382a15e23bc Mon Sep 17 00:00:00 2001 From: RedFlames Date: Mon, 20 Feb 2023 18:15:22 +0100 Subject: [PATCH] Hopefully got all of the new websocket stuff working in the frontend, skipping many of the API polls --- .editorconfig | 4 ++ .../Content/frontend/cp/js/panels/channels.js | 48 +++++++++++++++++++ .../Content/frontend/cp/js/panels/players.js | 19 ++++++++ CelesteNet.Server.FrontendModule/Frontend.cs | 10 ++-- CelesteNet.Server/Channels.cs | 11 +++-- 5 files changed, 83 insertions(+), 9 deletions(-) diff --git a/.editorconfig b/.editorconfig index 9d69b3a9..5347b97b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,6 +4,10 @@ root = true indent_style = space indent_size = 4 +[*.js] +indent_style = space +indent_size = 2 + # https://kent-boogaart.com/blog/editorconfig-reference-for-c-developers csharp_indent_block_contents = true diff --git a/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/channels.js b/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/channels.js index c2a3e8b4..3e72ca5c 100644 --- a/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/channels.js +++ b/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/channels.js @@ -29,11 +29,59 @@ export class FrontendChannelsPanel extends FrontendBasicPanel { this.ep = "/api/channels"; /** @type {ChannelData[]} */ this.data = []; + frontend.sync.register("chan_create", data => this.channelAdd(data.Channel, data.Count)); + frontend.sync.register("chan_remove", data => this.channelRemove(data.ID, data.Count)); + frontend.sync.register("chan_move", data => this.channelMove(data.SessionID, data.fromID, data.toID)); + } + + channelMove(sid, fromID, toID) { + if (fromID != null) { + let from = this.data.find(c => c.ID == fromID ); + if (from) { + let idx = from.Players.indexOf(sid); + if (idx != -1) + from.Players.splice(idx, 1); + } + } + + if (toID != null) { + let to = this.data.find(c => c.ID == toID ); + if (to) + to.Players.push(sid); + } + + this.rebuildList(); + this.render(null); + } + + channelAdd(c, total) { + this.data.push(c); + if (this.data.length == total) { + this.rebuildList(); + this.render(null); + } else { + this.refresh(); + } + } + + channelRemove(cID, total) { + let idx = this.data.findIndex(c => c.ID == cID ); + if (idx != -1) + this.data.splice(idx, 1); + if (this.data.length == total) { + this.rebuildList(); + this.render(null); + } else { + this.refresh(); + } } async update() { this.data = await fetch(this.ep).then(r => r.json()); + this.rebuildList(); + } + rebuildList() { // @ts-ignore this.list = this.data.map(c => el => { el = mdcrd.list.item(el => rd$(el)` diff --git a/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/players.js b/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/players.js index 1f4437ae..325342db 100644 --- a/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/players.js +++ b/CelesteNet.Server.FrontendModule/Content/frontend/cp/js/panels/players.js @@ -37,6 +37,22 @@ export class FrontendPlayersPanel extends FrontendBasicPanel { /** @type {PlayerData[]} */ this.data = []; this.input = null; + frontend.sync.register("sess_join", data => this.playerJoin(data)); + frontend.sync.register("sess_leave", data => this.playerLeave(data)); + } + + playerJoin(data) { + this.data.push(data); + this.rebuildList(); + this.render(null); + } + + playerLeave(data) { + let idx = this.data.findIndex(p => p.ID == data.ID); + if (idx != -1) + this.data.splice(idx, 1); + this.rebuildList(); + this.render(null); } render(el) { @@ -63,7 +79,10 @@ export class FrontendPlayersPanel extends FrontendBasicPanel { async update() { this.data = await fetch(this.ep).then(r => r.json()); + this.rebuildList(); + } + rebuildList() { // @ts-ignore this.input = this.elInput.getElementsByTagName("input")[0]; let filter = this.input.value.trim().toLowerCase(); diff --git a/CelesteNet.Server.FrontendModule/Frontend.cs b/CelesteNet.Server.FrontendModule/Frontend.cs index 14880026..20ece936 100644 --- a/CelesteNet.Server.FrontendModule/Frontend.cs +++ b/CelesteNet.Server.FrontendModule/Frontend.cs @@ -138,14 +138,14 @@ private void OnConnect(CelesteNetServer server, CelesteNetConnection con) { private void OnSessionStart(CelesteNetPlayerSession session) { BroadcastCMD(false, "sess_join", PlayerSessionToFrontend(session, shorten: true)); TryBroadcastUpdate(Settings.APIPrefix + "/status"); - TryBroadcastUpdate(Settings.APIPrefix + "/players"); + //TryBroadcastUpdate(Settings.APIPrefix + "/players"); session.OnEnd += OnSessionEnd; } private void OnSessionEnd(CelesteNetPlayerSession session, DataPlayerInfo? lastPlayerInfo) { BroadcastCMD(false, "sess_leave", PlayerSessionToFrontend(session, shorten: true)); TryBroadcastUpdate(Settings.APIPrefix + "/status"); - TryBroadcastUpdate(Settings.APIPrefix + "/players"); + //TryBroadcastUpdate(Settings.APIPrefix + "/players"); } private void OnDisconnect(CelesteNetServer server, CelesteNetConnection con, CelesteNetPlayerSession? session) { @@ -157,12 +157,12 @@ private void OnBroadcastChannels(Channels obj) { TryBroadcastUpdate(Settings.APIPrefix + "/channels"); } - private void OnChannelMove(CelesteNetPlayerSession session, Channel? from, Channel to) { - BroadcastCMD(false, "chan_move", new { session.SessionID, session.UID, fromID = from?.ID, toID = to.ID }); + private void OnChannelMove(CelesteNetPlayerSession session, Channel? from, Channel? to) { + BroadcastCMD(false, "chan_move", new { session.SessionID, session.UID, fromID = from?.ID, toID = to?.ID }); } private void OnCreateChannel(Channel channel, int total) { - BroadcastCMD(false, "chan_create", new { channel.Name, channel.ID, channel.IsPrivate, Count = total }); + BroadcastCMD(false, "chan_create", new { Channel = new { channel.ID, channel.Name, channel.IsPrivate, Players = channel.Players.Select(p => p.SessionID) }, Count = total }); } private void OnRemoveChannel(string name, uint id, int total) { diff --git a/CelesteNet.Server/Channels.cs b/CelesteNet.Server/Channels.cs index 597f91e1..a8e46034 100644 --- a/CelesteNet.Server/Channels.cs +++ b/CelesteNet.Server/Channels.cs @@ -49,6 +49,7 @@ public bool SessionStartupMove(CelesteNetPlayerSession session) { return curr != prev; } else { Default.Add(session); + OnMove?.Invoke(session, null, Default); BroadcastList(); } return false; @@ -90,14 +91,15 @@ public void SendListTo(CelesteNetPlayerSession session) { // On removal this only gives the Name & ID of the removed channel, plus total count as above public Action? OnRemove; - public Action? OnMove; + public Action? OnMove; - public void BroadcastList() { + public void BroadcastList(bool invokeAction = false) { using (ListSnapshot snapshot = All.ToSnapshot()) foreach (Channel c in snapshot) c.RemoveStale(); - OnBroadcastList?.Invoke(this); + if (invokeAction) + OnBroadcastList?.Invoke(this); lock (All) using (Server.ConLock.R()) @@ -226,15 +228,16 @@ public void Remove(CelesteNetPlayerSession session) { if (Players.Count > 0) return; - Ctx.OnRemove?.Invoke(Name, ID, Ctx.All.Count); Ctx.All.Remove(this); Ctx.ByName.Remove(Name); Ctx.ByID.Remove(ID); + Ctx.OnRemove?.Invoke(Name, ID, Ctx.All.Count); } } private void RemoveByDC(CelesteNetPlayerSession session, DataPlayerInfo? lastInfo) { Remove(session); + Ctx.OnMove?.Invoke(session, this, null); Ctx.BroadcastList(); }