Skip to content

Commit

Permalink
Hopefully got all of the new websocket stuff working in the frontend,…
Browse files Browse the repository at this point in the history
… skipping many of the API polls
  • Loading branch information
RedFlames committed Feb 20, 2023
1 parent d0f0afd commit 3e03e12
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions CelesteNet.Server.FrontendModule/Frontend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions CelesteNet.Server/Channels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public bool SessionStartupMove(CelesteNetPlayerSession session) {
return curr != prev;
} else {
Default.Add(session);
OnMove?.Invoke(session, null, Default);
BroadcastList();
}
return false;
Expand Down Expand Up @@ -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<string, uint, int>? OnRemove;

public Action<CelesteNetPlayerSession, Channel?, Channel>? OnMove;
public Action<CelesteNetPlayerSession, Channel?, Channel?>? OnMove;

public void BroadcastList() {
public void BroadcastList(bool invokeAction = false) {
using (ListSnapshot<Channel> snapshot = All.ToSnapshot())
foreach (Channel c in snapshot)
c.RemoveStale();

OnBroadcastList?.Invoke(this);
if (invokeAction)
OnBroadcastList?.Invoke(this);

lock (All)
using (Server.ConLock.R())
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 3e03e12

Please sign in to comment.