Skip to content

Commit

Permalink
wip: socketio changes + some playerlist events
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Nov 29, 2023
1 parent c2ab7a4 commit b9d6318
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 14 deletions.
7 changes: 4 additions & 3 deletions core/components/PlayerlistManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TxAdmin from '@core/txAdmin.js';
import { ServerPlayer } from '@core/playerLogic/playerClasses.js';
import { DatabasePlayerType } from '../PlayerDatabase/databaseTypes';
import consoleFactory from '@extras/console';
import { PlayerDroppedEventType, PlayerJoiningEventType } from '@shared/socketioTypes';
const console = consoleFactory(modulename);


Expand Down Expand Up @@ -106,7 +107,7 @@ export default class PlayerlistManager {
* Returns a specifc ServerPlayer or undefined.
* NOTE: this returns the actual object and not a deep clone!
*/
getOnlinePlayersByLicense(searchLicense: string) {
getOnlinePlayersByLicense(searchLicense: string) {
return this.#playerlist.filter(p => p && p.license === searchLicense && p.isConnected) as ServerPlayer[];
}

Expand All @@ -129,7 +130,7 @@ export default class PlayerlistManager {
ts: Date.now(),
data: { ids: this.#playerlist[payload.id]!.ids }
}], mutex);
this.#txAdmin.webServer.webSocket!.buffer('playerlist', {
this.#txAdmin.webServer.webSocket.buffer<PlayerJoiningEventType>('playerlist', {
mutex,
type: 'playerJoining',
netid: svPlayer.netid,
Expand All @@ -153,7 +154,7 @@ export default class PlayerlistManager {
ts: Date.now(),
data: { reason: payload.reason }
}], mutex);
this.#txAdmin.webServer.webSocket!.buffer('playerlist', {
this.#txAdmin.webServer.webSocket.buffer<PlayerDroppedEventType>('playerlist', {
mutex,
type: 'playerDropped',
netid: this.#playerlist[payload.id]!.netid,
Expand Down
2 changes: 1 addition & 1 deletion core/components/WebServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class WebServer {
private koaCallback: (req: any, res: any) => Promise<void>;
//setupWebSocket
private io: SocketIO;
private webSocket: WebSocket;
public webSocket: WebSocket;
//setupServerCallbacks
private httpServer?: HttpClass.Server;

Expand Down
2 changes: 1 addition & 1 deletion core/components/WebServer/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default class WebSocket {
/**
* Adds data to the buffer
*/
buffer(roomName: RoomNames, data: any) {
buffer<T>(roomName: RoomNames, data: T) {
const room = this.#rooms[roomName];
if (!room) throw new Error('Room not found');

Expand Down
3 changes: 2 additions & 1 deletion core/components/WebServer/wsRooms/playerlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const modulename = 'SocketRoom:Playerlist';
import TxAdmin from "@core/txAdmin";
import { RoomType } from "../webSocket";
import consoleFactory from '@extras/console';
import { FullPlayerlistEventType } from "@shared/socketioTypes";
const console = consoleFactory(modulename);


Expand All @@ -18,6 +19,6 @@ export default (txAdmin: TxAdmin): RoomType => ({
mutex: txAdmin.fxRunner.currentMutex,
type: 'fullPlayerlist',
playerlist: txAdmin.playerlistManager.getPlayerList(),
}];
} satisfies FullPlayerlistEventType];
},
})
1 change: 0 additions & 1 deletion core/components/WebServer/wsRooms/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const getinitialData = (txAdmin: TxAdmin): GlobalStatusType => {
// @ts-ignore simplifying the status enum to a string
discord: txAdmin.discordBot.wsStatus, //no push events, only passively updated
server: {
mutex: txAdmin.fxRunner?.currentMutex,
status: txAdmin.healthMonitor.currentStatus || '??',
process: txAdmin.fxRunner.getStatus(),
instantiated: !!txAdmin.fxRunner.fxChild, //used to disable the control buttons
Expand Down
4 changes: 4 additions & 0 deletions docs/dev_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ Quickies

Bugs
- [ ] when you open the server sheet, the control tooltip shows automatically
- [ ] nui iframe scroll on 1080p screen
- should not scroll even with 7 server menu items
- possible solution would be to hide the zap ad if !isWebInterface
- [ ] no `target="_blank"` will work in NUI (zap ad + support), so either hide the buttons or find a way to open links


=======================================================================
Expand Down
32 changes: 31 additions & 1 deletion panel/src/hooks/playerlist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PlayerlistEventType, PlayerlistPlayerType } from "@shared/socketioTypes";
import { atom, useAtomValue } from "jotai";
import { atom, useAtomValue, useSetAtom } from "jotai";


/**
Expand All @@ -8,3 +8,33 @@ import { atom, useAtomValue } from "jotai";
export const playerlistAtom = atom<PlayerlistPlayerType[]>([]);
export const playerCountAtom = atom((get) => get(playerlistAtom).length);

//NOTE: In the old UI, we didn't store the mutex separately, but as a prop of players
// export const serverMutexAtom = atom<string | null>(null);


/**
* Hooks
*/
export const useProcessPlayerlistEvents = () => {
const setPlayerlist = useSetAtom(playerlistAtom);

return (events: PlayerlistEventType[]) => {
//If there is a fullPlayerlist, skip everything before it
const fullListIndex = events.findIndex(e => e.type === 'fullPlayerlist');
if (fullListIndex > 0) events = events.slice(fullListIndex);

//Process events
for (const event of events) {
if (event.type === 'fullPlayerlist') {
setPlayerlist(event.playerlist);
} else if (event.type === 'playerJoining') {
setPlayerlist((oldList) => [...oldList, event]);
} else if (event.type === 'playerDropped') {
setPlayerlist((oldList) => oldList.filter(p => p.netid !== event.netid));
} else {
console.error('Unknown playerlist event type', event);
}
}
}
};

9 changes: 5 additions & 4 deletions panel/src/layout/MainShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useSetOfflineWarning } from '@/hooks/useWarningBar';
import { pageTitleWatcher } from '@/hooks/pages';
import { useAtomValue } from 'jotai';
import { getSocket } from '@/lib/utils';
import { useProcessPlayerlistEvents } from '@/hooks/playerlist';



