Exact source used by this build. SHA-256: 2614a55e9bb83aaa236f9cc0fc77bba62d111a286f3853cb5d086ed1c11c0938.

1 import type { WorldState } from "../../types";
2 
3 export function walletBalance(state: WorldState, playerId: string): number {
4   return state.wallet.mode === "shared" ? state.wallet.shared : state.wallet.balances[playerId] ?? 0;
5 }
6 /** Transaction helpers preserve revision; the owning handler increments it once. */
7 export function creditMoney(state: WorldState, playerId: string, amount: number): WorldState {
8   if (!Number.isSafeInteger(amount) || amount < 0 || !state.players[playerId]) return state;
9   const wallet = state.wallet.mode === "shared"
10     ? { ...state.wallet, shared: state.wallet.shared + amount }
11     : { ...state.wallet, balances: { ...state.wallet.balances, [playerId]: walletBalance(state, playerId) + amount } };
12   return { ...state, wallet };
13 }
14 export function debitMoney(state: WorldState, playerId: string, amount: number): WorldState | null {
15   if (!Number.isSafeInteger(amount) || amount < 0 || !state.players[playerId] || walletBalance(state, playerId) < amount) return null;
16   const wallet = state.wallet.mode === "shared"
17     ? { ...state.wallet, shared: state.wallet.shared - amount }
18     : { ...state.wallet, balances: { ...state.wallet.balances, [playerId]: walletBalance(state, playerId) - amount } };
19   return { ...state, wallet };
20 }
21