1253 lines
50 KiB
TypeScript
1253 lines
50 KiB
TypeScript
import assert from 'assert';
|
|
import { chunk, groupBy, isEqual, range, zip } from 'lodash';
|
|
import seedrandom from 'seedrandom';
|
|
import { availableMoves } from './available-moves';
|
|
import pointCards from './cards';
|
|
import { canReviseBid } from './choice-revisions';
|
|
import {
|
|
ContainerColor,
|
|
containerColors,
|
|
ContainerPiece,
|
|
FactoryPiece,
|
|
GameOptions,
|
|
GameState,
|
|
Phase,
|
|
Player,
|
|
ShipPosition,
|
|
} from './gamestate';
|
|
import { GameEventName, LogItem, LogMove } from './log';
|
|
import { Move, MoveName, Moves } from './move';
|
|
import { applyBankMove, bankUpkeep, normalizeBankDistribution, payBankCash, settleBank } from './offshore';
|
|
import { asserts, shuffle } from './utils';
|
|
|
|
export const playerColors = ['dodgerblue', 'red', 'yellow', 'limegreen', 'mediumorchid'];
|
|
|
|
export function setup(numPlayers: number, { beginner = true, offshoreIsland }: GameOptions, seed?: string): GameState {
|
|
seed = seed ?? Math.random().toString();
|
|
const rng = seedrandom(seed);
|
|
|
|
const cards = shuffle(pointCards, rng() + '');
|
|
|
|
const players: Player[] = range(numPlayers).map((id) => ({
|
|
id,
|
|
pointCard: cards.shift()!,
|
|
factories: [],
|
|
warehouses: [],
|
|
ship: {
|
|
piece: { id: `S${id}` },
|
|
containers: [],
|
|
shipPosition: ShipPosition.OpenSea,
|
|
},
|
|
containersOnFactoryStore: [],
|
|
containersOnWarehouseStore: [],
|
|
containersOnIsland: [],
|
|
money: 20,
|
|
loans: [],
|
|
produced: [],
|
|
availableMoves: null,
|
|
lastMove: null,
|
|
actions: 0,
|
|
isDropped: false,
|
|
bid: 0,
|
|
additionalBid: 0,
|
|
showBid: false,
|
|
showAdditionalBid: false,
|
|
isAI: false,
|
|
didDomesticSale: false,
|
|
}));
|
|
|
|
const totalContainers = players.length * 4;
|
|
const containersLeft: ContainerPiece[] = [];
|
|
for (const [containerIds, color] of zip(chunk(range(0, totalContainers * 5), totalContainers), containerColors)) {
|
|
containersLeft.push(...containerIds!.map((i) => ({ id: `C${i}` as const, color: color! })));
|
|
}
|
|
|
|
const factoriesLeft: FactoryPiece[] = [];
|
|
for (const [factoryIds, color] of zip(chunk(range(0, containerColors.length * 5), 5), containerColors)) {
|
|
factoriesLeft.push(...factoryIds!.map((i) => ({ id: `F${i}` as const, color: color! })));
|
|
}
|
|
|
|
const warehousesLeft = range(players.length * 5).map((i) => ({ id: `W${i}` }));
|
|
const loansLeft = range(players.length * 2).map((i) => ({ id: `L${i}` }));
|
|
|
|
const startingPlayer = Math.abs(rng.int32()) % players.length;
|
|
const G: GameState = {
|
|
players,
|
|
startingPlayer,
|
|
currentPlayers: [startingPlayer],
|
|
containersLeft,
|
|
factoriesLeft,
|
|
warehousesLeft,
|
|
loansLeft,
|
|
auctioningPlayer: -1,
|
|
highestBidders: [],
|
|
phase: Phase.Move,
|
|
options: { beginner },
|
|
log: [],
|
|
hiddenLog: [],
|
|
seed,
|
|
round: 1,
|
|
newTurn: true,
|
|
} as GameState;
|
|
|
|
const colors = shuffle(containerColors, rng() + '');
|
|
|
|
G.players.forEach((player, i) => {
|
|
const color = colors[i];
|
|
let index = G.factoriesLeft.findIndex((f) => f.color == color);
|
|
const factory = G.factoriesLeft.splice(index, 1)[0];
|
|
player.factories.push(factory);
|
|
|
|
index = G.containersLeft.findIndex((c) => c.color == color);
|
|
const container = G.containersLeft.splice(index, 1)[0];
|
|
player.containersOnFactoryStore.push({ piece: container, price: 2, moved: false });
|
|
|
|
player.warehouses.push(G.warehousesLeft.pop()!);
|
|
});
|
|
|
|
if (offshoreIsland) {
|
|
G.options.offshoreIsland = true;
|
|
G.offshoreBank = {
|
|
initialContainerLots: [],
|
|
cashLots: [1, 2, 3],
|
|
containerLots: [[], [], []],
|
|
auctions: [],
|
|
called: false,
|
|
won: false,
|
|
};
|
|
const bankColors = shuffle(containerColors, rng() + '');
|
|
for (let i = 0; i < 3; i++) {
|
|
const index = G.containersLeft.findIndex((c) => c.color === bankColors[i]);
|
|
G.offshoreBank.containerLots[i < 2 ? 0 : 1].push(G.containersLeft.splice(index, 1)[0]);
|
|
}
|
|
G.offshoreBank.initialContainerLots = G.offshoreBank.containerLots.map((lot) => lot.slice());
|
|
G.players.forEach((p) => {
|
|
p.bankHolding = [];
|
|
p.offshoreStats = { cashAuctionsWon: 0, containerAuctionsWon: 0 };
|
|
});
|
|
}
|
|
|
|
G.log.push({ type: 'event', event: { name: GameEventName.GameStart } });
|
|
|
|
G.players[startingPlayer].actions = 2;
|
|
G.players[startingPlayer].availableMoves = availableMoves(G, G.players[startingPlayer]);
|
|
|
|
return G;
|
|
}
|
|
|
|
export function stripSecret(G: GameState, player?: number): GameState {
|
|
return {
|
|
...G,
|
|
seed: 'secret',
|
|
hiddenLog: [],
|
|
players: G.players.map((pl, i) => {
|
|
if (player === i) {
|
|
return pl;
|
|
} else {
|
|
return {
|
|
...pl,
|
|
pointCard: null,
|
|
availableMoves: pl.availableMoves ? {} : null,
|
|
money: ended(G) ? pl.money : 0,
|
|
bid: pl.showBid ? pl.bid : 0,
|
|
additionalBid: pl.showAdditionalBid ? pl.additionalBid : 0,
|
|
// Bid moves contain sealed prices (including tie-breaks). Public results
|
|
// are exposed through the reveal-controlled fields and log, not lastMove.
|
|
lastMove: pl.lastMove?.name === MoveName.Bid ? null : pl.lastMove,
|
|
};
|
|
}
|
|
}),
|
|
log: G.log,
|
|
};
|
|
}
|
|
|
|
export function currentPlayers(G: GameState): number[] {
|
|
return G.currentPlayers;
|
|
}
|
|
|
|
export function move(G: GameState, move: Move, playerNumber: number, fake?: boolean): GameState {
|
|
const player = G.players[playerNumber];
|
|
const revision = move.revision !== undefined;
|
|
assert(!revision || canReviseBid(G, move, playerNumber), 'This bid can no longer be changed');
|
|
const available = revision ? availableMoves(G, player)[move.name] : player.availableMoves?.[move.name];
|
|
|
|
assert(revision || G.currentPlayers.includes(playerNumber), 'It is not your turn!');
|
|
assert(available, 'You are not allowed to run the command ' + move.name);
|
|
assert(
|
|
available.some((x) => isEqual(x, move.data)),
|
|
'Wrong argument for the command ' + move.name
|
|
);
|
|
assert(
|
|
move.name != MoveName.Bid ||
|
|
(Number.isInteger(move.extraData?.price) &&
|
|
move.extraData.price >= 0 &&
|
|
move.extraData.price + (G.highestBidders.length ? player.bid : 0) <= player.money),
|
|
"Can't bid more money than you have!"
|
|
);
|
|
|
|
if (G.offshoreBank && move.name === MoveName.Produce) {
|
|
assert(
|
|
move.extraData?.piece?.color === move.data &&
|
|
G.containersLeft.some((c) => isEqual(c, move.extraData.piece)),
|
|
'Invalid produced container'
|
|
);
|
|
assert(
|
|
Number.isInteger(move.extraData.price) && move.extraData.price >= 1 && move.extraData.price <= 4,
|
|
'Invalid production price'
|
|
);
|
|
}
|
|
G.liveUpdate = revision;
|
|
const previousBid = revision
|
|
? G.hiddenLog
|
|
.map((e) => e.type === 'move' && e.player === playerNumber && e.move.name === MoveName.Bid)
|
|
.lastIndexOf(true)
|
|
: -1;
|
|
const bankResult = applyBankMove(G, move, player);
|
|
if (bankResult.handled) {
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} ${move.name}`,
|
|
pretty: `${player.name} ${move.name}`,
|
|
});
|
|
if (bankResult.ending && !fake) {
|
|
G.phase = Phase.GameEnd;
|
|
G.currentPlayers = [];
|
|
calculateEndScore(G);
|
|
}
|
|
}
|
|
switch (move.name) {
|
|
// These moves were executed by applyBankMove; common bookkeeping follows below.
|
|
case MoveName.CallBank:
|
|
case MoveName.LoadBankContainer:
|
|
case MoveName.ReturnBankContainer:
|
|
case MoveName.DistributeBankContainer:
|
|
case MoveName.SeizeBankContainer:
|
|
break;
|
|
case MoveName.DomesticSale: {
|
|
asserts<Moves.MoveDomesticSale>(move);
|
|
if (player.containersOnFactoryStore.length > 0) {
|
|
const aux = player.containersOnFactoryStore.find((x) => isEqual(x.piece, move.data))!;
|
|
player.containersOnFactoryStore.splice(player.containersOnFactoryStore.indexOf(aux), 1);
|
|
} else {
|
|
const aux = player.containersOnWarehouseStore.find((x) => isEqual(x.piece, move.data))!;
|
|
player.containersOnWarehouseStore.splice(player.containersOnWarehouseStore.indexOf(aux), 1);
|
|
}
|
|
|
|
G.containersLeft.push(move.data);
|
|
|
|
player.money += 2;
|
|
player.didDomesticSale = true;
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} sells a ${move.data.color} container back to the supply for $2`,
|
|
pretty: `${playerNameHTML(player)} sells a ${containerColorHTML(
|
|
move.data.color
|
|
)} container back to the supply for $2`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.BuyFactory: {
|
|
asserts<Moves.MoveBuyFactory>(move);
|
|
remove(G.factoriesLeft, move.extraData);
|
|
player.factories.push(move.extraData);
|
|
player.money -= player.factories.length * 3;
|
|
player.actions--;
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} buys a ${move.data} factory for $${player.factories.length * 3}`,
|
|
pretty: `${playerNameHTML(player)} buys a ${containerColorHTML(move.data)} factory for $${
|
|
player.factories.length * 3
|
|
}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.BuyWarehouse: {
|
|
asserts<Moves.MoveBuyWarehouse>(move);
|
|
remove(G.warehousesLeft, move.extraData);
|
|
player.warehouses.push(move.extraData);
|
|
player.money -= player.warehouses.length + 2;
|
|
player.actions--;
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} buys a warehouse for $${player.warehouses.length + 2}`,
|
|
pretty: `${playerNameHTML(player)} buys a warehouse for $${player.warehouses.length + 2}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.BuyFromFactory: {
|
|
asserts<Moves.MoveBuyFromFactory>(move);
|
|
const otherPlayer = G.players[move.data.player];
|
|
const aux = otherPlayer.containersOnFactoryStore.find((x) => isEqual(x.piece, move.data.piece))!;
|
|
otherPlayer.containersOnFactoryStore.splice(otherPlayer.containersOnFactoryStore.indexOf(aux), 1);
|
|
player.containersOnWarehouseStore.push({
|
|
piece: move.data.piece,
|
|
price: move.extraData.price,
|
|
moved: false,
|
|
});
|
|
|
|
player.money -= aux.price;
|
|
otherPlayer.money += aux.price;
|
|
|
|
if (
|
|
player.lastMove?.name !== MoveName.BuyFromFactory ||
|
|
player.lastMove?.data.player !== move.data.player
|
|
) {
|
|
player.actions -= 1;
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} buys a ${move.data.piece.color} container from ${otherPlayer.name} for $${aux.price}, new price is $${move.extraData.price}`,
|
|
pretty: `${playerNameHTML(player)} buys a ${containerColorHTML(
|
|
move.data.piece.color
|
|
)} container from ${playerNameHTML(otherPlayer)} for $${aux.price}, new price is $${
|
|
move.extraData.price
|
|
}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.BuyFromWarehouse: {
|
|
asserts<Moves.MoveBuyFromWarehouse>(move);
|
|
const otherPlayer = G.players[move.data.player];
|
|
const aux = otherPlayer.containersOnWarehouseStore.find((x) => isEqual(x.piece, move.data.piece))!;
|
|
otherPlayer.containersOnWarehouseStore.splice(otherPlayer.containersOnWarehouseStore.indexOf(aux), 1);
|
|
player.ship.containers.push(move.data.piece);
|
|
|
|
player.money -= aux.price;
|
|
otherPlayer.money += aux.price;
|
|
|
|
if (
|
|
(player.lastMove?.name !== MoveName.BuyFromWarehouse ||
|
|
player.lastMove?.data.player !== move.data.player) &&
|
|
player.lastMove?.name !== MoveName.Sail
|
|
) {
|
|
player.actions -= 1;
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} buys a ${move.data.piece.color} container from ${otherPlayer.name} for $${aux.price}`,
|
|
pretty: `${playerNameHTML(player)} buys a ${containerColorHTML(
|
|
move.data.piece.color
|
|
)} container from ${playerNameHTML(otherPlayer)} for $${aux.price}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.GetLoan: {
|
|
asserts<Moves.MoveGetLoan>(move);
|
|
const loan = G.loansLeft.pop()!;
|
|
player.loans.push(loan);
|
|
player.money += 10;
|
|
|
|
const log: LogItem = {
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} takes a loan`,
|
|
pretty: `${playerNameHTML(player)} takes a loan`,
|
|
};
|
|
|
|
if (G.phase == Phase.Bid) {
|
|
G.hiddenLog.push(log);
|
|
} else {
|
|
G.log.push(log);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.PayLoan: {
|
|
asserts<Moves.MovePayLoan>(move);
|
|
const loan = player.loans.pop()!;
|
|
G.loansLeft.push(loan);
|
|
player.money -= 10;
|
|
|
|
const log: LogItem = {
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} pays a loan`,
|
|
pretty: `${playerNameHTML(player)} pays a loan`,
|
|
};
|
|
|
|
if (G.phase == Phase.Bid) {
|
|
G.hiddenLog.push(log);
|
|
} else {
|
|
G.log.push(log);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.Produce: {
|
|
asserts<Moves.MoveProduce>(move);
|
|
remove(G.containersLeft, move.extraData.piece);
|
|
player.containersOnFactoryStore.push({
|
|
piece: move.extraData.piece,
|
|
price: move.extraData.price,
|
|
moved: false,
|
|
});
|
|
player.produced.push(move.extraData.piece.color);
|
|
if (player.lastMove?.name !== MoveName.Produce) {
|
|
player.actions -= 1;
|
|
player.money -= 1;
|
|
playerBefore(player, G).money += 1;
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} produces a ${move.extraData.piece.color} container, price is $${move.extraData.price}`,
|
|
pretty: `${playerNameHTML(player)} produces a ${containerColorHTML(
|
|
move.extraData.piece.color
|
|
)} container, price is $${move.extraData.price}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.Sail: {
|
|
asserts<Moves.MoveSail>(move);
|
|
player.ship.shipPosition = move.data;
|
|
if (move.data === ShipPosition.Island) {
|
|
player.actions = 0;
|
|
G.auctioningPlayer = G.currentPlayers[0];
|
|
G.phase = Phase.Bid;
|
|
G.currentPlayers = G.players
|
|
.filter((p) => p.id !== G.auctioningPlayer && !p.isDropped)
|
|
.map((p) => p.id);
|
|
} else {
|
|
player.actions--;
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} sails to ${prettyShipPosition(G, move.data, true)}`,
|
|
pretty: `${playerNameHTML(player)} sails to ${prettyShipPosition(G, move.data)}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.ArrangeFactory: {
|
|
asserts<Moves.MoveArrangeFactory>(move);
|
|
const aux = player.containersOnFactoryStore.find((x) => isEqual(x.piece, move.data))!;
|
|
player.containersOnFactoryStore.splice(player.containersOnFactoryStore.indexOf(aux), 1);
|
|
player.containersOnFactoryStore.push({
|
|
piece: move.data,
|
|
price: move.extraData.price,
|
|
moved: true,
|
|
});
|
|
|
|
if (player.lastMove?.name !== MoveName.Produce && player.lastMove?.name !== MoveName.ArrangeFactory) {
|
|
player.actions--;
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} changes the price of a ${move.data.color} factory container from $${aux.price} to $${move.extraData.price}`,
|
|
pretty: `${playerNameHTML(player)} changes the price of a ${containerColorHTML(
|
|
move.data.color
|
|
)} factory container from $${aux.price} to $${move.extraData.price}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.ArrangeWarehouse: {
|
|
asserts<Moves.MoveArrangeWarehouse>(move);
|
|
const aux = player.containersOnWarehouseStore.find((x) => isEqual(x.piece, move.data))!;
|
|
player.containersOnWarehouseStore.splice(player.containersOnWarehouseStore.indexOf(aux), 1);
|
|
player.containersOnWarehouseStore.push({
|
|
piece: move.data,
|
|
price: move.extraData.price,
|
|
moved: true,
|
|
});
|
|
|
|
if (
|
|
player.lastMove?.name !== MoveName.BuyFromFactory &&
|
|
player.lastMove?.name !== MoveName.ArrangeWarehouse
|
|
) {
|
|
player.actions--;
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} changes the price of a ${move.data.color} warehouse container from $${aux.price} to $${move.extraData.price}`,
|
|
pretty: `${playerNameHTML(player)} changes the price of a ${containerColorHTML(
|
|
move.data.color
|
|
)} warehouse container from $${aux.price} to $${move.extraData.price}`,
|
|
});
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.Bid: {
|
|
asserts<Moves.MoveBid>(move);
|
|
const { revision: ignored, ...recordedMove } = move;
|
|
|
|
if (G.highestBidders.length === 0) {
|
|
G.hiddenLog.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move: recordedMove,
|
|
simple: `${player.name} bids $${move.extraData.price}`,
|
|
pretty: `${playerNameHTML(player)} bids $${move.extraData.price}`,
|
|
});
|
|
player.bid = move.extraData.price;
|
|
G.currentPlayers = G.currentPlayers.filter((id) => id !== player.id);
|
|
} else {
|
|
G.hiddenLog.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move: recordedMove,
|
|
simple: `${player.name} bids additional $${move.extraData.price}`,
|
|
pretty: `${playerNameHTML(player)} bids additional $${move.extraData.price}`,
|
|
});
|
|
player.additionalBid = move.extraData.price;
|
|
G.currentPlayers = G.currentPlayers.filter((id) => id !== player.id);
|
|
}
|
|
|
|
if (revision && previousBid >= 0) {
|
|
G.hiddenLog[previousBid] = G.hiddenLog.pop()!;
|
|
}
|
|
if (G.currentPlayers.length === 0) {
|
|
if (G.highestBidders.length === 0) {
|
|
G.players
|
|
.filter((p) => p.id !== G.auctioningPlayer)
|
|
.forEach((p) => {
|
|
p.showBid = true;
|
|
});
|
|
// Only players still in the game can win the auction, so a dropped
|
|
// player's bid must not enter the maximum either.
|
|
const eligible = G.players.filter((p) => p.id != G.auctioningPlayer && !p.isDropped);
|
|
const highestBid = Math.max(...eligible.map((p) => p.bid));
|
|
const highestBidders = eligible.filter((p) => p.bid === highestBid).map((p) => p.id);
|
|
G.highestBidders = highestBidders;
|
|
if (highestBidders.length > 1) {
|
|
G.currentPlayers = highestBidders;
|
|
} else {
|
|
G.currentPlayers = [G.auctioningPlayer!];
|
|
G.phase = Phase.AcceptDecline;
|
|
}
|
|
} else {
|
|
const eligible = G.players.filter((p) => p.id != G.auctioningPlayer && !p.isDropped);
|
|
const highestBid = Math.max(...eligible.map((p) => p.bid + p.additionalBid));
|
|
const highestBidders = eligible
|
|
.filter((p) => p.bid + p.additionalBid === highestBid)
|
|
.map((p) => p.id);
|
|
G.highestBidders = highestBidders;
|
|
G.players.forEach((p) => {
|
|
p.showAdditionalBid = true;
|
|
});
|
|
G.currentPlayers = [G.auctioningPlayer!];
|
|
G.phase = Phase.AcceptDecline;
|
|
}
|
|
}
|
|
|
|
if (G.phase == Phase.AcceptDecline) {
|
|
G.log.push(...G.hiddenLog);
|
|
G.hiddenLog = [];
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.Accept: {
|
|
const otherPlayer = G.players[move.data];
|
|
const bid = otherPlayer.bid + otherPlayer.additionalBid;
|
|
otherPlayer.containersOnIsland.push(...player.ship.containers);
|
|
player.ship.containers = [];
|
|
otherPlayer.money -= bid;
|
|
player.money += bid * 2;
|
|
G.players.forEach((p) => {
|
|
p.bid = p.additionalBid = 0;
|
|
});
|
|
G.players.forEach((p) => {
|
|
p.showBid = p.showAdditionalBid = false;
|
|
});
|
|
G.auctioningPlayer = undefined;
|
|
G.highestBidders = [];
|
|
G.phase = Phase.Move;
|
|
|
|
nextPlayer(G);
|
|
if (!fake) {
|
|
if ([...new Set(G.containersLeft.map((c) => c.color))].length >= 4) {
|
|
doUpkeep(G);
|
|
} else {
|
|
if (!G.offshoreBank || !settleBank(G, true)) {
|
|
G.phase = Phase.GameEnd;
|
|
G.currentPlayers = [];
|
|
calculateEndScore(G);
|
|
}
|
|
}
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} accepts ${otherPlayer.name}'s bid of $${bid} and receives $${bid * 2}`,
|
|
pretty: `${playerNameHTML(player)} accepts ${playerNameHTML(
|
|
otherPlayer
|
|
)}'s bid of $${bid} and receives $${bid * 2}`,
|
|
});
|
|
if (!fake) {
|
|
const entry = G.log[G.log.length - 1] as LogMove;
|
|
(G.pendingMessages ||= []).push(entry.simple);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.Decline: {
|
|
player.containersOnIsland.push(...player.ship.containers);
|
|
player.ship.containers = [];
|
|
const bid = G.players[G.highestBidders[0]].bid + G.players[G.highestBidders[0]].additionalBid;
|
|
player.money -= bid;
|
|
payBankCash(G, bid);
|
|
G.players.forEach((p) => {
|
|
p.bid = p.additionalBid = 0;
|
|
});
|
|
G.players.forEach((p) => {
|
|
p.showBid = p.showAdditionalBid = false;
|
|
});
|
|
G.auctioningPlayer = undefined;
|
|
G.highestBidders = [];
|
|
G.phase = Phase.Move;
|
|
|
|
nextPlayer(G);
|
|
if (!fake) {
|
|
if ([...new Set(G.containersLeft.map((c) => c.color))].length >= 4) {
|
|
doUpkeep(G);
|
|
} else {
|
|
if (!G.offshoreBank || !settleBank(G, true)) {
|
|
G.phase = Phase.GameEnd;
|
|
G.currentPlayers = [];
|
|
calculateEndScore(G);
|
|
}
|
|
}
|
|
}
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} declines all bids and buys the cargo for $${bid}`,
|
|
pretty: `${playerNameHTML(player)} declines all bids and buys the cargo for $${bid}`,
|
|
});
|
|
if (!fake) {
|
|
const entry = G.log[G.log.length - 1] as LogMove;
|
|
(G.pendingMessages ||= []).push(entry.simple);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case MoveName.Pass: {
|
|
asserts<Moves.MovePass>(move);
|
|
|
|
G.log.push({
|
|
type: 'move',
|
|
player: playerNumber,
|
|
move,
|
|
simple: `${player.name} passes`,
|
|
pretty: `${playerNameHTML(player)} passes`,
|
|
});
|
|
|
|
nextPlayer(G);
|
|
if (!fake) {
|
|
if ([...new Set(G.containersLeft.map((c) => c.color))].length >= 4) {
|
|
doUpkeep(G);
|
|
} else {
|
|
if (!G.offshoreBank || !settleBank(G, true)) {
|
|
G.phase = Phase.GameEnd;
|
|
G.currentPlayers = [];
|
|
calculateEndScore(G);
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Normalize after every transition that can create a payment decision, before
|
|
// publishing legal moves. Forced placements are effects, not synthetic player moves.
|
|
const forcedBankResult = normalizeBankDistribution(G);
|
|
if (forcedBankResult.ending && !fake) {
|
|
G.phase = Phase.GameEnd;
|
|
G.currentPlayers = [];
|
|
calculateEndScore(G);
|
|
}
|
|
|
|
player.availableMoves = null;
|
|
|
|
if (move.name != MoveName.GetLoan && move.name != MoveName.PayLoan) {
|
|
const { revision: ignoredRevision, ...recordedMove } = move;
|
|
player.lastMove = recordedMove;
|
|
}
|
|
|
|
G.currentPlayers.forEach((p) => (G.players[p].availableMoves = availableMoves(G, G.players[p])));
|
|
|
|
// Tentative-turn bookkeeping: the state is committed (`newTurn`) once the mover can
|
|
// no longer undo. This mirrors the old undo-availability rule — undo was offered
|
|
// whenever the last visible log entry was a move by a player still in
|
|
// `currentPlayers` — with turn-boundary moves committing unconditionally:
|
|
// - Pass / Accept / Decline end the turn (even in the edge case where the same
|
|
// player is up again because everyone else is dropped);
|
|
// - Bid (and loans taken during the bid phase) go to the hidden log and were never
|
|
// undoable: bids are single-move committed turns.
|
|
const lastLog = G.log[G.log.length - 1];
|
|
const turnBoundary =
|
|
move.name === MoveName.Pass ||
|
|
move.name === MoveName.Accept ||
|
|
move.name === MoveName.Decline ||
|
|
move.name === MoveName.Bid;
|
|
G.newTurn =
|
|
(bankResult.handled && move.name !== MoveName.LoadBankContainer && move.name !== MoveName.CallBank) ||
|
|
(bankResult.handled && G.phase === Phase.BankDecision) ||
|
|
turnBoundary ||
|
|
!(lastLog?.type === 'move' && G.currentPlayers.includes(lastLog.player));
|
|
|
|
while (G.offshoreBank && G.phase === Phase.BankDecision && G.players[G.currentPlayers[0]].isDropped) {
|
|
G = moveAI(G, G.currentPlayers[0]);
|
|
}
|
|
return G;
|
|
}
|
|
|
|
export function moveAI(G: GameState, playerNumber: number): GameState {
|
|
const player = G.players[playerNumber];
|
|
if (G.offshoreBank && G.phase === Phase.BankDecision) {
|
|
const name = Object.keys(player.availableMoves!)[0] as MoveName;
|
|
const data = player.availableMoves![name]![0];
|
|
const c = G.offshoreBank.decision?.containers.find((c) => c.container.piece.id === (data as ContainerPiece).id);
|
|
return move(
|
|
G,
|
|
{
|
|
name,
|
|
data,
|
|
...(name === MoveName.ReturnBankContainer ? { extraData: { price: c!.container.price } } : {}),
|
|
} as Move,
|
|
playerNumber
|
|
);
|
|
}
|
|
// Keep expansion decisions deterministic and entirely outside the classic random stream.
|
|
if (G.offshoreBank && G.phase === Phase.Move) {
|
|
const moves = player.availableMoves!;
|
|
const load = moves[MoveName.LoadBankContainer];
|
|
if (load?.length) return move(G, { name: MoveName.LoadBankContainer, data: load[0] }, playerNumber);
|
|
|
|
// Collect paid-for cargo before shopping for more. Do not detour a full ship.
|
|
if (player.bankHolding!.length && player.ship.containers.length < 5) {
|
|
const destination = moves[MoveName.Sail]?.includes(ShipPosition.OffshoreBank)
|
|
? ShipPosition.OffshoreBank
|
|
: player.ship.shipPosition !== ShipPosition.OpenSea &&
|
|
player.ship.shipPosition !== ShipPosition.OffshoreBank &&
|
|
moves[MoveName.Sail]?.includes(ShipPosition.OpenSea)
|
|
? ShipPosition.OpenSea
|
|
: undefined;
|
|
if (destination) return move(G, { name: MoveName.Sail, data: destination }, playerNumber);
|
|
}
|
|
|
|
let offer: Move | undefined;
|
|
let bestProfit = 0;
|
|
for (const data of moves[MoveName.CallBank] || []) {
|
|
const active = G.offshoreBank.auctions.find((a) => a.type === data.type);
|
|
if (data.type === 'containers') {
|
|
const count = G.offshoreBank.containerLots[data.lot].length;
|
|
const cash = (active?.cash || 0) + 1;
|
|
// Value cargo conservatively, cap the backlog, and reserve working cash plus interest.
|
|
const profit = count * 2 - cash;
|
|
if (
|
|
count &&
|
|
count + player.bankHolding!.length <= 5 &&
|
|
cash <= player.money - player.loans.length - 5 &&
|
|
profit > bestProfit
|
|
) {
|
|
offer = { name: MoveName.CallBank, data, extraData: { cash } };
|
|
bestProfit = profit;
|
|
}
|
|
} else {
|
|
const count = (active?.containers.length || 0) + 1;
|
|
const stock = [
|
|
...player.containersOnFactoryStore.map((c) => ({ c, value: Math.max(2, c.price) })),
|
|
...player.containersOnWarehouseStore.map((c) => ({ c, value: Math.max(3, c.price) })),
|
|
].sort((a, b) => a.value - b.value);
|
|
const payment = stock.slice(0, count);
|
|
const profit = G.offshoreBank.cashLots[data.lot] - payment.reduce((sum, c) => sum + c.value, 0);
|
|
// Stored cargo is productive inventory: only exchange it for a clear cash gain.
|
|
if (payment.length === count && profit >= 2 && profit > bestProfit) {
|
|
offer = {
|
|
name: MoveName.CallBank,
|
|
data,
|
|
extraData: { containers: payment.map((c) => c.c.piece.id) },
|
|
};
|
|
bestProfit = profit;
|
|
}
|
|
}
|
|
}
|
|
if (offer) return move(G, offer, playerNumber);
|
|
}
|
|
let moveName, data;
|
|
let retry = 0;
|
|
do {
|
|
if (player.actions > 0 && player.money < 5 && player.loans.length < 2 && player.availableMoves!.getLoan) {
|
|
moveName = MoveName.GetLoan;
|
|
const dataArr = player.availableMoves![moveName];
|
|
data = dataArr[Math.floor(Math.random() * dataArr.length)];
|
|
} else if (player.money > 15 && player.loans.length > 0 && player.availableMoves!.payLoan) {
|
|
moveName = MoveName.PayLoan;
|
|
const dataArr = player.availableMoves![moveName];
|
|
data = dataArr[Math.floor(Math.random() * dataArr.length)];
|
|
} else if (
|
|
G.phase == Phase.Move &&
|
|
player.actions > 0 &&
|
|
player.ship.containers.length > 0 &&
|
|
player.lastMove?.name != MoveName.BuyFromWarehouse
|
|
) {
|
|
moveName = MoveName.Sail;
|
|
data = player.ship.shipPosition == ShipPosition.OpenSea ? ShipPosition.Island : ShipPosition.OpenSea;
|
|
} else {
|
|
const moves = Object.keys(player.availableMoves!).filter((n) => n !== MoveName.CallBank);
|
|
|
|
if (
|
|
player.lastMove?.name == MoveName.Sail &&
|
|
player.ship.containers.length < 5 &&
|
|
((player.lastMove?.data.startsWith('playerHarbor0') &&
|
|
G.players[0].containersOnWarehouseStore.length > 0) ||
|
|
(player.lastMove?.data.startsWith('playerHarbor1') &&
|
|
G.players[1].containersOnWarehouseStore.length > 0) ||
|
|
(player.lastMove?.data.startsWith('playerHarbor2') &&
|
|
G.players[2].containersOnWarehouseStore.length > 0) ||
|
|
(player.lastMove?.data.startsWith('playerHarbor3') &&
|
|
G.players[3].containersOnWarehouseStore.length > 0) ||
|
|
(player.lastMove?.data.startsWith('playerHarbor4') &&
|
|
G.players[4].containersOnWarehouseStore.length > 0))
|
|
) {
|
|
moveName = MoveName.BuyFromWarehouse;
|
|
} else {
|
|
moveName = moves[Math.floor(Math.random() * moves.length)];
|
|
}
|
|
|
|
let dataArr = player.availableMoves![moveName];
|
|
if (!dataArr) {
|
|
moveName = moves[Math.floor(Math.random() * moves.length)];
|
|
dataArr = player.availableMoves![moveName];
|
|
}
|
|
|
|
data = dataArr[Math.floor(Math.random() * dataArr.length)];
|
|
|
|
if (moveName == MoveName.GetLoan || moveName == MoveName.PayLoan) {
|
|
moveName = null;
|
|
} else if (moveName == MoveName.Sail) {
|
|
if (data == ShipPosition.Island) {
|
|
if (player.ship.containers.length == 0) moveName = null;
|
|
} else if (G.offshoreBank && data === ShipPosition.OffshoreBank) {
|
|
if (!player.bankHolding!.length || player.ship.containers.length === 5) moveName = null;
|
|
} else if (data.startsWith('playerHarbor0')) {
|
|
if (G.players[0].containersOnWarehouseStore.length == 0) moveName = null;
|
|
} else if (data.startsWith('playerHarbor1')) {
|
|
if (G.players[1].containersOnWarehouseStore.length == 0) moveName = null;
|
|
} else if (data.startsWith('playerHarbor2')) {
|
|
if (G.players[2].containersOnWarehouseStore.length == 0) moveName = null;
|
|
} else if (data.startsWith('playerHarbor3')) {
|
|
if (G.players[3].containersOnWarehouseStore.length == 0) moveName = null;
|
|
} else if (data.startsWith('playerHarbor4')) {
|
|
if (G.players[4].containersOnWarehouseStore.length == 0) moveName = null;
|
|
} else {
|
|
if (player.lastMove?.name == MoveName.Sail) moveName = null;
|
|
}
|
|
} else if (moveName == MoveName.ArrangeFactory) {
|
|
if (player.lastMove?.name != MoveName.Produce) moveName = null;
|
|
} else if (moveName == MoveName.ArrangeWarehouse) {
|
|
if (player.lastMove?.name != MoveName.BuyFromFactory) moveName = null;
|
|
} else if (moveName == MoveName.GetLoan) {
|
|
moveName = null;
|
|
} else if (moveName == MoveName.Produce) {
|
|
if (player.factories.length < 2) {
|
|
moveName = null;
|
|
}
|
|
} else if (moveName == MoveName.BuyFromFactory) {
|
|
if (player.warehouses.length < 2) {
|
|
moveName = null;
|
|
}
|
|
} else if (moveName == MoveName.Pass) {
|
|
if (retry < 10 && player.actions > 0 && Object.keys(player.availableMoves!).length > 3) {
|
|
moveName = null;
|
|
}
|
|
} else if (moveName == MoveName.BuyFactory) {
|
|
if (player.factories.length == 3 || player.money < 15) moveName = null;
|
|
} else if (moveName == MoveName.BuyWarehouse) {
|
|
if (player.warehouses.length == 3 || player.money < 17) moveName = null;
|
|
} else if (moveName == MoveName.DomesticSale) {
|
|
if (player.money > 5) moveName = null;
|
|
}
|
|
}
|
|
|
|
retry++;
|
|
} while (!moveName);
|
|
|
|
let playerMove: Move;
|
|
switch (moveName) {
|
|
case MoveName.BuyFromFactory:
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: { price: Math.floor(Math.random() * 3) + 2 },
|
|
};
|
|
break;
|
|
|
|
case MoveName.BuyFactory:
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: G.factoriesLeft.find((p) => p.color == data),
|
|
};
|
|
break;
|
|
|
|
case MoveName.BuyWarehouse:
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: G.warehousesLeft[0],
|
|
};
|
|
break;
|
|
|
|
case MoveName.Produce:
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: {
|
|
piece: G.containersLeft.find((p) => p.color == data),
|
|
price: Math.floor(Math.random() * 3) + 1,
|
|
},
|
|
};
|
|
break;
|
|
|
|
case MoveName.ArrangeFactory:
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: { price: Math.floor(Math.random() * 3) + 1 },
|
|
};
|
|
break;
|
|
|
|
case MoveName.ArrangeWarehouse:
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: { price: Math.floor(Math.random() * 3) + 2 },
|
|
};
|
|
break;
|
|
|
|
case MoveName.Bid: {
|
|
const bid = Math.floor(Math.random() * (player.money - player.bid));
|
|
playerMove = {
|
|
name: moveName,
|
|
data,
|
|
extraData: { price: bid > 10 ? Math.ceil(bid / 2) : bid },
|
|
};
|
|
break;
|
|
}
|
|
|
|
default:
|
|
playerMove = { name: moveName, data };
|
|
break;
|
|
}
|
|
|
|
return move(G, playerMove, playerNumber);
|
|
}
|
|
|
|
export function ended(G: GameState): boolean {
|
|
return G.phase == Phase.GameEnd;
|
|
}
|
|
|
|
function calculateEndScore(G: GameState) {
|
|
G.players.forEach((player) => {
|
|
player.finalScoreBreakdown = [];
|
|
player.finalScoreBreakdown.push('$' + player.money);
|
|
|
|
if (player.containersOnIsland.length > 0) {
|
|
const hasOneOfEach = [...new Set(player.containersOnIsland.map((c) => c.color))].length == 5;
|
|
|
|
const grouped = groupBy(player.containersOnIsland, (piece) => piece.color);
|
|
const most = Object.keys(grouped).reduce((a, b) => {
|
|
if (grouped[a].length == grouped[b].length) {
|
|
if (a == player.pointCard!.containerValues[1].containerColor) {
|
|
return a;
|
|
} else if (b == player.pointCard!.containerValues[1].containerColor) {
|
|
return b;
|
|
} else {
|
|
const aValue = player.pointCard!.containerValues.find(
|
|
(cv) => cv.containerColor == a
|
|
)!.baseValue;
|
|
const bValue = player.pointCard!.containerValues.find(
|
|
(cv) => cv.containerColor == b
|
|
)!.baseValue;
|
|
return aValue > bValue ? b : a;
|
|
}
|
|
}
|
|
|
|
return grouped[a].length > grouped[b].length ? a : b;
|
|
});
|
|
|
|
const points = player.pointCard!.containerValues.map((cv) => {
|
|
if (grouped[cv.containerColor]) {
|
|
player.finalScoreBreakdown!.push(
|
|
`$${grouped[cv.containerColor].length * (hasOneOfEach ? cv.specialValue : cv.baseValue)} (${
|
|
cv.containerColor
|
|
} x ${grouped[cv.containerColor].length})`
|
|
);
|
|
return grouped[cv.containerColor].length * (hasOneOfEach ? cv.specialValue : cv.baseValue);
|
|
} else {
|
|
player.finalScoreBreakdown!.push(`$0 (${cv.containerColor} x 0)`);
|
|
return 0;
|
|
}
|
|
});
|
|
|
|
player.money += points.reduce((a, b) => a + b, 0);
|
|
|
|
const mostValue = player.pointCard!.containerValues.find((cv) => cv.containerColor === most)!;
|
|
player.finalScoreBreakdown!.push(
|
|
'-$' + grouped[most].length * (hasOneOfEach ? mostValue.specialValue : mostValue.baseValue)
|
|
);
|
|
player.money -= grouped[most].length * (hasOneOfEach ? mostValue.specialValue : mostValue.baseValue);
|
|
} else {
|
|
player.finalScoreBreakdown.push('-');
|
|
player.finalScoreBreakdown.push('-');
|
|
player.finalScoreBreakdown.push('-');
|
|
player.finalScoreBreakdown.push('-');
|
|
player.finalScoreBreakdown.push('-');
|
|
player.finalScoreBreakdown.push('-');
|
|
}
|
|
|
|
player.finalScoreBreakdown.push('$' + player.containersOnWarehouseStore.length * 2);
|
|
player.money += player.containersOnWarehouseStore.length * 2;
|
|
player.finalScoreBreakdown.push('$' + (player.ship.containers.length + (player.bankHolding?.length || 0)) * 3);
|
|
player.money += (player.ship.containers.length + (player.bankHolding?.length || 0)) * 3;
|
|
player.finalScoreBreakdown.push('-$' + player.loans.length * 11);
|
|
player.money += player.loans.length * -11;
|
|
});
|
|
}
|
|
|
|
export function scores(G: GameState): number[] {
|
|
return ended(G) ? G.players.map((p) => p.money) : G.players.map((_) => 0);
|
|
}
|
|
|
|
function playerBefore(player: Player, G: GameState) {
|
|
return player.id === 0 ? G.players[G.players.length - 1] : G.players[player.id - 1];
|
|
}
|
|
|
|
export function nextPlayer(G: GameState) {
|
|
G.currentPlayers = [(G.currentPlayers[0] + 1) % G.players.length];
|
|
|
|
if (G.players[G.currentPlayers[0]].isDropped && G.players.some((p) => !p.isDropped)) nextPlayer(G);
|
|
}
|
|
|
|
function doUpkeep(G: GameState) {
|
|
// Container seizures must be reproducible: the platform guarantees that the same
|
|
// seed yields the same game, and `wrapper.replay` re-runs upkeep from the log. The
|
|
// seed comes from the persisted state, and the visible log length uniquely
|
|
// identifies this upkeep within the game (each upkeep immediately follows its own
|
|
// Pass/Accept/Decline log entry), so together they pin a per-upkeep random stream.
|
|
const rng = seedrandom(`${G.seed}:upkeep:${G.log.length}`);
|
|
const player = G.players[G.currentPlayers[0]];
|
|
const loanCount = G.offshoreBank ? 0 : player.loans.length;
|
|
const interest: string[] = [];
|
|
for (let i = 0; i < loanCount; i++) {
|
|
if (player.money > 0) {
|
|
player.money--;
|
|
interest.push('money');
|
|
} else if (player.containersOnIsland.length > 0) {
|
|
const container = removeRandom(player.containersOnIsland, rng);
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a ${containerColorHTML(container.color)} container from ${playerNameHTML(
|
|
player
|
|
)}'s island`,
|
|
},
|
|
});
|
|
} else if (player.containersOnWarehouseStore.length + player.containersOnFactoryStore.length > 0) {
|
|
if (player.containersOnWarehouseStore.length >= 2) {
|
|
const c1 = removeRandom(player.containersOnWarehouseStore, rng);
|
|
const c2 = removeRandom(player.containersOnWarehouseStore, rng);
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a ${containerColorHTML(
|
|
c1.piece.color
|
|
)} container and a ${containerColorHTML(c2.piece.color)} container from ${playerNameHTML(
|
|
player
|
|
)}'s warehouses`,
|
|
},
|
|
});
|
|
} else if (player.containersOnWarehouseStore.length == 1) {
|
|
const c1 = removeRandom(player.containersOnWarehouseStore, rng);
|
|
if (player.containersOnFactoryStore.length >= 1) {
|
|
const c2 = removeRandom(player.containersOnFactoryStore, rng);
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a ${containerColorHTML(
|
|
c1.piece.color
|
|
)} container from ${playerNameHTML(player)}'s warehouses and a ${containerColorHTML(
|
|
c2.piece.color
|
|
)} container from ${playerNameHTML(player)}'s factory`,
|
|
},
|
|
});
|
|
} else {
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a ${containerColorHTML(
|
|
c1.piece.color
|
|
)} container from ${playerNameHTML(player)}'s warehouses`,
|
|
},
|
|
});
|
|
}
|
|
} else {
|
|
const c1 = removeRandom(player.containersOnFactoryStore, rng);
|
|
if (player.containersOnFactoryStore.length >= 1) {
|
|
const c2 = removeRandom(player.containersOnFactoryStore, rng);
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a ${containerColorHTML(
|
|
c1.piece.color
|
|
)} container and a ${containerColorHTML(c2.piece.color)} container from ${playerNameHTML(
|
|
player
|
|
)}'s factory`,
|
|
},
|
|
});
|
|
} else {
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a ${containerColorHTML(
|
|
c1.piece.color
|
|
)} container from ${playerNameHTML(player)}'s factory`,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
} else if (player.warehouses.length > 2) {
|
|
player.warehouses.pop();
|
|
const loan = player.loans.pop()!;
|
|
G.loansLeft.push(loan);
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a warehouse from ${playerNameHTML(player)}`,
|
|
},
|
|
});
|
|
} else if (player.factories.length > 2) {
|
|
player.factories.pop();
|
|
const loan = player.loans.pop()!;
|
|
G.loansLeft.push(loan);
|
|
G.log.push({
|
|
type: 'event',
|
|
event: {
|
|
name: GameEventName.Upkeep,
|
|
interest: `The bank seizes a factory from ${playerNameHTML(player)}`,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
const currentPlayer = G.currentPlayers[0];
|
|
G.players[currentPlayer].actions = 2;
|
|
G.players[currentPlayer].produced = [];
|
|
G.players[currentPlayer].containersOnFactoryStore.forEach((c) => (c.moved = false));
|
|
G.players[currentPlayer].containersOnWarehouseStore.forEach((c) => (c.moved = false));
|
|
G.players[currentPlayer].didDomesticSale = false;
|
|
|
|
if (currentPlayer == G.startingPlayer) {
|
|
G.round++;
|
|
}
|
|
if (G.offshoreBank) bankUpkeep(G);
|
|
}
|
|
|
|
function remove(array, value) {
|
|
const aux = array.find((x) => isEqual(x, value));
|
|
return array.splice(array.indexOf(aux), 1);
|
|
}
|
|
|
|
function removeRandom<T>(array: T[], rng: () => number): T {
|
|
return array.splice(Math.floor(rng() * array.length), 1)[0];
|
|
}
|
|
|
|
function prettyShipPosition(G: GameState, data: ShipPosition, simple = false): string {
|
|
switch (data) {
|
|
case ShipPosition.OffshoreBank:
|
|
return 'the Off-Shore Bank';
|
|
case ShipPosition.Island:
|
|
return 'the island harbor';
|
|
case ShipPosition.OpenSea:
|
|
return 'the open sea';
|
|
default: {
|
|
const index = data[12];
|
|
return simple ? `${G.players[index].name}'s harbor` : `${playerNameHTML(G.players[index])}'s harbor`;
|
|
}
|
|
}
|
|
}
|
|
|
|
function playerNameHTML(player) {
|
|
return `<span style="background-color: ${playerColors[player.id]}; font-weight: bold; padding: 0 3px;">${
|
|
player.name
|
|
}</span>`;
|
|
}
|
|
|
|
function containerColorHTML(containerColor: ContainerColor) {
|
|
switch (containerColor) {
|
|
case ContainerColor.Brown:
|
|
case ContainerColor.Orange:
|
|
case ContainerColor.Tan:
|
|
case ContainerColor.White:
|
|
return `<span style="background-color: ${containerColor}; color: ${
|
|
containerColor === ContainerColor.Brown ? '#fff' : '#111'
|
|
}; font-weight: bold; border: 1px solid black; padding: 0 3px;">${containerColor}</span>`;
|
|
case ContainerColor.Black:
|
|
return '<span style="background-color: #2F4F4F; color: #fff; font-weight: bold; border: 1px solid black; padding: 0 3px;">darkslategray</span>';
|
|
}
|
|
}
|