diff --git a/src/scene-switcher.ts b/src/scene-switcher.ts index e457d70..87d7efb 100644 --- a/src/scene-switcher.ts +++ b/src/scene-switcher.ts @@ -4,11 +4,11 @@ import { PacketType, SceneChangePacket, SceneReadyPacket } from "./types/client" let switcher: any[] = [] export const SceneSwitcher = { - waitForPlayers: (aznopoly: AzNopolyGame, sceneKey: string, callback: () => void) => { + waitForPlayers: (aznopoly: AzNopolyGame, sceneKey: string, launchMethod: "launch" | "start" | "wake", callback: () => void) => { switcher.push(new SceneReadyListener(aznopoly, sceneKey, callback)); - sendSceneChangePacket(aznopoly, sceneKey, false); + sendSceneChangePacket(aznopoly, sceneKey.split("_")[0], launchMethod); }, - updateScene: sendSceneReadyPacket, + broadcastSceneReady: sendSceneReadyPacket, listen: listenForSceneSwitch, } @@ -24,6 +24,8 @@ class SceneReadyListener { private listener?: EventListener; private readyList: Set = new Set(); + private id = Math.random(); + constructor(aznopoly: AzNopolyGame, sceneName: string, callback: () => void) { this.aznopoly = aznopoly; this.sceneName = sceneName; @@ -37,6 +39,7 @@ class SceneReadyListener { this.readyList.add(packet.detail.sender); if (this.readyList.size == this.aznopoly.room.connectedPlayerIds.length) { this.callback(); + console.log("Removing listener", this.id) this.aznopoly.client.removeEventListener(PacketType.SCEEN_READY, this.listener!); switcher = switcher.filter((listener) => listener != this); } @@ -44,6 +47,7 @@ class SceneReadyListener { } private startListening() { + console.log("Starting listener", this.id) this.listener = this.sceneReadyListener.bind(this) as EventListener; this.aznopoly.client.addEventListener(PacketType.SCEEN_READY, this.listener); } @@ -68,20 +72,29 @@ function sendSceneReadyPacket(aznopoly: AzNopolyGame, sceneName: string) { } } -function sendSceneChangePacket(aznopoly: AzNopolyGame, sceneName: string, returnable: boolean) { +function sendSceneChangePacket(aznopoly: AzNopolyGame, sceneName: string, launchMethod: "start" | "launch" | "wake") { const packet: SceneChangePacket = { type: PacketType.SCENE_CHANGE, sender: aznopoly.client.id, data: { scene: sceneName, - launchMethod: returnable ? "launch" : "start", + launchMethod, } } + console.log("Sending scene change packet", packet); aznopoly.client.sendPacket(packet); } function listenForSceneSwitch(scene: Phaser.Scene, aznopoly: AzNopolyGame) { - const listener = aznopoly.addPacketListener(PacketType.SCENE_CHANGE, ((event: CustomEvent) => { + const id = Math.random(); + + console.log("Listening for scene switch", id, scene.scene.key) + const packetListener = aznopoly.addPacketListener(PacketType.SCENE_CHANGE, ((event: CustomEvent) => { + if (!scene.scene.isActive(scene.scene.key ) || scene.scene.isSleeping(scene.scene.key)) { + console.warn("Received scene change packet for inactive scene", id, scene.scene.key, scene.scene.isActive(scene.scene.key ), scene.scene.isSleeping(scene.scene.key )) + return; + } + const packet = event.detail if (!aznopoly.isPlayerHost(packet.sender)) { console.warn("Received scene change packet from non-host player"); @@ -89,11 +102,22 @@ function listenForSceneSwitch(scene: Phaser.Scene, aznopoly: AzNopolyGame) { } if (packet.data.launchMethod == "launch") { + console.log("Sleeping scene", id, scene.scene.key) scene.scene.sleep(); scene.scene.launch(packet.data.scene, { returnScene: scene.scene.key}); - } else { + } else if (packet.data.launchMethod == "wake") { + console.trace("Waking scene", id, scene, packet.data.scene) + scene.scene.stop(); + scene.scene.wake(packet.data.scene); + } else if (packet.data.launchMethod == "start") { + console.log("Starting scene", id, packet.data.scene) scene.scene.start(packet.data.scene); + } else { + console.error("Unknown launch method", packet.data.launchMethod); } }) as EventListener); - scene.events.once(Phaser.Scenes.Events.SHUTDOWN, () => aznopoly.removePacketListener(PacketType.SCENE_CHANGE, listener)); + scene.events.once(Phaser.Scenes.Events.SHUTDOWN, () => { + console.log("Removing scene switch listener", id) + aznopoly.removePacketListener(PacketType.SCENE_CHANGE, packetListener) + }); } \ No newline at end of file diff --git a/src/scene/base/base-scene-controller.ts b/src/scene/base/base-scene-controller.ts index fd0ecb4..14ae565 100644 --- a/src/scene/base/base-scene-controller.ts +++ b/src/scene/base/base-scene-controller.ts @@ -29,7 +29,7 @@ export default abstract class NetworkSceneController { constructor(scene: Phaser.Scene, aznopoly: AzNopolyGame) { this.scene = scene; this.aznopoly = aznopoly; - this.packetType = "CLIENT_MINIGAME_" + this.constructor.name.toUpperCase(); + this.packetType = "CLIENT_" + this.constructor.name.toUpperCase(); scene.events.once(Phaser.Scenes.Events.CREATE, () => { this.registerPacketListener(); diff --git a/src/scene/base/minigame-scene-controller.ts b/src/scene/base/minigame-scene-controller.ts index 50f9eb4..12d24e9 100644 --- a/src/scene/base/minigame-scene-controller.ts +++ b/src/scene/base/minigame-scene-controller.ts @@ -1,15 +1,16 @@ import AzNopolyGame from "../../game"; +import { SceneSwitcher } from "../../scene-switcher"; import MinigameScene from "./minigame-scene"; import SyncedSceneController from "./synced-scene-controller"; - +const RESULT_DISPLAY_TIME = 2000; export default abstract class MinigameSceneController extends SyncedSceneController { declare protected scene: MinigameScene; private previousScene: string; constructor(scene: Phaser.Scene, aznopoly: AzNopolyGame, /*previousScene: string */) { - super(scene, aznopoly); + super(scene, aznopoly, "launch"); this.previousScene = "game"; this.registerSyncedMethod(this.showReady, true); @@ -37,14 +38,15 @@ export default abstract class MinigameSceneController extends SyncedSceneControl protected endGame(playerWon: string[], sorted: boolean) { this.scene.showResultOverlay(playerWon); - setTimeout(() => { - this.onGameOver(); - }, 3000); + setTimeout(() => this.onGameOver(), RESULT_DISPLAY_TIME); } abstract onMiniGameStart() : void; private onGameOver() { + if (!this.aznopoly.isHost) { + return; + } this.scene.scene.stop(); this.scene.scene.wake(this.previousScene); } diff --git a/src/scene/base/minigame-scene.ts b/src/scene/base/minigame-scene.ts index 9406c85..a6e0dfe 100644 --- a/src/scene/base/minigame-scene.ts +++ b/src/scene/base/minigame-scene.ts @@ -7,7 +7,7 @@ const START_TIME = 500; export default abstract class MinigameScene extends BaseScene { private overlay!: Phaser.GameObjects.Image; - private waitingForPlayersText!: Phaser.GameObjects.Text; + private centerText!: Phaser.GameObjects.Text; private startTimer = 0; @@ -24,9 +24,9 @@ export default abstract class MinigameScene e this.overlay.setDepth(1000); this.overlay.setVisible(false); - this.waitingForPlayersText = this.add.text(WIDTH/2, HEIGHT/2, 'Waiting for players...', FONT_STYLE_HEADLINE); - this.waitingForPlayersText.setOrigin(0.5, 0.5); - this.waitingForPlayersText.setDepth(1000); + this.centerText = this.add.text(WIDTH/2, HEIGHT/2, 'Waiting for players...', FONT_STYLE_HEADLINE); + this.centerText.setOrigin(0.5, 0.5); + this.centerText.setDepth(1000); } update(time: number, delta: number) { @@ -45,7 +45,7 @@ export default abstract class MinigameScene e public showReadyOverlay() { this.overlay.setTexture('minigame_ready'); this.overlay.setVisible(true); - this.waitingForPlayersText.setVisible(false); + this.centerText.setVisible(false); } public showStartOverlay() { @@ -61,15 +61,15 @@ export default abstract class MinigameScene e public showResultOverlay(playerWon: string[]) { const won = playerWon.includes(this.aznopoly.uuid); - console.log("showResultOverlay", won); this.overlay.setVisible(false); this.overlay.alpha = 1; this.overlay.scale = 1; + this.overlay.y -= 100; + + const names = playerWon.map(uuid => this.aznopoly.room.getPlayerName(uuid) + " won"); + this.centerText.setText(names.join("\n")) + this.centerText.setVisible(true); - const names = playerWon.map(uuid => this.aznopoly.room.getPlayerName(uuid) + "won"); - this.waitingForPlayersText.setText(names.join("\n")) - this.waitingForPlayersText.setVisible(true); - if (won) { this.showGameWon(); } else { diff --git a/src/scene/base/synced-scene-controller.ts b/src/scene/base/synced-scene-controller.ts index 2536aba..df712bd 100644 --- a/src/scene/base/synced-scene-controller.ts +++ b/src/scene/base/synced-scene-controller.ts @@ -5,31 +5,52 @@ import NetworkSceneController from "./base-scene-controller"; export default abstract class SyncedSceneController extends NetworkSceneController { - constructor(scene: Phaser.Scene, aznopoly: AzNopolyGame) { + private launchMethod: "launch" | "start" | "wake"; + + constructor(scene: Phaser.Scene, aznopoly: AzNopolyGame, launchMethod: "launch" | "start" | "wake") { super(scene, aznopoly); + this.launchMethod = launchMethod; this.registerSyncedMethod(this.onAllPlayersReady, true); + this.registerSyncedMethod(this.onAllPlayersWake, true); SceneSwitcher.listen(scene, aznopoly) + + const listener = this.onSceneWake.bind(this); + scene.events.on(Phaser.Scenes.Events.WAKE, listener); + scene.events.once(Phaser.Scenes.Events.SHUTDOWN, () => scene.events.off(Phaser.Scenes.Events.WAKE, listener)); } onSceneCreate(): void { - this.updateSceneSwitcher(); + if (this.aznopoly.isHost) { + SceneSwitcher.waitForPlayers(this.aznopoly, this.scene.scene.key + "_CREATE", this.launchMethod, this.onAllPlayersJoined.bind(this)); + } + SceneSwitcher.broadcastSceneReady(this.aznopoly, this.scene.scene.key + "_CREATE") } - private updateSceneSwitcher() { + onSceneWake(): void { if (this.aznopoly.isHost) { - SceneSwitcher.waitForPlayers(this.aznopoly, this.scene.scene.key, this.onAllPlayersJoined.bind(this)); + SceneSwitcher.waitForPlayers(this.aznopoly, this.scene.scene.key + "_WAKE", "wake", this.onAllPlayersRejoined.bind(this)); } - SceneSwitcher.updateScene(this.aznopoly, this.scene.scene.key) + SceneSwitcher.broadcastSceneReady(this.aznopoly, this.scene.scene.key + "_WAKE") } /** * Will be called after all the host as acknowledged that that all players have joined the scene */ abstract onAllPlayersReady(): void; + /** + * Will be called after all the host as acknowledged that that all players have waked the scene and is ready + */ + onAllPlayersWake(): void { + + } private onAllPlayersJoined() { this.syncProxy.onAllPlayersReady(); } + private onAllPlayersRejoined() { + this.syncProxy.onAllPlayersWake(); + } + } \ No newline at end of file diff --git a/src/scene/board-scene-controller.ts b/src/scene/board-scene-controller.ts index bdfb5ee..4aa2ecb 100644 --- a/src/scene/board-scene-controller.ts +++ b/src/scene/board-scene-controller.ts @@ -1,4 +1,5 @@ import AzNopolyGame from "../game"; +import { SceneSwitcher } from "../scene-switcher"; import SyncedSceneController from "./base/synced-scene-controller"; import BoardScene from "./board-scene"; @@ -17,7 +18,7 @@ export default class BoardSceneController extends SyncedSceneController { private players!: Player[]; constructor(scene: BoardScene, aznopoly: AzNopolyGame) { - super(scene, aznopoly); + super(scene, aznopoly, "start"); this.registerSyncedMethod(this.addPlayersToBoard, true); this.registerSyncedMethod(this.updatePlayerPosition, true); @@ -38,7 +39,6 @@ export default class BoardSceneController extends SyncedSceneController { this.syncProxy.addPlayersToBoard(players); this.syncProxy.startTurn(players[0].uuid); } - } private addPlayersToBoard(players: Player[]) { @@ -87,17 +87,13 @@ export default class BoardSceneController extends SyncedSceneController { const nextIndex = (currentIndex + 1) % this.players.length; const nextPlayer = this.players[nextIndex]; + this.syncProxy.startTurn(nextPlayer.uuid); + if (nextIndex == 0) { this.syncProxy.startMinigame(); - } else { - this.syncProxy.startTurn(nextPlayer.uuid); } } - private onMinigameResult() { - - } - private startMinigame() { this.scene.showMinigameSelect("Roomba Outrage").then(() => { setTimeout(() => { diff --git a/src/scene/board-scene.ts b/src/scene/board-scene.ts index 7fb065c..e2e0e31 100644 --- a/src/scene/board-scene.ts +++ b/src/scene/board-scene.ts @@ -1,7 +1,6 @@ import GameBoard from "../board/board"; import { HEIGHT, WIDTH } from "../main"; import { FONT_STYLE_BODY } from "../style"; -import { GameTurnRollPacket, GameTurnStartPacket, PacketType } from "../types/client"; import { AzNopolyButton } from "../ui/button"; import PlayerList from "../ui/player-list"; import RandomSelectionWheel from "../ui/random-selection-wheel"; diff --git a/src/scene/minigame/roomba-scene-controller.ts b/src/scene/minigame/roomba-scene-controller.ts index 4f77baa..ebe464f 100644 --- a/src/scene/minigame/roomba-scene-controller.ts +++ b/src/scene/minigame/roomba-scene-controller.ts @@ -14,7 +14,7 @@ export default class RoombaSceneController extends MinigameSceneController { private locked = false; - private colorUuuidMap = new Map(); + private colorUuuidMap = new Map(); constructor(scene: RoombaScene, aznopoly: AzNopolyGame) { super(scene, aznopoly); @@ -29,36 +29,39 @@ export default class RoombaSceneController extends MinigameSceneController { } onMiniGameStart(): void { - this.scene.events.on("roomba-dragged", ({id, offset} : {id: string, offset: Phaser.Math.Vector2}) => { - if (this.locked) return; - - this.syncProxy.updateRoombaDirection(id, offset); - }); + this.scene.events.on("roomba-dragged", this.onRoombaDragged.bind(this)); if (!this.aznopoly.isHost) { return; } - const roombaConfigs = []; - for (let j = 0; j < this.aznopoly.connectedUuids.length; j++) { - const uuid = this.aznopoly.connectedUuids[j] ; - for (let i = 0; i < 5; i++) { - roombaConfigs.push(this.generateRandomRoombaConfig(uuid)); - } - this.colorUuuidMap.set(roombaConfigs[roombaConfigs.length-1].color, uuid); - } + + const roombaConfigs = this.generateRoombaConfigs(); this.syncProxy.initRoombas(roombaConfigs); setTimeout(() => { this.syncProxy.lockAllGameplay(); - const array = this.scene.getAAAAA(); - Object.keys(array).map((key) => { - console.log(this.colorUuuidMap.get(array[key])); - }); - this.syncProxy.endGame([], false); + const won = this.getPlayersWon(); + this.syncProxy.endGame(won, false); }, MAX_GAME_TIME) } + private getPlayersWon() { + const paintMap = this.scene.getPaintMap(); + const paintedColors = Object.keys(paintMap).filter(e => e != "000000"); + + const won = paintedColors.sort() + .map((key) => this.colorUuuidMap.get(key)!) + .slice(0, 1); + + return won; + } + + private onRoombaDragged({ id, offset}: {id: string, offset: Phaser.Math.Vector2}) { + if (this.locked) return; + this.syncProxy.updateRoombaDirection(id, offset); + } + private updateRoombaDirection(id: string, direction: Phaser.Math.Vector2) { this.scene.updateRoombaDirection(id, new Phaser.Math.Vector2(direction.x, direction.y)); } @@ -72,6 +75,18 @@ export default class RoombaSceneController extends MinigameSceneController { this.scene.stopRoombas(); } + private generateRoombaConfigs() { + const roombaConfigs = []; + for (let j = 0; j < this.aznopoly.connectedUuids.length; j++) { + const uuid = this.aznopoly.connectedUuids[j] ; + for (let i = 0; i < 5; i++) { + roombaConfigs.push(this.generateRandomRoombaConfig(uuid)); + } + this.colorUuuidMap.set(roombaConfigs[roombaConfigs.length-1].paintColor.toString(16).toUpperCase(), uuid); + } + return roombaConfigs; + } + private generateRandomRoombaConfig(playerid: string) { const x = Math.random() * (GAME_WIDTH - Roomba.SIZE * 2) + Roomba.SIZE; const y = Math.random() * (GAME_HEIGHT - Roomba.SIZE * 2) + Roomba.SIZE; diff --git a/src/scene/minigame/roomba-scene.ts b/src/scene/minigame/roomba-scene.ts index bc8ba30..ed24be7 100644 --- a/src/scene/minigame/roomba-scene.ts +++ b/src/scene/minigame/roomba-scene.ts @@ -25,7 +25,11 @@ export class RoombaScene extends MinigameScene { } init() { + console.log("RoombaScene init", this.roombas); this.controller = new RoombaSceneController(this, this.aznopoly); + this.roombas = [] + this.timeSinceLastPaint = 0; + this.timeSinceGraphicsSwap = 0; } preload() { @@ -40,6 +44,11 @@ export class RoombaScene extends MinigameScene { this.paint = this.add.graphics(); this.paintTexture = this.textures.addDynamicTexture("roomba-paint", WIDTH, HEIGHT)!; + this.events.once(Phaser.Scenes.Events.SHUTDOWN, () => { + this.paint.destroy(); + this.paintTexture.destroy(); + }); + this.colorProgressBar = new ColorProgressBar(this, WIDTH / 2 - 200, 25, 400, 40); this.add.sprite(0, 0, "roomba-paint").setOrigin(0, 0).setDepth(-1); @@ -80,6 +89,8 @@ export class RoombaScene extends MinigameScene { this.timeSinceLastPaint += delta / 1000; if (this.timeSinceLastPaint > PAINT_REFRESH_TIME) { this.timeSinceLastPaint = 0; + + console.log("Painting", this.roombas.length); this.roombas.forEach(roomba => { roomba.paintPath(this.paint); }); @@ -99,11 +110,11 @@ export class RoombaScene extends MinigameScene { } private calculatePaintPercentage() { - const result = this.getAAAAA(); + const result = this.getPaintMap(); this.updateProgressBar(result); } - public getAAAAA() { + public getPaintMap() { if (this.paintTexture.renderTarget) { const renderer = this.paintTexture.renderer as Phaser.Renderer.WebGL.WebGLRenderer; diff --git a/src/scene/title-scene-controller.ts b/src/scene/title-scene-controller.ts index 9c08c54..0891391 100644 --- a/src/scene/title-scene-controller.ts +++ b/src/scene/title-scene-controller.ts @@ -18,8 +18,10 @@ export default class TitleSceneController { public onMusicButtonClicked() { if (this.musicOn) { this.scene.stopMusic(); + this.musicOn = false; } else { this.scene.startMusic(); + this.musicOn = true; } } @@ -40,6 +42,7 @@ export default class TitleSceneController { private joinRoom(room: string) { this.scene.playStartSound(); setTimeout(() => { + this.scene.stopMusic(); this.aznopoly.init(room); this.aznopoly.room.addEventListener(RoomEvent.READY, () => { this.scene.scene.start('lobby'); diff --git a/src/types/client.ts b/src/types/client.ts index cb002de..9bd60b0 100644 --- a/src/types/client.ts +++ b/src/types/client.ts @@ -94,7 +94,7 @@ export interface SceneChangePacket extends PlayerPacket { type: PacketType.SCENE_CHANGE; data: { scene: string, - launchMethod: "start" | "launch", + launchMethod: "start" | "launch" | "wake", }; } diff --git a/src/ui/random-selection-wheel.ts b/src/ui/random-selection-wheel.ts index cea05b4..25b3637 100644 --- a/src/ui/random-selection-wheel.ts +++ b/src/ui/random-selection-wheel.ts @@ -1,8 +1,7 @@ import { COLOR_PRIMARY, COLOR_PRIMARY_2, FONT_STYLE_BODY } from "../style"; - -const SPIN_TIME = 3000; -const NUM_CHOICE_CHANGES = 40; +const SPIN_TIME = 2000; +const NUM_CHOICE_CHANGES = 50; const FADE_IN_TIME = 100; const PADDING = 10; export default class RandomSelectionWheel extends Phaser.GameObjects.Container { diff --git a/test/base-scene-controller.test.ts b/test/base-scene-controller.test.ts index 8008dcf..5d40a0c 100644 --- a/test/base-scene-controller.test.ts +++ b/test/base-scene-controller.test.ts @@ -1,101 +1,106 @@ import AzNopolyGame from '../src/game' -import BaseSceneController from '../src/scene/base/base-scene-controller' +import NetworkSceneController from '../src/scene/base/base-scene-controller'; import {EventEmitter} from 'eventemitter3'; +import Phaser from 'phaser'; -class TestController extends BaseSceneController { - public bobo: number = 0; - public exampleMethod(num: number) { - this.bobo = num; - } -} - -let scene = {} as Phaser.Scene; -let aznopoly = {} as AzNopolyGame; -let controller!: TestController; - -beforeEach(() => { - scene = { - events: new EventEmitter() - } as any as Phaser.Scene; - - aznopoly = { - broadcastPacket: () => {}, - isPlayerHost: () => true, - } as any as AzNopolyGame; - - - global.Phaser = { - Scenes: { - Events: { - SHUTDOWN: 'shutdown', - CREATE: 'create', - } +it("should create a new controller", () => { + class TestController extends NetworkSceneController { + onSceneCreate(): void { } - }; - controller = new TestController(scene, aznopoly); -}) - -test('Proxy call', () => { - controller.registerSyncedMethod(controller.exampleMethod, false); - - controller.syncProxy.exampleMethod(5) - expect(controller.bobo).toBe(5); -}) - -test('Proxy call not allowed', () => { - expect(() => controller.syncProxy.exampleMethod(5)).toThrow(); -}) - -test('Proxy call not allowed by host', () => { - controller.registerSyncedMethod(controller.exampleMethod, true); - - (aznopoly as any).isHost = false; - expect(() => controller.syncProxy.exampleMethod(5)).toThrow(); -}) - -test('Proxy call allowed by host', () => { - controller.registerSyncedMethod(controller.exampleMethod, true); - - (aznopoly as any).isHost = true; - controller.syncProxy.exampleMethod(5); - expect(controller.bobo).toBe(5); -}) - -test('Proxy call sends packet', () => { - controller.registerSyncedMethod(controller.exampleMethod, false); + public bobo: number = 0; + public exampleMethod(num: number) { + this.bobo = num; + } + } - let packet; - aznopoly.broadcastPacket = (p: any) => packet = p; - - controller.syncProxy.exampleMethod(5); - expect(packet).toEqual({ - type: 'CLIENT_MINIGAME_TESTCONTROLLER', - data: { - method: 'exampleMethod', - arguments: [5], + let scene = {} as Phaser.Scene; + let aznopoly = {} as AzNopolyGame; + let controller!: TestController; + + beforeEach(() => { + scene = { + events: new EventEmitter() + } as any as Phaser.Scene; + + aznopoly = { + broadcastPacket: () => {}, + isPlayerHost: () => true, + } as any as AzNopolyGame; + + + (global as any).Phaser = { + Scenes: { + Events: { + SHUTDOWN: 'shutdown', + CREATE: 'create', + } + } + }; + controller = new TestController(scene, aznopoly); + }) + + describe('Proxy call', () => { + controller.registerSyncedMethod(controller.exampleMethod, false); + + controller.syncProxy.exampleMethod(5) + expect(controller.bobo).toBe(5); + }) + + describe('Proxy call not allowed', () => { + expect(() => controller.syncProxy.exampleMethod(5)).toThrow(); + }) + + describe('Proxy call not allowed by host', () => { + controller.registerSyncedMethod(controller.exampleMethod, true); + + (aznopoly as any).isHost = false; + expect(() => controller.syncProxy.exampleMethod(5)).toThrow(); + }) + + describe('Proxy call allowed by host', () => { + controller.registerSyncedMethod(controller.exampleMethod, true); + + (aznopoly as any).isHost = true; + controller.syncProxy.exampleMethod(5); + expect(controller.bobo).toBe(5); + }) + + describe('Proxy call sends packet', () => { + controller.registerSyncedMethod(controller.exampleMethod, false); + + let packet; + aznopoly.broadcastPacket = (p: any) => packet = p; + + controller.syncProxy.exampleMethod(5); + expect(packet).toEqual({ + type: 'CLIENT_MINIGAME_TESTCONTROLLER', + data: { + method: 'exampleMethod', + arguments: [5], + } + }); + }) + + describe('Receiving packet will call method', () => { + controller.registerSyncedMethod(controller.exampleMethod, false); + + let listener!: EventListener; + (aznopoly as any).addPacketListener = (_: string, l: EventListener) => { + listener = l; + }; + + scene.events.emit(global.Phaser.Scenes.Events.CREATE); + expect(listener).toBeDefined(); + + let packet = { + type: 'CLIENT_MINIGAME_TESTCONTROLLER', + data: { + method: 'exampleMethod', + arguments: [5], + } } + listener(new CustomEvent('test', {detail: packet}) as any); + expect(controller.bobo).toBe(5); }); -}) - -test('Receiving packet will call method', () => { - controller.registerSyncedMethod(controller.exampleMethod, false); - - let listener!: EventListener; - (aznopoly as any).addPacketListener = (_: string, l: EventListener) => { - listener = l; - }; - - scene.events.emit(global.Phaser.Scenes.Events.CREATE); - expect(listener).toBeDefined(); - - let packet = { - type: 'CLIENT_MINIGAME_TESTCONTROLLER', - data: { - method: 'exampleMethod', - arguments: [5], - } - } - listener(new CustomEvent('test', {detail: packet}) as any); - expect(controller.bobo).toBe(5); }); \ No newline at end of file