Skip to content

Commit

Permalink
spectate
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Dec 27, 2024
1 parent ef57adc commit 5b84bac
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/server.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/container/browsercontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class BrowserContainer {
score: 0,
}
cushionModel
spectator
assets: Assets
now
constructor(canvas3d, params) {
Expand All @@ -40,6 +41,7 @@ export class BrowserContainer {
this.wss = params.get("websocketserver")
this.canvas3d = canvas3d
this.cushionModel = this.cushion(params.get("cushionModel"))
this.spectator = params.has("spectator")
}

cushion(model) {
Expand Down Expand Up @@ -76,7 +78,7 @@ export class BrowserContainer {
this.container.table.cushionModel = this.cushionModel
this.setReplayLink()
if (this.wss) {
const params = `name=${this.playername}&tableId=${this.tableId}&clientId=${this.clientId}`
const params = `name=${this.playername}&tableId=${this.tableId}&clientId=${this.clientId}${this.spectator ? "&spectator" : ""}`
this.container.isSinglePlayer = false
this.sc = new SocketConnection(`${this.wss}?${params}`, this.clientId)
this.networkButton()
Expand Down
8 changes: 8 additions & 0 deletions src/network/server/lobby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export class Lobby {
tableInfo.otherClients(client).forEach((c) => {
this.send(c, tableId, event)
})
tableInfo.spectators.forEach((c) => {
this.send(c, tableId, event)
})
}

handleLeaveTable(client, tableId) {
Expand Down Expand Up @@ -132,10 +135,15 @@ export class Lobby {

handleUnspectateTable(client: Client, tableId) {
ServerLog.log(`${client.name}:${client.clientId} unspectating ${tableId}`)
const tableInfo = this.tables.getTable(tableId)
tableInfo.spectatorLeave(client)
return true
}

spectateTable(client: Client, tableId) {
ServerLog.log(`${client.name}:${client.clientId} spectating ${tableId}`)
const tableInfo = this.tables.getTable(tableId)
tableInfo.spectatorJoin(client)
return true
}

Expand Down
9 changes: 9 additions & 0 deletions src/network/server/tableinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class TableInfo {
readonly tableId: string
readonly owningClientIds: string[] = []
clients: Client[] = []
spectators: Client[] = []

eventHistory: Map<string, EventHistory> = new Map()

Expand Down Expand Up @@ -39,6 +40,14 @@ export class TableInfo {
this.clients.push(client)
}

spectatorJoin(client: Client) {
this.spectators.push(client)
}

spectatorLeave(client: Client) {
this.spectators = this.spectators.filter((c) => c.clientId !== client.clientId)
}

rejoin(client: Client) {
this.clients.push(client)
}
Expand Down
21 changes: 21 additions & 0 deletions test/server/lobby.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ beforeEach(function (done) {
player1.ws.reset()
player1r.ws.reset()
player2.ws.reset()
player3.ws.reset()
})

const jestConsole = console
Expand Down Expand Up @@ -277,4 +278,24 @@ describe("Lobby", () => {
done()
})

it("spectate full table and unspectate", (done) => {
lobby.joinTable(player1, tableId)
lobby.joinTable(player2, tableId)
expect(lobby.spectateTable(player3, tableId)).to.be.true
expect(lobby.handleUnspectateTable(player3, tableId)).to.be.true
done()
})

it("spectator gets messages", (done) => {
lobby.joinTable(player1, tableId)
lobby.joinTable(player2, tableId)
expect(lobby.spectateTable(player3, tableId)).to.be.true
const message = EventUtil.serialise(new AimEvent())
lobby.handleTableMessage(player1, tableId, message)
const event = EventUtil.fromSerialised(player3.ws.messages[0])
expect(event).to.be.an.instanceof(AimEvent)
done()
})


})

0 comments on commit 5b84bac

Please sign in to comment.