mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-01 23:33:33 +00:00
single player in progress
This commit is contained in:
@@ -77,4 +77,62 @@ export class LocalSocket implements WebSocket {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class GameMessageEvent implements MessageEvent {
|
||||
|
||||
readonly data: any;
|
||||
readonly origin: string;
|
||||
readonly lastEventId: string;
|
||||
readonly source: WindowProxy | null;
|
||||
readonly ports: ReadonlyArray<MessagePort>;
|
||||
|
||||
constructor(data: any) {
|
||||
this.data = data;
|
||||
}
|
||||
returnValue: boolean;
|
||||
srcElement: EventTarget;
|
||||
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
NONE: 0;
|
||||
CAPTURING_PHASE: 1;
|
||||
AT_TARGET: 2;
|
||||
BUBBLING_PHASE: 3;
|
||||
|
||||
// MessageEvent interface methods
|
||||
initMessageEvent(type: string, bubbles?: boolean, cancelable?: boolean, data?: any, origin?: string, lastEventId?: string, source?: WindowProxy | null, ports?: MessagePort[]): void {
|
||||
// This method is deprecated, so we'll leave it as a no-op
|
||||
console.warn('initMessageEvent is deprecated');
|
||||
}
|
||||
|
||||
// Event interface properties and methods
|
||||
readonly bubbles: boolean = false;
|
||||
readonly cancelBubble: boolean = false;
|
||||
readonly cancelable: boolean = false;
|
||||
readonly composed: boolean = false;
|
||||
readonly currentTarget: EventTarget | null = null;
|
||||
readonly defaultPrevented: boolean = false;
|
||||
readonly eventPhase: number = Event.NONE;
|
||||
readonly isTrusted: boolean = false;
|
||||
readonly target: EventTarget | null = null;
|
||||
readonly timeStamp: number = Date.now();
|
||||
readonly type: string = 'message';
|
||||
|
||||
// Event interface methods
|
||||
composedPath(): EventTarget[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
preventDefault(): void {
|
||||
// No-op for this example
|
||||
}
|
||||
|
||||
stopImmediatePropagation(): void {
|
||||
// No-op for this example
|
||||
}
|
||||
|
||||
stopPropagation(): void {
|
||||
// No-op for this example
|
||||
}
|
||||
}
|
||||
+31
-2
@@ -1,12 +1,22 @@
|
||||
import {Config} from "./configuration/Config";
|
||||
import {LocalSocket} from "./GameSocket";
|
||||
import {ClientMessage, ClientMessageSchema} from "./Schemas";
|
||||
import {GameMessageEvent, LocalSocket} from "./GameSocket";
|
||||
import {ClientMessage, ClientMessageSchema, Intent, ServerTurnMessageSchema, Turn} from "./Schemas";
|
||||
|
||||
export class LocalServer {
|
||||
|
||||
private gameID = "LOCAL"
|
||||
|
||||
public localSocket: LocalSocket
|
||||
|
||||
private turns: Turn[] = []
|
||||
private intents: Intent[] = []
|
||||
|
||||
private endTurnIntervalID
|
||||
|
||||
|
||||
|
||||
constructor(private config: Config) {
|
||||
this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs());
|
||||
}
|
||||
|
||||
onConnect() {
|
||||
@@ -16,6 +26,25 @@ export class LocalServer {
|
||||
onMessage(message: string) {
|
||||
const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message))
|
||||
if (clientMsg.type == "intent") {
|
||||
this.intents.push(clientMsg.intent)
|
||||
}
|
||||
}
|
||||
|
||||
private endTurn() {
|
||||
const pastTurn: Turn = {
|
||||
turnNumber: this.turns.length,
|
||||
gameID: this.gameID,
|
||||
intents: this.intents
|
||||
}
|
||||
this.turns.push(pastTurn)
|
||||
this.intents = []
|
||||
|
||||
const msg = JSON.stringify(ServerTurnMessageSchema.parse(
|
||||
{
|
||||
type: "turn",
|
||||
turn: pastTurn
|
||||
}
|
||||
))
|
||||
this.localSocket.onmessage(new GameMessageEvent(msg))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user