diff --git a/README.md b/README.md index 1591c44..8a78311 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,9 @@ pnpm dev # viewer dev server + mock backend harness (human + bots) pnpm check # fmt:check + lint + tsc + test + build ``` -Dev harness URL params: `?players=4&seed=xyz&delay=700&auto=1&fastBid=1&kicker=1`. +An optional **Catch-up pricing** rule (`catchUp: true`) applies to auctions in Era II and III when at least four players remain. Based on current victory points (ties broken by seat), the top two players pay a surcharge of ◈ 10/5 in Era II or ◈ 20/10 in Era III; the bottom two receive the corresponding discounts. The adjustment applies to colony upgrades and Kicker cards, combines with existing card discounts, and cannot reduce payment below zero. Printed minimum bids are unchanged. Factories, colonists, and robots retain their normal costs. Games created without the option are unaffected. To expose it on BGS game creation, add `{ "name": "catchUp", "label": "Catch-up pricing (4+ players)", "type": "checkbox", "default": false, "requiredPlayers": [4, 5, 6, 7, 8, 9] }` to the `options` array of the Outpost gameinfo doc. This declaration only affects newly created games. + +Dev harness URL params: `?players=4&seed=xyz&delay=700&auto=1&fastBid=1&kicker=1&catchUp=1`. Node >= 24, pnpm 11 (see `packageManager`). All dependencies pinned. diff --git a/packages/engine/package.json b/packages/engine/package.json index f641bbe..dba7dc4 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "outpost-engine", - "version": "0.10.33", + "version": "0.10.34", "description": "Unofficial Outpost rules engine for boardgamers.space", "license": "AGPL-3.0", "files": [ diff --git a/packages/engine/src/ai.ts b/packages/engine/src/ai.ts index c3d55b7..47f5e09 100644 --- a/packages/engine/src/ai.ts +++ b/packages/engine/src/ai.ts @@ -2,6 +2,7 @@ import { FACTORIES, KICKER_SPECS, MEGA_CARDS, PRODUCTION_DECKS, UPGRADE_SPECS } import { applyMove } from "./moves.js"; import { auctionCard, + auctionDiscount, bestDiscard, bestPayment, canBuyFactory, @@ -13,7 +14,7 @@ import { operators as availableOperators, populationCost, populationMax, - upgradeDiscount, + maxBid, } from "./state.js"; import type { GameState, Move, PlayerState, Resource, TurnBuy, Upgrade } from "./types.js"; @@ -39,7 +40,7 @@ export function chooseMove(state: GameState, seat: number): Move { // fastBid: bid the max affordable when the list price can be exceeded, // otherwise pass (the sealed bid keeps a weak hand hidden). if (auction?.bids) { - const max = handValue(player) + (auction.upgrade ? upgradeDiscount(player, auction.upgrade) : 0); + const max = maxBid(state, seat, auction.upgrade, auction.kicker); if (max > auctionCard(auction).price) { return { action: "bid", amount: max }; } @@ -51,11 +52,11 @@ export function chooseMove(state: GameState, seat: number): Move { if (!auction) { throw new Error("no auction in progress"); } - const due = Math.max(0, auction.highBid - (auction.upgrade ? upgradeDiscount(player, auction.upgrade) : 0)); + const due = Math.max(0, auction.highBid - auctionDiscount(state, seat, auction)); return { action: "pay", cards: bestPayment(player, due) ?? player.hand.map((_, i) => i) }; } case "actions": - return chooseAction(state, player); + return chooseAction(state, seat, player); default: throw new Error(`no move expected in phase ${state.phase}`); } @@ -130,7 +131,7 @@ function chooseDiscard(player: PlayerState): Move { return { action: "discard", cards: bestDiscard(player) }; } -function chooseAction(state: GameState, player: PlayerState): Move { +function chooseAction(state: GameState, seat: number, player: PlayerState): Move { const cash = handValue(player); // 1. Auction the highest-VP card we can afford at list price (upgrades and @@ -139,7 +140,7 @@ function chooseAction(state: GameState, player: PlayerState): Move { let bestVp = -1; for (const [index, upgrade] of state.market.entries()) { const spec = UPGRADE_SPECS[upgrade]; - const due = Math.max(0, spec.price - upgradeDiscount(player, upgrade)); + const due = Math.max(0, spec.price - auctionDiscount(state, seat, { upgrade })); if (cash >= due && spec.vp > bestVp) { bestVp = spec.vp; best = { kicker: false, index, bid: spec.price }; @@ -147,7 +148,7 @@ function chooseAction(state: GameState, player: PlayerState): Move { } for (const [index, kicker] of state.kickerMarket.entries()) { const spec = KICKER_SPECS[kicker]; - if (cash >= spec.price && spec.vp > bestVp) { + if (cash >= Math.max(0, spec.price - auctionDiscount(state, seat, { kicker })) && spec.vp > bestVp) { bestVp = spec.vp; best = { kicker: true, index, bid: spec.price }; } diff --git a/packages/engine/src/analysis.spec.ts b/packages/engine/src/analysis.spec.ts index b37a5c6..acfd683 100644 --- a/packages/engine/src/analysis.spec.ts +++ b/packages/engine/src/analysis.spec.ts @@ -133,3 +133,15 @@ test("bounced exchanges constrain the target's retained resource values", async assert.ok(sample.players[0]!.hand.every((card) => card.t !== "water" || card.v <= 9)); } }); + +test("analysis retains catchUp pricing and its active-player ranks", async () => { + const state = await init(4, [], { catchUp: true }, "analysis-catchup"); + state.era = 2; + const scenario = createAnalysisScenario(state, { player: 0, seed: "simulation" }); + assert.equal(scenario.options.catchUp, true); + assert.equal(scenario.era, 2); + assert.equal( + maxBid(scenario, 0, "heavyEquipment"), + scenario.players[0]!.hand.reduce((sum, card) => sum + card.v, 0) - 10 + ); +}); diff --git a/packages/engine/src/catch-up-replay.spec.ts b/packages/engine/src/catch-up-replay.spec.ts new file mode 100644 index 0000000..1587f7c --- /dev/null +++ b/packages/engine/src/catch-up-replay.spec.ts @@ -0,0 +1,46 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { applyMove, initGame } from "./moves.js"; +import { replay } from "./replay.js"; +import { stripSecret } from "../wrapper.js"; +import { auctionDiscount } from "./state.js"; + +test("catchUp sealed and open payment replay from stripped logs", () => { + for (const fastBid of [false, true]) { + const s = initGame(4, { catchUp: true, fastBid }, `replay-catchup-${fastBid}`); + const round = s.log.find((e) => e.type === "round"); + assert.ok(round && round.type === "round"); + round.era = 2; + s.era = 2; + round.market = ["heavyEquipment"]; + s.market = ["heavyEquipment"]; + for (const { player, cards } of round.produced) { + cards.splice(0, cards.length, { t: "research", v: 20 }, { t: "research", v: 20 }); + s.players[player]!.hand = cards.map((c) => ({ ...c })); + } + const opener = s.activeSeat; + assert.equal(auctionDiscount(s, opener, { upgrade: "heavyEquipment" }), -10); + applyMove(s, { action: "auction", marketIndex: 0, bid: 25 }, opener); + if (fastBid) { + for (let seat = 0; seat < 4; seat++) { + if (seat !== opener) { + applyMove(s, { action: "bidPass" }, seat); + } + } + } else { + while (s.phase === "auction") { + applyMove(s, { action: "bidPass" }, s.auction!.activeBidder); + } + } + assert.equal(s.phase, "auctionPayment"); + applyMove(s, { action: "pay", cards: [0, 1] }, opener); + const stripped = stripSecret(s); + const restored = replay(stripped); + assert.equal(restored.players[opener]!.upgrades.heavyEquipment, s.players[opener]!.upgrades.heavyEquipment); + assert.equal(restored.era, 2); + assert.equal( + auctionDiscount(restored, opener, { upgrade: "heavyEquipment" }), + auctionDiscount(s, opener, { upgrade: "heavyEquipment" }) + ); + } +}); diff --git a/packages/engine/src/catch-up.spec.ts b/packages/engine/src/catch-up.spec.ts new file mode 100644 index 0000000..c98d823 --- /dev/null +++ b/packages/engine/src/catch-up.spec.ts @@ -0,0 +1,196 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { chooseMove } from "./ai.js"; +import { applyMove, initGame } from "./moves.js"; +import { auctionDiscount, maxBid, mustAutoPassBid, publicMaxBid, rankAdjustment } from "./state.js"; +import type { GameState } from "./types.js"; + +function game(count = 4, fastBid = false): GameState { + const s = initGame(count, { catchUp: true, fastBid, kicker: true }, "catchup-test"); + s.era = 2; + s.market = ["scientists", "heavyEquipment"]; + s.kickerMarket = ["smelter"]; + for (const p of s.players) { + p.hand = [ + { t: "research", v: 30 }, + { t: "research", v: 30 }, + ]; + } + // Distinct VP without using the auctioned cards. + s.players[0]!.upgrades.outpost = 3; + s.players[1]!.upgrades.outpost = 2; + s.players[2]!.upgrades.outpost = 1; + return s; +} + +test("rank applies only to four active players in era II/III; VP ties use seat, not spent", () => { + const s = game(); + assert.deepEqual( + s.players.map((_, i) => rankAdjustment(s, i)), + [-10, -5, 5, 10] + ); + s.era = 3; + assert.deepEqual( + s.players.map((_, i) => rankAdjustment(s, i)), + [-20, -10, 10, 20] + ); + s.era = 1; + assert.deepEqual( + s.players.map((_, i) => rankAdjustment(s, i)), + [0, 0, 0, 0] + ); + s.era = 2; + for (const p of s.players) { + p.upgrades.outpost = 0; + p.spent = 100; + } + s.players[3]!.spent = 9999; + assert.deepEqual( + s.players.map((_, i) => rankAdjustment(s, i)), + [-10, -5, 5, 10] + ); + s.players[1]!.dropped = true; + assert.deepEqual( + s.players.map((_, i) => rankAdjustment(s, i)), + [0, 0, 0, 0] + ); + const three = game(3); + assert.deepEqual( + three.players.map((_, i) => rankAdjustment(three, i)), + [0, 0, 0] + ); + s.players[1]!.dropped = false; + s.options.catchUp = false; + assert.equal(rankAdjustment(s, 0), 0); +}); + +test("signed discounts combine with existing upgrade discount, Kicker and zero floor", () => { + const s = game(); + s.players[0]!.upgrades.dataLibrary = 2; + assert.equal(auctionDiscount(s, 0, { upgrade: "scientists" }), 10); + assert.equal(auctionDiscount(s, 0, { kicker: "smelter" }), -10); + assert.equal(auctionDiscount(s, 3, { kicker: "smelter" }), 10); + assert.equal(maxBid(s, 0, "scientists"), 70); + assert.equal(publicMaxBid(s, 0, "scientists"), 44); // two research cards, public max 17 each + s.players[3]!.upgrades.dataLibrary = 10; + assert.equal(auctionDiscount(s, 3, { upgrade: "scientists" }), 105); // VP ranking is recalculated after the upgrade change +}); + +test("open auction enforces surcharge for opening and raises, discounts payment and Kicker", () => { + const s = game(); + s.activeSeat = 0; + s.players[0]!.hand = [{ t: "research", v: 30 }]; + assert.throws(() => applyMove(s, { action: "auction", marketIndex: 1, bid: 25 }, 0), /cannot afford/); + s.players[0]!.hand.push({ t: "research", v: 10 }); + applyMove(s, { action: "auction", marketIndex: 1, bid: 25 }, 0); + assert.equal(s.auction?.highBid, 25); + const bidder = s.auction?.activeBidder as number; + s.players[bidder]!.hand = [{ t: "research", v: 20 }]; + assert.throws(() => applyMove(s, { action: "bid", amount: 26 }, bidder), /cannot afford/); + applyMove(s, { action: "bidPass" }, bidder); + while (s.phase === "auction") { + applyMove(s, { action: "bidPass" }, s.auction!.activeBidder); + } + assert.equal(s.phase, "auctionPayment"); + assert.throws(() => applyMove(s, { action: "pay", cards: [0] }, 0), /pay|enough|cover/i); + applyMove(s, { action: "pay", cards: [0, 1] }, 0); + const last = s.log.at(-1); + assert.equal(last?.type === "move" ? last.info?.paid : undefined, 40); + s.activeSeat = 3; + s.players[3]!.hand = []; + // Kicker list price remains the minimum bid despite the buyer's discount. + const kickerPrice = 15; + assert.throws( + () => applyMove(s, { action: "auction", kicker: true, marketIndex: 0, bid: kickerPrice - 1 }, 3), + /at least/ + ); +}); + +test("public bounds and auto-pass account for signed Kicker adjustment", () => { + const s = game(); + s.players[0]!.hand = [{ t: "ore", v: 5 }]; + s.players[3]!.hand = [{ t: "ore", v: 5 }]; + assert.equal(maxBid(s, 0, undefined, "smelter"), -5); + assert.equal(maxBid(s, 3, undefined, "smelter"), 15); + s.auction = { + marketIndex: 0, + kicker: "smelter", + auctioneer: 1, + highBid: 12, + highBidder: 1, + activeBidder: 0, + passed: [], + }; + assert.equal(mustAutoPassBid(s, 0), true); + assert.equal(mustAutoPassBid(s, 3), false); +}); + +test("sealed auctions apply rank to bids and second-price payment; AI uses it", () => { + const s = game(4, true); + s.activeSeat = 0; + s.players[0]!.hand = [{ t: "research", v: 30 }]; + assert.throws(() => applyMove(s, { action: "auction", marketIndex: 1, bid: 25 }, 0), /cannot afford/); + s.players[0]!.hand.push({ t: "research", v: 10 }); + applyMove(s, { action: "auction", marketIndex: 1, bid: 25 }, 0); + assert.equal(chooseMove(s, 3).action, "bid"); + applyMove(s, { action: "bid", amount: 60 }, 3); + for (const seat of [1, 2]) { + applyMove(s, { action: "bidPass" }, seat); + } + assert.equal(s.auction?.highBidder, 3); + assert.equal(s.auction?.highBid, 26); + assert.equal(chooseMove(s, 3).action, "pay"); + assert.deepEqual(chooseMove(s, 3), { action: "pay", cards: [0] }); + applyMove(s, { action: "pay", cards: [0] }, 3); + const last = s.log.at(-1); + assert.equal(last?.type === "move" ? last.info?.paid : undefined, 30); +}); + +test("discounts can offset surcharges and payment never goes below zero", () => { + const s = game(); + s.activeSeat = 0; + s.players[0]!.upgrades.dataLibrary = 5; + s.players[0]!.hand = []; + assert.equal(auctionDiscount(s, 0, { upgrade: "scientists" }), 40); + applyMove(s, { action: "auction", marketIndex: 0, bid: 40 }, 0); + while (s.phase === "auction") { + applyMove(s, { action: "bidPass" }, s.auction!.activeBidder); + } + applyMove(s, { action: "pay", cards: [] }, 0); + assert.equal(s.players[0]!.upgrades.scientists, 1); +}); + +test("Kicker auction uses signed pricing for both open and sealed modes", () => { + for (const fastBid of [false, true]) { + const s = game(4, fastBid); + s.activeSeat = 0; + s.players[0]!.hand = [{ t: "research", v: 20 }]; + assert.throws(() => applyMove(s, { action: "auction", kicker: true, marketIndex: 0, bid: 15 }, 0), /cannot afford/); + s.players[0]!.hand.push({ t: "research", v: 10 }); + applyMove(s, { action: "auction", kicker: true, marketIndex: 0, bid: 15 }, 0); + if (fastBid) { + for (const seat of [1, 2, 3]) { + applyMove(s, { action: "bidPass" }, seat); + } + } else { + while (s.phase === "auction") { + applyMove(s, { action: "bidPass" }, s.auction!.activeBidder); + } + } + assert.equal(s.auction?.highBid, 15); + assert.deepEqual(chooseMove(s, 0), { action: "pay", cards: [0, 1] }); + applyMove(s, { action: "pay", cards: [0, 1] }, 0); + assert.equal(s.players[0]!.kickers.smelter, 1); + } +}); + +test("sealed auto-pass uses public bounds after a rank surcharge", () => { + const s = game(4, true); + s.activeSeat = 3; + s.players[0]!.hand = [{ t: "ore", v: 5 }]; + s.players[1]!.hand = [{ t: "ore", v: 5 }]; + applyMove(s, { action: "auction", kicker: true, marketIndex: 0, bid: 15 }, 3); + assert.equal(s.auction?.bids?.[0], 0); + assert.equal(s.auction?.bids?.[1], 0); + assert.equal(s.auction?.bids?.[2], undefined); +}); diff --git a/packages/engine/src/moves.ts b/packages/engine/src/moves.ts index dd04337..ef1f9b3 100644 --- a/packages/engine/src/moves.ts +++ b/packages/engine/src/moves.ts @@ -3,6 +3,7 @@ import { FACTORIES, KICKER_SPECS, MEGA_CARDS, ROBOT_COST, UPGRADE_SPECS, VICTORY import { colonyEra, refillKickers, refillMarket, updateEraStreaks } from "./market.js"; import { dealStartingHand, producePlayer } from "./production.js"; import { + auctionDiscount, canBuyFactory, computePurchaseOrder, countingHandSize, @@ -21,7 +22,6 @@ import { operators, scores, setup, - upgradeDiscount, } from "./state.js"; import { sanitizeMove } from "./sanitize.js"; import type { @@ -580,8 +580,8 @@ function moveAuction( if (!replayMode && (!Number.isInteger(bid) || bid < price)) { err(`opening bid must be at least ${price}`); } - // Kicker cards have no discounts; upgrades use the buyer's discount. - const discount = upgrade ? upgradeDiscount(player, upgrade) : 0; + // The list bid reserve is unchanged; adjust only the buyer's payment. + const discount = auctionDiscount(state, seat, { upgrade, kicker }); if (!replayMode && handValue(player) < Math.max(0, bid - discount)) { err(`cannot afford a bid of ${bid} (needs ${Math.max(0, bid - discount)}, holds ${handValue(player)})`); } @@ -617,27 +617,23 @@ function moveAuction( return autoPassed.length > 0 ? { ...info, autoPassed } : info; } -function assertCanPayBid(player: PlayerState, upgrade: Upgrade, bid: number): void { - if (replayMode) { - return; - } - const due = Math.max(0, bid - upgradeDiscount(player, upgrade)); - if (handValue(player) < due) { - err(`cannot afford a bid of ${bid} (needs ${due}, holds ${handValue(player)})`); - } -} - /** The log info identifying the card under auction (a colony upgrade or a Kicker card). */ function auctionInfo(auction: NonNullable): MoveInfo { return auction.kicker ? { kicker: auction.kicker } : { upgrade: auction.upgrade }; } /** Kicker-aware affordability check for a bid on the card under auction. */ -function assertCanPayBidFor(player: PlayerState, auction: NonNullable, bid: number): void { +function assertCanPayBidFor( + state: GameState, + seat: number, + auction: NonNullable, + bid: number +): void { + const player = state.players[seat] as PlayerState; if (replayMode) { return; } - const discount = auction.upgrade ? upgradeDiscount(player, auction.upgrade) : 0; + const discount = auctionDiscount(state, seat, auction); const due = Math.max(0, bid - discount); if (handValue(player) < due) { err(`cannot afford a bid of ${bid} (needs ${due}, holds ${handValue(player)})`); @@ -650,8 +646,8 @@ function auctionPrice(auction: NonNullable): number { } /** The winner's discount on the card under auction (Kicker cards have none). */ -function auctionDue(state: GameState, auction: NonNullable, player: PlayerState): number { - const discount = auction.upgrade ? upgradeDiscount(player, auction.upgrade) : 0; +function auctionDue(state: GameState, auction: NonNullable, seat: number): number { + const discount = auctionDiscount(state, seat, auction); return Math.max(0, auction.highBid - discount); } @@ -717,7 +713,7 @@ function moveBid(state: GameState, move: Move & { action: "bid" }, seat: number, if (!Number.isInteger(move.amount) || move.amount <= auction.highBid) { err(`bid must be higher than ${auction.highBid}`); } - assertCanPayBidFor(player, auction, move.amount); + assertCanPayBidFor(state, seat, auction, move.amount); auction.highBid = move.amount; auction.highBidder = seat; const autoPassed: number[] = []; @@ -764,11 +760,11 @@ function fastAutoPass(state: GameState, auction: NonNullable ({ p, i })).filter(({ p }) => !p.dropped); + if (active.length < 4) { + return 0; + } + active.sort((a, b) => victoryPoints(b.p) - victoryPoints(a.p) || a.i - b.i); + const rank = active.findIndex(({ i }) => i === seat); + const unit = state.era === 3 ? 10 : 5; + if (rank === 0) { + return -2 * unit; + } + if (rank === 1) { + return -unit; + } + if (rank === active.length - 1) { + return 2 * unit; + } + if (rank === active.length - 2) { + return unit; + } + return 0; +} + +/** Total signed discount on an auctioned card, including rank adjustment. */ +export function auctionDiscount( + state: GameState, + seat: number, + auction: { upgrade?: Upgrade; kicker?: Kicker } +): number { + const player = state.players[seat]; + if (!player) { + return 0; + } + return (auction.upgrade ? upgradeDiscount(player, auction.upgrade) : 0) + rankAdjustment(state, seat); } /** Resource types a Wily Trader owner may offer in an exchange. */ @@ -569,13 +609,13 @@ export function drawCard(state: GameState, resource: Resource): number { return state.decks[resource].pop() as number; } -/** Max a player could pay for a card right now: hand value plus their discount on it (none for Kicker cards). */ -export function maxBid(state: GameState, seat: number, upgrade?: Upgrade): number { +/** Highest affordable pre-adjustment bid: hand value plus the signed auction discount. */ +export function maxBid(state: GameState, seat: number, upgrade?: Upgrade, kicker?: Kicker): number { const player = state.players[seat]; if (!player) { return 0; } - return handValue(player) + (upgrade ? upgradeDiscount(player, upgrade) : 0); + return handValue(player) + auctionDiscount(state, seat, { upgrade, kicker }); } /** @@ -585,7 +625,7 @@ export function maxBid(state: GameState, seat: number, upgrade?: Upgrade): numbe * any hand they can see. Computed the same way from a stripped log (v = -1) as * from the live state, so replay is identical and it never leaks hand values. */ -export function publicMaxBid(state: GameState, seat: number, upgrade?: Upgrade): number { +export function publicMaxBid(state: GameState, seat: number, upgrade?: Upgrade, kicker?: Kicker): number { const player = state.players[seat]; if (!player) { return 0; @@ -596,7 +636,7 @@ export function publicMaxBid(state: GameState, seat: number, upgrade?: Upgrade): // deck max (Mega Titanium 44 vs deck max 13) — use it, not the deck max. value += card.m ? card.v : MAX_CARD_VALUE[card.t]; } - return value + (upgrade ? upgradeDiscount(player, upgrade) : 0); + return value + auctionDiscount(state, seat, { upgrade, kicker }); } /** True when every card in the player's hand is hidden (v = -1), i.e. a stripped state. */ @@ -620,11 +660,13 @@ export function mustAutoPassBid(state: GameState, seat: number): boolean { if (!player) { return false; } - if (publicMaxBid(state, seat, auction.upgrade) <= auction.highBid) { + if (publicMaxBid(state, seat, auction.upgrade, auction.kicker) <= auction.highBid) { return true; } // Optional chain: states saved before the settings field existed lack it. - return player.settings?.autoPassBids === true && maxBid(state, seat, auction.upgrade) <= auction.highBid; + return ( + player.settings?.autoPassBids === true && maxBid(state, seat, auction.upgrade, auction.kicker) <= auction.highBid + ); } /** Coarse list of the moves available to a player, for the BGS sidebar. */ diff --git a/packages/engine/wrapper.ts b/packages/engine/wrapper.ts index 67ea67d..06c8bfe 100644 --- a/packages/engine/wrapper.ts +++ b/packages/engine/wrapper.ts @@ -510,7 +510,11 @@ export function createAnalysisScenario( observed.rngCounter = 0; observed.log = []; observed.messages = []; - observed.options = { fastBid: data.options.fastBid === true, kicker: data.options.kicker === true }; + observed.options = { + fastBid: data.options.fastBid === true, + kicker: data.options.kicker === true, + catchUp: data.options.catchUp === true, + }; for (const p of observed.players) { p.dropped = false; p.settings = { autoPassBids: false }; @@ -571,17 +575,17 @@ export function createAnalysisScenario( } const a = copy.auction; if (a) { - if (knowledge.bidFloors.some((amount, seat) => amount > maxBid(copy, seat, a.upgrade))) { + if (knowledge.bidFloors.some((amount, seat) => amount > maxBid(copy, seat, a.upgrade, a.kicker))) { continue; } if (a.bids && copy.phase === "auction") { const floor = a.kicker ? KICKER_SPECS[a.kicker].price : UPGRADE_SPECS[a.upgrade!].price; - if (maxBid(copy, a.auctioneer, a.upgrade) < floor) { + if (maxBid(copy, a.auctioneer, a.upgrade, a.kicker) < floor) { continue; } let valid = true; for (const [seat, amount] of Object.entries(a.bids)) { - const max = maxBid(copy, Number(seat), a.upgrade); + const max = maxBid(copy, Number(seat), a.upgrade, a.kicker); if (amount < 0) { a.bids[seat] = Number(seat) === a.auctioneer @@ -600,7 +604,7 @@ export function createAnalysisScenario( continue; } a.highBid = a.bids[String(a.auctioneer)]!; - } else if (maxBid(copy, a.highBidder, a.upgrade) < a.highBid) { + } else if (maxBid(copy, a.highBidder, a.upgrade, a.kicker) < a.highBid) { continue; } } diff --git a/packages/viewer/package.json b/packages/viewer/package.json index 620a155..1ada889 100644 --- a/packages/viewer/package.json +++ b/packages/viewer/package.json @@ -1,6 +1,6 @@ { "name": "outpost-viewer", - "version": "0.1.32", + "version": "0.1.33", "description": "Browser viewer for the Outpost game on boardgamers.space", "license": "AGPL-3.0", "files": [ diff --git a/packages/viewer/src/dev.ts b/packages/viewer/src/dev.ts index b0e2221..4460576 100644 --- a/packages/viewer/src/dev.ts +++ b/packages/viewer/src/dev.ts @@ -9,6 +9,7 @@ const auto = params.get("auto") === "1" || params.get("auto") === "true"; const delayMs = params.get("delay") ? Number(params.get("delay")) : undefined; const fastBid = params.get("fastBid") === "1" || params.get("fastBid") === "true"; const kicker = params.get("kicker") === "1" || params.get("kicker") === "true"; +const catchUp = params.get("catchUp") === "1" || params.get("catchUp") === "true"; const gameOptions: Record = {}; if (fastBid) { gameOptions.fastBid = true; @@ -16,6 +17,9 @@ if (fastBid) { if (kicker) { gameOptions.kicker = true; } +if (catchUp) { + gameOptions.catchUp = true; +} const emitter = window.outpost.launch("#app"); mountSoundTests((sound) => emitter.emit("preferences", { sound })); diff --git a/packages/viewer/src/lib/AuctionBanner.svelte b/packages/viewer/src/lib/AuctionBanner.svelte index c460de2..a0521f9 100644 --- a/packages/viewer/src/lib/AuctionBanner.svelte +++ b/packages/viewer/src/lib/AuctionBanner.svelte @@ -22,9 +22,7 @@ ); const nameOf = (seat: number) => state.players[seat]?.name ?? formatText("Player {p0}", [seat + 1], locale); const meIndex = $derived(store.playerIndex); - const discount = $derived( - auction && auction.upgrade && meIndex !== undefined ? store.discountOf(meIndex, auction.upgrade) : 0 - ); + const discount = $derived(auction && meIndex !== undefined ? store.discountOf(meIndex, auction) : 0); const due = $derived(store.auctionDue()); const fast = $derived(!!auction?.bids); const minBid = $derived(store.minBid); @@ -128,9 +126,9 @@ > - {#if discount > 0} + {#if discount !== 0}
- {`Discount ◈ ${discount}`} + {discount > 0 ? `Discount ◈ ${discount}` : `+ ◈ ${-discount}`} {formatText( fast ? "Pay at most ◈ {p0}" : "Pay ◈ {p0}", @@ -168,7 +166,15 @@ {#if store.myPayment} {formatText( "You won: select hand cards worth at least ◈ {p0} and confirm below (bid {p1}{p2}).", - [due, auction.highBid, discount > 0 ? ` ${formatText("− {p0} discount", [discount], locale)}` : ""], + [ + due, + auction.highBid, + discount > 0 + ? ` ${formatText("− {p0} discount", [discount], locale)}` + : discount < 0 + ? ` + ${-discount}` + : "", + ], locale )} {:else} diff --git a/packages/viewer/src/lib/LogFeed.svelte b/packages/viewer/src/lib/LogFeed.svelte index 61b706d..8a77972 100644 --- a/packages/viewer/src/lib/LogFeed.svelte +++ b/packages/viewer/src/lib/LogFeed.svelte @@ -1,6 +1,6 @@ @@ -141,7 +141,7 @@ {@const spec = UPGRADE_SPECS[upgrade]} {@const open = pick?.marketIndex === i && pick.kicker !== true} {@const due = myDue(upgrade)} - {@const discount = store.playerIndex === undefined ? 0 : store.discountOf(store.playerIndex, upgrade)} + {@const discount = store.playerIndex === undefined ? 0 : store.discountOf(store.playerIndex, { upgrade })} {@const blocked = store.turnBuys.length > 0}
@@ -268,7 +273,9 @@
{text(store.fastBid ? "Pay at most ◈ {p0}" : "Pay ◈ {p0}", [pick.bid])}{text(store.fastBid ? "Pay at most ◈ {p0}" : "Pay ◈ {p0}", [ + Math.max(0, pick.bid - discount), + ])} · you hold ◈ {store.myHandValue}
diff --git a/packages/viewer/src/lib/store.svelte.ts b/packages/viewer/src/lib/store.svelte.ts index a2b07ff..0cd36c3 100644 --- a/packages/viewer/src/lib/store.svelte.ts +++ b/packages/viewer/src/lib/store.svelte.ts @@ -8,6 +8,7 @@ import { applyMove, applyTurnBuy, auctionCard, + auctionDiscount, bestDiscard, bestPayment, canBuyFactory, @@ -30,7 +31,6 @@ import { replay as replayEngine, robotMax, scores, - upgradeDiscount, type FactoryType, type GameState, type Kicker, @@ -736,7 +736,7 @@ export class ViewerStore { if (!me || !auction) { return 0; } - return handValue(me) + (auction.upgrade ? upgradeDiscount(me, auction.upgrade) : 0); + return handValue(me) + auctionDiscount(this.liveState!, this.playerIndex!, auction); } /** Max opening bid for the staged auction pick (hand value + that card's discount; Kicker cards have none). */ @@ -747,11 +747,10 @@ export class ViewerStore { if (!me || !s || !pick) { return 0; } - if (pick.kicker) { - return handValue(me); - } - const upgrade = s.market[pick.marketIndex] as Upgrade; - return handValue(me) + upgradeDiscount(me, upgrade); + const card = pick.kicker + ? { kicker: s.kickerMarket[pick.marketIndex] as Kicker } + : { upgrade: s.market[pick.marketIndex] as Upgrade }; + return handValue(me) + auctionDiscount(s, this.playerIndex!, card); } get finalRankings(): number[] { @@ -789,9 +788,9 @@ export class ViewerStore { return p ? robotMax(p) : 0; } - discountOf(index: number, upgrade: Upgrade): number { - const p = this.state?.players[index]; - return p ? upgradeDiscount(p, upgrade) : 0; + discountOf(index: number, card: { upgrade?: Upgrade; kicker?: Kicker }): number { + const state = this.liveState; + return state ? auctionDiscount(state, index, card) : 0; } auctionDue(): number { @@ -800,7 +799,7 @@ export class ViewerStore { if (!me || !auction) { return 0; } - return Math.max(0, auction.highBid - (auction.upgrade ? upgradeDiscount(me, auction.upgrade) : 0)); + return Math.max(0, auction.highBid - auctionDiscount(this.liveState!, this.playerIndex!, auction)); } pickTotal(): number { diff --git a/tests/catch-up-browser.mjs b/tests/catch-up-browser.mjs new file mode 100644 index 0000000..bc0ab38 --- /dev/null +++ b/tests/catch-up-browser.mjs @@ -0,0 +1,71 @@ +import assert from "node:assert/strict"; +import { chromium } from "playwright"; +import { initGame } from "../packages/engine/dist/index.js"; +import { stripSecret } from "../packages/engine/dist/wrapper.js"; +import { previewServer } from "./tutorial-preview.mjs"; + +const server = previewServer(); +await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); +const browser = await chromium.launch({ executablePath: process.env.OUTPOST_CHROMIUM_EXECUTABLE }); +try { + for (const width of [390, 1400]) { + const page = await browser.newPage({ viewport: { width, height: 950 } }); + const errors = []; + page.on("pageerror", (error) => errors.push(error.message)); + await page.setContent('
'); + await page.addStyleTag({ path: "packages/viewer/dist/outpost-viewer.css" }); + await page.addScriptTag({ url: `http://127.0.0.1:${server.address().port}/bundle.js` }); + const state = initGame(4, { catchUp: true, kicker: true }, "catch-up-browser"); + state.era = 2; + state.phase = "actions"; + state.activeSeat = 0; + state.market = ["dataLibrary"]; + state.kickerMarket = ["smelter"]; + state.players[0].upgrades.outpost = 3; + state.players[1].upgrades.outpost = 2; + state.players[2].upgrades.outpost = 1; + state.players[0].hand = [{ t: "water", v: 20 }]; + await page.evaluate( + (state) => { + window.emitter = outpost.launch("#app"); + emitter.emit("player", { index: 0 }); + emitter.emit("state", state); + }, + stripSecret(state, 0) + ); + const market = page.locator(".market"); + await market.locator(".ucard").filter({ hasText: "Data Library" }).first().waitFor(); + assert.match( + await market.locator(".ucard").filter({ hasText: "Data Library" }).first().innerText(), + /you pay ◈ 25/ + ); + assert.match(await market.locator(".kcard").filter({ hasText: "Smelter" }).first().innerText(), /you pay ◈ 25/); + await market.locator(".kcard").filter({ hasText: "Smelter" }).first().click(); + assert.match(await market.locator(".payment-preview").innerText(), /Pay ◈ 25/); + assert.equal(await market.locator(".maxhint").innerText(), "max ◈ 10"); + assert.equal(await market.locator(".confirm").isDisabled(), true); + await page.evaluate(() => emitter.emit("player", { index: 3 })); + assert.match(await market.locator(".ucard").filter({ hasText: "Data Library" }).first().innerText(), /you pay ◈ 5/); + assert.match(await market.locator(".kcard").filter({ hasText: "Smelter" }).first().innerText(), /you pay ◈ 5/); + await page.evaluate(() => emitter.emit("player", { index: 0 })); + state.phase = "auctionPayment"; + state.auction = { + marketIndex: 0, + kicker: "smelter", + auctioneer: 0, + highBid: 15, + highBidder: 0, + passed: [1, 2, 3], + activeBidder: 0, + }; + await page.evaluate((state) => emitter.emit("state", state), stripSecret(state, 0)); + assert.match(await page.locator('[data-tutorial="auction"] .turn').innerText(), /at least ◈ 25/); + assert.match(await page.locator(".actionbar").innerText(), /at least ◈ 25/); + assert.deepEqual(errors, []); + await page.close(); + } + console.log("Catch-up pricing in the market and auction payment matches the engine on mobile and desktop."); +} finally { + await browser.close(); + await new Promise((resolve) => server.close(resolve)); +}