✨ (all) Simultaneous bids

This commit is contained in:
Lucas Mello 2021-09-19 16:20:04 -03:00
commit 560e272e49
6 changed files with 67 additions and 67 deletions

View file

@ -38,7 +38,7 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
// Undo
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && lastLog.player == G.currentPlayer) moves[MoveName.Undo] = [true];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player)) moves[MoveName.Undo] = [true];
return moves;
}
@ -55,7 +55,7 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
// Undo
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && lastLog.player == G.currentPlayer) moves[MoveName.Undo] = [true];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player)) moves[MoveName.Undo] = [true];
return moves;
}
@ -299,7 +299,7 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
// Undo
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && lastLog.player == G.currentPlayer) moves[MoveName.Undo] = [true];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player)) moves[MoveName.Undo] = [true];
return moves;
}

View file

@ -17,7 +17,7 @@ describe('Engine', () => {
it('should play full game', () => {
const G = setup(5, { beginner: false }, 'test');
// G.currentPlayer = 0;
// G.currentPlayers = [0];
// G.players[0].actions = 2;
// G.players[0].availableMoves = availableMoves(G, G.players[0]);
// G.players[0].factories[0] = ContainerColor.Brown;

View file

@ -72,7 +72,7 @@ export function setup(numPlayers: number, { beginner = true }: GameOptions, seed
const G: GameState = {
players,
startingPlayer,
currentPlayer: startingPlayer,
currentPlayers: [startingPlayer],
containersLeft,
factoriesLeft,
warehousesLeft,
@ -104,8 +104,8 @@ export function setup(numPlayers: number, { beginner = true }: GameOptions, seed
G.log.push({ type: 'event', event: { name: GameEventName.GameStart } });
G.players[G.currentPlayer!].actions = 2;
G.players[G.currentPlayer!].availableMoves = availableMoves(G, G.players[G.currentPlayer!]);
G.players[startingPlayer].actions = 2;
G.players[startingPlayer].availableMoves = availableMoves(G, G.players[startingPlayer]);
return G;
}
@ -134,14 +134,14 @@ export function stripSecret(G: GameState, player?: number): GameState {
}
export function currentPlayers(G: GameState): number[] {
return [G.currentPlayer!];
return G.currentPlayers;
}
export function move(G: GameState, move: Move, playerNumber: number, fake?: boolean): GameState {
const player = G.players[playerNumber];
const available = player.availableMoves?.[move.name];
assert(G.currentPlayer === playerNumber, 'It is not your turn!');
assert(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)),
@ -365,9 +365,9 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
player.ship.shipPosition = move.data;
if (move.data === ShipPosition.Island) {
player.actions = 0;
G.auctioningPlayer = G.currentPlayer;
G.auctioningPlayer = G.currentPlayers[0];
G.phase = Phase.Bid;
nextPlayer(G);
G.currentPlayers = G.players.map((p) => p.id).filter((id) => id !== G.auctioningPlayer);
} else {
player.actions--;
}
@ -452,7 +452,7 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
pretty: `${playerNameHTML(player)} bids $${move.extraData.price}`,
});
player.bid = move.extraData.price;
nextPlayer(G);
G.currentPlayers = G.currentPlayers.filter((id) => id !== player.id);
} else {
G.hiddenLog.push({
type: 'move',
@ -462,39 +462,37 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
pretty: `${playerNameHTML(player)} bids additional $${move.extraData.price}`,
});
player.additionalBid = move.extraData.price;
do {
nextPlayer(G);
} while (G.highestBidders.indexOf(G.currentPlayer) === -1 && G.auctioningPlayer !== G.currentPlayer);
G.currentPlayers = G.currentPlayers.filter((id) => id !== player.id);
}
if (G.auctioningPlayer === G.currentPlayer) {
if (G.currentPlayers.length === 0) {
if (G.highestBidders.length === 0) {
G.players
.filter((p) => p.id !== G.currentPlayer)
.filter((p) => p.id !== G.auctioningPlayer)
.forEach((p) => {
p.showBid = true;
});
const highestBid = Math.max(...G.players.map((p) => p.bid));
const highestBidders = G.players
.filter((p) => p.id != G.currentPlayer && p.bid === highestBid)
.filter((p) => p.id != G.auctioningPlayer && p.bid === highestBid)
.map((p) => p.id);
G.highestBidders = highestBidders;
if (highestBidders.length > 1) {
while (highestBidders.indexOf(G.currentPlayer) === -1) {
nextPlayer(G);
}
G.currentPlayers = highestBidders;
} else {
G.currentPlayers = [G.auctioningPlayer!];
G.phase = Phase.AcceptDecline;
}
} else {
const highestBid = Math.max(...G.players.map((p) => p.bid + p.additionalBid));
const highestBidders = G.players
.filter((p) => p.id != G.currentPlayer && p.bid + p.additionalBid === highestBid)
.filter((p) => p.id != G.auctioningPlayer && 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;
}
}
@ -529,7 +527,7 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
doUpkeep(G);
} else {
G.phase = Phase.GameEnd;
G.currentPlayer = undefined;
G.currentPlayers = [];
calculateEndScore(G);
}
}
@ -566,7 +564,7 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
doUpkeep(G);
} else {
G.phase = Phase.GameEnd;
G.currentPlayer = undefined;
G.currentPlayers = [];
calculateEndScore(G);
}
}
@ -599,7 +597,7 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
doUpkeep(G);
} else {
G.phase = Phase.GameEnd;
G.currentPlayer = undefined;
G.currentPlayers = [];
calculateEndScore(G);
}
}
@ -611,7 +609,7 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
asserts<Moves.MoveUndo>(move);
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && lastLog.player == G.currentPlayer && !fake) {
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player) && !fake) {
G.log.pop();
G = reconstructState(getBaseState(G), G.log);
}
@ -624,8 +622,7 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
if (move.name != MoveName.GetLoan && move.name != MoveName.PayLoan) player.lastMove = move;
if (G.currentPlayer !== undefined)
G.players[G.currentPlayer].availableMoves = availableMoves(G, G.players[G.currentPlayer]);
G.currentPlayers.forEach((p) => (G.players[p].availableMoves = availableMoves(G, G.players[p])));
return G;
}
@ -904,11 +901,11 @@ function playerBefore(player: Player, G: GameState) {
}
function nextPlayer(G: GameState) {
G.currentPlayer = (G.currentPlayer! + 1) % G.players.length;
G.currentPlayers = [(G.currentPlayers[0] + 1) % G.players.length];
}
function doUpkeep(G: GameState) {
const player = G.players[G.currentPlayer!];
const player = G.players[G.currentPlayers[0]];
const loanCount = player.loans.length;
const interest: string[] = [];
for (let i = 0; i < loanCount; i++) {
@ -1019,13 +1016,14 @@ function doUpkeep(G: GameState) {
}
}
G.players[G.currentPlayer!].actions = 2;
G.players[G.currentPlayer!].produced = [];
G.players[G.currentPlayer!].containersOnFactoryStore.forEach((c) => (c.moved = false));
G.players[G.currentPlayer!].containersOnWarehouseStore.forEach((c) => (c.moved = false));
G.players[G.currentPlayer!].didDomesticSale = false;
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 (G.currentPlayer == G.startingPlayer) {
if (currentPlayer == G.startingPlayer) {
G.round++;
}
}

View file

@ -122,7 +122,7 @@ export interface ShipPiece {
export interface GameState {
players: Player[];
startingPlayer: number;
currentPlayer: number | undefined;
currentPlayers: number[];
containersLeft: ContainerPiece[];
factoriesLeft: FactoryPiece[];
warehousesLeft: WarehousePiece[];

View file

@ -69,7 +69,7 @@ export async function dropPlayer(G: GameState, player: number) {
}
export function currentPlayer(G: GameState) {
return G.currentPlayer;
return G.currentPlayers;
}
export function messages(G: GameState) {

View file

@ -623,7 +623,7 @@ export default class Game extends Vue {
if (!fake && this.preferences.sound && this.G?.log[this.G?.log.length - 1].type == 'move') {
const move = (this.G?.log[this.G?.log.length - 1] as LogMove).move;
if (move.name == MoveName.Pass && this.G.currentPlayer == this.player) {
if (move.name == MoveName.Pass && this.G.currentPlayers.includes(this.player!)) {
(document.getElementById('notification')!.cloneNode(true) as HTMLAudioElement).play();
} else {
if (
@ -873,6 +873,7 @@ export default class Game extends Vue {
}
onPieceDrop(e: PieceComponent, d: any) {
const currentPlayer = this.G!.currentPlayers[0];
switch (e.pieceType) {
case PieceType.Warehouse:
this.sendMove({ name: MoveName.BuyWarehouse, data: true, extraData: { id: e.pieceId } });
@ -900,7 +901,7 @@ export default class Game extends Vue {
} else if (container.state == ContainerState.OnFactoryStore) {
if (d.type == DropZoneType.FactoryStore) {
if (
this.G!.players[this.G!.currentPlayer!].containersOnFactoryStore.find(
this.G!.players[currentPlayer].containersOnFactoryStore.find(
(c) => c.piece.id == e.pieceId
)!.price === d.price
) {
@ -927,7 +928,7 @@ export default class Game extends Vue {
} else if (container.state == ContainerState.OnWarehouseStore) {
if (d.type == DropZoneType.WarehouseStore) {
if (
this.G!.players[this.G!.currentPlayer!].containersOnWarehouseStore.find(
this.G!.players[currentPlayer].containersOnWarehouseStore.find(
(c) => c.piece.id == e.pieceId
)!.price === d.price
) {
@ -955,7 +956,7 @@ export default class Game extends Vue {
break;
case PieceType.Ship:
const player = this.G!.players[this.G!.currentPlayer!];
const player = this.G!.players[currentPlayer];
if (d.type.startsWith('playerHarbor')) {
if (!player.ship.shipPosition.startsWith('playerHarbor'))
this.sendMove({ name: MoveName.Sail, data: d.type });
@ -999,7 +1000,7 @@ export default class Game extends Vue {
const loan = event as LoanCard;
if (loan.owner == -1) {
this.sendMove({ name: MoveName.GetLoan, data: true });
} else if (loan.owner == this.G?.currentPlayer) {
} else if (loan.owner == this.player) {
this.sendMove({ name: MoveName.PayLoan, data: true });
}
}
@ -1028,8 +1029,9 @@ export default class Game extends Vue {
canMove() {
return (
this.player &&
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves
);
@ -1038,7 +1040,7 @@ export default class Game extends Vue {
canDragContainer(container: Piece) {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
switch (container.state) {
@ -1083,7 +1085,7 @@ export default class Game extends Vue {
canDragFactory(factory: Piece) {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
if (factory.owner !== -1) return false;
@ -1095,7 +1097,7 @@ export default class Game extends Vue {
canDragWarehouse(warehouse: Piece) {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
if (warehouse.owner !== -1) return false;
@ -1106,7 +1108,7 @@ export default class Game extends Vue {
canDragLoan(loanCard: Piece) {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
if (loanCard.owner == -1) {
@ -1121,7 +1123,7 @@ export default class Game extends Vue {
canDragShip(ship: Piece) {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
return availableMoves[MoveName.Sail] && this.isCurrentPlayer(ship.owner);
@ -1130,7 +1132,7 @@ export default class Game extends Vue {
canPass() {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
return !!availableMoves[MoveName.Pass];
@ -1139,7 +1141,7 @@ export default class Game extends Vue {
canUndo() {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
return !!availableMoves[MoveName.Undo];
@ -1148,7 +1150,7 @@ export default class Game extends Vue {
canDecline() {
if (!this.canMove()) return false;
const currentPlayer = this.G!.players[this.G!.currentPlayer!];
const currentPlayer = this.G!.players[this.player!];
const availableMoves = currentPlayer.availableMoves!;
return !!availableMoves[MoveName.Decline];
@ -1167,9 +1169,9 @@ export default class Game extends Vue {
}
getStatusMessage() {
if (this.G?.currentPlayer == undefined) {
if (!this.G || this.G.currentPlayers == []) {
return 'Game ended!';
} else if (this.G?.currentPlayer == this.player) {
} else if (this.player !== undefined && this.G?.currentPlayers.includes(this.player)) {
if (this.G.players[this.player].availableMoves![MoveName.Bid]) {
if (this.G.highestBidders.length != 0) {
return 'It\'s a tie, choose your ADDITIONAL bid!';
@ -1186,7 +1188,7 @@ export default class Game extends Vue {
this.G.log[this.G.log.length - 1].type != 'move' ||
(this.G.log[this.G.log.length - 1] as LogMove).move.name == MoveName.Pass
)
return `Waiting for ${this.G?.players[this.G!.currentPlayer].name} to play...`;
return `Waiting for ${this.G!.currentPlayers.map(p => this.G?.players[p].name).join(', ')} to play...`;
let log = (this.G.log[this.G.log.length - 1] as LogMove).pretty;
while (log?.indexOf('>') != -1) {
@ -1198,13 +1200,13 @@ export default class Game extends Vue {
}
isCurrentPlayer(player) {
return this.G && this.G.currentPlayer === player;
return this.G && this.G.currentPlayers.includes(player);
}
canBuyFactory(color) {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.BuyFactory] &&
@ -1215,7 +1217,7 @@ export default class Game extends Vue {
canBuyWarehouse() {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.BuyWarehouse]
@ -1225,7 +1227,7 @@ export default class Game extends Vue {
canProduce(color) {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.Produce] &&
@ -1236,7 +1238,7 @@ export default class Game extends Vue {
canGetLoan() {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.GetLoan]
@ -1246,7 +1248,7 @@ export default class Game extends Vue {
canPayLoan() {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.PayLoan]
@ -1256,7 +1258,7 @@ export default class Game extends Vue {
canSail() {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.Sail]
@ -1266,7 +1268,7 @@ export default class Game extends Vue {
canArrangeFactory() {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.ArrangeFactory]
@ -1276,7 +1278,7 @@ export default class Game extends Vue {
canArrangeWarehouse() {
return (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.ArrangeWarehouse]
@ -1286,7 +1288,7 @@ export default class Game extends Vue {
canBuyFromPlayerFactory(otherPlayer) {
if (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.BuyFromFactory]
@ -1304,7 +1306,7 @@ export default class Game extends Vue {
canBuyFromPlayerWarehouse(otherPlayer) {
if (
this.G &&
this.G.currentPlayer == this.player! &&
this.G.currentPlayers.includes(this.player!) &&
this.G.players[this.player!] &&
this.G.players[this.player!].availableMoves &&
this.G.players[this.player!].availableMoves![MoveName.BuyFromWarehouse]
@ -1402,7 +1404,7 @@ export default class Game extends Vue {
if (!this.canMove())
return [];
return this.G!.players[this.G!.currentPlayer!].availableMoves!;
return this.G!.players[this.player!].availableMoves!;
}
getFinalScoreHTML(player, i) {