powergrid/viewer/src/self-contained.ts
goldjohn6 0edd7fd8a7
HawaiiCustom: a new custom map (island prices, ferry, shipped-in fuel) (#172)
* HawaiiCustom draft: custom map, ferry curve, sea crossings, house-price legend

Local draft for the community custom-map thread (not for release yet).
- engine: HawaiiCustom map (44 cities, 6 regions, house prices by island,
  payout +1, Kauai-Big Island ferry); customMaps list (by name only, never
  Random); GameMap.paymentTable / houseLegend; Connection.via / sea /
  ignoreForRegions
- viewer: curved via-links, dashed blue sea crossings, house-price legend,
  Hawaii land outlines, ?fullmap=1 for layout checks
- tests: hawaii-custom.spec.ts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* HawaiiCustom: Kauai at standard prices, shipped-in fuel (coal 2, oil 4)

Kauai already had the cheapest links on the board, so cheap houses on top made
it too strong (BootlegTampico on Discord); it moves to the standard 10/15/20.
Fuel is shipped in: the market starts with the three cheapest coal and oil left
in the supply, so coal starts at 2 and oil at 4. Garbage and uranium start as
each variant has them (Recharged still fills garbage to 9). Rules text updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Achievements: community custom maps get no Champion badge

Master's achievements give every map a Champion badge typed as
Record<MapName, string>, so adding HawaiiCustom broke the build. A badge needs
its own artwork and a manifest publication, so custom maps are left out for
now (the lookup already handles a map with no badge). A test covers a
HawaiiCustom win: grid-champion and full-table, no map badge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Geography test: cover custom maps (Hawaii has no neighbouring coast)

The catalog now holds Hawaii, so the count is standard maps plus custom maps,
and Hawaii is checked like the others. Its context is empty on purpose: it
sits in open ocean, with no other coast inside its frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:47:14 -08:00

162 lines
7.4 KiB
TypeScript

import { cloneDeep } from 'lodash';
import { Move, Phase, setup, stripSecret } from 'powergrid-engine';
import { isAnalysisMoneyCommand } from 'powergrid-engine/src/analysis-money';
import type { GameState, MapName, PowerPlantDeck, Variant } from 'powergrid-engine/src/gamestate';
import { customMaps, maps } from 'powergrid-engine/src/maps';
import type { PremoveCommand } from 'powergrid-engine/src/premoves';
import { move as wrapperMove, moveAI as wrapperAI } from 'powergrid-engine/wrapper';
import { installLocalChat } from './game-chat';
import launch from './launch';
import { mountSoundTests } from './sounds';
const delayBase = 0;
function launchSelfContained(selector = '#app') {
const strip = true;
const emitter = launch(selector);
mountSoundTests(emitter);
installLocalChat(emitter);
// The sandbox game can be steered from the URL so a layout or rules change can
// be checked against several maps without editing this file each time, e.g.
// ?map=Australia&players=3&variant=original&seed=7
const params = new URLSearchParams(window.location.search);
// Note: randomizeMap short-circuits region selection in setup(), so ticking
// both gives a randomized board with no draft. They are alternatives.
const gameOptions = {
map: (params.get('map') || 'Bremen') as MapName,
variant: (params.get('variant') || 'recharged') as Variant,
showMoney: true,
randomizeMap: params.get('randomize') === '1',
chooseRegions: params.get('regions') === '1',
alternatePowerPlants: params.get('altdeck') === '1',
// deck=deck1|deck2|random; altdeck=1 is the legacy Deck 2 switch (Europe/North America only).
powerPlantDeck: (params.get('deck') || undefined) as PowerPlantDeck | 'random' | undefined,
};
let gameState = setup(Number(params.get('players')) || 6, gameOptions, params.get('seed') || '0');
// ?fullmap=1 shows every region of the map, whatever the player count (layout checks).
if (params.get('fullmap') === '1') {
const full = [...maps, ...customMaps].find((m) => m.name === gameState.map.name);
if (full) {
gameState.map.cities = cloneDeep(full.cities);
gameState.map.connections = cloneDeep(full.connections);
}
}
// Dev coord-picker example (commented; uncomment + drop a board photo into
// viewer/public/ to author city coordinates for a new map). See the picker
// notes in CLAUDE/memory or in the engine map-authoring docs.
//
// import { map as fullMap } from 'powergrid-engine/src/maps/southafrica';
// gameState.map.cities = fullMap.cities.map((c) => ({ ...c }));
// gameState.map.connections = fullMap.connections.map((c) => ({ ...c, nodes: [...c.nodes] }));
// gameState.map.devBackdrop = { src: '/southafrica.jpg', width: 1200, height: 863, opacity: 0.5 };
// gameState.map.adjustRatio = [1, 1];
// gameState.map.mapRotation = 0;
// gameState.map.mapPosition = [0, 0];
// gameState.map.viewBox = [1200, 863];
// gameState.map.playerBoardsPosition = [-9999, -9999];
// gameState.map.powerPlantMarketPosition = [-9999, -9999];
// gameState.map.buttonsPosition = [-9999, -9999];
// gameState.map.supplyPosition = [-9999, -9999];
// gameState.map.roundInfoPosition = [-9999, -9999];
// gameState.map.cityCountPosition = [-9999, -9999];
// gameState.map.playerOrderPosition = [-9999, -9999];
for (let i = 0; i < gameState.players.length; i++) {
gameState.players[i].name = `Player ${i + 1}`;
}
let playerIndex = 1;
for (const player of gameState.players) {
if (player.id != playerIndex) player.isAI = true;
}
// Mimic BGS's "Undo my move" in a game against bots: every committed move of the
// human records the state before it, and undo goes back to the latest of these,
// dropping the bots' replies.
const undoPoints: GameState[] = [];
const offerUndo = () => emitter.emit('undo:available', undoPoints.length > 0);
emitter.on('undo', () => {
const previous = undoPoints.pop();
if (!previous) return;
gameState = previous;
emitter.emit('state', cloneDeep(strip ? stripSecret(gameState, playerIndex) : gameState));
offerUndo();
});
emitter.on('move', async (moves) => {
// This standalone demo is a live game, never an analysis host.
if (Array.isArray(moves) && moves.some(isAnalysisMoneyCommand)) return;
const liveMoves = moves as Move[] | PremoveCommand;
setTimeout(async () => {
console.log('moves received', moves);
// Mimic the platform: replay the whole turn buffer from the last committed
// state; only keep (persist) the result once the turn is committed.
const newState = await wrapperMove(cloneDeep(gameState), liveMoves, playerIndex);
console.log('new game state', newState);
if (newState.newTurn === false) {
// Tentative: just echo the state back to the acting player
emitter.emit('state', cloneDeep(strip ? stripSecret(newState, playerIndex) : newState));
return;
}
undoPoints.push(gameState);
gameState = newState;
emitter.emit('state', cloneDeep(strip ? stripSecret(gameState, playerIndex) : gameState));
offerUndo();
let delay = delayBase;
const moveAIAux = () => {
if (gameState.players.some((pl) => pl.isAI && pl.availableMoves)) {
gameState = wrapperAI(
gameState,
gameState.players.findIndex((pl) => pl.isAI && pl.availableMoves)
);
// Only broadcast committed states: the human viewer discards
// tentative states that don't match its own turn buffer.
if (gameState.newTurn !== false) {
let newAIState = cloneDeep(strip ? stripSecret(gameState, playerIndex) : gameState);
console.log('new game state', newAIState);
emitter.emit('state', newAIState);
}
setTimeout(moveAIAux, gameState.phase == Phase.Bureaucracy ? delay : 0);
}
};
setTimeout(moveAIAux, gameState.phase == Phase.Bureaucracy ? delay : 0);
console.log('available moves', gameState.players[playerIndex].availableMoves);
}, 100);
});
emitter.on('fetchState', () => {
emitter.emit('state', cloneDeep(strip ? stripSecret(gameState, playerIndex) : gameState));
});
emitter.emit('player', { index: playerIndex });
emitter.emit('state', cloneDeep(strip ? stripSecret(gameState, playerIndex) : gameState));
let delay = delayBase;
while (gameState.players.some((pl) => pl.isAI && pl.availableMoves)) {
gameState = wrapperAI(
gameState,
gameState.players.findIndex((pl) => pl.isAI && pl.availableMoves)
);
// Only broadcast committed states (see moveAIAux above).
if (gameState.newTurn !== false) {
let newState = cloneDeep(strip ? stripSecret(gameState, playerIndex) : gameState);
setTimeout(() => emitter.emit('state', newState), delay);
delay += delayBase;
}
}
console.log('available moves', gameState.players[playerIndex].availableMoves);
}
export default launchSelfContained;