Skip to content

Commit

Permalink
Added host reassignment
Browse files Browse the repository at this point in the history
Allows the host to be reassigned based on who is first in line.
Will end a game if someone disconnects for over 5 seconds.

Need to:
Move people to the end of the line after a game.
Allow people to choose their names.
Make the queue phone friendly.
  • Loading branch information
ymmot239 committed Nov 22, 2024
1 parent 6485c2e commit dab1559
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 23 deletions.
26 changes: 15 additions & 11 deletions src/client/game/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ChessEngine } from "../../common/chess-engine";
import { Move } from "../../common/game-types";
import { NonIdealState, Spinner } from "@blueprintjs/core";
import { AcceptDrawDialog, OfferDrawDialog } from "./draw-dialog";
import { Sidebar } from "../setup/sidebar";

/**
* Creates a MessageHandler function.
Expand Down Expand Up @@ -127,17 +128,20 @@ export function Game(): JSX.Element {
side={side}
setRotation={setRotation}
/>
<div id="body-container">
<ChessboardWrapper
side={side}
chess={chess}
onMove={handleMove}
rotation={rotation ? rotation : 0}
/>
{gameEndDialog}
{gameOfferDialog}
{gameAcceptDialog}
<Outlet />
<Sidebar />
<div className="main-dialog">
<div id="body-container">
<ChessboardWrapper
side={side}
chess={chess}
onMove={handleMove}
rotation={rotation ? rotation : 0}
/>
{gameEndDialog}
{gameOfferDialog}
{gameAcceptDialog}
<Outlet />
</div>
</div>
</>
);
Expand Down
6 changes: 5 additions & 1 deletion src/client/setup/lobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export function Lobby() {
return (
<SetupBase>
<NonIdealState
title="Waiting For Game to Start"
title={
data.clientType === ClientType.CLIENT ?
"Waiting For Host to Start"
: "Waiting in line"
}
icon={<Spinner intent="primary" />}
/>
</SetupBase>
Expand Down
1 change: 0 additions & 1 deletion src/client/setup/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { get, useEffectQuery, useSocket } from "../api";

function getMessageHandler(setQueue: Dispatch<string[]>): MessageHandler {
return (message) => {
console.log("cool message box");
if (message instanceof UpdateQueue) {
console.log(message.queue);
setQueue(message.queue.slice());
Expand Down
59 changes: 54 additions & 5 deletions src/server/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GameFinishedMessage,
GameHoldMessage,
GameInterruptedMessage,
GameStartedMessage,
JoinQueue,
MoveMessage,
UpdateQueue,
Expand All @@ -16,7 +17,7 @@ import {
} from "../../common/message/robot-message";

import { TCPServer } from "./tcp-interface";
import { Difficulty } from "../../common/client-types";
import { ClientType, Difficulty } from "../../common/client-types";
import { RegisterWebsocketMessage } from "../../common/message/message";
import { clientManager, robotManager, socketManager } from "./managers";
import {
Expand All @@ -32,6 +33,7 @@ import { VirtualBotTunnel, virtualRobots } from "../simulator";
import { Position } from "../robot/position";
import { DEGREE } from "../utils/units";
import { PriorityQueue } from "./queue";
import { GameInterruptedReason } from "../../common/game-end-reasons";

export const tcpServer: TCPServer | null =
USE_VIRTUAL_ROBOTS ? null : new TCPServer();
Expand All @@ -48,11 +50,55 @@ const names = new Map<string, string>();
export const websocketHandler: WebsocketRequestHandler = (ws, req) => {
ws.on("close", () => {
socketManager.handleSocketClosed(req.cookies.id);
//wait in case the client is just reloading or disconnected instead of leaving
setTimeout(() => {
if (socketManager.getSocket(req.cookies.id) === undefined) {
//remove the person from the queue to free up space
queue.popInd(queue.find(req.cookies.id));
names.delete(req.cookies.id);
const clientType = clientManager.getClientType(req.cookies.id);

//if the person was a host / client, a new one needs to be reassigned
if (clientManager.isPlayer(req.cookies.id)) {
//clear the existing game
const ids = clientManager.getIds();
if (ids) {
if (
SaveManager.loadGame(req.cookies.id)?.host ===
ids[0]
)
SaveManager.endGame(ids[0], ids[1]);
else SaveManager.endGame(ids[1], ids[0]);
}

gameManager = null;

//remove the old host/client
clientType === ClientType.HOST ?
clientManager.removeHost()
: clientManager.removeClient();

//if there exists someone to take their place
const newPlayer = queue.pop();
if (newPlayer) {
//transfer them from spectator to the newly-opened spot and remove them from queue
clientManager.removeSpectator(newPlayer);
clientManager.assignPlayer(newPlayer);
names.delete(newPlayer);
socketManager.sendToAll(
new GameInterruptedMessage(
GameInterruptedReason.ABORTED,
),
);
}
//else they were a spectator and don't need game notifications anymore
} else {
clientManager.removeSpectator(req.cookies.id);
}

//update the queue and reload all the pages
socketManager.sendToAll(new UpdateQueue([...names.values()]));
socketManager.sendToAll(new GameStartedMessage());
}
}, 2000);
});
Expand All @@ -76,10 +122,13 @@ export const websocketHandler: WebsocketRequestHandler = (ws, req) => {
} else if (message instanceof SetRobotVariableMessage) {
doSetRobotVariable(message);
} else if (message instanceof JoinQueue) {
const previous = queue.find(req.cookies.id);
names.set(req.cookies.id, message.queue);
previous ? queue : queue.insert(req.cookies.id, 0);
socketManager.sendToAll(new UpdateQueue([...names.values()]));
if (!clientManager.isPlayer(req.cookies.id)) {
if (queue.find(req.cookies.id) === undefined) {
queue.insert(req.cookies.id, 0);
}
names.set(req.cookies.id, message.queue);
socketManager.sendToAll(new UpdateQueue([...names.values()]));
}
}
});
};
Expand Down
16 changes: 16 additions & 0 deletions src/server/api/client-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ export class ClientManager {
}
}

public removeHost(): void {
this.hostId = undefined;
}

public removeClient(): void {
this.clientId = undefined;
}

public removeSpectator(id: string): void {
this.spectatorIds.delete(id);
}

public isPlayer(id: string): boolean {
return id === this.hostId || id === this.clientId;
}

public getIds(): undefined | string[] {
if (this.hostId && this.clientId) {
return [this.hostId, this.clientId];
Expand Down
14 changes: 9 additions & 5 deletions src/server/api/queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ export class PriorityQueue<T> {
return this.data.length === 0 ? undefined : this.data[0][1];
}
public pop(): T | undefined {
return this.data !== undefined ? this.data.pop()?.[1] : undefined;
return this.data !== undefined ? this.data.shift()?.[1] : undefined;
}
public popInd(num: number | undefined): T {
return num !== undefined && this.data !== undefined ?
this.data[num].pop()?.[1]
: undefined;
public popInd(num: number | undefined): T | undefined {
if (num !== undefined && this.data !== undefined) {
const data = this.data[num][1];
this.data.slice(0, num).concat(this.data.slice(num + 1, -1));
return data;
}
return undefined;
}
public size(): number {
return this.data.length;
Expand All @@ -49,5 +52,6 @@ export class PriorityQueue<T> {
return x;
}
}
return undefined;
}
}

0 comments on commit dab1559

Please sign in to comment.