Skip to content

Commit

Permalink
fix: notify client about new players and new games registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Aug 6, 2024
1 parent 1284c06 commit ffe166b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/game/backend/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ wss.on('connection', (ws, request) => {
msg?.Tags?.push({ name: 'From-Process', value: processTagValue });
}
console.log(`-- Sending message to ${msg.Target} `, msg);
addresseeProcess.state = await sendMessage(addresseeProcess.quickJS, addresseeProcess.state, msg);
const addresseeResult = await sendMessage(addresseeProcess.quickJS, addresseeProcess.state, msg);
addresseeProcess.state = addresseeResult.State;
if (addresseeResult.Output) {
logAndBroadcast(addresseeResult.Output, msg.Target);
}
} else {
// no process spawned in dev, probably aimed for AO, like transfer action
console.log(`-- No process, sending debit notice back to ${msg.Target} `, msg);
Expand Down Expand Up @@ -96,8 +100,7 @@ function logAndBroadcast(message, processId) {
async function sendMessage(quickJs, state, message) {
return await new Promise((resolve, reject) => {
setTimeout(async () => {
const result = await quickJs.handle(message, processEnv, state);
resolve(result.State);
resolve(await quickJs.handle(message, processEnv, state));
}, 10);
});
}
Expand Down
13 changes: 13 additions & 0 deletions src/game/process/cmd/info.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ export function standInQueue(state, action) {
walletsBench.push(walletAddress);
} else {
walletsQueue.push(walletAddress);
sendHubNotification(state);
}
return { walletsQueue, walletsBench };
}

function sendHubNotification(state) {
console.log(`---- GAME -- updating wallets queue`, state.hubProcessId);
ao.send({
Target: state.hubProcessId,
Data: '1234',
Action: JSON.stringify({
cmd: Const.Command.hubGamePlayers,
players: state.walletsQueue,
}),
});
}

export function gameStats(state) {
const { gameTreasuresCounter, lastTxs } = state;
return {
Expand Down
8 changes: 8 additions & 0 deletions src/game/process/hub.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ export function handle(state, message) {
...action.game,
};
console.log(`-- HUB registered game`, state.games[gameProcess]);
ao.result({
cmd: Const.Command.hubStats,
games: state.games,
});
break;

case Const.Command.hubGamePlayers:
console.log(`-- HUB updating players`, action);
state.games[gameProcess].players = action.players;
ao.result({
cmd: Const.Command.hubStats,
games: state.games,
});
break;

default:
Expand Down
1 change: 0 additions & 1 deletion tools/deploy-spawn-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { spawnGame, transferToken, setupGameContract } from './game-common.js';
import { replaceId } from './replace-id.js';
import { dateFromArg } from './common.mjs';
import { activeGamesConfig, hourSessionGamesConfig } from './deploy-spawn-session-config.js';
import ids from '../src/game/config/warp-ao-ids.js';

const jwk = JSON.parse(readFileSync('./.secrets/wallet.json', 'utf-8'));
const signer = new ArweaveSigner(jwk);
Expand Down

0 comments on commit ffe166b

Please sign in to comment.