Expand All @@ -28,6 +29,7 @@ export default function MainShell() {
const socketStateChangeCounter = useRef(0);
const setIsSocketOffline = useSetOfflineWarning();
const setGlobalStatus = useSetGlobalStatus();
const processPlayerlistEvents = useProcessPlayerlistEvents();

useEffect(() => {
const rooms = window.txConsts.isWebInterface ? ['status', 'playerlist'] : ['status'];
Expand All @@ -43,7 +45,7 @@ export default function MainShell() {
const newId = socketStateChangeCounter.current + 1;
socketStateChangeCounter.current = newId;
setTimeout(() => {
if(socketStateChangeCounter.current === newId){
if (socketStateChangeCounter.current === newId) {
setIsSocketOffline(true);
}
}, 500);
Expand All @@ -59,8 +61,7 @@ export default function MainShell() {
});
socket.on('playerlist', function (playerlistData) {
if (!window.txConsts.isWebInterface) return;
console.log('playerlist', playerlistData);
// processPlayerlistEvents(playerlistData);
processPlayerlistEvents(playerlistData);
});

return () => {
Expand All @@ -78,7 +79,7 @@ export default function MainShell() {
<main className="flex flex-1 min-h-[calc(100vh-5.5rem-1px)]">
<MainRouter />
</main>
<PlayersSidebar />
{window.txConsts.isWebInterface && <PlayersSidebar />}
</div>

<MainSheets />
Expand Down
6 changes: 5 additions & 1 deletion panel/src/layout/PlayersSidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { playerCountAtom } from '@/hooks/playerlist';
import { cn } from '@/lib/utils';
import { useAtomValue } from 'jotai';

type PlayerSidebarProps = {
isSheet?: boolean;
};
export function PlayersSidebar({ isSheet }: PlayerSidebarProps) {
const playerCount = useAtomValue(playerCountAtom);

return (
<aside
className={cn(
Expand All @@ -17,7 +21,7 @@ export function PlayersSidebar({ isSheet }: PlayerSidebarProps) {
text-3xl font-extralight text-center tracking-wider
rounded-xl border border-border bg-card text-card-foreground shadow-sm"
>
PLAYER CNT
PLAYERS: {playerCount}
</div>
<div className="flex justify-center items-center h-[500px]
text-3xl font-extralight text-center tracking-wider
Expand Down
36 changes: 35 additions & 1 deletion shared/socketioTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* Status channel
*/
export type GlobalStatusType = {
discord: false | number;
server: {
mutex: string | null;
status: string;
process: string;
instantiated: boolean;
Expand All @@ -18,3 +20,35 @@ export type GlobalStatusType = {
nextIsTemp: false;
};
}

/**
* Playerlist channel
* TODO: apply those types to the playerlistManager
*/
export type FullPlayerlistEventType = {
mutex: string | null,
type: 'fullPlayerlist',
playerlist: PlayerlistPlayerType[],
}

export type PlayerlistPlayerType = {
netid: number,
displayName: string,
pureName: string,
ids: string[],
license: string | null,
}

export type PlayerDroppedEventType = {
mutex: string,
type: 'playerDropped',
netid: number,
}

export type PlayerJoiningEventType = {
mutex: string,
type: 'playerJoining',
} & PlayerlistPlayerType;


export type PlayerlistEventType = FullPlayerlistEventType | PlayerDroppedEventType | PlayerJoiningEventType;

0 comments on commit b9d6318

Please sign in to comment.