have client send winner to server

This commit is contained in:
Evan
2025-02-01 12:05:11 -08:00
parent d053a82ff6
commit a5a2f46099
8 changed files with 58 additions and 10 deletions
+7 -1
View File
@@ -16,6 +16,8 @@ export class LocalServer {
private paused = false
private winner: ClientID | null = null
constructor(
private serverConfig: ServerConfig,
@@ -58,6 +60,9 @@ export class LocalServer {
}
this.intents.push(clientMsg.intent)
}
if (clientMsg.type == "winner") {
this.winner = clientMsg.winner
}
}
private endTurn() {
@@ -92,7 +97,8 @@ export class LocalServer {
players,
this.turns,
this.startedAt,
Date.now()
Date.now(),
this.winner
)
// Clear turns because beacon only supports up to 64kb
record.turns = []
+23 -1
View File
@@ -2,7 +2,7 @@ import { Config, ServerConfig } from "../core/configuration/Config"
import { SendLogEvent } from "../core/Consolex"
import { EventBus, GameEvent } from "../core/EventBus"
import { AllianceRequest, AllPlayers, Cell, GameType, Player, PlayerID, PlayerType, UnitType } from "../core/game/Game"
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ClientPingMessageSchema, GameConfig, ClientLogMessageSchema } from "../core/Schemas"
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ClientPingMessageSchema, GameConfig, ClientLogMessageSchema, ClientSendWinnerSchema } from "../core/Schemas"
import { LobbyConfig } from "./ClientGameRunner"
import { LocalServer } from "./LocalServer"
import { UsernameInput } from "./UsernameInput";
@@ -93,6 +93,12 @@ export class SendSetTargetTroopRatioEvent implements GameEvent {
) { }
}
export class SendWinnerEvent implements GameEvent {
constructor(
public readonly winner: ClientID
) { }
}
export class Transport {
private socket: WebSocket
@@ -132,6 +138,7 @@ export class Transport {
this.eventBus.on(SendLogEvent, (e) => this.onSendLogEvent(e))
this.eventBus.on(PauseGameEvent, (e) => this.onPauseGameEvent(e))
this.eventBus.on(SendWinnerEvent, (e) => this.onSendWinnerEvent(e))
}
private startPing() {
@@ -375,6 +382,21 @@ export class Transport {
}
}
private onSendWinnerEvent(event: SendWinnerEvent) {
if (this.isLocal || this.socket.readyState === WebSocket.OPEN) {
const msg = ClientSendWinnerSchema.parse({
type: "winner",
clientID: this.lobbyConfig.clientID,
gameID: this.lobbyConfig.gameID,
winner: event.winner,
})
this.sendMsg(JSON.stringify(msg))
} else {
console.log('WebSocket is not open. Current state:', this.socket.readyState);
console.log('attempting reconnect')
}
}
private sendIntent(intent: Intent) {
if (this.isLocal || this.socket.readyState === WebSocket.OPEN) {
const msg = ClientIntentMessageSchema.parse({
+1
View File
@@ -81,6 +81,7 @@ export function createRenderer(canvas: HTMLCanvasElement, game: GameView, eventB
if (!(winModel instanceof WinModal)) {
console.error('win modal not found')
}
winModel.eventBus = eventBus
winModel.game = game
const optionsMenu = document.querySelector('options-menu') as OptionsMenu
+4
View File
@@ -7,6 +7,8 @@ import { Layer } from './Layer';
import { GameUpdateType } from '../../../core/game/GameUpdates';
import { PseudoRandom } from '../../../core/PseudoRandom';
import { simpleHash } from '../../../core/Util';
import { EventBus } from '../../../core/EventBus';
import { SendWinnerEvent } from '../../Transport';
const lowRadiationVictoryQuotes = [
@@ -147,6 +149,7 @@ export const defeatQuotes = [
@customElement('win-modal')
export class WinModal extends LitElement implements Layer {
public game: GameView
public eventBus: EventBus
private rand: PseudoRandom;
@@ -300,6 +303,7 @@ export class WinModal extends LitElement implements Layer {
this.game.updatesSinceLastTick()[GameUpdateType.WinUpdate]
.forEach(wu => {
const winner = this.game.playerBySmallID(wu.winnerID) as PlayerView
this.eventBus.emit(new SendWinnerEvent(winner.clientID()))
if (winner == this.game.myPlayer()) {
this._title = 'You Won!'
if (this.game.numTilesWithFallout() / this.game.numLandTiles() > .6) {