Night 1: v1.1 ruleset, DEFAULT_RULESET = RULESET_V1_1 (#36)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit was merged in pull request #36.
This commit is contained in:
2026-11-17 06:01:43 +00:00
co-authored by Claude Opus 5.5
parent a61c1a1342
commit ec75875f0d
7 changed files with 83 additions and 41 deletions
+4 -10
View File
@@ -3,7 +3,8 @@
* Pure and deterministic. The engine itself never reads raw 0–100 stats; hosts call
* deriveCard() before createMatch. Owner: engine-b.
*/
import { FACTION_KEYWORD, V1_KEYWORDS } from './keywords.ts';
import { FACTION_KEYWORD } from './keywords.ts';
import { DEFAULT_RULESET } from './ruleset.ts';
import { STATS, type Faction, type GameCard, type Rarity, type Stat } from './types.ts';
/** §1.4 tie-break order: depth and reach are the hardest to inflate. */
@@ -44,15 +45,8 @@ export interface DerivationRules {
uncheckedCostCap: number;
}
export const DEFAULT_DERIVATION: DerivationRules = {
costThresholds: [15, 35, 55, 75, 90],
powerByCost: [0, 1, 2, 4, 6, 8, 10],
vanillaBonus: 1,
keywordsEnabled: V1_KEYWORDS,
keywordParams: {},
trendUp: 25, trendDown: -200, trendMinDaily: 20, // −200: no down arrow in v1 (§1.6)
uncheckedCostCap: 4,
};
/** Derivation defaults are the engine's DEFAULT_RULESET (a Ruleset is a DerivationRules). */
export const DEFAULT_DERIVATION: DerivationRules = DEFAULT_RULESET;
/** §1.1: cut points strictly below, plus half of those equal, rounded down. */
export function percentile(score: number, cuts: readonly number[]): number {
+1 -1
View File
@@ -1,7 +1,7 @@
/** Public entry point of the WikiDeck game engine. Other teams import only from here. See docs/game/ENGINE_API.md. */
export * from './types.ts';
export { Rng, seedFromString, normalizeSeed, type RngState } from './rng.ts';
export { DEFAULT_RULESET, RULESET_V1_0, DEFAULT_TOPIC_POOL, RulesetSchema, TopicSchema, parseRuleset } from './ruleset.ts';
export { DEFAULT_RULESET, RULESET_V1_0, RULESET_V1_1, DEFAULT_TOPIC_POOL, RulesetSchema, TopicSchema, parseRuleset } from './ruleset.ts';
export {
ENGINE_VERSION, createMatch, legalActions, applyAction, actionProblem, isOver, result, viewFor,
serialize, deserialize, replay, validateDeck, cardPower, lanePower, laneLeader, laneCapacity, scoringPower, topicsLed, other, placeCard, revealPortal,
+19 -2
View File
@@ -127,5 +127,22 @@ export const RULESET_V1_0: Ruleset = {
starterDecks: 'default',
};
/** DESIGN §9 defaults: the current rules version. */
export const DEFAULT_RULESET: Ruleset = RULESET_V1_0;
/**
* DESIGN §9 v1.1 (PR #32): config-only changes on top of v1.0. costThresholds are fame quantiles of the
* 236-card card-sample percentile table (#28), so they ship together with that table (shared/game/card-data.ts).
*/
export const RULESET_V1_1: Ruleset = {
...RULESET_V1_0,
version: '1.1',
costThresholds: [26, 39, 51, 63, 78],
cultureKeyword: 'home',
keywordParams: {
...RULESET_V1_0.keywordParams,
viral: { base: 1, amount: 1, cap: 3, powerDelta: 0 },
rising: { amount: 1, cap: 2, powerDelta: -1 },
encyclopedic: { amount: 2, sides: 1, powerDelta: 0 },
},
};
/** DESIGN §9 defaults: the current rules version (v1.1, BALANCE.md 00:45: ship #32 as is). */
export const DEFAULT_RULESET: Ruleset = RULESET_V1_1;
+15 -15
View File
@@ -1,7 +1,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
DEFAULT_DERIVATION, percentile, percentiles, fame, costFor, homeStat, trendFor, keywordFor, deriveCard, deriveDeck,
RULESET_V1_0, percentile, percentiles, fame, costFor, homeStat, trendFor, keywordFor, deriveCard, deriveDeck,
type CardInput, type PercentileTable, type DerivationRules, type Faction,
} from '../shared/game/index.ts';
import { forAll } from './game/property.ts';
@@ -9,7 +9,7 @@ import { forAll } from './game/property.ts';
/** Cut points 1..99: score s sits at percentile ~s, which makes examples readable. */
const LINEAR = Array.from({ length: 99 }, (_, i) => i + 1);
const TABLE: PercentileTable = { attention: LINEAR, depth: LINEAR, reach: LINEAR };
const RULES = DEFAULT_DERIVATION;
const RULES: DerivationRules = RULESET_V1_0; // pinned: these examples are v1.0 numbers
const LIVE = { trendPercent: null, dailyAverage: null };
function input(stats: CardInput['snapshot']['stats'], overrides: Partial<CardInput> = {}): CardInput {
@@ -76,10 +76,10 @@ test('keyword choice: cost 1 vanilla, faction keyword otherwise, disabled → pl
test('deriveCard: cost, power, vanillaBonus, home, trend and source', () => {
// pct 80/70/60 → fame 70 → cost 4 → power 6, Science → encyclopedic
const card = deriveCard(input({ attention: 81, depth: 71, reach: 61 }, { live: { trendPercent: 40, dailyAverage: 50 } }), TABLE);
const card = deriveCard(input({ attention: 81, depth: 71, reach: 61 }, { live: { trendPercent: 40, dailyAverage: 50 } }), TABLE, RULES);
assert.deepEqual(card, { cardId: 'c1', pageId: 1, title: 'T', faction: 'Science', rarity: 'Common',
cost: 4, power: 6, home: 'attention', trend: 1, keyword: 'encyclopedic', source: 'owned', check: 'verified' });
const cheap = deriveCard(input({ attention: 5, depth: 5, reach: 5 }, { check: 'loaner' }), TABLE);
const cheap = deriveCard(input({ attention: 5, depth: 5, reach: 5 }, { check: 'loaner' }), TABLE, RULES);
assert.deepEqual([cheap.cost, cheap.power, cheap.keyword, cheap.source], [1, 2, null, 'loaner']);
const disabled = deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE, { ...RULES, keywordsEnabled: [] });
assert.deepEqual([disabled.power, disabled.keyword], [6, null]);
@@ -87,14 +87,14 @@ test('deriveCard: cost, power, vanillaBonus, home, trend and source', () => {
test('rarity is cosmetic: it never changes cost, power or keyword (§1.7)', () => {
const stats = { attention: 40, depth: 60, reach: 20 };
const cards = (['Common', 'Rare', 'Epic', 'Legendary'] as const).map(rarity => deriveCard(input(stats, { rarity }), TABLE));
const cards = (['Common', 'Rare', 'Epic', 'Legendary'] as const).map(rarity => deriveCard(input(stats, { rarity }), TABLE, RULES));
for (const card of cards) assert.deepEqual({ ...card, rarity: 'Common' }, { ...cards[0], rarity: 'Common' });
});
test('unchecked cards are capped at uncheckedCostCap and keep their home stat (§1.10)', () => {
const stats = { attention: 100, depth: 100, reach: 99 };
const checked = deriveCard(input(stats), TABLE);
const unchecked = deriveCard(input(stats, { check: 'unchecked' }), TABLE);
const checked = deriveCard(input(stats), TABLE, RULES);
const unchecked = deriveCard(input(stats, { check: 'unchecked' }), TABLE, RULES);
assert.equal(checked.cost, 6);
assert.equal(unchecked.cost, 4);
assert.equal(unchecked.power, RULES.powerByCost[4]);
@@ -114,18 +114,18 @@ test('deriveDeck marks copies for Edition when it is enabled', () => {
const c = { ...a, cardId: 'c', pageId: 8 };
const rules = { ...RULES, keywordsEnabled: [...RULES.keywordsEnabled, 'edition'] };
assert.deepEqual(deriveDeck([a, b, c], TABLE, rules).map(card => card.keyword), ['edition', 'edition', 'viral']);
assert.deepEqual(deriveDeck([a, b, c], TABLE).map(card => card.keyword), ['viral', 'viral', 'viral']);
assert.deepEqual(deriveDeck([a, b, c], TABLE, RULES).map(card => card.keyword), ['viral', 'viral', 'viral']);
});
test('power depends only on cost: forged stats only move a card up the curve (§1.3)', () => {
forAll(300, g => {
const stats = { attention: g.bool(0.1) ? null : g.int(0, 100), depth: g.int(0, 100), reach: g.int(0, 100) };
const card = deriveCard(input(stats, { category: g.pick(['Space', 'Nature', 'People', 'Science', 'History', 'Culture'] as const) }), TABLE);
const card = deriveCard(input(stats, { category: g.pick(['Space', 'Nature', 'People', 'Science', 'History', 'Culture'] as const) }), TABLE, RULES);
assert.ok(card.cost >= 1 && card.cost <= 6);
assert.equal(card.power, RULES.powerByCost[card.cost] + (card.cost === 1 ? RULES.vanillaBonus : 0));
assert.ok(stats[card.home] !== null, 'home is never a null stat');
const bumped = { ...stats, depth: Math.min(100, stats.depth + g.int(1, 30)) };
assert.ok(deriveCard(input(bumped), TABLE).cost >= card.cost, 'raising a stat never lowers cost');
assert.ok(deriveCard(input(bumped), TABLE, RULES).cost >= card.cost, 'raising a stat never lowers cost');
});
});
@@ -136,19 +136,19 @@ test('deriveCard rejects a rules config with no power for the derived cost', ()
test('pinned values replace §1.1–1.5 for loaners only (§2.1)', () => {
const pinned = { cost: 5, home: 'reach' as const, faction: 'History' as const };
const stats = { attention: 5, depth: 5, reach: 5 };
const loaner = deriveCard(input(stats, { check: 'loaner', pinned, rarity: 'Epic', live: { trendPercent: 99, dailyAverage: 999 } }), TABLE);
const loaner = deriveCard(input(stats, { check: 'loaner', pinned, rarity: 'Epic', live: { trendPercent: 99, dailyAverage: 999 } }), TABLE, RULES);
assert.deepEqual([loaner.cost, loaner.power, loaner.home, loaner.faction, loaner.keyword, loaner.rarity, loaner.trend, loaner.source],
[5, 8, 'reach', 'History', 'nostalgic', 'Common', 0, 'loaner']);
const owned = deriveCard(input(stats, { pinned }), TABLE);
const owned = deriveCard(input(stats, { pinned }), TABLE, RULES);
assert.deepEqual([owned.cost, owned.faction], [1, 'Science'], 'pinned is ignored when check !== loaner');
const cheap = deriveCard(input(stats, { check: 'loaner', pinned: { ...pinned, cost: 1 } }), TABLE);
const cheap = deriveCard(input(stats, { check: 'loaner', pinned: { ...pinned, cost: 1 } }), TABLE, RULES);
assert.deepEqual([cheap.power, cheap.keyword], [2, null], 'vanillaBonus still applies to pinned cost-1');
});
test('cultureKeyword (v1.1 hook): home gives Culture cards {attention: viral, depth: encyclopedic, reach: translated}', () => {
const home = { ...RULES, cultureKeyword: 'home' as const };
const culture = (stats: CardInput['snapshot']['stats']) => input(stats, { category: 'Culture' });
assert.equal(deriveCard(culture({ attention: 80, depth: 40, reach: 40 }), TABLE).keyword, 'viral', 'v1 default: faction');
assert.equal(deriveCard(culture({ attention: 80, depth: 40, reach: 40 }), TABLE, RULES).keyword, 'viral', 'v1 default: faction');
assert.equal(deriveCard(culture({ attention: 80, depth: 40, reach: 40 }), TABLE, home).keyword, 'viral');
assert.equal(deriveCard(culture({ attention: 40, depth: 80, reach: 40 }), TABLE, home).keyword, 'encyclopedic');
assert.equal(deriveCard(culture({ attention: 40, depth: 40, reach: 80 }), TABLE, home).keyword, 'translated');
@@ -160,7 +160,7 @@ test('a full Ruleset works as DerivationRules (hosts can derive with the mode ru
const { DEFAULT_RULESET } = await import('../shared/game/index.ts');
const a = deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE, DEFAULT_RULESET);
const b = deriveCard(input({ attention: 81, depth: 71, reach: 61 }), TABLE);
assert.deepEqual(a, b);
assert.deepEqual(a, b, 'default derivation rules are DEFAULT_RULESET');
});
test('parseRuleset accepts a stored pre-v1.1 ruleset without cultureKeyword (defaults to faction)', async () => {
+32 -5
View File
@@ -8,7 +8,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
RULESET_V1_0, applyAction, createMatch, isOver, lanePower, result, starterDeck, topicsLed,
RULESET_V1_0, RULESET_V1_1, applyAction, createMatch, isOver, lanePower, result, starterDeck, topicsLed,
type Action, type GameCard, type MatchState, type PlayerId, type Ruleset, type Stat, type Faction,
} from '../shared/game/index.ts';
@@ -54,16 +54,19 @@ const PLAN: [PlayerId, string, number][][] = [
[[1, 'Fern', T1], [0, 'Library of Alexandria', T1], [1, 'Mona Lisa', T2], [0, 'Periodic table', T2]],
];
function setup(): MatchState {
const rules: Ruleset = structuredClone(RULESET_V1_0); // §12 is pinned to the v1.0 ruleset
function setup(base: Ruleset = RULESET_V1_0): MatchState {
const rules: Ruleset = structuredClone(base); // §12 is pinned to the v1.0 ruleset; §12.1 passes v1.1
const state = createMatch(rules, [starterDeck('golden-a'), starterDeck('golden-b')], 1);
const pool = state.ruleset.topicPool;
const find = (id: string) => pool.find(t => t.id === id) ?? assert.fail(`topic ${id} missing from the default pool`);
const topics = [find('archive'), find('main-page'), find('translations')];
state.lanes = topics.map(topic => ({ topic, portalRevealed: false, cards: [[], []] }));
// Inject: every card the example plays starts in hand, nothing left to draw.
state.players[0].hand = structuredClone(ANA);
state.players[1].hand = structuredClone(BO);
// The §12 table gives v1.0-derived cards; under another ruleset apply its keyword powerDelta (§10.1).
const adjust = (cards: GameCard[]) => cards.map(c => ({ ...c,
power: c.power + (c.keyword ? (rules.keywordParams[c.keyword]?.powerDelta ?? 0) : 0) }));
state.players[0].hand = adjust(structuredClone(ANA));
state.players[1].hand = adjust(structuredClone(BO));
for (const p of state.players) p.deck = [];
state.initiative = 0; // Ana has initiative on odd turns
return state;
@@ -122,3 +125,27 @@ test('§12 golden: per-turn Topic powers match the table', opts, () => {
assert.deepEqual(state.lanes.map((_, lane) => lanePower(state, lane)), expected[turn], `after turn ${turn + 1}`);
});
});
test('§12.1 golden (v1.1 ruleset): 18–14, 15–21, 13–11; Ana wins 2–1; T1 8–8 consensus to Ana after turn 5', () => {
let state = setup(RULESET_V1_1);
const after: [number, number][][] = [];
for (const plays of PLAN) {
state = playTurn(state, plays);
after.push(state.lanes.map((_, lane) => lanePower(state, lane)));
}
assert.deepEqual(after.slice(0, 5), [
[[3, 0], [0, 3], [0, 0]],
[[4, 0], [0, 3], [3, 0]],
[[7, 0], [0, 9], [3, 3]],
[[8, 0], [9, 9], [3, 10]],
[[8, 8], [9, 10], [13, 11]],
]);
const r = result(state)!;
assert.deepEqual(r.lanePower, [[18, 14], [15, 21], [13, 11]]);
assert.equal(r.winner, 0);
// Replay turns 1–5 to check the T1 consensus lead.
let five = setup(RULESET_V1_1);
for (const plays of PLAN.slice(0, 5)) five = playTurn(five, plays);
assert.deepEqual(lanePower(five, T1), [8, 8]);
assert.deepEqual(topicsLed(five), [2, 1]); // T1 by consensus + T3; Bo leads T2
});
+10 -6
View File
@@ -2,7 +2,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
DEFAULT_DERIVATION, DEFAULT_RULESET, FACTION_KEYWORD, STARTER_DECKS, createMatch, starterDeckById, validateDeck,
DEFAULT_DERIVATION, DEFAULT_RULESET, RULESET_V1_0, RULESET_V1_1, keywordFor, STARTER_DECKS, createMatch, starterDeckById, validateDeck,
} from '../shared/game/index.ts';
const countBy = (keys: string[]) => keys.reduce<Record<string, number>>((m, k) => ({ ...m, [k]: (m[k] ?? 0) + 1 }), {});
@@ -19,14 +19,17 @@ test('four starter decks, each with the frozen curve, 6+6 factions and 4 cards p
}
});
test('starter cards follow §10.1 and the power curve like owned cards', () => {
for (const def of STARTER_DECKS) for (const card of starterDeckById(def.id)) {
for (const [label, rules] of [['v1.0', RULESET_V1_0], ['v1.1', RULESET_V1_1]] as const)
test(`starter cards follow §10.1 and the power curve like owned cards (${label})`, () => {
for (const def of STARTER_DECKS) for (const card of starterDeckById(def.id, rules)) {
assert.equal(card.source, 'loaner');
assert.equal(card.rarity, 'Common');
assert.equal(card.trend, 0);
assert.match(card.cardId, /^loaner:[a-z0-9-]+$/);
if (card.cost === 1) assert.deepEqual([card.keyword, card.power], [null, 2]);
else assert.deepEqual([card.keyword, card.power], [FACTION_KEYWORD[card.faction], DEFAULT_DERIVATION.powerByCost[card.cost]]);
if (card.cost === 1) { assert.deepEqual([card.keyword, card.power], [null, 2]); continue; }
const { keyword } = keywordFor(card.faction, card.cost, false, rules, card.home);
assert.equal(card.keyword, keyword);
assert.equal(card.power, rules.powerByCost[card.cost] + (rules.keywordParams[keyword!]?.powerDelta ?? 0));
}
});
@@ -67,7 +70,8 @@ test('previewPool derives the frozen starter-cards.json loaners through §1', as
assert.equal(c.source, 'loaner');
assert.match(c.cardId, /^loaner:\d+$/);
assert.ok(c.cost >= 1 && c.cost <= 6);
assert.equal(c.power, DEFAULT_DERIVATION.powerByCost[c.cost] + (c.keyword === null && c.cost === 1 ? DEFAULT_DERIVATION.vanillaBonus : 0));
const delta = c.keyword ? (DEFAULT_DERIVATION.keywordParams[c.keyword]?.powerDelta ?? 0) : 0;
assert.equal(c.power, DEFAULT_DERIVATION.powerByCost[c.cost] + (c.keyword === null && c.cost === 1 ? DEFAULT_DERIVATION.vanillaBonus : 0) + delta);
}
// 12 of them make a legal loaner deck.
assert.deepEqual(validateDeck(DEFAULT_RULESET, pool.slice(0, 12)), []);
+2 -2
View File
@@ -1,6 +1,6 @@
/** Hand-built boards for keyword and Portal unit tests (engine-b). */
import {
DEFAULT_RULESET, KEYWORDS, PORTALS, Rng, cardPower, createMatch, lanePower, starterDeck,
RULESET_V1_0, KEYWORDS, PORTALS, Rng, cardPower, createMatch, lanePower, starterDeck,
type CardInPlay, type EffectContext, type GameCard, type MatchState, type PlayerId, type Stat, type TopicDef,
} from '../../shared/game/index.ts';
@@ -9,7 +9,7 @@ export const topic = (stat: Stat, portal: string | null = null, id = portal ?? `
/** Empty board, turn 1; lanes are depth / attention / reach plain Topics unless given. */
export function board(topics: TopicDef[] = [topic('depth'), topic('attention'), topic('reach')], revealed = true): MatchState {
const state = createMatch(DEFAULT_RULESET, [starterDeck('a'), starterDeck('b')], 'keyword-test');
const state = createMatch(RULESET_V1_0, [starterDeck('a'), starterDeck('b')], 'keyword-test'); // unit tests use v1.0 numbers
state.lanes = topics.map(t => ({ topic: t, portalRevealed: revealed, cards: [[], []] as [CardInPlay[], CardInPlay[]] }));
return state;
}