forked from WikiDeck/wikideck
Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).
## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
- The server checks ownership, and 40-card eligibility is enforced.
- Modes: Classic (ranked), Draft and Trending.
- Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).
PR numbers refer to `claude/wikideck`.
## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
- The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
- Bot matches aren't reachable from Play.
- Card identity pre-bake hasn't been done.
- The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.
This PR is for Eric to merge.
🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: WikiDeck/wikideck#23
Co-authored-by: Claude <claude@eom.dev>
87 lines
4.6 KiB
TypeScript
87 lines
4.6 KiB
TypeScript
/**
|
|
* Binds the mode host to the Night 2 duel engine (shared/duel; DUEL_API.md "Host binding").
|
|
*
|
|
* Cards: an owned NFT (`ChainCard`, built by ownership.ts from its block) becomes a `DuelCard`
|
|
* through ONE function, `toDuelCard`, shared by the host and the collection API, so PvP, the
|
|
* deck builder and the browser bot all see the same card. The card profile (P31 classes,
|
|
* categories, description, links) comes from testnet-ops' frozen IdentityStore (#68). On a
|
|
* miss it is derived from what the chain itself carries (weaker typing), and nothing is
|
|
* fetched inline: no Wikimedia call ever happens at deck validation or match time.
|
|
*/
|
|
import { derive, type DuelCard } from '../../shared/duel/derive.ts';
|
|
import { profileFromSources } from '../../shared/duel/profile.ts';
|
|
import { classify } from '../../shared/duel/classify.ts';
|
|
import { toProfile, type IdentityStore } from './identity.ts';
|
|
import type { ChainCard, EnginePort } from './engine-port.ts';
|
|
|
|
export type DuelCardSources = {
|
|
/** Frozen per-token identities (server/game/identity.ts). */
|
|
identity?: Pick<IdentityStore, 'get'>;
|
|
};
|
|
|
|
type AnyPort = EnginePort<any, any, any, any>;
|
|
|
|
/**
|
|
* Generic type hints from the mint-time `article.category` (server/wiki.ts `category()`), used
|
|
* only when neither a frozen identity nor the extract's first sentence types the card. They are
|
|
* class words, not facts about the subject, and never reach the Connected keys.
|
|
*/
|
|
const CATEGORY_HINTS: Readonly<Record<string, string>> = {
|
|
People: 'person', Nature: 'organism', Space: 'astronomical object', History: 'event', Science: 'concept', Culture: 'organization',
|
|
};
|
|
|
|
/** First sentence of the on-chain extract, as a description-like signal for the classifier. */
|
|
function firstSentence(extract: string): string {
|
|
const text = extract.replace(/\s+/g, ' ').trim();
|
|
return (/^.*?[.!?](?=\s|$)/.exec(text)?.[0] ?? text).slice(0, 300);
|
|
}
|
|
|
|
/**
|
|
* The single ChainCard → DuelCard conversion. Deterministic for a given block and identity.
|
|
* Miss path (no frozen identity yet): classify from what the block itself carries, the extract's
|
|
* first sentence, then the mint-time category hint. No fetch; card faces stay real data only.
|
|
*/
|
|
export function toDuelCard(card: ChainCard, sources: DuelCardSources = {}): DuelCard {
|
|
const article = card.article as { pageId: number; title: string; extract: string; revisionId: number; category?: string } | undefined;
|
|
const statsSnapshot = card.statsSnapshot as { pageBytes: number; languageCount: number; stats: any } | undefined;
|
|
if (!article || !statsSnapshot) throw new Error(`Card ${card.tokenId} has no on-chain article or stats snapshot`);
|
|
const chain = { tokenId: card.tokenId, article, statsSnapshot };
|
|
const frozen = sources.identity?.get(card.tokenId);
|
|
if (frozen) return derive(chain, toProfile(frozen) as any);
|
|
const base = profileFromSources({ enrichment: {
|
|
description: null, wikidataId: null, categories: [], revisionId: article.revisionId ?? null } as any });
|
|
const sentence = firstSentence(article.extract ?? '');
|
|
let profile = { ...base, description: sentence || null };
|
|
if (classify(profile).source === 'fallback') {
|
|
const hint = CATEGORY_HINTS[article.category ?? ''];
|
|
if (hint) profile = { ...profile, instanceOf: [{ id: '', label: hint }] };
|
|
}
|
|
const out = derive(chain, profile as any);
|
|
// The sentence is the flavour text already; it is not a Wikidata short description.
|
|
delete (out as { description?: string }).description;
|
|
out.connect = { ...out.connect, p31: out.connect.p31.filter(id => id !== '') };
|
|
(out as { typedBy?: string }).typedBy = 'fallback';
|
|
return out;
|
|
}
|
|
|
|
/** Wrap duel-core's `duelEnginePort` with what the host adds: owned-card building and the copy limit. */
|
|
export function duelBinding(port: AnyPort, sources: DuelCardSources = {}): AnyPort {
|
|
return {
|
|
...port,
|
|
fromChain: (_rules, card) => toDuelCard(card, sources),
|
|
copyLimit: rules => (rules as { maxCopiesPerPage?: number }).maxCopiesPerPage ?? 4,
|
|
// BRIEF "Clock": 60 s per turn plus a 5-minute bank, both ruleset data.
|
|
bankSeconds: rules => (rules as { bankSeconds?: number }).bankSeconds ?? 0,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* The duel engine, if this build has it (`duelEnginePort` from shared/duel/index.ts, the engine
|
|
* alpha). Null before that lands: duel modes are then skipped at startup with a log line.
|
|
*/
|
|
export async function loadDuelEngine(sources: DuelCardSources = {}): Promise<AnyPort | null> {
|
|
const mod = await import('../../shared/duel/index.ts') as Record<string, unknown>;
|
|
const port = mod.duelEnginePort as AnyPort | undefined;
|
|
return port && typeof port.createMatch === 'function' ? duelBinding(port, sources) : null;
|
|
}
|