@chrischall/warapp-mcp 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +211 -9
- package/dist/analysis/accept-surrender.js +56 -0
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/calibrate.js +239 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/finished-game.js +136 -0
- package/dist/analysis/game-lessons.js +217 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +179 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +552 -0
- package/dist/analysis/opponents.js +1253 -0
- package/dist/analysis/outlook.js +695 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1405 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +1001 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2927 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +584 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1112 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +121082 -36148
- package/dist/calibration-source.js +75 -0
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +265 -0
- package/dist/client.js +439 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/finished-store.js +357 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/lessons-store.js +356 -0
- package/dist/live.js +287 -0
- package/dist/map-details.js +127 -0
- package/dist/map-stats.js +160 -0
- package/dist/map-store.js +242 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +218 -0
- package/dist/normalize.js +443 -0
- package/dist/notifications.js +143 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +165 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +231 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +727 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +615 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +227 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +291 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2223 -0
- package/dist/surrender-pending.js +126 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/accept-surrender.js +311 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +158 -0
- package/dist/tools/chat-read-mark.js +179 -0
- package/dist/tools/chat.js +525 -0
- package/dist/tools/check-plan.js +689 -0
- package/dist/tools/commander-check.js +103 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/end-of-game.js +384 -0
- package/dist/tools/game.js +151 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +152 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-details.js +88 -0
- package/dist/tools/map-stats.js +130 -0
- package/dist/tools/map.js +141 -0
- package/dist/tools/my-games.js +193 -0
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +163 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/past-games.js +94 -0
- package/dist/tools/picks.js +305 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +445 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +257 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender-pending.js +74 -0
- package/dist/tools/surrender.js +247 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +178 -0
- package/dist/tools/turn.js +2825 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +300 -0
- package/dist/tools/write-kit.js +164 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +577 -0
- package/dist/version.js +4 -0
- package/manifest.json +194 -4
- package/mint.yaml +106 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +1096 -21
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Reading the surrenders pending in a game (#317), for warapp_my_games, warapp_wait_for_turn and
|
|
2
|
+
// warapp_plan_turn. A pending surrender lives only in the game details (src/surrender-pending.ts):
|
|
3
|
+
// one GameRefresh (39) asked for the details and not the chat. UNVERIFIED live.
|
|
4
|
+
//
|
|
5
|
+
// Never throws: a failed or unreadable read is `null` (unknown), never an empty list — an empty list
|
|
6
|
+
// is a game whose details were read and showed no surrender pending.
|
|
7
|
+
import { inGameOf } from '../live.js';
|
|
8
|
+
import { Method, writeGameRefreshParams } from '../protocol/methods.js';
|
|
9
|
+
import { pendingSurrenders, readGameDetailsSeats } from '../surrender-pending.js';
|
|
10
|
+
import { untrustedText } from './common.js';
|
|
11
|
+
/** Where the read comes from, for a result's note. */
|
|
12
|
+
export const SURRENDER_PENDING_NOTE = "Read from the game details (the web client's GameRefresh), UNVERIFIED live: a list is what the details showed " +
|
|
13
|
+
'(empty: no surrender pending); null means they could not be read, not that none is pending. owedByMe true: the ' +
|
|
14
|
+
'player is waiting for you to accept its surrender. Accepting is the owner\'s decision (it hands that player\'s land ' +
|
|
15
|
+
'on and can end the game).';
|
|
16
|
+
/** Game states in which a surrender can be pending. */
|
|
17
|
+
const IN_PLAY = new Set(['Playing', 'DistributingTerritories']);
|
|
18
|
+
/**
|
|
19
|
+
* Whether a pending surrender can matter to me in a game: it is being played or picked and my seat is
|
|
20
|
+
* still playing. Elsewhere no read is made (and the answer is `null`).
|
|
21
|
+
*/
|
|
22
|
+
export function surrenderMayBePending(state, mySeatState) {
|
|
23
|
+
return IN_PLAY.has(state) && mySeatState === 'Playing';
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Read the game details' seats: one GameRefresh for the details only. `undefined` when the reply is
|
|
27
|
+
* unreadable; a failed request (a timeout, a call not sent, a refusal) throws, so a caller that reads
|
|
28
|
+
* many times can stop asking (warapp_wait_for_turn: a frozen tab stalls every call until it times out).
|
|
29
|
+
*/
|
|
30
|
+
export async function fetchDetailsSeats(ctx, gameId) {
|
|
31
|
+
const env = await ctx.rpc.call('GameRefresh', Method.GameRefresh, (w) => writeGameRefreshParams(w, { gameId, chat: false, nonChat: true, lobby: false }));
|
|
32
|
+
return readGameDetailsSeats(await ctx.bundle(), env.body);
|
|
33
|
+
}
|
|
34
|
+
/** {@link fetchDetailsSeats} that never throws: any failure is `undefined` (unknown). */
|
|
35
|
+
export async function readDetailsSeats(ctx, gameId) {
|
|
36
|
+
try {
|
|
37
|
+
return await fetchDetailsSeats(ctx, gameId);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The surrenders pending in a GameFeed game (`me`: my GameFeed seat), each with the player's GameFeed id
|
|
45
|
+
* and name; `null` when the details could not be read, or when no surrender can matter to me there (the
|
|
46
|
+
* game not being played or picked, or my seat out).
|
|
47
|
+
*/
|
|
48
|
+
export async function readPendingSurrenders(ctx, game, me, read = (id) => readDetailsSeats(ctx, id)) {
|
|
49
|
+
if (!me || !surrenderMayBePending(game.state, me.state))
|
|
50
|
+
return null;
|
|
51
|
+
const seats = await read(game.id);
|
|
52
|
+
if (!seats)
|
|
53
|
+
return null;
|
|
54
|
+
const byInGame = new Map();
|
|
55
|
+
for (const p of game.players) {
|
|
56
|
+
const id = inGameOf(p);
|
|
57
|
+
if (id !== undefined)
|
|
58
|
+
byInGame.set(id, p);
|
|
59
|
+
}
|
|
60
|
+
return pendingSurrenders(seats, inGameOf(me), (id) => byInGame.get(id)?.id ?? null).map((p) => {
|
|
61
|
+
const player = byInGame.get(p.inGameId);
|
|
62
|
+
return { ...p, name: player ? untrustedText(player.name) : null };
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** The line a result leads with when a surrender waits on my acceptance; undefined when none does. */
|
|
66
|
+
export function surrenderOwedLine(pending) {
|
|
67
|
+
const owed = (pending ?? []).filter((p) => p.owedByMe === true);
|
|
68
|
+
if (!owed.length)
|
|
69
|
+
return undefined;
|
|
70
|
+
const who = owed.map((p) => `${p.name ?? 'a player'} (${p.player ?? `in-game ${p.inGameId}`})`).join(', ');
|
|
71
|
+
return (`A surrender is waiting for you to accept it: ${who} asked to surrender, and the game details show your ` +
|
|
72
|
+
'acceptance still owed (read from the client bundle, UNVERIFIED live). Accepting is the owner\'s decision: ' +
|
|
73
|
+
'ask before accepting, unless accepting simply wins the game.');
|
|
74
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// warapp_surrender: surrender a game (or take back a pending surrender) with
|
|
2
|
+
// the web client's Surrender (method 67), gated on the user's confirmation.
|
|
3
|
+
// The game is read from GameFeed first, so finished games and games the
|
|
4
|
+
// signed-in account is not playing are refused before anything is sent, and
|
|
5
|
+
// the outcome is read back from GameFeed afterwards — never from the reply,
|
|
6
|
+
// which carries no result.
|
|
7
|
+
import { CONFIRM_INJECTION_RULE, toolAnnotations } from '@chrischall/mcp-utils';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { parseGameId } from '../client.js';
|
|
10
|
+
import { leavers } from '../normalize.js';
|
|
11
|
+
import { readOutlookLog } from '../outlook-log.js';
|
|
12
|
+
import { Method, writeSurrenderParams } from '../protocol/methods.js';
|
|
13
|
+
import { gameIdArg, shownAccount, UNTRUSTED_PREVIEW_NOTE, untrustedText } from './common.js';
|
|
14
|
+
import { noBrowserNote } from './session.js';
|
|
15
|
+
import { mintMeta } from './hints.js';
|
|
16
|
+
import { requireApprovedAccount, writeAccount } from './live-context.js';
|
|
17
|
+
import { defineGatedWrite, mySeat, refuse, requireMe, requirePlaying, sendAndVerify, worded } from './write-kit.js';
|
|
18
|
+
const PLAYING = 'Playing';
|
|
19
|
+
const SURRENDERED = 'SurrenderAccepted';
|
|
20
|
+
const FINISHED = 'Finished';
|
|
21
|
+
const DISTRIBUTING = 'DistributingTerritories';
|
|
22
|
+
/** The Surrender refusals war.app answers with (verified live, issue #30). */
|
|
23
|
+
const SURRENDER_ERRORS = {
|
|
24
|
+
4: 'The game is over, so there is nothing to surrender.',
|
|
25
|
+
7: 'You are no longer in the game.',
|
|
26
|
+
230: 'You are no longer in the game.',
|
|
27
|
+
};
|
|
28
|
+
/** #178: opponents close to a boot are listed when the nearest boot is this close (a day). */
|
|
29
|
+
const LEAVER_WINDOW_MINUTES = 24 * 60;
|
|
30
|
+
/**
|
|
31
|
+
* #178: whether the others must accept a surrender — from `InstantSurrender` when it reads (#228:
|
|
32
|
+
* the game is read with its settings, which carry it). Unread, two players: no (a surrender ends a
|
|
33
|
+
* 1v1 at once); more than two: `null` — it MAY be needed (#228: two of two such surrenders went
|
|
34
|
+
* through at once, so the player count no longer states it is).
|
|
35
|
+
*/
|
|
36
|
+
function acceptanceOf(game, players) {
|
|
37
|
+
const instant = game.endRules?.instantSurrender;
|
|
38
|
+
if (instant === true)
|
|
39
|
+
return { needed: false, from: 'setting', note: 'Surrender is instant in this game: it takes effect at once, without the others accepting it.' };
|
|
40
|
+
if (instant === false)
|
|
41
|
+
return {
|
|
42
|
+
needed: true,
|
|
43
|
+
from: 'setting',
|
|
44
|
+
note: 'Surrender is not instant in this game: the others must accept it before it ends your game, and until then ' +
|
|
45
|
+
'it can be taken back (undo).' +
|
|
46
|
+
(players <= 2 ? ' UNVERIFIED: whether a two-player game really waits for the opponent to accept.' : ''),
|
|
47
|
+
};
|
|
48
|
+
const raw = game.endRules?.instantSurrenderRaw;
|
|
49
|
+
return {
|
|
50
|
+
needed: players > 2 ? null : false,
|
|
51
|
+
from: 'player count',
|
|
52
|
+
note: `The InstantSurrender setting could not be read${raw !== undefined ? ` (${JSON.stringify(raw)})` : ' (not sent)'}, so this goes by the player count: ` +
|
|
53
|
+
(players > 2
|
|
54
|
+
? 'with more than two players the others MAY have to accept the surrender (unknown: such surrenders have also gone through at once), so it may end your game now.'
|
|
55
|
+
: 'with two players, a surrender ends the game at once.'),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const gameLabel = (game) => game.name ?? `game ${game.id}`;
|
|
59
|
+
/** Players still in the game (a surrender in a game of more than two needs the others' acceptance). */
|
|
60
|
+
const activePlayers = (game) => game.players.filter((p) => p.state === PLAYING);
|
|
61
|
+
/** The signed-in account's seat in `game`, refused when it cannot (or should not) be surrendered. */
|
|
62
|
+
function surrenderable(game, me, undo) {
|
|
63
|
+
if (game.state === FINISHED) {
|
|
64
|
+
refuse(`Game ${game.id} is already over, so there is nothing to surrender.`, 'Check warapp_get_game.');
|
|
65
|
+
}
|
|
66
|
+
if (game.state !== PLAYING && game.state !== DISTRIBUTING) {
|
|
67
|
+
refuse(`Game ${game.id} has not started (game state: ${game.state}), so there is nothing to surrender.`, 'A lobby is left with warapp_leave_game, not surrendered.');
|
|
68
|
+
}
|
|
69
|
+
const seat = mySeat(game, me);
|
|
70
|
+
if (undo && seat.state === SURRENDERED) {
|
|
71
|
+
refuse(`Your surrender in game ${game.id} was already accepted, so it cannot be taken back.`, 'Check warapp_get_game.');
|
|
72
|
+
}
|
|
73
|
+
requirePlaying(game, seat);
|
|
74
|
+
// GameFeed exposes no pending-surrender flag, so this can only refuse where a surrender is never
|
|
75
|
+
// pending: an instant surrender (#178: from the setting), else the two-player case.
|
|
76
|
+
if (undo && acceptanceOf(game, activePlayers(game).length).needed === false) {
|
|
77
|
+
refuse(game.endRules?.instantSurrender === true
|
|
78
|
+
? `There is no pending surrender to take back in game ${game.id}: surrender is instant in this game, so it is never pending.`
|
|
79
|
+
: `There is no pending surrender to take back in game ${game.id}: with two players a surrender ends the game at once.`, 'Check warapp_get_game.');
|
|
80
|
+
}
|
|
81
|
+
return seat;
|
|
82
|
+
}
|
|
83
|
+
export function registerSurrenderTool(server, ctx) {
|
|
84
|
+
defineGatedWrite(server, {
|
|
85
|
+
title: 'Surrender a war.app game',
|
|
86
|
+
_meta: mintMeta('change', ['surrender game', 'resign warzone game', 'give up game']),
|
|
87
|
+
}, {
|
|
88
|
+
name: 'warapp_surrender',
|
|
89
|
+
description: 'Surrender a war.app (Warzone) game as the signed-in account: you lose it and cannot rejoin. When the ' +
|
|
90
|
+
"game's InstantSurrender setting asks for acceptance (unread with more than two players: it may) the surrender " +
|
|
91
|
+
'waits for the others to accept it (reported `pending`); `undo: true` takes back such a pending ' +
|
|
92
|
+
'surrender. The preview shows what bears on the decision: the outlook warapp_plan_turn last logged, ' +
|
|
93
|
+
'its basis and its `evidence` (the incomes compared, how much of each opponent\'s frontier was read, `thin` when little), AI opponents, whether acceptance is needed (`acceptance.needed`: true, false, or null when the setting is unread and it may be), what your land becomes (AI or neutral) and ' +
|
|
94
|
+
'opponents close to a boot. The game is read from GameFeed first (finished games and games you are not ' +
|
|
95
|
+
'playing are refused) and the outcome is read back from GameFeed.' +
|
|
96
|
+
`${noBrowserNote(ctx.hosted)} ${CONFIRM_INJECTION_RULE}`,
|
|
97
|
+
annotations: toolAnnotations({
|
|
98
|
+
title: 'Surrender a war.app game',
|
|
99
|
+
readOnly: false,
|
|
100
|
+
destructive: true,
|
|
101
|
+
idempotent: false,
|
|
102
|
+
openWorld: true,
|
|
103
|
+
}),
|
|
104
|
+
inputSchema: z.object({
|
|
105
|
+
game_id: gameIdArg,
|
|
106
|
+
undo: z
|
|
107
|
+
.boolean()
|
|
108
|
+
.optional()
|
|
109
|
+
.describe('Take back your pending surrender instead of surrendering (default false).'),
|
|
110
|
+
}),
|
|
111
|
+
async plan({ game_id, undo }) {
|
|
112
|
+
const gameId = parseGameId(game_id);
|
|
113
|
+
const takeBack = undo ?? false;
|
|
114
|
+
const me = await requireMe(ctx);
|
|
115
|
+
// #228: with its settings — InstantSurrender is one; without them the preview always fell
|
|
116
|
+
// back to the player count.
|
|
117
|
+
const game = await ctx.client.getGame(gameId, { map: false, settings: true });
|
|
118
|
+
const seat = surrenderable(game, me, takeBack);
|
|
119
|
+
const name = gameLabel(game);
|
|
120
|
+
const players = activePlayers(game).length;
|
|
121
|
+
const acceptance = acceptanceOf(game, players);
|
|
122
|
+
// #178: what bears on the decision — the outlook warapp_plan_turn last logged, the AI
|
|
123
|
+
// opponents, what my land becomes, and opponents close to a boot.
|
|
124
|
+
const log = ctx.dataDir ? await readOutlookLog(ctx.dataDir, gameId, me) : undefined;
|
|
125
|
+
const outlook = log?.latest
|
|
126
|
+
? {
|
|
127
|
+
turn: log.latest.turn,
|
|
128
|
+
state: log.latest.state,
|
|
129
|
+
heldTurns: log.latest.heldTurns,
|
|
130
|
+
basis: log.latest.basis,
|
|
131
|
+
// #304: the incomes compared and each opponent's frontier read; `thin` when the state rested on little.
|
|
132
|
+
...(log.latest.evidence ? { evidence: log.latest.evidence } : {}),
|
|
133
|
+
}
|
|
134
|
+
: { state: null, note: 'No outlook logged for this game: warapp_plan_turn records one each turn it plans.' };
|
|
135
|
+
const aiOpponents = game.players
|
|
136
|
+
.filter((p) => p.id !== me && p.state === PLAYING && (p.isAI || p.humanTurnedIntoAI))
|
|
137
|
+
.map((p) => ({ id: p.id, name: untrustedText(p.name) }));
|
|
138
|
+
const surrenderedToAI = game.endRules?.surrenderedTurnIntoAI;
|
|
139
|
+
const soon = leavers(game, Date.now(), { withinMinutes: LEAVER_WINDOW_MINUTES });
|
|
140
|
+
const nameOf = (id) => untrustedText(game.players.find((p) => p.id === id)?.name ?? '');
|
|
141
|
+
const leaving = soon.players
|
|
142
|
+
.filter((l) => l.playerId !== me)
|
|
143
|
+
.map(({ playerId, ...rest }) => ({ id: playerId, name: nameOf(playerId), ...rest }));
|
|
144
|
+
// The sentences name the game by id only: its name is its creator's words and
|
|
145
|
+
// goes, delimited, in `game.name` (the preview sits beside the confirmToken).
|
|
146
|
+
const summary = takeBack
|
|
147
|
+
? `Take back your pending surrender in war.app game ${gameId}`
|
|
148
|
+
: `This ENDS your war.app game ${gameId} — you lose it and cannot rejoin`;
|
|
149
|
+
return {
|
|
150
|
+
gate: {
|
|
151
|
+
action: takeBack ? 'warapp.take_back_surrender' : 'warapp.surrender',
|
|
152
|
+
summary,
|
|
153
|
+
account: writeAccount(me),
|
|
154
|
+
target: gameId,
|
|
155
|
+
payload: { method: 'Surrender', gameId, surrender: !takeBack },
|
|
156
|
+
willSend: { game: gameId, surrender: !takeBack },
|
|
157
|
+
preview: {
|
|
158
|
+
warning: takeBack
|
|
159
|
+
? `This will take back your pending surrender in game ${gameId}.`
|
|
160
|
+
: `This ENDS your game ${gameId} — you lose it and cannot rejoin.`,
|
|
161
|
+
account: shownAccount(seat.name, me),
|
|
162
|
+
game: { id: gameId, name: game.name === undefined ? null : untrustedText(game.name), state: game.state, playersStillIn: players },
|
|
163
|
+
...(takeBack ? {} : { acceptance }),
|
|
164
|
+
...(takeBack || acceptance.needed === false
|
|
165
|
+
? {}
|
|
166
|
+
: {
|
|
167
|
+
note: acceptance.needed === true
|
|
168
|
+
? 'The others must accept the surrender before it ends your game.'
|
|
169
|
+
: 'The others may have to accept the surrender before it ends your game, or it may end it at once (unknown).',
|
|
170
|
+
}),
|
|
171
|
+
outlook,
|
|
172
|
+
aiOpponents,
|
|
173
|
+
yourLandBecomes: surrenderedToAI === undefined ? null : surrenderedToAI ? 'AI' : 'neutral',
|
|
174
|
+
...(leaving.length ? { leavers: { players: leaving, unverified: soon.unverified } } : {}),
|
|
175
|
+
untrusted: UNTRUSTED_PREVIEW_NOTE,
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
state: { gameId, name, me, undo: takeBack, acceptanceNeeded: acceptance.needed },
|
|
179
|
+
};
|
|
180
|
+
},
|
|
181
|
+
async execute({ gameId, name, me, undo, acceptanceNeeded }) {
|
|
182
|
+
await requireApprovedAccount(ctx, writeAccount(me));
|
|
183
|
+
const game = { id: gameId, name };
|
|
184
|
+
return sendAndVerify({
|
|
185
|
+
send: async () => {
|
|
186
|
+
await ctx.rpc.call('Surrender', Method.Surrender, (w) => writeSurrenderParams(w, { gameId, surrender: !undo }));
|
|
187
|
+
},
|
|
188
|
+
refused: worded(SURRENDER_ERRORS),
|
|
189
|
+
unanswered: (failure) => ({
|
|
190
|
+
status: 'unknown',
|
|
191
|
+
outcome: 'unknown',
|
|
192
|
+
game,
|
|
193
|
+
warning: `war.app did not confirm the ${undo ? 'take-back' : 'surrender'} ` +
|
|
194
|
+
`(${failure}), so it may already have gone through. ` +
|
|
195
|
+
'Check warapp_get_game to see your state in the game.',
|
|
196
|
+
}),
|
|
197
|
+
readBack: () => ctx.client.getGame(gameId, { map: false }),
|
|
198
|
+
unreadable: (failure) => ({
|
|
199
|
+
status: 'unconfirmed',
|
|
200
|
+
outcome: 'unconfirmed',
|
|
201
|
+
game,
|
|
202
|
+
warning: `war.app accepted the request, but GameFeed could not be read back (${failure}). Check warapp_get_game.`,
|
|
203
|
+
}),
|
|
204
|
+
classify: (after) => {
|
|
205
|
+
const seat = after.players.find((p) => p.id === me);
|
|
206
|
+
const state = { yourState: seat?.state ?? null, gameState: after.state };
|
|
207
|
+
if (seat?.state === SURRENDERED)
|
|
208
|
+
return { status: 'surrendered', outcome: 'confirmed', game, ...state };
|
|
209
|
+
if (undo) {
|
|
210
|
+
// GameFeed shows no pending surrender, so "still playing" is all that can be read (#130).
|
|
211
|
+
if (seat?.state === PLAYING) {
|
|
212
|
+
return {
|
|
213
|
+
status: 'still_playing',
|
|
214
|
+
outcome: 'accepted',
|
|
215
|
+
game,
|
|
216
|
+
...state,
|
|
217
|
+
message: 'You are still playing. GameFeed has no pending-surrender state, so whether a surrender was ' +
|
|
218
|
+
'pending (and is now taken back) cannot be read; if none was, nothing changed.',
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else if (seat?.state === PLAYING && acceptanceNeeded !== false) {
|
|
223
|
+
return {
|
|
224
|
+
status: 'pending',
|
|
225
|
+
outcome: 'accepted',
|
|
226
|
+
game,
|
|
227
|
+
...state,
|
|
228
|
+
// GameFeed has no pending-surrender state: war.app accepted the request
|
|
229
|
+
// and the seat is still Playing, which is what a pending surrender looks like.
|
|
230
|
+
message: 'war.app accepted the surrender and you are still playing, so it should be pending: the other ' +
|
|
231
|
+
`players must accept it before it ends your game (${acceptanceNeeded ? 'the game asks for acceptance' : 'the game may ask for acceptance: its setting was not read'}). (GameFeed shows no pending state, so this is ` +
|
|
232
|
+
'inferred, not read.)',
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
status: 'unchanged',
|
|
237
|
+
outcome: 'unconfirmed',
|
|
238
|
+
game,
|
|
239
|
+
...state,
|
|
240
|
+
warning: 'war.app accepted the request, but GameFeed does not show your state changing yet. Check ' +
|
|
241
|
+
'warapp_get_game shortly.',
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Surviving a wrong estimate (#275, pattern 1), as the tools show it: the broken-estimate tripwire
|
|
2
|
+
// (src/analysis/tripwire.ts) read over my board history and the survival side store (src/trend.ts), and
|
|
3
|
+
// the headroom table (src/analysis/headroom.ts) in a view. warapp_plan_turn writes the store;
|
|
4
|
+
// warapp_check_plan and warapp_submit_turn only read.
|
|
5
|
+
import { headroomTable } from '../analysis/headroom.js';
|
|
6
|
+
import { symmetricStart, trackGame } from '../analysis/opponents.js';
|
|
7
|
+
import { activeTripwires, brokenEstimates } from '../analysis/tripwire.js';
|
|
8
|
+
import { reinforcementHand } from '../strategy/card-clock.js';
|
|
9
|
+
import { readSurvival, writeSurvival } from '../trend.js';
|
|
10
|
+
import { carriesTeam } from '../teams.js';
|
|
11
|
+
import { opponentCanHold } from './card-advice.js';
|
|
12
|
+
import { namer } from './common.js';
|
|
13
|
+
/** Headroom rows the compact view lists (the weakest first); the full view lists them all. */
|
|
14
|
+
export const HEADROOM_COMPACT = 10;
|
|
15
|
+
/**
|
|
16
|
+
* #275 review: the Reinforcement armies an opponent may have banked and played at once on the turn before
|
|
17
|
+
* `turn` (the one my latest board shows the result of), which its estimate (one card at most) leaves out:
|
|
18
|
+
* every card the clock says it can hold then (at most MaxCardsHold) when that is more than one, else 0; `Infinity`
|
|
19
|
+
* when Reinforcement is in play and the cards or their armies cannot be counted.
|
|
20
|
+
*/
|
|
21
|
+
export function bankedReinforcement(settings, turn) {
|
|
22
|
+
const hand = reinforcementHand(settings, turn - 1);
|
|
23
|
+
if (!hand)
|
|
24
|
+
return 0;
|
|
25
|
+
if (hand.cards === null)
|
|
26
|
+
return Infinity;
|
|
27
|
+
if (hand.cards <= 1)
|
|
28
|
+
return 0;
|
|
29
|
+
return hand.armiesPerCard === null ? Infinity : hand.cards * hand.armiesPerCard;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The tripwires running on `turn`: the marks kept, and one from each observation on my latest two
|
|
33
|
+
* boards that this turn's estimates (or the ones kept for the turns it reads) cannot explain. None while
|
|
34
|
+
* the teams are unknown (`teamsKnown: false`: a game of 3+ seats whose teams could not be read, where an
|
|
35
|
+
* opponent may be a teammate), and no observation once an opponent may hold an Airlift card (the card
|
|
36
|
+
* clock); no capture or held attack once one may hold a Bomb, no held attack once anyone may have held a
|
|
37
|
+
* Diplomacy card on the previous turn, and each allows the Reinforcement cards one
|
|
38
|
+
* may have banked (`bankedReinforcement`). A static seat (#270) and a player with no
|
|
39
|
+
* estimate this turn or kept are never read. While the teams are unknown the stored marks are kept unchanged (`keep`), never applied. Never throws.
|
|
40
|
+
*/
|
|
41
|
+
export async function readTripwires(a) {
|
|
42
|
+
const estimatesNow = new Map(a.estimates.map((e) => [e.playerId, e.estimate]));
|
|
43
|
+
const survival = a.dataDir ? await readSurvival(a.dataDir, a.gameId, a.me) : { estimates: new Map(), tripwires: [] };
|
|
44
|
+
if (a.teamsKnown === false)
|
|
45
|
+
return { tripwires: [], estimatesNow, survival, keep: [...survival.tripwires] };
|
|
46
|
+
if (a.turn === null)
|
|
47
|
+
return { tripwires: [], estimatesNow, survival, keep: [] };
|
|
48
|
+
let fresh = [];
|
|
49
|
+
try {
|
|
50
|
+
// A start only when every player began alike (#275 review): a custom scenario sets each start by
|
|
51
|
+
// hand, and unread distribution may be one, so the settings' territory limit × armies says nothing
|
|
52
|
+
// of what a stack never seen began with — no start, and such a stack is not read.
|
|
53
|
+
const banked = bankedReinforcement(a.gameSettings, a.turn);
|
|
54
|
+
const start = symmetricStart(a.gameSettings) ? trackGame([], a.gameSettings, a.map).start : undefined;
|
|
55
|
+
// Who else may play a card (a Gift of a territory with its stack): every player with an estimate, on a
|
|
56
|
+
// team or owning a territory on a board, me included, a static seat (#270) aside.
|
|
57
|
+
const players = new Set([a.me, ...estimatesNow.keys(), ...(a.teams?.keys() ?? [])]);
|
|
58
|
+
for (const b of a.boards)
|
|
59
|
+
for (const t of b.board.territories)
|
|
60
|
+
if (t.owner.kind === 'player')
|
|
61
|
+
players.add(t.owner.playerId);
|
|
62
|
+
for (const q of a.staticSeats?.keys() ?? [])
|
|
63
|
+
players.delete(q);
|
|
64
|
+
fresh = brokenEstimates({
|
|
65
|
+
...(start ? { start } : {}),
|
|
66
|
+
graph: a.graph,
|
|
67
|
+
boards: a.boards,
|
|
68
|
+
me: a.me,
|
|
69
|
+
settings: a.settings,
|
|
70
|
+
...(a.teams ? { teams: a.teams } : {}),
|
|
71
|
+
...(a.peace ? { peace: new Set(a.peace) } : {}),
|
|
72
|
+
...(opponentCanHold(a.gameSettings, 'Airlift', a.turn) ? { airlift: true } : {}),
|
|
73
|
+
...(opponentCanHold(a.gameSettings, 'Bomb', a.turn) ? { bomb: true } : {}),
|
|
74
|
+
// A Diplomacy card in force on the turn my attack ran may have worn off since: the cards in effect
|
|
75
|
+
// now (`peace`) do not say. Any player may hold one by then: no held attack is read.
|
|
76
|
+
...(opponentCanHold(a.gameSettings, 'Diplomacy', a.turn - 1) ? { diplomacy: true } : {}),
|
|
77
|
+
...(banked > 0 ? { banked } : {}),
|
|
78
|
+
...(a.staticSeats?.size ? { staticSeats: a.staticSeats } : {}),
|
|
79
|
+
// The estimate the plan of that turn used when kept, else a stand-in: the most it was kept at or is
|
|
80
|
+
// now (#275 review: today's figure alone under-counts an opponent whose estimate a bonus break has
|
|
81
|
+
// lowered since). None for a player with no estimate now (a static seat is neutralized, #270): it is
|
|
82
|
+
// not read. The stock bound reads only kept figures (`estimateKept`).
|
|
83
|
+
estimateAt: (p, t) => {
|
|
84
|
+
const now = estimatesNow.get(p);
|
|
85
|
+
if (now === undefined)
|
|
86
|
+
return undefined;
|
|
87
|
+
const kept = survival.estimates.get(t)?.get(p);
|
|
88
|
+
if (kept !== undefined)
|
|
89
|
+
return kept;
|
|
90
|
+
let most = now;
|
|
91
|
+
for (const row of survival.estimates.values())
|
|
92
|
+
most = Math.max(most, row.get(p) ?? 0);
|
|
93
|
+
return most;
|
|
94
|
+
},
|
|
95
|
+
estimateKept: (p, t) => survival.estimates.get(t)?.has(p) === true,
|
|
96
|
+
// My Gift of a territory is no capture; with my card plays unrecorded, captures of mine are unknown.
|
|
97
|
+
...(!a.gameSettings || a.gameSettings.cards.includes('Gift') ? { gift: true, players: [...players].sort((x, y) => x - y) } : {}),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// A board this reader cannot follow: no observation.
|
|
102
|
+
}
|
|
103
|
+
const tripwires = activeTripwires(survival.tripwires, fresh, a.turn);
|
|
104
|
+
return { tripwires, estimatesNow, survival, keep: tripwires };
|
|
105
|
+
}
|
|
106
|
+
/** Keep this turn's estimates and the marks `read.keep` names (warapp_plan_turn only). Never throws. */
|
|
107
|
+
export async function keepTripwires(dataDir, gameId, me, turn, read) {
|
|
108
|
+
if (!dataDir || turn === null)
|
|
109
|
+
return;
|
|
110
|
+
await writeSurvival(dataDir, gameId, me, turn, { estimates: read.estimatesNow, tripwires: read.keep });
|
|
111
|
+
}
|
|
112
|
+
/** What an untrusted opponent is sized for: the larger of what tripped it and my own income. */
|
|
113
|
+
export const untrustedDeploy = (w, myIncome) => Math.max(w.implied, Math.max(0, Math.floor(myIncome)));
|
|
114
|
+
const KIND_TEXT = {
|
|
115
|
+
'fog-stack': 'a stack out of the fog',
|
|
116
|
+
capture: 'a capture',
|
|
117
|
+
'held-attack': 'an attack of yours it held',
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* One warning per untrusted opponent, by id (the first warning of the answer). `sizedFor` is the figure
|
|
121
|
+
* the plan or the stress test used for it (`untrustedDeploy` on the income they used), so the warning
|
|
122
|
+
* never names another. `whatIf` is the caller's `enemy_deploy`: the planner then ignores the mark, and
|
|
123
|
+
* the warning names that figure instead.
|
|
124
|
+
*/
|
|
125
|
+
export function tripwireWarnings(tripwires, sizedFor, estimateOf, whatIf) {
|
|
126
|
+
return tripwires.map((w) => {
|
|
127
|
+
const est = estimateOf(w.player);
|
|
128
|
+
return (`Player ${w.player}'s deploy estimate${est !== undefined ? ` (${est})` : ''} is untrusted until turn ${w.until} (#275): on turn ${w.since} ` +
|
|
129
|
+
`${KIND_TEXT[w.kind]} on territory ${w.territory} needed ${w.observed} armies where the estimate allowed ${w.allowed}, ` +
|
|
130
|
+
`about ${w.implied} a turn. ` +
|
|
131
|
+
(whatIf !== undefined
|
|
132
|
+
? `This what-if plan sizes every border for enemy_deploy ${whatIf} instead; without it, borders of your held ` +
|
|
133
|
+
`bonuses facing it would be sized for ${sizedFor(w)} (the larger of that and your own income).`
|
|
134
|
+
: `Borders of your held bonuses facing it are sized for ${sizedFor(w)} ` +
|
|
135
|
+
'(the larger of that and your own income), and warapp_check_plan tries that deploy by default.'));
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** The headroom table on these orders, never throwing (an empty table when the board cannot be carried out). */
|
|
139
|
+
export function safeHeadroom(input) {
|
|
140
|
+
try {
|
|
141
|
+
return headroomTable(input);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The headroom table as the tools show it: names beside ids, each row with whether it is below my income.
|
|
149
|
+
* `mayBeTeammate` (the teams unknown, `resolveTeams`' `known: false`): every row says its opponent may be a teammate.
|
|
150
|
+
*/
|
|
151
|
+
export function headroomView(rows, map, myIncome, full, mayBeTeammate = false) {
|
|
152
|
+
const name = namer(map);
|
|
153
|
+
const bonusName = new Map(map.bonuses.map((b) => [b.id, b.name]));
|
|
154
|
+
return rows.slice(0, full ? rows.length : HEADROOM_COMPACT).map((r) => ({
|
|
155
|
+
territory: name(r.territory),
|
|
156
|
+
armies: r.armies,
|
|
157
|
+
deploy: r.deploy,
|
|
158
|
+
opponent: r.opponent,
|
|
159
|
+
from: name(r.from),
|
|
160
|
+
...(r.firstMover ? { needsFirstMove: r.firstMover === 'me' ? 'you' : 'opponent' } : {}),
|
|
161
|
+
belowMyIncome: r.deploy !== null && r.deploy < myIncome,
|
|
162
|
+
bonuses: r.bonuses.map((b) => ({ id: b.id, name: bonusName.get(b.id) ?? String(b.id), value: b.value })),
|
|
163
|
+
behind: r.behind.map(name),
|
|
164
|
+
behindIncome: r.behindIncome,
|
|
165
|
+
breach: { territories: r.breach.territories.map(name), income: r.breach.income },
|
|
166
|
+
...(mayBeTeammate ? { mayBeTeammate: true } : {}),
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* #275 (c): a warning for each border of a held bonus that falls to a single deploy below my own income,
|
|
171
|
+
* by id. A warning, never a refusal. `mayBeTeammate`: the teams are unknown, and the warning says so.
|
|
172
|
+
*/
|
|
173
|
+
export function headroomWarnings(rows, myIncome, mayBeTeammate = false) {
|
|
174
|
+
return rows
|
|
175
|
+
.filter((r) => r.bonuses.length > 0 && r.deploy !== null && r.deploy < myIncome)
|
|
176
|
+
.map((r) => `Territory ${r.territory} guards bonus${r.bonuses.length === 1 ? '' : 'es'} ${r.bonuses.map((b) => `${b.id} (+${b.value})`).join(', ')} ` +
|
|
177
|
+
`and these orders leave it falling to a deploy of ${r.deploy} by player ${r.opponent}` +
|
|
178
|
+
`${mayBeTeammate ? " (may be a teammate: this game's teams could not be read)" : ''} on territory ${r.from}` +
|
|
179
|
+
`${r.firstMover ? ` (when ${r.firstMover === 'me' ? 'you move' : 'it moves'} first)` : ''}: below your own income of ${myIncome} (#275 headroom). ` +
|
|
180
|
+
'An estimate is a floor; consider more armies there.');
|
|
181
|
+
}
|
|
182
|
+
/** The opponents' teams for the tripwire and the headroom table, from the players' team fields. */
|
|
183
|
+
export function teamsOf(players) {
|
|
184
|
+
const m = new Map();
|
|
185
|
+
for (const p of players)
|
|
186
|
+
if (carriesTeam(p.team))
|
|
187
|
+
m.set(p.id, p.team);
|
|
188
|
+
return m.size ? m : undefined;
|
|
189
|
+
}
|