@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,327 @@
|
|
|
1
|
+
// warapp_join_game: take an open seat in a game from war.app's open games list
|
|
2
|
+
// (AcceptOpenSeat, method 61), gated on the user's confirmation. The game is
|
|
3
|
+
// re-read from the list first, so the preview shows what is actually being
|
|
4
|
+
// joined, and the join is reported only once GameFeed (or GetMyGames) shows
|
|
5
|
+
// the account in the game. war.app needs a colour with the join (src/join-color.ts):
|
|
6
|
+
// the account's preferred one, else its secondary, else a standard colour still free.
|
|
7
|
+
import { CONFIRM_INJECTION_RULE, toolAnnotations } from '@chrischall/mcp-utils';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { parseGameId } from '../client.js';
|
|
10
|
+
import { chooseJoinColor, readAccountColors, STANDARD_COLORS } from '../join-color.js';
|
|
11
|
+
import { feedScreen, joiningPolicy, parseOpenGamesPage } from '../open-games.js';
|
|
12
|
+
import { GAME_COLOR_COUNT, Method, readAcceptOpenSeatReturn, writeAcceptOpenSeatParams, } from '../protocol/methods.js';
|
|
13
|
+
import { WarappServerError } from '../protocol/rpc.js';
|
|
14
|
+
import { gameIdArg, UNTRUSTED_PREVIEW_NOTE, untrustedText } from './common.js';
|
|
15
|
+
import { noBrowserNote } from './session.js';
|
|
16
|
+
import { mintMeta } from './hints.js';
|
|
17
|
+
import { requireApprovedAccount, writeAccount } from './live-context.js';
|
|
18
|
+
import { fetchMyGames } from './my-games.js';
|
|
19
|
+
import { markLotteries, openGamesPage } from './open-games.js';
|
|
20
|
+
import { holdsSeat, playerStateName } from '../player-states.js';
|
|
21
|
+
import { NO_TEAM } from '../teams.js';
|
|
22
|
+
import { accountUnknown, defineGatedWrite, refuse, sendAndVerify } from './write-kit.js';
|
|
23
|
+
/** war.app's "already in the game" code: the client treats it as a successful join. */
|
|
24
|
+
const ALREADY_JOINED = 121;
|
|
25
|
+
/** war.app's generic assertion failure; with "color is null" it is a join sent without a colour. */
|
|
26
|
+
const ASSERT_FAILED = 1;
|
|
27
|
+
const COLOR_UNAVAILABLE = 'That color is not available. Please pick a different color (pass color: a war.app colour index ' +
|
|
28
|
+
`0–${STANDARD_COLORS - 1} that no player in the game has).`;
|
|
29
|
+
/**
|
|
30
|
+
* The client's wording for the other refusals it handles on a join (254, 473
|
|
31
|
+
* and 750 are in the RPC error table). These codes are only read here, where
|
|
32
|
+
* their meaning is the join's.
|
|
33
|
+
*/
|
|
34
|
+
const JOIN_ERRORS = {
|
|
35
|
+
4: 'Could not join the game because it has already begun.',
|
|
36
|
+
7: 'Sorry, the host has removed you.',
|
|
37
|
+
8: COLOR_UNAVAILABLE,
|
|
38
|
+
15: 'Sorry, this game is already full.',
|
|
39
|
+
75: COLOR_UNAVAILABLE,
|
|
40
|
+
88: 'Could not join the game because it has been deleted.',
|
|
41
|
+
120: 'Could not join the game because the open seat is no longer available.',
|
|
42
|
+
227: 'Sorry, the creator of this game has added you to their block list. You cannot join this game.',
|
|
43
|
+
239: "Sorry, you don't meet the prerequisites the host has entered for open seats in this game.",
|
|
44
|
+
390: COLOR_UNAVAILABLE,
|
|
45
|
+
};
|
|
46
|
+
/** The wording for a join refusal this tool names itself, or undefined. */
|
|
47
|
+
function joinErrorWording(e) {
|
|
48
|
+
if (e.code === ASSERT_FAILED && /color is null/i.test(e.message)) {
|
|
49
|
+
return 'war.app refused the join because it was sent with no colour ("color is null").';
|
|
50
|
+
}
|
|
51
|
+
return JOIN_ERRORS[e.code];
|
|
52
|
+
}
|
|
53
|
+
/** The open game `gameId`, refused when it cannot (or should not) be joined. */
|
|
54
|
+
function joinable(games, gameId, allowTeam, acceptFee) {
|
|
55
|
+
const game = games.find((g) => g.id === gameId);
|
|
56
|
+
if (!game) {
|
|
57
|
+
refuse(`Game ${gameId} is not on war.app's open games list, so it was not joined.`, 'It may have started or filled, be hidden from this account by prerequisites, or already include you. ' +
|
|
58
|
+
'Pick a game from warapp_list_open_games.');
|
|
59
|
+
}
|
|
60
|
+
if (game.openSeats === 0)
|
|
61
|
+
refuse(`Game ${gameId} has no open seat.`, 'Pick another game from warapp_list_open_games.');
|
|
62
|
+
if (game.rules.includes('Custom scenario')) {
|
|
63
|
+
refuse(`Game ${gameId} uses a custom scenario, which needs a starting slot this tool does not pick.`, 'Join it from war.app instead.');
|
|
64
|
+
}
|
|
65
|
+
if (game.rules.includes('Mods')) {
|
|
66
|
+
refuse(`Game ${gameId} uses mods, which can hook the join in ways this tool cannot reproduce.`, 'Join it from war.app instead.');
|
|
67
|
+
}
|
|
68
|
+
if (game.teamGame && !allowTeam) {
|
|
69
|
+
refuse(`Game ${gameId} is a team game.`, "Pass team: a team number with an open seat (the preview lists them), or allow_team: true to take the first open seat's team.");
|
|
70
|
+
}
|
|
71
|
+
if (game.entryFeeCoins > 0 && !acceptFee) {
|
|
72
|
+
refuse(`Game ${gameId} costs ${game.entryFeeCoins} coins to join.`, 'Pass accept_entry_fee: true to pay it, or pick a free game.');
|
|
73
|
+
}
|
|
74
|
+
return game;
|
|
75
|
+
}
|
|
76
|
+
const TEAM_CHOICE_NOTE = "The team is sent as the chosen open seat's team, the int32 AcceptOpenSeat already carries (the client's team " +
|
|
77
|
+
'picker passes it; read from the client bundle, UNVERIFIED live). Seats are by in-game id; an AI seat plays by rule.';
|
|
78
|
+
/**
|
|
79
|
+
* The team to join (C4): `requested` when an open seat carries it — refused when the team is full (its players
|
|
80
|
+
* seated, no open seat) or not in the game — else the first open seat's team; NO_TEAM outside a team game (a
|
|
81
|
+
* team asked for there is refused).
|
|
82
|
+
*/
|
|
83
|
+
function chooseTeam(game, lobby, requested) {
|
|
84
|
+
const offered = [...new Set(game.openSeatTeams ?? [])].sort((a, b) => a - b);
|
|
85
|
+
if (requested === undefined)
|
|
86
|
+
return game.teamGame ? (game.openSeatTeams?.[0] ?? NO_TEAM) : NO_TEAM;
|
|
87
|
+
if (!game.teamGame)
|
|
88
|
+
refuse(`Game ${game.id} is not a team game, so it has no team to choose.`, 'Leave team out.');
|
|
89
|
+
if (game.randomTeamsOf !== undefined)
|
|
90
|
+
refuse(`Game ${game.id} deals random teams, so a team cannot be chosen.`, 'Leave team out (allow_team: true) to join it.');
|
|
91
|
+
if (offered.includes(requested))
|
|
92
|
+
return requested;
|
|
93
|
+
const list = offered.length ? offered.join(', ') : 'none';
|
|
94
|
+
if (lobby?.seated.some((p) => p.team === requested))
|
|
95
|
+
refuse(`Team ${requested} is full in game ${game.id}: no open seat is on it.`, `Teams with an open seat: ${list}.`);
|
|
96
|
+
refuse(`Team ${requested} is not offered in game ${game.id}.`, `Teams with an open seat: ${list}.`);
|
|
97
|
+
}
|
|
98
|
+
/** The preview's team section (C4): the team joined, the teams offered, and the seats already on it by id, human or AI. */
|
|
99
|
+
function teamView(game, lobby, team) {
|
|
100
|
+
return {
|
|
101
|
+
id: team,
|
|
102
|
+
offered: [...new Set(game.openSeatTeams ?? [])].sort((a, b) => a - b),
|
|
103
|
+
seats: (lobby?.seated ?? [])
|
|
104
|
+
.filter((p) => p.team === team)
|
|
105
|
+
.sort((a, b) => a.id - b.id)
|
|
106
|
+
.map((p) => ({ inGameId: p.id, kind: p.ai ? 'ai' : 'human' })),
|
|
107
|
+
openSeats: (game.openSeatTeams ?? []).filter((t) => t === team).length,
|
|
108
|
+
note: TEAM_CHOICE_NOTE,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** Refuse a lobby the signed-in account (in-game id `me`) already holds a seat in. */
|
|
112
|
+
function refuseJoined(lobby, gameId, me) {
|
|
113
|
+
if (lobby?.seatedPlayerIds.includes(me)) {
|
|
114
|
+
refuse(`You have already joined game ${gameId}.`, 'It is in warapp_my_games; pick another game from warapp_list_open_games.');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** The colour to send: the one asked for (refused when a lobby player has it), else the client's choice. */
|
|
118
|
+
function joinColor(requested, html, bundle, lobby, gameId) {
|
|
119
|
+
const taken = lobby?.colorsTaken ?? [];
|
|
120
|
+
if (requested !== undefined) {
|
|
121
|
+
if (taken.includes(requested)) {
|
|
122
|
+
refuse(`Colour ${requested} is taken by another player in game ${gameId}.`, 'Pass another color, or leave it out to let this tool pick one.');
|
|
123
|
+
}
|
|
124
|
+
return { color: requested, source: 'requested' };
|
|
125
|
+
}
|
|
126
|
+
const chosen = chooseJoinColor(readAccountColors(html, bundle), taken);
|
|
127
|
+
if (!chosen) {
|
|
128
|
+
refuse(`Every standard colour is taken in game ${gameId}.`, `Pass color: a war.app colour index (${STANDARD_COLORS}–${GAME_COLOR_COUNT - 1}) your account has unlocked.`);
|
|
129
|
+
}
|
|
130
|
+
return chosen;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The lobby's screen from its GameFeed settings (#132): the open-games record carries no neutral
|
|
134
|
+
* sizes or fog, GameFeed does. Best effort — a failed read leaves the preview without a screen.
|
|
135
|
+
*/
|
|
136
|
+
async function lobbyScreen(ctx, gameId) {
|
|
137
|
+
try {
|
|
138
|
+
const game = await ctx.client.getGame(gameId, { settings: true, map: false });
|
|
139
|
+
const { screen, gaps } = feedScreen(game.settings ?? { cards: [], raw: {} });
|
|
140
|
+
return screen
|
|
141
|
+
? { screen, gaps }
|
|
142
|
+
: { screen: null, gaps, note: "GameFeed's settings name no neutral size, so the neutral-cost screen was skipped." };
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
return {
|
|
146
|
+
screen: null,
|
|
147
|
+
gaps: [],
|
|
148
|
+
note: `GameFeed could not read the lobby (${e instanceof Error ? e.message : String(e)}), so there is no neutral-cost or fog screen.`,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Whether GameFeed, else GetMyGames, shows the signed-in account in the game. */
|
|
153
|
+
async function confirmSeated(ctx, gameId) {
|
|
154
|
+
const token = await ctx.myInviteToken();
|
|
155
|
+
if (token !== undefined) {
|
|
156
|
+
try {
|
|
157
|
+
const game = await ctx.client.getGame(gameId, { map: false });
|
|
158
|
+
if (game.players.some((p) => p.id === token && holdsSeat(p.state)))
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// No GameFeed credentials or a failed read: try the web client's list.
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const { me, games } = await fetchMyGames(ctx);
|
|
167
|
+
return games.some((g) => g.id === gameId && g.players.some((p) => p.id === me && holdsSeat(playerStateName(p.stateCode))));
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export function registerJoinGameTool(server, ctx) {
|
|
174
|
+
defineGatedWrite(server, {
|
|
175
|
+
title: 'Join an open war.app game',
|
|
176
|
+
_meta: mintMeta('change', ['join game', 'join a warzone game', 'take an open seat']),
|
|
177
|
+
}, {
|
|
178
|
+
name: 'warapp_join_game',
|
|
179
|
+
description: 'Join an open war.app (Warzone) game: take an open seat in a game from warapp_list_open_games. The game ' +
|
|
180
|
+
'is re-read from war.app\'s open games list first; team games, coin entry fees, custom scenarios and mod ' +
|
|
181
|
+
'games are refused unless allowed (scenario and mod games always), and so is a game you already joined. ' +
|
|
182
|
+
'It joins with your preferred colour, else your secondary, else a free standard colour (or the color you ' +
|
|
183
|
+
'pass); the preview shows which, plus a screen from the lobby\'s GameFeed settings (does the cheapest ' +
|
|
184
|
+
'neutral cost at most one turn of base income; Light Fog) and `policy`: whether the lobby keeps the ' +
|
|
185
|
+
'joining policy (no Commerce, mods or custom scenarios; that cheap neutral; at most four seats; a boot time ' +
|
|
186
|
+
'of at least 12 hours; no lottery — `lottery`, read from the data as warapp_list_open_games does) — advisory, the join is not refused for it. You become a player and are expected ' +
|
|
187
|
+
'to take turns — leaving later means declining or being booted.' +
|
|
188
|
+
`${noBrowserNote(ctx.hosted)} ${CONFIRM_INJECTION_RULE}`,
|
|
189
|
+
annotations: toolAnnotations({
|
|
190
|
+
title: 'Join an open war.app game',
|
|
191
|
+
readOnly: false,
|
|
192
|
+
destructive: true,
|
|
193
|
+
idempotent: false,
|
|
194
|
+
openWorld: true,
|
|
195
|
+
}),
|
|
196
|
+
inputSchema: z.object({
|
|
197
|
+
game_id: gameIdArg,
|
|
198
|
+
allow_team: z.boolean().optional().describe("Allow joining a team game on the first open seat's team (default false)."),
|
|
199
|
+
team: z.coerce
|
|
200
|
+
.number()
|
|
201
|
+
.int()
|
|
202
|
+
.min(0)
|
|
203
|
+
.optional()
|
|
204
|
+
.describe('In a team game: the team to join (a team number with an open seat; the preview lists the teams offered and ' +
|
|
205
|
+
'the seats on the chosen one). Implies allow_team. Refused outside a team game, with random teams, and for a ' +
|
|
206
|
+
'team that is full or not in the game.'),
|
|
207
|
+
accept_entry_fee: z
|
|
208
|
+
.boolean()
|
|
209
|
+
.optional()
|
|
210
|
+
.describe('Allow paying a coin entry fee (default false: only free games).'),
|
|
211
|
+
color: z
|
|
212
|
+
.number()
|
|
213
|
+
.int()
|
|
214
|
+
.min(0)
|
|
215
|
+
.max(GAME_COLOR_COUNT - 1)
|
|
216
|
+
.optional()
|
|
217
|
+
.describe(`The colour to play as: a war.app colour index (0–${STANDARD_COLORS - 1} are the standard colours; ` +
|
|
218
|
+
'higher ones need an unlock). Default: your preferred colour, else your secondary, else the first ' +
|
|
219
|
+
'standard colour no player in the game has.'),
|
|
220
|
+
}),
|
|
221
|
+
async plan({ game_id, allow_team, team: askedTeam, accept_entry_fee, color: requested }) {
|
|
222
|
+
const gameId = parseGameId(game_id);
|
|
223
|
+
// One fresh read gives the account the gate binds and its in-game id (sliced from the
|
|
224
|
+
// token TEXT, #130), so the already-joined check is for that same account. A join always
|
|
225
|
+
// acts as the signed-in account, so it is refused when that cannot be read (#133).
|
|
226
|
+
const [html, me] = await Promise.all([openGamesPage(ctx), ctx.myIdentity({ fresh: true })]);
|
|
227
|
+
if (me === undefined)
|
|
228
|
+
throw accountUnknown(ctx.hosted);
|
|
229
|
+
const { token, inGameId: myId } = me;
|
|
230
|
+
const bundle = await ctx.bundle();
|
|
231
|
+
const parsed = parseOpenGamesPage(html, bundle);
|
|
232
|
+
const lobby = parsed.lobbies.get(gameId);
|
|
233
|
+
refuseJoined(lobby, gameId, myId);
|
|
234
|
+
const listed = joinable(parsed.games, gameId, (allow_team ?? false) || askedTeam !== undefined, accept_entry_fee ?? false);
|
|
235
|
+
const team = chooseTeam(listed, lobby, askedTeam);
|
|
236
|
+
const { color, source } = joinColor(requested, html, bundle, lobby, gameId);
|
|
237
|
+
// #217: the lobby's screen, then whether it is a lottery (its map read only for that settings shape).
|
|
238
|
+
const screened = await lobbyScreen(ctx, gameId);
|
|
239
|
+
const [game] = (await markLotteries(ctx, [listed]));
|
|
240
|
+
return {
|
|
241
|
+
gate: {
|
|
242
|
+
action: 'warapp.join_game',
|
|
243
|
+
summary: `Join war.app game ${gameId} (${game.pace}, ${game.openSeats} open of ${game.seats} seats)`,
|
|
244
|
+
account: writeAccount(token),
|
|
245
|
+
target: gameId,
|
|
246
|
+
payload: { method: 'AcceptOpenSeat', gameId, team, color },
|
|
247
|
+
willSend: { game: gameId, team: team === NO_TEAM ? 'none' : team, color },
|
|
248
|
+
preview: {
|
|
249
|
+
game: {
|
|
250
|
+
id: game.id,
|
|
251
|
+
name: untrustedText(game.name),
|
|
252
|
+
url: game.url,
|
|
253
|
+
pace: game.pace,
|
|
254
|
+
...(game.bootTime ? { bootTime: game.bootTime } : {}),
|
|
255
|
+
players: game.players,
|
|
256
|
+
openSeats: game.openSeats,
|
|
257
|
+
seats: game.seats,
|
|
258
|
+
teamGame: game.teamGame,
|
|
259
|
+
entryFeeCoins: game.entryFeeCoins,
|
|
260
|
+
prizeCoins: game.prizeCoins,
|
|
261
|
+
rules: game.rules,
|
|
262
|
+
...(game.lottery !== undefined ? { lottery: game.lottery, lotteryBasis: game.lotteryBasis } : {}),
|
|
263
|
+
plannerGaps: [...game.plannerGaps, ...screened.gaps],
|
|
264
|
+
},
|
|
265
|
+
// #132: worth joining only when the cheapest neutral costs at most a turn of base income.
|
|
266
|
+
screen: screened.screen,
|
|
267
|
+
...(screened.note ? { screenNote: screened.note } : {}),
|
|
268
|
+
// #134: the owner's joining policy — advisory, the join is not refused for it.
|
|
269
|
+
policy: joiningPolicy(game, screened.screen),
|
|
270
|
+
color: { index: color, source },
|
|
271
|
+
// C4: in a team game, the team joined and who already sits on it.
|
|
272
|
+
...(game.teamGame ? { team: teamView(game, lobby, team) } : {}),
|
|
273
|
+
account: String(token),
|
|
274
|
+
note: 'Joining makes you a player who must take turns until the game ends.',
|
|
275
|
+
untrusted: UNTRUSTED_PREVIEW_NOTE,
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
state: {
|
|
279
|
+
gameId,
|
|
280
|
+
name: game.name,
|
|
281
|
+
url: game.url,
|
|
282
|
+
team,
|
|
283
|
+
color,
|
|
284
|
+
account: String(token),
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
async execute({ gameId, name, url, team, color, account }) {
|
|
289
|
+
await requireApprovedAccount(ctx, account);
|
|
290
|
+
const game = { id: gameId, name, url };
|
|
291
|
+
let vacationEnded = false;
|
|
292
|
+
const extra = () => (vacationEnded ? { vacationEnded: true } : {});
|
|
293
|
+
const notShown = () => ({
|
|
294
|
+
status: 'unconfirmed',
|
|
295
|
+
outcome: 'unconfirmed',
|
|
296
|
+
game,
|
|
297
|
+
...extra(),
|
|
298
|
+
warning: 'war.app accepted the join, but neither GameFeed nor your games list shows you in the game yet, so ' +
|
|
299
|
+
'this could not confirm it. Check warapp_my_games shortly.',
|
|
300
|
+
});
|
|
301
|
+
return sendAndVerify({
|
|
302
|
+
send: async () => {
|
|
303
|
+
const env = await ctx.rpc.call('AcceptOpenSeat', Method.AcceptOpenSeat, (w) => writeAcceptOpenSeatParams(w, { gameId, team, color }));
|
|
304
|
+
vacationEnded = readAcceptOpenSeatReturn(env.body)?.vacationEnded ?? false;
|
|
305
|
+
},
|
|
306
|
+
// "Already in the game": the client treats it as a successful join.
|
|
307
|
+
refused: (e) => {
|
|
308
|
+
if (e.code === ALREADY_JOINED)
|
|
309
|
+
return 'ignore';
|
|
310
|
+
const wording = joinErrorWording(e);
|
|
311
|
+
return wording ? new WarappServerError(e.code, `${wording} (war.app error ${e.code})`) : undefined;
|
|
312
|
+
},
|
|
313
|
+
unanswered: (failure) => ({
|
|
314
|
+
status: 'unknown',
|
|
315
|
+
outcome: 'unknown',
|
|
316
|
+
game,
|
|
317
|
+
warning: `war.app did not confirm the join (${failure}), so you may ` +
|
|
318
|
+
'already have joined. Check warapp_my_games before trying again.',
|
|
319
|
+
}),
|
|
320
|
+
// Never throws: a failed read is "not shown".
|
|
321
|
+
readBack: () => confirmSeated(ctx, gameId),
|
|
322
|
+
unreadable: () => notShown(),
|
|
323
|
+
classify: (seated) => (seated ? { status: 'joined', outcome: 'confirmed', game, ...extra() } : notShown()),
|
|
324
|
+
});
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// warapp_leave_game: give up the signed-in account's seat in a game that has not started
|
|
2
|
+
// (`WaitingForPlayers`) with the web client's DeclineGame (method 64) — the lobby's Decline
|
|
3
|
+
// button, gated on the user's confirmation (#217). The encoding is read from the client bundle's
|
|
4
|
+
// writer, verified live 2026-11-14 for an open-seat lobby (docs/WEB-PROTOCOL.md "DeclineGame"). The game is read from GameFeed
|
|
5
|
+
// first (refused once started, for a seat the account does not hold, and for mod games), and the
|
|
6
|
+
// leave is reported only once GameFeed (else GetMyGames) shows the account no longer seated —
|
|
7
|
+
// never from the reply, whose one bool says whether war.app removed the account's chat.
|
|
8
|
+
import { CONFIRM_INJECTION_RULE, toolAnnotations } from '@chrischall/mcp-utils';
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import { parseGameId } from '../client.js';
|
|
11
|
+
import { holdsSeat, playerStateName } from '../player-states.js';
|
|
12
|
+
import { Method, readDeclineGameReturn, writeDeclineGameParams } from '../protocol/methods.js';
|
|
13
|
+
import { WarappServerError } from '../protocol/rpc.js';
|
|
14
|
+
import { isTeamGame } from '../teams.js';
|
|
15
|
+
import { gameIdArg, shownAccount, UNTRUSTED_PREVIEW_NOTE, untrustedText } from './common.js';
|
|
16
|
+
import { mintMeta } from './hints.js';
|
|
17
|
+
import { requireApprovedAccount, writeAccount } from './live-context.js';
|
|
18
|
+
import { BRIDGE_NOTE } from './live.js';
|
|
19
|
+
import { fetchMyGames } from './my-games.js';
|
|
20
|
+
import { defineGatedWrite, mySeat, refuse, requireMe, sendAndVerify } from './write-kit.js';
|
|
21
|
+
/** The only game state a seat can be left in: the lobby, still filling. */
|
|
22
|
+
const LOBBY = 'WaitingForPlayers';
|
|
23
|
+
/**
|
|
24
|
+
* The DeclineGame refusals the client words (read from the bundle). It also swallows 17
|
|
25
|
+
* (NotInGame), 4 and 7 (InvalidPlayerState) without a word; here all three are read back from
|
|
26
|
+
* game state like an accepted leave ({@link READ_BACK_ERRORS}), so game state, not a guessed
|
|
27
|
+
* wording, says whether the seat is gone.
|
|
28
|
+
*/
|
|
29
|
+
const LEAVE_ERRORS = {
|
|
30
|
+
191: 'You cannot leave an open game you created; delete it in war.app instead.',
|
|
31
|
+
88: 'This game has been deleted.',
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* NotInGame and the codes the client swallows (4, 7): the seat may already be gone, so game
|
|
35
|
+
* state decides rather than a guessed wording.
|
|
36
|
+
*/
|
|
37
|
+
const READ_BACK_ERRORS = new Set([17, 4, 7]);
|
|
38
|
+
/** The signed-in account's seat in lobby `game`, refused unless the account holds one there. */
|
|
39
|
+
function leavable(game, token) {
|
|
40
|
+
if (game.state !== LOBBY) {
|
|
41
|
+
refuse(`Game ${game.id} is not a lobby (game state: ${game.state}): only a game still ${LOBBY} can be left, so nothing was sent.`, 'A game that has started is surrendered with warapp_surrender, not left.');
|
|
42
|
+
}
|
|
43
|
+
const seat = mySeat(game, token);
|
|
44
|
+
if (!holdsSeat(seat.state)) {
|
|
45
|
+
refuse(`You hold no seat in game ${game.id} (your state: ${seat.state ?? 'unknown'}), so there is nothing to leave.`, 'An invitation is declined in war.app; check warapp_my_games.');
|
|
46
|
+
}
|
|
47
|
+
// Unread settings are treated as possibly modded: the mod-hook flag is sent false, which a
|
|
48
|
+
// mod game could refuse or mishandle.
|
|
49
|
+
const mods = game.settings?.mods;
|
|
50
|
+
if (mods === undefined || mods !== 0) {
|
|
51
|
+
refuse(mods === undefined
|
|
52
|
+
? `Game ${game.id}'s settings could not be read, so whether it uses mods is unknown and nothing was sent.`
|
|
53
|
+
: `Game ${game.id} uses mods, which can hook leaving the game in ways this tool cannot reproduce, so nothing was sent.`, 'Leave it from war.app instead.');
|
|
54
|
+
}
|
|
55
|
+
return seat;
|
|
56
|
+
}
|
|
57
|
+
/** GameFeed's answer for a game the account is no longer in (seen live after a leave, #217). */
|
|
58
|
+
const NOT_IN_GAME = /can only be used for games you are in/i;
|
|
59
|
+
/**
|
|
60
|
+
* Whether game state shows account `me` (its GameFeed id) out of the game: GameFeed listing it
|
|
61
|
+
* with no seat (declined) or not at all, else GetMyGames listing the game with its seat given up.
|
|
62
|
+
* A game missing from GetMyGames proves nothing (the list may be partial, #260), and nothing read is `false`.
|
|
63
|
+
*/
|
|
64
|
+
async function confirmLeft(ctx, gameId, me) {
|
|
65
|
+
try {
|
|
66
|
+
const game = await ctx.client.getGame(gameId, { map: false });
|
|
67
|
+
const seat = game.players.find((p) => p.id === me);
|
|
68
|
+
return !seat || !holdsSeat(seat.state);
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
// Seen live (#217): once the seat is given up, GameFeed refuses the game outright ("This API
|
|
72
|
+
// can only be used for games you are in, tournament games…") instead of listing me as gone.
|
|
73
|
+
if (e instanceof Error && NOT_IN_GAME.test(e.message))
|
|
74
|
+
return true;
|
|
75
|
+
// No GameFeed credentials or another failed read: try the web client's list.
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const { me: inGame, games } = await fetchMyGames(ctx);
|
|
79
|
+
const seat = games.find((g) => g.id === gameId)?.players.find((p) => p.id === inGame);
|
|
80
|
+
return seat !== undefined && !holdsSeat(playerStateName(seat.stateCode));
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export function registerLeaveGameTool(server, ctx) {
|
|
87
|
+
defineGatedWrite(server, {
|
|
88
|
+
title: 'Leave a war.app game that has not started',
|
|
89
|
+
_meta: mintMeta('change', ['leave game', 'leave a lobby', 'back out of a game', 'quit a lobby']),
|
|
90
|
+
}, {
|
|
91
|
+
name: 'warapp_leave_game',
|
|
92
|
+
description: 'Leave a war.app (Warzone) game that has not started — a lobby still WaitingForPlayers — giving up your ' +
|
|
93
|
+
"seat (war.app's DeclineGame, the lobby's Decline button; verified live for a lobby joined through an open " +
|
|
94
|
+
'seat). Refuses a game that has started (surrender it with warapp_surrender), a lobby you ' +
|
|
95
|
+
'hold no seat in, and mod games. Always as the signed-in war.app account; reported `left` only once ' +
|
|
96
|
+
'GameFeed (or your games list) no longer shows you seated — `unconfirmed` otherwise, or `unknown` when ' +
|
|
97
|
+
`war.app did not answer (never resend blindly). ${BRIDGE_NOTE} ${CONFIRM_INJECTION_RULE}`,
|
|
98
|
+
annotations: toolAnnotations({
|
|
99
|
+
title: 'Leave a war.app lobby',
|
|
100
|
+
readOnly: false,
|
|
101
|
+
destructive: true,
|
|
102
|
+
idempotent: false,
|
|
103
|
+
openWorld: true,
|
|
104
|
+
}),
|
|
105
|
+
inputSchema: z.object({ game_id: gameIdArg }),
|
|
106
|
+
async plan({ game_id }) {
|
|
107
|
+
const game = await ctx.client.getGame(parseGameId(game_id), { settings: true, map: false });
|
|
108
|
+
const token = await requireMe(ctx);
|
|
109
|
+
const seat = leavable(game, token);
|
|
110
|
+
const teamGame = isTeamGame(game.players);
|
|
111
|
+
return {
|
|
112
|
+
gate: {
|
|
113
|
+
action: 'warapp.leave_game',
|
|
114
|
+
// By id only: the game and player names (other people's words) go, delimited, in labelled fields.
|
|
115
|
+
summary: `Leave war.app game ${game.id}, which has not started, giving up your seat`,
|
|
116
|
+
account: writeAccount(seat.id),
|
|
117
|
+
target: game.id,
|
|
118
|
+
payload: { method: 'DeclineGame', gameId: game.id },
|
|
119
|
+
willSend: { game: game.id, player: untrustedText(seat.name) },
|
|
120
|
+
preview: {
|
|
121
|
+
warning: `This gives up your seat in game ${game.id}. You can only come back through an open seat that is ` +
|
|
122
|
+
'still free, and war.app may remove the chat messages you sent to the game.' +
|
|
123
|
+
(teamGame ? ' It is a team game: leaving can leave your teammates a player short.' : ''),
|
|
124
|
+
account: shownAccount(seat.name, seat.id),
|
|
125
|
+
game: {
|
|
126
|
+
id: game.id,
|
|
127
|
+
name: game.name === undefined ? null : untrustedText(game.name),
|
|
128
|
+
state: game.state,
|
|
129
|
+
players: game.players.filter((p) => holdsSeat(p.state)).length,
|
|
130
|
+
},
|
|
131
|
+
untrusted: UNTRUSTED_PREVIEW_NOTE,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
state: { gameId: game.id, name: game.name ?? `game ${game.id}`, me: seat.id },
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
async execute({ gameId, name, me }) {
|
|
138
|
+
await requireApprovedAccount(ctx, writeAccount(me));
|
|
139
|
+
const game = { id: gameId, name };
|
|
140
|
+
let chatRemoved;
|
|
141
|
+
const extra = () => (chatRemoved ? { chatRemoved: true } : {});
|
|
142
|
+
const check = 'Check warapp_my_games (or warapp_get_game) before doing anything else.';
|
|
143
|
+
return sendAndVerify({
|
|
144
|
+
send: async () => {
|
|
145
|
+
const env = await ctx.rpc.call('DeclineGame', Method.DeclineGame, (w) => writeDeclineGameParams(w, gameId));
|
|
146
|
+
try {
|
|
147
|
+
chatRemoved = readDeclineGameReturn(env.body);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// An unreadable reply bool is only the chat note: the leave is read back below.
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
refused: (e) => {
|
|
154
|
+
if (READ_BACK_ERRORS.has(e.code))
|
|
155
|
+
return 'ignore';
|
|
156
|
+
const wording = LEAVE_ERRORS[e.code];
|
|
157
|
+
return wording ? new WarappServerError(e.code, `${wording} (war.app error ${e.code})`) : undefined;
|
|
158
|
+
},
|
|
159
|
+
unanswered: (failure) => ({
|
|
160
|
+
status: 'unknown',
|
|
161
|
+
outcome: 'unknown',
|
|
162
|
+
game,
|
|
163
|
+
warning: `war.app did not confirm the leave (${failure}), so you may already have left. ${check}`,
|
|
164
|
+
}),
|
|
165
|
+
// Never throws: nothing read is "not shown".
|
|
166
|
+
readBack: () => confirmLeft(ctx, gameId, me),
|
|
167
|
+
unreadable: () => ({ status: 'unconfirmed', outcome: 'unconfirmed', game, warning: check }),
|
|
168
|
+
classify: (left) => left
|
|
169
|
+
? { status: 'left', outcome: 'confirmed', game, ...extra() }
|
|
170
|
+
: {
|
|
171
|
+
status: 'unconfirmed',
|
|
172
|
+
outcome: 'unconfirmed',
|
|
173
|
+
game,
|
|
174
|
+
...extra(),
|
|
175
|
+
warning: 'war.app accepted the request, but GameFeed still shows you seated (or could not be read, and ' +
|
|
176
|
+
`your games list does not show the seat given up), so the leave could not be confirmed. ${check}`,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
}
|