Skip to content

Commit

Permalink
fix: #58 avoid pick scene if limit reached, default wallets limit fix…
Browse files Browse the repository at this point in the history
…, show wallets queue as players count
  • Loading branch information
Tadeuchi committed Aug 7, 2024
1 parent ffe166b commit b9ed217
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/game/common/const.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const BEAVER_TYPES = {

// ------- Queue Config
const Queue = {
limit: 15,
defaultLimit: 15,
};

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/game/config/warp-ao-ids.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/game/process/cmd/info.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -29,7 +29,7 @@ function sendHubNotification(state) {
Data: '1234',
Action: JSON.stringify({
cmd: Const.Command.hubGamePlayers,
players: state.walletsQueue,
walletsQueue: state.walletsQueue,
}),
});
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/game/process/cmd/registerPlayer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
Expand Down Expand Up @@ -86,7 +86,7 @@ function sendHubNotification(state) {
Data: '1234',
Action: JSON.stringify({
cmd: Const.Command.hubGamePlayers,
players: Object.keys(state.players),
walletsQueue: state.walletsQueue,
}),
});
}
8 changes: 4 additions & 4 deletions src/game/process/cmd/setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/game/process/hub.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/game/scenes/GameHubScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
167 changes: 98 additions & 69 deletions src/game/scenes/LoungeAreaScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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',
});
Expand All @@ -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);
}
}

Expand All @@ -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);
Expand All @@ -144,50 +136,87 @@ 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);
}
}
}
break;
}
}

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(
Expand Down

0 comments on commit b9ed217

Please sign in to comment.