engine: stop dropPlayer from clobbering simultaneous bidders
dropPlayer unconditionally called nextPlayer, which resets currentPlayers to a single player. During an auction (currentPlayers = all pending bidders) dropping one bidder handed the turn to the auctioneer — who has no available moves yet — wedging the game and discarding the other bidders' pending turns. It also advanced the turn even when the dropped player was not up at all, and skipped the next player's upkeep (actions/available moves) in the move phase. Now the drop auto-plays only the dropped player's own pending decision through the regular engine path — Bid bash in the bid phase (including the additional-bid round), Accept the highest bid as auctioneer, Pass in the move phase — and touches nothing if the player is not up. The engine's existing isDropped filters take care of skipping them afterwards. Also make the highest-bid computation ignore dropped players' bids: a dropped bidder's stale high bid used to poison Math.max, leaving highestBidders empty and crashing the accept/decline phase.
This commit is contained in:
parent
501a33103c
commit
46534ebd30
3 changed files with 81 additions and 12 deletions
|
|
@ -475,10 +475,11 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
|
|||
.forEach((p) => {
|
||||
p.showBid = true;
|
||||
});
|
||||
const highestBid = Math.max(...G.players.map((p) => p.bid));
|
||||
const highestBidders = G.players
|
||||
.filter((p) => p.id != G.auctioningPlayer && !p.isDropped && p.bid === highestBid)
|
||||
.map((p) => p.id);
|
||||
// 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;
|
||||
|
|
@ -487,11 +488,10 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
|
|||
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.auctioningPlayer && !p.isDropped && p.bid + p.additionalBid === highestBid
|
||||
)
|
||||
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) => {
|
||||
|
|
|
|||
|
|
@ -451,5 +451,49 @@ describe('wrapper (tentative turns)', () => {
|
|||
expect(wrapper.toSave(result)).to.not.be.undefined;
|
||||
expect(result.players[dropped].isDropped).to.be.true;
|
||||
expect(result.currentPlayers).to.not.include(dropped);
|
||||
// The drop played the pending turn through the regular path: the next player
|
||||
// got their upkeep (actions, available moves) and can actually play
|
||||
expect(result.players[result.currentPlayers[0]].availableMoves).to.not.be.null;
|
||||
expect(result.players[result.currentPlayers[0]].actions).to.equal(2);
|
||||
});
|
||||
|
||||
it('should only auto-play the dropped bidder and keep the other bidders current', async () => {
|
||||
const platform = new Platform(3, 'wrapper-test-drop-bid');
|
||||
const { A, B, C } = await playToBidPhase(platform);
|
||||
|
||||
// Both B and C are deciding on their bids; dropping C must auto-play only C's
|
||||
// pending bid and leave B's turn untouched
|
||||
platform.saved = await wrapper.dropPlayer(platform.saved, C);
|
||||
expect(wrapper.toSave(platform.saved)).to.not.be.undefined;
|
||||
expect(platform.saved.phase).to.equal(Phase.Bid);
|
||||
expect(platform.saved.currentPlayers).to.deep.equal([B]);
|
||||
expect(platform.saved.players[B].availableMoves).to.not.be.null;
|
||||
|
||||
// B's bid then resolves the auction normally, with the dropped bidder excluded
|
||||
const bid = await platform.send([{ name: MoveName.Bid, data: true, extraData: { price: 3 } }], B);
|
||||
expect(bid.saved).to.be.true;
|
||||
expect(platform.saved.phase).to.equal(Phase.AcceptDecline);
|
||||
expect(platform.saved.currentPlayers).to.deep.equal([A]);
|
||||
expect(platform.saved.highestBidders).to.deep.equal([B]);
|
||||
});
|
||||
|
||||
it('should not advance the turn when dropping a player who is not up', async () => {
|
||||
const platform = new Platform(3, 'wrapper-test-drop-idle');
|
||||
const { A, B, C } = await playToBidPhase(platform);
|
||||
|
||||
// B has already bid; C is still deciding
|
||||
await platform.send([{ name: MoveName.Bid, data: true, extraData: { price: 3 } }], B);
|
||||
expect(platform.saved.currentPlayers).to.deep.equal([C]);
|
||||
|
||||
// Dropping B (not a current player) must not touch C's pending turn
|
||||
platform.saved = await wrapper.dropPlayer(platform.saved, B);
|
||||
expect(platform.saved.currentPlayers).to.deep.equal([C]);
|
||||
expect(platform.saved.players[C].availableMoves).to.not.be.null;
|
||||
|
||||
// C's lower bid wins because dropped players are excluded from the auction
|
||||
await platform.send([{ name: MoveName.Bid, data: true, extraData: { price: 2 } }], C);
|
||||
expect(platform.saved.phase).to.equal(Phase.AcceptDecline);
|
||||
expect(platform.saved.highestBidders).to.deep.equal([C]);
|
||||
expect(platform.saved.currentPlayers).to.deep.equal([A]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import { cloneDeep } from 'lodash';
|
||||
import type { GameState } from './index';
|
||||
import * as engine from './src/engine';
|
||||
import { Phase } from './src/gamestate';
|
||||
import type { LogMove } from './src/log';
|
||||
import { Move } from './src/move';
|
||||
import { Move, MoveName } from './src/move';
|
||||
import { asserts } from './src/utils';
|
||||
|
||||
export async function init(nbPlayers: number, expansions: string[], options: {}, seed?: string): Promise<GameState> {
|
||||
|
|
@ -144,10 +145,34 @@ export function round(G: GameState) {
|
|||
export async function dropPlayer(G: GameState, player: number) {
|
||||
G.players[player].isDropped = true;
|
||||
|
||||
engine.nextPlayer(G);
|
||||
// Auto-play only the dropped player's own pending decision, if any. Everything
|
||||
// else — in particular the other simultaneous bidders' turns during an auction —
|
||||
// stays untouched: from here on the engine itself skips dropped players
|
||||
// (`nextPlayer` recursion, the `!isDropped` filters in the auction logic).
|
||||
if (G.currentPlayers.includes(player)) {
|
||||
switch (G.phase) {
|
||||
case Phase.Bid:
|
||||
// A dropped bidder bids nothing (also covers the additional-bid round)
|
||||
G = engine.move(G, { name: MoveName.Bid, data: true, extraData: { price: 0 } }, player);
|
||||
break;
|
||||
|
||||
case Phase.AcceptDecline:
|
||||
// A dropped auctioneer accepts the highest bid (Accept is always
|
||||
// available; Decline requires enough money to pay the bid)
|
||||
G = engine.move(G, { name: MoveName.Accept, data: G.highestBidders[0] }, player);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Move phase: the dropped player simply passes, which advances the
|
||||
// turn through the regular path (upkeep, next player's actions, ...)
|
||||
G = engine.move(G, { name: MoveName.Pass, data: true }, player);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Dropping a player always yields a committed state: tentative states are never
|
||||
// persisted, so `G` was committed to begin with and the drop simply advances the turn.
|
||||
// persisted, so `G` was committed to begin with, and the auto-played moves above
|
||||
// (Bid / Accept / Pass) are all turn-boundary moves.
|
||||
G.newTurn = true;
|
||||
|
||||
return G;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue