Finishing implementation
This commit is contained in:
parent
6e02f2ab14
commit
08b526a546
9 changed files with 404 additions and 333 deletions
66
.eslintrc.js
66
.eslintrc.js
|
|
@ -1,35 +1,35 @@
|
|||
module.exports = {
|
||||
root: true,
|
||||
env: {
|
||||
node: true
|
||||
},
|
||||
parserOptions: {
|
||||
ecmaVersion: 2020
|
||||
},
|
||||
parser: '@typescript-eslint/parser',
|
||||
plugins: [
|
||||
'@typescript-eslint',
|
||||
],
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/eslint-recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
],
|
||||
rules: {
|
||||
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
|
||||
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
|
||||
semi: ['error', 'always'],
|
||||
'@typescript-eslint/no-non-null-assertion': 'off',
|
||||
// '@typescript-eslint/consistent-type-assertions': 'off',
|
||||
// '@typescript-eslint/no-explicit-any': 'off',
|
||||
'@typescript-eslint/no-empty-function': 'off',
|
||||
// '@typescript-eslint/no-unused-vars': 'off',
|
||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||
'@typescript-eslint/no-use-before-define': 'off',
|
||||
'@typescript-eslint/no-namespace': 'off',
|
||||
'no-inner-declarations': 'off',
|
||||
'no-return-assign': 'off',
|
||||
// 'no-fallthrough': 'off',
|
||||
quotes: 'off'
|
||||
}
|
||||
root: true,
|
||||
env: {
|
||||
node: true
|
||||
},
|
||||
parserOptions: {
|
||||
ecmaVersion: 2020
|
||||
},
|
||||
parser: '@typescript-eslint/parser',
|
||||
plugins: [
|
||||
'@typescript-eslint',
|
||||
],
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/eslint-recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
],
|
||||
rules: {
|
||||
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
|
||||
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
|
||||
semi: ['error', 'always'],
|
||||
'@typescript-eslint/no-non-null-assertion': 'off',
|
||||
// '@typescript-eslint/consistent-type-assertions': 'off',
|
||||
// '@typescript-eslint/no-explicit-any': 'off',
|
||||
'@typescript-eslint/no-empty-function': 'off',
|
||||
'@typescript-eslint/no-unused-vars': ["warn", { "argsIgnorePattern": "^_" }],
|
||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||
'@typescript-eslint/no-use-before-define': 'off',
|
||||
'@typescript-eslint/no-namespace': 'off',
|
||||
'no-inner-declarations': 'off',
|
||||
'no-return-assign': 'off',
|
||||
// 'no-fallthrough': 'off',
|
||||
quotes: 'off'
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
"scripts": {
|
||||
"prepublishOnly": "npm run test && npm run clean && tsc",
|
||||
"build": "npm run clean && tsc",
|
||||
"clean": "del dist",
|
||||
"clean": "del /q dist",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"test": "mocha -r ts-node/register src/**/*.spec.ts src/*.spec.ts"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
import { ContainerColor, ContainerOnStore, GameState, Phase, Player, ShipPosition } from "./gamestate";
|
||||
import { ContainerColor, ContainerOnStore, ContainerPiece, GameState, Phase, Player, ShipPosition } from "./gamestate";
|
||||
import { MoveName } from "./move";
|
||||
|
||||
export interface AvailableMoves {
|
||||
[MoveName.BuyFromFactory]?: Array<{ player: number; container: ContainerOnStore }>;
|
||||
[MoveName.BuyFromWarehouse]?: Array<{ player: number; container: ContainerOnStore }>;
|
||||
[MoveName.BuyFromFactory]?: Array<{ player: number; piece: ContainerPiece }>;
|
||||
[MoveName.BuyFromWarehouse]?: Array<{ player: number; piece: ContainerPiece }>;
|
||||
[MoveName.BuyFactory]?: ContainerColor[];
|
||||
[MoveName.BuyWarehouse]?: boolean[];
|
||||
[MoveName.GetLoan]?: boolean[];
|
||||
[MoveName.PayLoan]?: boolean[];
|
||||
[MoveName.Produce]?: ContainerColor[];
|
||||
[MoveName.Sail]?: ShipPosition[];
|
||||
[MoveName.ArrangeFactory]?: ContainerOnStore[];
|
||||
[MoveName.ArrangeWarehouse]?: ContainerOnStore[];
|
||||
[MoveName.ArrangeFactory]?: ContainerPiece[];
|
||||
[MoveName.ArrangeWarehouse]?: ContainerPiece[];
|
||||
[MoveName.Bid]?: boolean[];
|
||||
[MoveName.Accept]?: number[];
|
||||
[MoveName.Decline]?: boolean[];
|
||||
|
|
@ -22,11 +22,23 @@ export interface AvailableMoves {
|
|||
export function availableMoves(G: GameState, player: Player): AvailableMoves {
|
||||
switch (G.phase) {
|
||||
case Phase.Bid: {
|
||||
return { [MoveName.GetLoan]: [true], [MoveName.Bid]: [true] };
|
||||
return {
|
||||
[MoveName.GetLoan]: [true],
|
||||
[MoveName.Bid]: [true]
|
||||
};
|
||||
}
|
||||
|
||||
case Phase.AcceptDecline: {
|
||||
return { [MoveName.GetLoan]: [true], [MoveName.Accept]: G.highestBidders, [MoveName.Decline]: [true] };
|
||||
const moves: AvailableMoves = {
|
||||
[MoveName.GetLoan]: [true],
|
||||
[MoveName.Accept]: G.highestBidders,
|
||||
};
|
||||
|
||||
const highestBidder = G.players[G.highestBidders[0]];
|
||||
if (player.money >= highestBidder.bid + highestBidder.additionalBid)
|
||||
moves[MoveName.Decline] = [true];
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
case Phase.Move: {
|
||||
|
|
@ -34,26 +46,23 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
|
|||
|
||||
// BuyFromFactory
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.BuyFromFactory) {
|
||||
if (player.containersOnWarehouseStore.length < player.warehouses) {
|
||||
if (player.containersOnWarehouseStore.length < player.warehouses.length) {
|
||||
const containersAvailable: { player: number; container: ContainerOnStore }[] = [];
|
||||
const playersToBuy: Player[] = player.actions === 0 && player.lastMove?.name === MoveName.BuyFromFactory ?
|
||||
[G.players[player.lastMove.data.player]] : G.players.filter(p => p.id !== player.id);
|
||||
playersToBuy.forEach(p => {
|
||||
containersAvailable.push(...p.containersOnFactoryStore.map(c => ({ player: p.id, container: c })));
|
||||
});
|
||||
moves[MoveName.BuyFromFactory] = containersAvailable.filter(c => c.container.price <= player.money);
|
||||
}
|
||||
}
|
||||
|
||||
// ArrangeWarehouse
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.BuyFromFactory) {
|
||||
if (player.containersOnWarehouseStore.length > 0) {
|
||||
moves[MoveName.ArrangeWarehouse] = player.containersOnWarehouseStore;
|
||||
const buyable = containersAvailable.filter(c => c.container.price <= player.money).map(c => ({ player: c.player, piece: c.container.piece }));
|
||||
|
||||
if (buyable.length > 0)
|
||||
moves[MoveName.BuyFromFactory] = buyable;
|
||||
}
|
||||
}
|
||||
|
||||
// BuyFromWarehouse
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.Sail) {
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.Sail || player.lastMove?.name === MoveName.BuyFromWarehouse) {
|
||||
if (player.ship.shipPosition !== ShipPosition.Island && player.ship.shipPosition !== ShipPosition.OpenSea) {
|
||||
let otherPlayer: Player;
|
||||
switch (player.ship.shipPosition) {
|
||||
|
|
@ -65,26 +74,25 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
|
|||
}
|
||||
|
||||
if (otherPlayer.containersOnWarehouseStore.length > 0) {
|
||||
const buyable: { player: number; container: ContainerOnStore }[] = [];
|
||||
const buyable: { player: number; piece: ContainerPiece }[] = [];
|
||||
otherPlayer.containersOnWarehouseStore.forEach(c => {
|
||||
if (c.price <= player.money)
|
||||
buyable.push({ player: otherPlayer.id, container: c });
|
||||
buyable.push({ player: otherPlayer.id, piece: c.piece });
|
||||
});
|
||||
moves[MoveName.BuyFromWarehouse] = buyable;
|
||||
|
||||
if (buyable.length > 0)
|
||||
moves[MoveName.BuyFromWarehouse] = buyable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BuyFactory
|
||||
if (player.actions > 0) {
|
||||
if (player.actions > 0 && player.money >= (player.factories.length + 1) * 3) {
|
||||
const factoriesLeft: ContainerColor[] = [];
|
||||
Object.values(ContainerColor).forEach(color => {
|
||||
if (G.factoriesLeft.filter(f => f.toString() === color).length > 0)
|
||||
if (G.factoriesLeft.filter(f => f.color.toString() === color).length > 0 && player.factories.map(f => f.color).indexOf(color) == -1)
|
||||
factoriesLeft.push(color);
|
||||
});
|
||||
// G.factoriesLeft.forEach((v, k) => {
|
||||
// if (v > 0 && player.factories.indexOf(k) === -1) factoriesLeft.push(k);
|
||||
// });
|
||||
|
||||
if (factoriesLeft.length > 0)
|
||||
moves[MoveName.BuyFactory] = factoriesLeft;
|
||||
|
|
@ -92,38 +100,35 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
|
|||
|
||||
// BuyWarehouse
|
||||
if (player.actions > 0) {
|
||||
if (player.warehouses < 5 && player.money >= player.warehouses + 3)
|
||||
if (player.warehouses.length < 5 && player.money >= player.warehouses.length + 3)
|
||||
moves[MoveName.BuyWarehouse] = [true];
|
||||
}
|
||||
|
||||
// GetLoan
|
||||
if (player.loans < 2)
|
||||
if (player.loans.length < 2)
|
||||
moves[MoveName.GetLoan] = [true];
|
||||
|
||||
// PayLoan
|
||||
if (player.money >= 10)
|
||||
if (player.loans.length > 0 && player.money >= 10)
|
||||
moves[MoveName.PayLoan] = [true];
|
||||
|
||||
// Produce
|
||||
const containersLeft: ContainerColor[] = [];
|
||||
// G.containersLeft.forEach((v, k) => {
|
||||
// if (v > 0) containersLeft.push(k);
|
||||
// });
|
||||
Object.keys(ContainerColor).forEach(key => {
|
||||
if (G.containersLeft.filter(f => f.toString() === key).length > 0)
|
||||
if (G.containersLeft.filter(f => f.color.toString() === ContainerColor[key]).length > 0)
|
||||
containersLeft.push(ContainerColor[key]);
|
||||
});
|
||||
|
||||
if ((player.produced.length === 0 && player.money >= 1) || (player.lastMove?.name === MoveName.Produce)) {
|
||||
if (player.containersOnFactoryStore.length < player.factories.length * 2) {
|
||||
if ((player.produced.length === 0 && player.money >= 1 && player.actions > 0) || (player.lastMove?.name === MoveName.Produce)) {
|
||||
if (player.containersOnFactoryStore.length < player.factories.length * 2 && player.produced.length < player.factories.length) {
|
||||
const canProduce: ContainerColor[] = [];
|
||||
player.factories.forEach(color => {
|
||||
if (canProduce.indexOf(color) === -1) {
|
||||
const totalFactories = player.factories.filter(factoryColor => factoryColor === color).length;
|
||||
const producedContainers = player.produced.filter(producedColor => producedColor === color).length;
|
||||
player.factories.forEach(factory => {
|
||||
if (canProduce.indexOf(factory.color) === -1) {
|
||||
const totalFactories = player.factories.filter(factoryColor => factoryColor === factory).length;
|
||||
const producedContainers = player.produced.filter(producedColor => producedColor === factory.color).length;
|
||||
|
||||
if (totalFactories > producedContainers)
|
||||
canProduce.push(color);
|
||||
if (totalFactories > producedContainers && containersLeft.indexOf(factory.color) !== -1)
|
||||
canProduce.push(factory.color);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -133,25 +138,34 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
|
|||
}
|
||||
|
||||
// ArrangeFactory
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.Produce) {
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.Produce || player.lastMove?.name === MoveName.ArrangeFactory) {
|
||||
if (player.containersOnFactoryStore.length > 0) {
|
||||
moves[MoveName.ArrangeFactory] = player.containersOnFactoryStore;
|
||||
moves[MoveName.ArrangeFactory] = player.containersOnFactoryStore.map(c => c.piece);
|
||||
}
|
||||
}
|
||||
|
||||
// ArrangeWarehouse
|
||||
if (player.actions > 0 || player.lastMove?.name === MoveName.BuyFromFactory) {
|
||||
if (player.containersOnWarehouseStore.length > 0) {
|
||||
moves[MoveName.ArrangeWarehouse] = player.containersOnWarehouseStore.map(c => c.piece);
|
||||
}
|
||||
}
|
||||
|
||||
// Sail
|
||||
const positions: ShipPosition[] = [];
|
||||
if (player.ship.shipPosition === ShipPosition.OpenSea) {
|
||||
positions.push(...G.players.filter(p => p !== player).map(p => ShipPosition["Player" + p.id as keyof ShipPosition]));
|
||||
if (player.actions > 0) {
|
||||
const positions: ShipPosition[] = [];
|
||||
if (player.ship.shipPosition === ShipPosition.OpenSea) {
|
||||
positions.push(...G.players.filter(p => p !== player).map(p => ShipPosition["Player" + p.id as keyof ShipPosition]));
|
||||
|
||||
if (player.ship.containers.length > 0) {
|
||||
positions.push(ShipPosition.Island);
|
||||
if (player.ship.containers.length > 0) {
|
||||
positions.push(ShipPosition.Island);
|
||||
}
|
||||
} else {
|
||||
positions.push(ShipPosition.OpenSea);
|
||||
}
|
||||
} else {
|
||||
positions.push(ShipPosition.OpenSea);
|
||||
}
|
||||
|
||||
moves[MoveName.Sail] = positions;
|
||||
moves[MoveName.Sail] = positions;
|
||||
}
|
||||
|
||||
moves[MoveName.Pass] = [true];
|
||||
moves[MoveName.Undo] = [true];
|
||||
|
|
|
|||
|
|
@ -4,11 +4,6 @@ import { setup, move as execMove, ended } from './engine';
|
|||
import { ContainerColor, ShipPosition } from "./gamestate";
|
||||
import { Move } from './move';
|
||||
|
||||
/*
|
||||
Current player: 2,
|
||||
Factories: blue, white, red,
|
||||
Point Cards: rybwu, ybwur, urybw
|
||||
*/
|
||||
describe("Engine", () => {
|
||||
it("should play full game", () => {
|
||||
let G = setup(5, { beginner: false }, "test");
|
||||
|
|
|
|||
418
src/engine.ts
418
src/engine.ts
|
|
@ -1,31 +1,30 @@
|
|||
import assert from "assert";
|
||||
import seedrandom from "seedrandom";
|
||||
import { GameOptions, Phase, GameState, Player, ContainerColor, ShipPosition } from "./gamestate";
|
||||
import { GameOptions, Phase, GameState, Player, ContainerColor, ShipPosition, FactoryPiece, ContainerPiece } from "./gamestate";
|
||||
import { availableMoves } from "./available-moves";
|
||||
import { Move, MoveName, Moves } from "./move";
|
||||
import { asserts, shuffle } from "./utils";
|
||||
import { GameEventName, LogItem } from "./log";
|
||||
import pointCards from "./cards";
|
||||
import { cloneDeep, isEqual } from "lodash";
|
||||
import { cloneDeep, groupBy, isEqual } from "lodash";
|
||||
|
||||
export function setup(numPlayers: number, { beginner = true }: GameOptions, seed?: string): GameState {
|
||||
const rng = seedrandom(seed || Math.random().toString());
|
||||
|
||||
const cards = shuffle(pointCards, rng() + '');
|
||||
const factories = shuffle([ContainerColor.Black, ContainerColor.Orange, ContainerColor.Tan, ContainerColor.White, ContainerColor.Brown], rng() + '');
|
||||
|
||||
let id = 0;
|
||||
const players: Player[] = new Array(numPlayers).fill(0).map(() => ({
|
||||
id: id++,
|
||||
pointCard: cards.shift()!,
|
||||
factories: [factories[0]],
|
||||
warehouses: 1,
|
||||
ship: { containers: [], shipPosition: ShipPosition.OpenSea },
|
||||
containersOnFactoryStore: [{ containerColor: factories.shift()!, price: 2 }],
|
||||
factories: [],
|
||||
warehouses: [],
|
||||
ship: { piece: { id: 'S' + id }, containers: [], shipPosition: ShipPosition.OpenSea },
|
||||
containersOnFactoryStore: [],
|
||||
containersOnWarehouseStore: [],
|
||||
containersOnIsland: [],
|
||||
money: 20,
|
||||
loans: 0,
|
||||
loans: [],
|
||||
produced: [],
|
||||
availableMoves: null,
|
||||
lastMove: null,
|
||||
|
|
@ -33,29 +32,30 @@ export function setup(numPlayers: number, { beginner = true }: GameOptions, seed
|
|||
isDropped: false,
|
||||
bid: 0,
|
||||
additionalBid: 0,
|
||||
showBid: false,
|
||||
showAdditionalBid: false,
|
||||
finalScore: 0
|
||||
}));
|
||||
|
||||
const totalContainers = players.length * 4 - 1;
|
||||
const containersLeft: ContainerColor[] = [];
|
||||
containersLeft.push(...new Array(totalContainers).fill(ContainerColor.Brown));
|
||||
containersLeft.push(...new Array(totalContainers).fill(ContainerColor.White));
|
||||
containersLeft.push(...new Array(totalContainers).fill(ContainerColor.Black));
|
||||
containersLeft.push(...new Array(totalContainers).fill(ContainerColor.Orange));
|
||||
containersLeft.push(...new Array(totalContainers).fill(ContainerColor.Tan));
|
||||
id = 0;
|
||||
const totalContainers = players.length * 4;
|
||||
const containersLeft: ContainerPiece[] = [];
|
||||
containersLeft.push(...new Array(totalContainers).fill(0).map(_ => ({ id: 'C' + id++, color: ContainerColor.Brown })));
|
||||
containersLeft.push(...new Array(totalContainers).fill(0).map(_ => ({ id: 'C' + id++, color: ContainerColor.White })));
|
||||
containersLeft.push(...new Array(totalContainers).fill(0).map(_ => ({ id: 'C' + id++, color: ContainerColor.Black })));
|
||||
containersLeft.push(...new Array(totalContainers).fill(0).map(_ => ({ id: 'C' + id++, color: ContainerColor.Orange })));
|
||||
containersLeft.push(...new Array(totalContainers).fill(0).map(_ => ({ id: 'C' + id++, color: ContainerColor.Tan })));
|
||||
|
||||
const factoriesLeft: ContainerColor[] = [];
|
||||
factoriesLeft.push(...new Array(4).fill(ContainerColor.Brown));
|
||||
factoriesLeft.push(...new Array(4).fill(ContainerColor.White));
|
||||
factoriesLeft.push(...new Array(4).fill(ContainerColor.Black));
|
||||
factoriesLeft.push(...new Array(4).fill(ContainerColor.Orange));
|
||||
factoriesLeft.push(...new Array(4).fill(ContainerColor.Tan));
|
||||
id = 0;
|
||||
const factoriesLeft: FactoryPiece[] = [];
|
||||
factoriesLeft.push(...new Array(5).fill(0).map(_ => ({ id: 'F' + id++, color: ContainerColor.Brown })));
|
||||
factoriesLeft.push(...new Array(5).fill(0).map(_ => ({ id: 'F' + id++, color: ContainerColor.White })));
|
||||
factoriesLeft.push(...new Array(5).fill(0).map(_ => ({ id: 'F' + id++, color: ContainerColor.Black })));
|
||||
factoriesLeft.push(...new Array(5).fill(0).map(_ => ({ id: 'F' + id++, color: ContainerColor.Orange })));
|
||||
factoriesLeft.push(...new Array(5).fill(0).map(_ => ({ id: 'F' + id++, color: ContainerColor.Tan })));
|
||||
|
||||
factories.forEach(f => {
|
||||
containersLeft.push(f);
|
||||
factoriesLeft.push(f);
|
||||
});
|
||||
|
||||
const warehousesLeft = players.length * 4;
|
||||
const warehousesLeft = new Array(players.length * 5).fill(0).map((_, i) => ({ id: 'W' + i }));
|
||||
const loansLeft = new Array(players.length * 2).fill(0).map((_, i) => ({ id: 'L' + i }));
|
||||
|
||||
const G: GameState = {
|
||||
players,
|
||||
|
|
@ -63,6 +63,7 @@ export function setup(numPlayers: number, { beginner = true }: GameOptions, seed
|
|||
containersLeft,
|
||||
factoriesLeft,
|
||||
warehousesLeft,
|
||||
loansLeft,
|
||||
auctioningPlayer: -1,
|
||||
highestBidders: [],
|
||||
phase: Phase.Move,
|
||||
|
|
@ -72,26 +73,31 @@ export function setup(numPlayers: number, { beginner = true }: GameOptions, seed
|
|||
round: 1
|
||||
} as GameState;
|
||||
|
||||
G.players[G.currentPlayer].actions = 2;
|
||||
G.players[G.currentPlayer].availableMoves = availableMoves(G, G.players[G.currentPlayer]);
|
||||
const colors = shuffle([ContainerColor.Black, ContainerColor.Orange, ContainerColor.Tan, ContainerColor.White, ContainerColor.Brown], 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 });
|
||||
|
||||
player.warehouses.push(G.warehousesLeft.pop()!);
|
||||
});
|
||||
|
||||
G.log.push({ type: "phase", phase: Phase.Setup });
|
||||
|
||||
G.players[G.currentPlayer!].actions = 2;
|
||||
G.players[G.currentPlayer!].availableMoves = availableMoves(G, G.players[G.currentPlayer!]);
|
||||
|
||||
G.log.push({ type: "event", event: { name: GameEventName.GameStart } });
|
||||
|
||||
addRoundStart(G);
|
||||
|
||||
return G;
|
||||
}
|
||||
|
||||
function addRoundStart(G: GameState) {
|
||||
G.log.push({
|
||||
type: "event",
|
||||
event: {
|
||||
name: GameEventName.RoundStart,
|
||||
round: G.round
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function stripSecret(G: GameState, player?: number): GameState {
|
||||
return {
|
||||
...G,
|
||||
|
|
@ -103,7 +109,10 @@ export function stripSecret(G: GameState, player?: number): GameState {
|
|||
return {
|
||||
...pl,
|
||||
pointCard: null,
|
||||
availableMoves: pl.availableMoves ? {} : null
|
||||
availableMoves: pl.availableMoves ? {} : null,
|
||||
money: ended(G) ? pl.money : 0,
|
||||
bid: pl.showBid ? pl.bid : 0,
|
||||
additionalBid: pl.showAdditionalBid ? pl.additionalBid : 0,
|
||||
};
|
||||
}
|
||||
}),
|
||||
|
|
@ -112,89 +121,94 @@ export function stripSecret(G: GameState, player?: number): GameState {
|
|||
}
|
||||
|
||||
export function currentPlayers(G: GameState): number[] {
|
||||
switch (G.phase) {
|
||||
case Phase.Move:
|
||||
default: {
|
||||
return [G.currentPlayer];
|
||||
}
|
||||
}
|
||||
return [G.currentPlayer!];
|
||||
}
|
||||
|
||||
export function move(G: GameState, move: Move, playerNumber: number): GameState {
|
||||
const player = G.players[playerNumber];
|
||||
const available = player.availableMoves?.[move.name];
|
||||
|
||||
// eslint-disable-next-line no-useless-catch
|
||||
try {
|
||||
assert(G.currentPlayer === 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);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
assert(G.currentPlayer === 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 || move.extraData.price <= player.money, "Can't bid more money than you have!");
|
||||
|
||||
G.log.push({ type: "move", player: playerNumber, move });
|
||||
|
||||
switch (move.name) {
|
||||
case MoveName.BuyFactory: {
|
||||
asserts<Moves.MoveBuyFactory>(move);
|
||||
remove(G.factoriesLeft, move.data);
|
||||
player.factories.push(move.data);
|
||||
remove(G.factoriesLeft, move.extraData);
|
||||
player.factories.push(move.extraData);
|
||||
player.money -= player.factories.length * 3;
|
||||
|
||||
player.lastMove = move;
|
||||
player.actions--;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.BuyWarehouse: {
|
||||
asserts<Moves.MoveBuyWarehouse>(move);
|
||||
G.warehousesLeft -= 1;
|
||||
player.warehouses += 1;
|
||||
player.money -= player.warehouses + 2;
|
||||
remove(G.warehousesLeft, move.extraData);
|
||||
player.warehouses.push(move.extraData);
|
||||
player.money -= player.warehouses.length + 2;
|
||||
player.actions--;
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.BuyFromFactory: {
|
||||
asserts<Moves.MoveBuyFromFactory>(move);
|
||||
const otherPlayer = G.players[move.data.player];
|
||||
remove(otherPlayer.containersOnFactoryStore, move.data.container);
|
||||
player.containersOnWarehouseStore.push({ containerColor: move.data.container.containerColor, price: move.extraData.price });
|
||||
player.money -= move.data.container.price;
|
||||
otherPlayer.money += move.data.container.price;
|
||||
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 });
|
||||
|
||||
player.money -= aux.price;
|
||||
otherPlayer.money += aux.price;
|
||||
|
||||
if (player.lastMove?.name !== MoveName.BuyFromFactory || player.lastMove?.data.player !== move.data.player) {
|
||||
player.actions -= 1;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.BuyFromWarehouse: {
|
||||
asserts<Moves.MoveBuyFromWarehouse>(move);
|
||||
const otherPlayer = G.players[move.data.player];
|
||||
remove(otherPlayer.containersOnWarehouseStore, move.data.container);
|
||||
player.ship.containers.push(move.data.container.containerColor);
|
||||
player.money -= move.data.container.price;
|
||||
otherPlayer.money += move.data.container.price;
|
||||
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;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.GetLoan: {
|
||||
asserts<Moves.MoveGetLoan>(move);
|
||||
player.loans += 1;
|
||||
player.loans.push(G.loansLeft.pop()!);
|
||||
player.money += 10;
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.PayLoan: {
|
||||
asserts<Moves.MovePayLoan>(move);
|
||||
player.loans -= 1;
|
||||
G.loansLeft.push(player.loans.splice(player.loans.length - 1, 1)[0]);
|
||||
player.money -= 10;
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.Produce: {
|
||||
asserts<Moves.MoveProduce>(move);
|
||||
remove(G.containersLeft, move.data);
|
||||
player.containersOnFactoryStore.push({ containerColor: move.data, price: move.extraData.price });
|
||||
remove(G.containersLeft, move.extraData.piece);
|
||||
player.containersOnFactoryStore.push({ piece: move.extraData.piece, price: move.extraData.price });
|
||||
player.produced.push(move.extraData.piece.color);
|
||||
if (player.lastMove?.name !== MoveName.Produce) {
|
||||
player.actions -= 1;
|
||||
player.money -= 1;
|
||||
playerBefore(player, G).money += 1;
|
||||
}
|
||||
|
|
@ -209,7 +223,9 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
player.actions = 0;
|
||||
G.auctioningPlayer = G.currentPlayer;
|
||||
G.phase = Phase.Bid;
|
||||
nextPlayer(G);
|
||||
nextPlayer(G, false);
|
||||
} else {
|
||||
player.actions--;
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -217,15 +233,27 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
|
||||
case MoveName.ArrangeFactory: {
|
||||
asserts<Moves.MoveArrangeFactory>(move);
|
||||
remove(player.containersOnFactoryStore, move.data);
|
||||
player.containersOnFactoryStore.push({ containerColor: move.data.containerColor, price: move.extraData.price });
|
||||
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 });
|
||||
|
||||
if (player.lastMove?.name !== MoveName.Produce && player.lastMove?.name !== MoveName.ArrangeFactory) {
|
||||
player.actions--;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MoveName.ArrangeWarehouse: {
|
||||
asserts<Moves.MoveArrangeWarehouse>(move);
|
||||
remove(player.containersOnWarehouseStore, move.data);
|
||||
player.containersOnWarehouseStore.push({ containerColor: move.data.containerColor, price: move.extraData.price });
|
||||
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 });
|
||||
|
||||
if (player.lastMove?.name !== MoveName.BuyFromFactory && player.lastMove?.name !== MoveName.ArrangeWarehouse) {
|
||||
player.actions--;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -234,26 +262,28 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
|
||||
if (G.highestBidders.length === 0) {
|
||||
player.bid = move.extraData.price;
|
||||
nextPlayer(G);
|
||||
nextPlayer(G, false);
|
||||
} else {
|
||||
player.additionalBid = move.extraData.price;
|
||||
while (G.highestBidders.indexOf(G.currentPlayer) === -1 && G.auctioningPlayer === G.currentPlayer) { nextPlayer(G); }
|
||||
do { nextPlayer(G, false); } while (G.highestBidders.indexOf(G.currentPlayer) === -1 && G.auctioningPlayer !== G.currentPlayer);
|
||||
}
|
||||
|
||||
if (G.auctioningPlayer === G.currentPlayer) {
|
||||
if (G.highestBidders.length === 0) {
|
||||
G.players.filter(p => p.id !== G.currentPlayer).forEach(p => { p.showBid = true; });
|
||||
const highestBid = Math.max(...G.players.map(p => p.bid));
|
||||
const highestBidders = G.players.filter(p => p.bid === highestBid).map(p => p.id);
|
||||
const highestBidders = G.players.filter(p => p.id != G.currentPlayer && p.bid === highestBid).map(p => p.id);
|
||||
G.highestBidders = highestBidders;
|
||||
if (highestBidders.length > 1) {
|
||||
while (highestBidders.indexOf(G.currentPlayer) === -1) { nextPlayer(G); }
|
||||
while (highestBidders.indexOf(G.currentPlayer) === -1) { nextPlayer(G, false); }
|
||||
} else {
|
||||
G.phase = Phase.AcceptDecline;
|
||||
}
|
||||
} else {
|
||||
const highestBid = Math.max(...G.players.map(p => p.bid + p.additionalBid));
|
||||
const highestBidders = G.players.filter(p => p.bid + p.additionalBid === highestBid).map(p => p.id);
|
||||
const highestBidders = G.players.filter(p => p.id != G.currentPlayer && p.bid + p.additionalBid === highestBid).map(p => p.id);
|
||||
G.highestBidders = highestBidders;
|
||||
G.players.forEach(p => { p.showAdditionalBid = true; });
|
||||
G.phase = Phase.AcceptDecline;
|
||||
}
|
||||
}
|
||||
|
|
@ -268,17 +298,15 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
otherPlayer.money -= otherPlayer.bid + otherPlayer.additionalBid;
|
||||
player.money += (otherPlayer.bid + otherPlayer.additionalBid) * 2;
|
||||
G.players.forEach(p => { p.bid = p.additionalBid = 0; });
|
||||
G.auctioningPlayer = -1;
|
||||
G.players.forEach(p => { p.showBid = p.showAdditionalBid = false; });
|
||||
G.auctioningPlayer = undefined;
|
||||
G.highestBidders = [];
|
||||
G.phase = Phase.Move;
|
||||
|
||||
if (!ended(G)) {
|
||||
nextPlayer(G);
|
||||
G.players[G.currentPlayer].money -= G.players[G.currentPlayer].loans;
|
||||
G.players[G.currentPlayer].actions = 2;
|
||||
nextPlayer(G, true);
|
||||
} else {
|
||||
G.currentPlayer = -1;
|
||||
// TODO: calculate scores
|
||||
G.currentPlayer = undefined;
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -290,17 +318,15 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
const bid = G.players[G.highestBidders[0]].bid + G.players[G.highestBidders[0]].additionalBid;
|
||||
player.money -= bid;
|
||||
G.players.forEach(p => { p.bid = p.additionalBid = 0; });
|
||||
G.auctioningPlayer = -1;
|
||||
G.players.forEach(p => { p.showBid = p.showAdditionalBid = false; });
|
||||
G.auctioningPlayer = undefined;
|
||||
G.highestBidders = [];
|
||||
G.phase = Phase.Move;
|
||||
|
||||
if (!ended(G)) {
|
||||
nextPlayer(G);
|
||||
G.players[G.currentPlayer].money -= G.players[G.currentPlayer].loans;
|
||||
G.players[G.currentPlayer].actions = 2;
|
||||
nextPlayer(G, true);
|
||||
} else {
|
||||
G.currentPlayer = -1;
|
||||
// TODO: calculate scores
|
||||
G.currentPlayer = undefined;
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -309,65 +335,78 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
case MoveName.Pass: {
|
||||
asserts<Moves.MovePass>(move);
|
||||
if (!ended(G)) {
|
||||
nextPlayer(G);
|
||||
G.players[G.currentPlayer].money -= G.players[G.currentPlayer].loans;
|
||||
G.players[G.currentPlayer].actions = 2;
|
||||
nextPlayer(G, true);
|
||||
} else {
|
||||
G.currentPlayer = -1;
|
||||
// TODO: calculate scores
|
||||
G.currentPlayer = undefined;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// case MoveName.ChooseCard: {
|
||||
// // Should not be needed, typescript should make the distinction itself
|
||||
// asserts<Moves.MoveChooseCard>(move);
|
||||
// player.faceDownCard = move.data;
|
||||
// player.hand.splice(player.hand.findIndex(c => isEqual(c, move.data)), 1);
|
||||
// delete player.availableMoves;
|
||||
case MoveName.Undo: {
|
||||
asserts<Moves.MoveUndo>(move);
|
||||
G.log.pop();
|
||||
|
||||
// if (G.players.every(pl => pl.faceDownCard)) {
|
||||
// G.phase = Phase.PlaceCard;
|
||||
const lastLog = G.log[G.log.length - 1];
|
||||
if (lastLog.type == "move" && lastLog.player == G.currentPlayer)
|
||||
G.log.pop();
|
||||
|
||||
// G.log.push({ type: "event", event: { name: GameEventName.RevealCards, cards: G.players.map(pl => pl.faceDownCard!) } });
|
||||
|
||||
// G = switchToNextPlayer(G);
|
||||
// }
|
||||
|
||||
// return G;
|
||||
// }
|
||||
|
||||
// case MoveName.PlaceCard: {
|
||||
// delete player.availableMoves;
|
||||
|
||||
// if (move.data.replace) {
|
||||
// player.discard.push(...G.rows[move.data.row]);
|
||||
// player.points = sumBy(player.discard, "points");
|
||||
// G.rows[move.data.row] = [player.faceDownCard!];
|
||||
// } else {
|
||||
// G.rows[move.data.row].push(player.faceDownCard!);
|
||||
// }
|
||||
|
||||
// delete player.faceDownCard;
|
||||
|
||||
// return switchToNextPlayer(G);
|
||||
// }
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
player.lastMove = move;
|
||||
if (move.name != MoveName.GetLoan && move.name != MoveName.PayLoan)
|
||||
player.lastMove = move;
|
||||
|
||||
G.players[G.currentPlayer].availableMoves = availableMoves(G, G.players[G.currentPlayer]);
|
||||
if (G.currentPlayer !== undefined)
|
||||
G.players[G.currentPlayer].availableMoves = availableMoves(G, G.players[G.currentPlayer]);
|
||||
|
||||
return G;
|
||||
}
|
||||
|
||||
export function ended(G: GameState): boolean {
|
||||
return G.containersLeft.filter((c, i) => G.containersLeft.indexOf(c) !== i).length <= 3;
|
||||
return [...new Set(G.containersLeft.map(c => c.color))].length <= 3;
|
||||
}
|
||||
|
||||
export function scores(G: GameState): number[] {
|
||||
return G.players.map(pl => pl.money);
|
||||
if (ended(G)) {
|
||||
return G.players.map(player => {
|
||||
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] && cv.containerColor != most)
|
||||
return grouped[cv.containerColor].length * (hasOneOfEach ? cv.specialValue : cv.baseValue);
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
player.money += points.reduce((a, b) => a + b, 0);
|
||||
player.money += player.containersOnWarehouseStore.length * 2;
|
||||
player.money += player.ship.containers.length * 3;
|
||||
player.money += player.loans.length * -11;
|
||||
|
||||
return player.money;
|
||||
});
|
||||
} else {
|
||||
return G.players.map(p => p.money);
|
||||
}
|
||||
}
|
||||
|
||||
export function reconstructState(initialState: GameState, log: LogItem[]): GameState {
|
||||
|
|
@ -378,38 +417,6 @@ export function reconstructState(initialState: GameState, log: LogItem[]): GameS
|
|||
|
||||
switch (item.type) {
|
||||
case "event": {
|
||||
// switch (item.event.name) {
|
||||
// case GameEventName.GameStart: {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// case GameEventName.GameEnd: {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// case GameEventName.RevealCards: {
|
||||
// let i = 0;
|
||||
// for (const pl of G.players) {
|
||||
// pl.faceDownCard = item.event.cards[i];
|
||||
// i++;
|
||||
// }
|
||||
|
||||
// break;
|
||||
// }
|
||||
|
||||
// case GameEventName.RoundStart: {
|
||||
// G.round = item.event.round;
|
||||
|
||||
// G.rows = item.event.cards.board.map(card => [card]) as [Card[], Card[], Card[], Card[]];
|
||||
|
||||
// for (let i = 0; i < item.event.cards.players.length; i++) {
|
||||
// G.players[i].hand = item.event.cards.players[i];
|
||||
// }
|
||||
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -419,24 +426,7 @@ export function reconstructState(initialState: GameState, log: LogItem[]): GameS
|
|||
}
|
||||
|
||||
case "move": {
|
||||
// switch (item.move.name) {
|
||||
// case MoveName.ChooseCard: {
|
||||
// G.players[item.player].faceDownCard = item.move.data;
|
||||
// break;
|
||||
// }
|
||||
|
||||
// case MoveName.PlaceCard: {
|
||||
// if (item.move.data.replace) {
|
||||
// G.players[item.player].points += sumBy(G.rows[item.move.data.row], "points");
|
||||
// G.players[item.player].discard.push(...(G.rows[item.move.data.row]));
|
||||
// G.rows[item.move.data.row] = [];
|
||||
// }
|
||||
|
||||
// G.rows[item.move.data.row].push(G.players[item.player].faceDownCard!);
|
||||
// G.players[item.player].faceDownCard = null;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -445,18 +435,54 @@ export function reconstructState(initialState: GameState, log: LogItem[]): GameS
|
|||
}
|
||||
|
||||
function playerBefore(player: Player, G: GameState) {
|
||||
if (player.id === 0) {
|
||||
return G.players[G.players.length - 1];
|
||||
} else {
|
||||
return G.players[player.id - 1];
|
||||
}
|
||||
return player.id === 0 ? G.players[G.players.length - 1] : G.players[player.id - 1];
|
||||
}
|
||||
|
||||
function nextPlayer(G: GameState) {
|
||||
G.currentPlayer = (G.currentPlayer + 1) % G.players.length;
|
||||
function nextPlayer(G: GameState, doUpkeep: boolean) {
|
||||
G.currentPlayer = (G.currentPlayer! + 1) % G.players.length;
|
||||
|
||||
const player = G.players[G.currentPlayer];
|
||||
if (doUpkeep) {
|
||||
const loanCount = player.loans.length;
|
||||
for (let i = 0; i < loanCount; i++) {
|
||||
if (player.money > 0) {
|
||||
player.money--;
|
||||
} else if (player.containersOnIsland.length > 0) {
|
||||
removeRandom(player.containersOnIsland);
|
||||
} else if (player.containersOnWarehouseStore.length + player.containersOnFactoryStore.length > 0) {
|
||||
if (player.containersOnWarehouseStore.length >= 2) {
|
||||
removeRandom(player.containersOnWarehouseStore);
|
||||
removeRandom(player.containersOnWarehouseStore);
|
||||
} else if (player.containersOnWarehouseStore.length == 1) {
|
||||
removeRandom(player.containersOnWarehouseStore);
|
||||
if (player.containersOnFactoryStore.length >= 1) {
|
||||
removeRandom(player.containersOnFactoryStore);
|
||||
}
|
||||
} else {
|
||||
removeRandom(player.containersOnFactoryStore);
|
||||
if (player.containersOnFactoryStore.length >= 1) {
|
||||
removeRandom(player.containersOnFactoryStore);
|
||||
}
|
||||
}
|
||||
} else if (player.warehouses.length > 2) {
|
||||
player.warehouses.pop();
|
||||
player.loans.pop();
|
||||
} else if (player.factories.length > 2) {
|
||||
player.factories.pop();
|
||||
player.loans.pop();
|
||||
}
|
||||
}
|
||||
|
||||
G.players[G.currentPlayer].actions = 2;
|
||||
G.players[G.currentPlayer].produced = [];
|
||||
}
|
||||
}
|
||||
|
||||
function remove(array, value) {
|
||||
const aux = array.find(x => isEqual(x, value));
|
||||
array.splice(array.indexOf(aux), 1);
|
||||
return array.splice(array.indexOf(aux), 1);
|
||||
}
|
||||
|
||||
function removeRandom(array) {
|
||||
array.splice(Math.floor(Math.random() * array.length), 1);
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ export interface PointCard {
|
|||
}
|
||||
|
||||
export interface ContainerOnStore {
|
||||
containerColor: ContainerColor;
|
||||
piece: ContainerPiece;
|
||||
price: number;
|
||||
}
|
||||
|
||||
|
|
@ -40,20 +40,21 @@ export enum ShipPosition {
|
|||
}
|
||||
|
||||
export interface Ship {
|
||||
piece: ShipPiece;
|
||||
shipPosition: ShipPosition;
|
||||
containers: ContainerColor[];
|
||||
containers: ContainerPiece[];
|
||||
}
|
||||
|
||||
export interface Player {
|
||||
id: number;
|
||||
pointCard: PointCard | null;
|
||||
factories: ContainerColor[];
|
||||
warehouses: number;
|
||||
factories: FactoryPiece[];
|
||||
warehouses: WarehousePiece[];
|
||||
ship: Ship;
|
||||
containersOnFactoryStore: ContainerOnStore[];
|
||||
containersOnWarehouseStore: ContainerOnStore[];
|
||||
money: number;
|
||||
loans: number;
|
||||
loans: LoanPiece[];
|
||||
produced: ContainerColor[];
|
||||
name?: string;
|
||||
availableMoves: AvailableMoves | null;
|
||||
|
|
@ -62,22 +63,48 @@ export interface Player {
|
|||
isDropped: boolean;
|
||||
bid: number;
|
||||
additionalBid: number;
|
||||
containersOnIsland: ContainerColor[];
|
||||
showBid: boolean;
|
||||
showAdditionalBid: boolean;
|
||||
containersOnIsland: ContainerPiece[];
|
||||
}
|
||||
|
||||
export enum Phase {
|
||||
Move = "move",
|
||||
Bid = "bid",
|
||||
AcceptDecline = "acceptDecline"
|
||||
AcceptDecline = "acceptDecline",
|
||||
Setup = "setup"
|
||||
}
|
||||
|
||||
export interface ContainerPiece {
|
||||
id: string;
|
||||
color: ContainerColor;
|
||||
}
|
||||
|
||||
export interface FactoryPiece {
|
||||
id: string;
|
||||
color: ContainerColor;
|
||||
}
|
||||
|
||||
export interface WarehousePiece {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface LoanPiece {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface ShipPiece {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface GameState {
|
||||
players: Player[];
|
||||
currentPlayer: number;
|
||||
containersLeft: ContainerColor[];
|
||||
factoriesLeft: ContainerColor[];
|
||||
warehousesLeft: number;
|
||||
auctioningPlayer: number;
|
||||
currentPlayer: number | undefined;
|
||||
containersLeft: ContainerPiece[];
|
||||
factoriesLeft: FactoryPiece[];
|
||||
warehousesLeft: WarehousePiece[];
|
||||
loansLeft: LoanPiece[];
|
||||
auctioningPlayer: number | undefined;
|
||||
highestBidders: number[];
|
||||
phase: Phase;
|
||||
options: GameOptions;
|
||||
|
|
|
|||
14
src/move.ts
14
src/move.ts
|
|
@ -1,11 +1,11 @@
|
|||
import { ContainerColor, ContainerOnStore, ShipPosition } from "./gamestate";
|
||||
import { ContainerColor, ContainerPiece, FactoryPiece, ShipPosition, WarehousePiece } from "./gamestate";
|
||||
|
||||
export declare namespace Moves {
|
||||
export interface MoveBuyFromFactory {
|
||||
name: MoveName.BuyFromFactory;
|
||||
data: {
|
||||
player: number;
|
||||
container: ContainerOnStore;
|
||||
piece: ContainerPiece;
|
||||
};
|
||||
extraData: { price: number };
|
||||
}
|
||||
|
|
@ -14,18 +14,20 @@ export declare namespace Moves {
|
|||
name: MoveName.BuyFromWarehouse;
|
||||
data: {
|
||||
player: number;
|
||||
container: ContainerOnStore;
|
||||
piece: ContainerPiece;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MoveBuyFactory {
|
||||
name: MoveName.BuyFactory;
|
||||
data: ContainerColor;
|
||||
extraData: FactoryPiece;
|
||||
}
|
||||
|
||||
export interface MoveBuyWarehouse {
|
||||
name: MoveName.BuyWarehouse;
|
||||
data: boolean;
|
||||
extraData: WarehousePiece;
|
||||
}
|
||||
|
||||
export interface MoveGetLoan {
|
||||
|
|
@ -41,7 +43,7 @@ export declare namespace Moves {
|
|||
export interface MoveProduce {
|
||||
name: MoveName.Produce;
|
||||
data: ContainerColor;
|
||||
extraData: { price: number };
|
||||
extraData: { piece: ContainerPiece; price: number };
|
||||
}
|
||||
|
||||
export interface MoveSail {
|
||||
|
|
@ -51,13 +53,13 @@ export declare namespace Moves {
|
|||
|
||||
export interface MoveArrangeFactory {
|
||||
name: MoveName.ArrangeFactory;
|
||||
data: ContainerOnStore;
|
||||
data: ContainerPiece;
|
||||
extraData: { price: number };
|
||||
}
|
||||
|
||||
export interface MoveArrangeWarehouse {
|
||||
name: MoveName.ArrangeWarehouse;
|
||||
data: ContainerOnStore;
|
||||
data: ContainerPiece;
|
||||
extraData: { price: number };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
/* Basic Options */
|
||||
"target": "es2018",
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"strictNullChecks": true,
|
||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
"sourceMap": true, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||
},
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"index.ts",
|
||||
"wrapper.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
"**/*.spec.ts"
|
||||
]
|
||||
"compilerOptions": {
|
||||
/* Basic Options */
|
||||
"target": "es2018",
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"strictNullChecks": true,
|
||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
"sourceMap": false, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||
},
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"index.ts",
|
||||
"wrapper.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
"**/*.spec.ts"
|
||||
]
|
||||
}
|
||||
13
wrapper.ts
13
wrapper.ts
|
|
@ -33,9 +33,16 @@ export async function move(G: GameState, move: Move, player: number) {
|
|||
export { ended, scores } from './src/engine';
|
||||
|
||||
export function rankings(G: GameState) {
|
||||
const sortedPoints = G.players.map(pl => pl.money).sort((a, b) => a - b);
|
||||
const sortedPlayers = G.players
|
||||
.sort((p1, p2) => {
|
||||
if (p1.money == p2.money) {
|
||||
return p1.containersOnIsland.length - p2.containersOnIsland.length;
|
||||
} else {
|
||||
return p1.money - p2.money;
|
||||
}
|
||||
}).map(pl => pl.id);
|
||||
|
||||
return G.players.map(pl => sortedPoints.indexOf(pl.money) + 1);
|
||||
return G.players.map(pl => sortedPlayers.indexOf(pl.id) + 1);
|
||||
}
|
||||
|
||||
export function replay(G: GameState) {
|
||||
|
|
@ -69,7 +76,7 @@ export async function dropPlayer(G: GameState, player: number) {
|
|||
}
|
||||
|
||||
export function currentPlayer(G: GameState) {
|
||||
return G.players.map((pl, i) => pl.availableMoves ? i : -1).filter(i => i !== -1);
|
||||
return G.currentPlayer;
|
||||
}
|
||||
|
||||
export { stripSecret } from './src/engine';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue