add currency to Player

This commit is contained in:
evanpelle
2024-10-19 20:32:58 -07:00
parent 2bbf0330b4
commit bb004b176b
2 changed files with 23 additions and 1 deletions
+4
View File
@@ -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 {
+19 -1
View File
@@ -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<Tile> = 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());
}