diff --git a/src/game/common/const.mjs b/src/game/common/const.mjs index 2d0501d7..852a7337 100644 --- a/src/game/common/const.mjs +++ b/src/game/common/const.mjs @@ -172,7 +172,7 @@ export const BEAVER_TYPES = { // ------- Queue Config const Queue = { - limit: 15, + defaultLimit: 15, }; export default { diff --git a/src/game/config/warp-ao-ids.js b/src/game/config/warp-ao-ids.js index 927f46d4..3ca62481 100644 --- a/src/game/config/warp-ao-ids.js +++ b/src/game/config/warp-ao-ids.js @@ -1,8 +1,8 @@ export default { "hub_moduleId_prod": "UAxQg76EePpwkPvuuXj3QPzxh7Xs2xAg8arEopOggSk", "hub_processId_prod": "bbPgrFb7SwRd0Htc52Verz1pvlMcgbGiEY66bKiV5PE", - "hub_moduleId_local": "UFuIxJvjwhA1FNnVs43Ecs_ZPu5NrdC1PLoI_eMhJQQ", - "hub_processId_local": "VffhYHXXkjeTyv54sKkBdBxKFrrT_QaIuUCsxbFfYOI", + "hub_moduleId_local": "Y5ah4dQ29jZcdue7Qh0rcmtcE3NQJHQKMxEpdrk626s", + "hub_processId_local": "jC6tfpu-RLKgcHUeVv44y9XHXH6qG3SPbv9BK2ViFNQ", "hub_moduleId_dev": "aaabbb", "hub_processId_dev": "aaabbb", "token_processId_prod": "rH_-7vT_IgfFWiDsrcTghIhb9aRclz7lXcK7RCOV2h8", diff --git a/src/game/process/cmd/info.mjs b/src/game/process/cmd/info.mjs index 5182baf4..bdfd4047 100644 --- a/src/game/process/cmd/info.mjs +++ b/src/game/process/cmd/info.mjs @@ -13,7 +13,7 @@ export function standInQueue(state, action) { return { walletsQueue, walletsBench }; } - if (walletsQueue.length >= Const.Queue.limit) { + if (walletsQueue.length >= state.playersLimit) { walletsBench.push(walletAddress); } else { walletsQueue.push(walletAddress); @@ -29,7 +29,7 @@ function sendHubNotification(state) { Data: '1234', Action: JSON.stringify({ cmd: Const.Command.hubGamePlayers, - players: state.walletsQueue, + walletsQueue: state.walletsQueue, }), }); } @@ -48,6 +48,7 @@ export function gameInfo(state, owner, ts) { active: isGameActive(state, ts), walletsQueue, walletsBench, + playersLimit: state.playersLimit, start: playWindow.begin, end: playWindow.end, players, diff --git a/src/game/process/cmd/registerPlayer.mjs b/src/game/process/cmd/registerPlayer.mjs index 83de9d22..69e01e56 100644 --- a/src/game/process/cmd/registerPlayer.mjs +++ b/src/game/process/cmd/registerPlayer.mjs @@ -17,7 +17,7 @@ export function registerPlayer(state, action) { if (!walletsQueue.includes(walletAddress)) { // List is full, forget about it - if (walletsQueue.length >= Const.Queue.limit) { + if (walletsQueue.length >= state.playersLimit) { const error = `Failed to register ${action.walletAddress}. Players limit exceeded ${walletsQueue.length}`; console.log(error); return { player: { walletAddress, error } }; @@ -86,7 +86,7 @@ function sendHubNotification(state) { Data: '1234', Action: JSON.stringify({ cmd: Const.Command.hubGamePlayers, - players: Object.keys(state.players), + walletsQueue: state.walletsQueue, }), }); } diff --git a/src/game/process/cmd/setup.mjs b/src/game/process/cmd/setup.mjs index a6c3b68d..96772ddd 100644 --- a/src/game/process/cmd/setup.mjs +++ b/src/game/process/cmd/setup.mjs @@ -35,7 +35,7 @@ export function setup(state, action, message) { state.chatProcessId = action.chatProcessId; state.chatModuleId = action.chatModuleId; state.hubProcessId = action.hubProcessId; - state.playersLimit = action.playersLimit || Const.Queue.limit; + state.playersLimit = action.playersLimit || Const.Queue.defaultLimit; console.log(`Setup ${action.type}`, action); sendHubNotification(state); break; @@ -55,7 +55,7 @@ export function setup(state, action, message) { state.chatProcessId = action.chatProcessId; state.chatModuleId = action.chatModuleId; state.hubProcessId = action.hubProcessId; - state.playersLimit = action.playersLimit || Const.Queue.limit; + state.playersLimit = action.playersLimit || Const.Queue.defaultLimit; console.log(`Setup ${action.type}`, action); sendHubNotification(state); break; @@ -68,7 +68,7 @@ export function setup(state, action, message) { state.chatProcessId = action.chatProcessId; state.chatModuleId = action.chatModuleId; state.hubProcessId = action.hubProcessId; - state.playersLimit = action.playersLimit || Const.Queue.limit; + state.playersLimit = action.playersLimit || Const.Queue.defaultLimit; console.log(`Setup custom`, action); sendHubNotification(state); break; @@ -96,7 +96,7 @@ function sendHubNotification(state) { game: { playWindow: state.playWindow, playersLimit: state.playersLimit, - players: state.walletsQueue, + walletsQueue: state.walletsQueue, chatProcessId: state.chatProcessId, chatModuleId: state.chatModuleId, mapTxId: state.mapTxId, diff --git a/src/game/process/hub.mjs b/src/game/process/hub.mjs index 0cbf0155..7e74caae 100644 --- a/src/game/process/hub.mjs +++ b/src/game/process/hub.mjs @@ -37,7 +37,7 @@ export function handle(state, message) { case Const.Command.hubGamePlayers: console.log(`-- HUB updating players`, action); - state.games[gameProcess].players = action.players; + state.games[gameProcess].walletsQueue = action.walletsQueue; ao.result({ cmd: Const.Command.hubStats, games: state.games, diff --git a/src/game/scenes/GameHubScene.js b/src/game/scenes/GameHubScene.js index 59e11ff2..2c59d64c 100644 --- a/src/game/scenes/GameHubScene.js +++ b/src/game/scenes/GameHubScene.js @@ -100,8 +100,8 @@ export default class GameHubScene extends Phaser.Scene { for (const [processId, game] of games) { console.log(`${++i} ${processId}: ${game}`); let bLabel = `${i} ${trimString(processId, 5, 2, 4)}`; - if (game.players && game.playersLimit) { - bLabel += `\nPlayers: ${game.players.length}/${game.playersLimit}`; + if (game.walletsQueue && game.playersLimit) { + bLabel += `\nPlayers: ${game.walletsQueue.length}/${game.playersLimit}`; } if (game.playWindow) { if (game.playWindow.begin && game.playWindow.begin > Date.now()) { diff --git a/src/game/scenes/LoungeAreaScene.js b/src/game/scenes/LoungeAreaScene.js index 561b74d7..31db3090 100644 --- a/src/game/scenes/LoungeAreaScene.js +++ b/src/game/scenes/LoungeAreaScene.js @@ -10,7 +10,6 @@ import { leaderboardSceneKey, } from '../main.js'; import { checkProfile } from '../utils/utils.js'; -import { Scrollbar } from '../objects/Scrollbar.js'; import Phaser from 'phaser'; import { EventBus } from '../EventBus.js'; @@ -54,7 +53,7 @@ export default class LoungeAreaScene extends Phaser.Scene { font: '20px', }); - this.tt = this.add.text(100, 150, '--:--:--', { + this.subHeader = this.add.text(100, 150, '--:--:--', { fill: colors.yellow, font: '20px', }); @@ -67,44 +66,38 @@ export default class LoungeAreaScene extends Phaser.Scene { } async update() { + if ( + this.enqueueButton && + this.walletsQueue.indexOf(this.walletAddress) > -1 && + this.walletsBench.indexOf(this.walletAddress) > -1 + ) { + this.enqueueButton.destroy(); + } await this.countdown(); } async countdown() { - if (this.gameError && this.gameEnd) { + if (!this.gameError) { const now = new Date(); - let diff = Math.round((this.gameEnd - now) / 1000); - if (diff >= 0) { - this.tt.setText(`The game will finish in\n${this.formatCountdownTo(diff)}`); - } else { - this.server.send({ cmd: Const.Command.info }); + let subHeaderTxt = '--:--:--'; + if (this.gameEnd) { + let diff = Math.round((this.gameEnd - now) / 1000); + if (diff >= 0) { + subHeaderTxt = `The game will finish in\n${this.formatCountdownTo(diff)}`; + } else { + // We don't want to go directly to leaderboard until we don't have the latest players stats + this.server.send({ cmd: Const.Command.info }); + } } - } else if (this.gameStart) { - const now = new Date(); - let diff = Math.round((this.gameStart - now) / 1000); - if (diff <= 0) { - if (this.beaverId) { - this.scene.start(mainSceneKey, { - walletAddress: this.walletAddress, - beaverId: this.beaverId, - gameStart: this.gameStart, - gameEnd: this.gameEnd, - }); - } else if (!this.gameError) { - if (!this.running) { - this.running = true; - const userName = (await this.profilePromise)?.Profile?.UserName; - this.scene.start(playerPickSceneKey, { - userName, - walletAddress: this.walletAddress, - gameStart: this.gameStart, - gameEnd: this.gameEnd, - }); - } + if (this.gameStart) { + let diff = Math.round((this.gameStart - now) / 1000); + if (diff <= 0) { + await this.gameActive(); + } else { + subHeaderTxt = this.formatCountdownTo(diff); } - } else { - this.tt.setText(this.formatCountdownTo(diff)); } + this.subHeader.setText(subHeaderTxt); } } @@ -124,17 +117,16 @@ export default class LoungeAreaScene extends Phaser.Scene { { console.log(`Got me some stats`, response); if (response.end && response.end < new Date()) { - this.scene.start(leaderboardSceneKey, { - players: response.players, - mainPlayer: { - walletAddress: this.walletAddress, - }, - }); + this.goToLeaderboard(response); return; } this.gameStart = response.start; this.gameEnd = response.end; + this.walletsQueue = response.walletsQueue; + this.walletsBench = response.walletsBench; + this.playersLimit = response.playersLimit; this.scrollbar = null; + this.displayWaitingList(response); if (response.error) { console.error('Failed to fetch game info', response.error); @@ -144,43 +136,15 @@ export default class LoungeAreaScene extends Phaser.Scene { this.beaverId = response.players[this.walletAddress].beaverId; } if (response.active) { - const userName = (await this.profilePromise)?.Profile?.UserName; - if (this.beaverId) { - this.scene.start(mainSceneKey, { - userName, - walletAddress: this.walletAddress, - beaverId: this.beaverId, - gameStart: response.start, - gameEnd: response.end, - }); - } else { - if (!this.running) { - this.running = true; - this.scene.start(playerPickSceneKey, { - userName, - walletAddress: this.walletAddress, - gameStart: response.start, - gameEnd: response.end, - }); - } - } + await this.gameActive(); } else if (Date.now() < response.start) { - if ( - !response.walletsQueue.includes(this.walletAddress) && - !response.walletsBench.includes(this.walletAddress) - ) { + if (!this.walletsQueue.includes(this.walletAddress) && !this.walletsBench.includes(this.walletAddress)) { this.displayEnqueueButton(); } else if (this.enqueueButton) { this.enqueueButton.destroy(); } - await this.countdown(); } else { - this.scene.start(leaderboardSceneKey, { - players: response.players, - mainPlayer: { - walletAddress: this.walletAddress, - }, - }); + this.goToLeaderboard(response); } } } @@ -188,6 +152,71 @@ export default class LoungeAreaScene extends Phaser.Scene { } } + canParticipate() { + const logPref = `Lounge Area - canParticipate`; + if (!this.playersLimit) { + console.log(`${logPref} no player limit`); + return true; + } + if (!this.walletsQueue) { + console.log(`${logPref} no wallets queue`); + return true; + } + if (this.playersLimit > this.walletsQueue.length) { + console.log(`${logPref} no limit not yet reached`); + return true; + } + if (this.walletsQueue.indexOf(this.walletAddress) > -1) { + console.log(`${logPref} player on the wallets queue`); + return true; + } + console.log(`${logPref} - nope, limit reached ${this.playersLimit}`); + return false; + } + + async gameActive() { + if (this.beaverId) { + await this.goToMainScene(); + } else if (this.canParticipate()) { + await this.goToPlayerPick(); + } else { + this.header.setText(`The game has started and reached players limit.`); + } + } + + goToLeaderboard(response) { + this.scene.start(leaderboardSceneKey, { + players: response.players, + mainPlayer: { + walletAddress: this.walletAddress, + }, + }); + } + + async goToMainScene() { + const userName = (await this.profilePromise)?.Profile?.UserName; + this.scene.start(mainSceneKey, { + userName, + walletAddress: this.walletAddress, + beaverId: this.beaverId, + gameStart: this.gameStart, + gameEnd: this.gameEnd, + }); + } + + async goToPlayerPick() { + if (!this.running) { + this.running = true; + const userName = (await this.profilePromise)?.Profile?.UserName; + this.scene.start(playerPickSceneKey, { + userName, + walletAddress: this.walletAddress, + gameStart: this.gameStart, + gameEnd: this.gameEnd, + }); + } + } + displayEnqueueButton() { const self = this; this.enqueueButton = new TextButton(