diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index 45075b128..eb9b4b927 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -8,6 +8,7 @@ import {DonateExecution} from "../execution/DonateExecution" export type PlayerID = string export type Tick = number +export type Currency = number export const AllPlayers = "AllPlayers" as const; @@ -185,6 +186,7 @@ export interface Player { canSendEmoji(recipient: Player | typeof AllPlayers): boolean outgoingEmojis(): EmojiMessage[] canDonate(recipient: Player): boolean + currency(): Currency } export interface MutablePlayer extends Player { @@ -209,6 +211,8 @@ export interface MutablePlayer extends Player { transitiveTargets(): MutablePlayer[] sendEmoji(recipient: Player | typeof AllPlayers, emoji: string): void donate(recipient: MutablePlayer, troops: number): void + addCurrency(toAdd: Currency): void + removeCurrency(toRemove: Currency): void } export interface Game { diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index 5cfd738cf..e6226d98d 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -1,4 +1,4 @@ -import {MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, TargetPlayerEvent, EmojiMessage, EmojiMessageEvent, AllPlayers} from "./Game"; +import {MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, TargetPlayerEvent, EmojiMessage, EmojiMessageEvent, AllPlayers, Currency} from "./Game"; import {ClientID} from "../Schemas"; import {simpleHash} from "../Util"; import {CellString, GameImpl} from "./GameImpl"; @@ -18,6 +18,9 @@ class Donation { } export class PlayerImpl implements MutablePlayer { + + private _currency: number + isTraitor_ = false public _borderTiles: Set = new Set(); @@ -263,6 +266,21 @@ export class PlayerImpl implements MutablePlayer { this.gs.displayMessage(`Recieved ${renderTroops(troops)} troops from ${this.name()}`, MessageType.SUCCESS, recipient.id()) } + currency(): Currency { + return this._currency + } + + addCurrency(toAdd: Currency): void { + this._currency += toAdd + } + + removeCurrency(toRemove: Currency): void { + if (toRemove > this._currency) { + throw Error(`cannot remove ${toRemove} from ${this} because only has ${this._currency}`) + } + this._currency -= toRemove + } + hash(): number { return simpleHash(this.id()) * (this.troops() + this.numTilesOwned()); }