@chrischall/warapp-mcp 0.23.2 → 0.25.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 +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/dist/analysis/calibrate.js +54 -0
- package/dist/analysis/cards-in-effect.js +19 -3
- package/dist/analysis/coalition.js +6 -3
- package/dist/analysis/commitments.js +257 -40
- package/dist/analysis/pacts.js +285 -0
- package/dist/analysis/relations.js +10 -0
- package/dist/analysis/stress.js +8 -3
- package/dist/bundle.js +1478 -268
- package/dist/chat.js +73 -0
- package/dist/mod-alliances.js +14 -0
- package/dist/protocol/codec.js +4 -0
- package/dist/protocol/methods.js +29 -3
- package/dist/relations-store.js +83 -18
- package/dist/tools/calibration-harness.js +22 -6
- package/dist/tools/chat-advice.js +2 -2
- package/dist/tools/chat.js +419 -36
- package/dist/tools/check-plan.js +14 -4
- package/dist/tools/commitments.js +40 -8
- package/dist/tools/diplomacy.js +37 -5
- package/dist/tools/mod-alliances.js +13 -1
- package/dist/tools/relations.js +411 -33
- package/dist/tools/turn.js +53 -13
- package/dist/tools/uncommit.js +6 -6
- package/dist/version.js +1 -1
- package/manifest.json +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
package/dist/chat.js
CHANGED
|
@@ -17,6 +17,7 @@ import { asInt } from './scalars.js';
|
|
|
17
17
|
export const GAME_DETAILS_AND_CHAT = 'warlight.shared.GameDetailsAndChat';
|
|
18
18
|
export const GAME_MESSAGE = 'warlight.shared.GameMessage';
|
|
19
19
|
export const MESSAGE_REACTION = 'warlight.shared.MessageReaction';
|
|
20
|
+
export const GAME_GROUP = 'warlight.shared.GameGroup';
|
|
20
21
|
/**
|
|
21
22
|
* `warlight.shared.ReactionType`, one byte, by its value (#634): the client's reaction menu lists exactly these five
|
|
22
23
|
* (its labels "Thumbs up", "Thumbs down", "Heart", "Laugh", "Angry").
|
|
@@ -24,6 +25,14 @@ export const MESSAGE_REACTION = 'warlight.shared.MessageReaction';
|
|
|
24
25
|
export const REACTION_TYPES = ['thumbs-up', 'thumbs-down', 'heart', 'laugh', 'angry'];
|
|
25
26
|
/** GameDetailsAndChat's reactions (243): a dict of message id → that message's reactions, read by position. */
|
|
26
27
|
const GDC_REACTIONS = 5;
|
|
28
|
+
/** GameDetailsAndChat's game groups: a list of optional `GameGroup`, last. */
|
|
29
|
+
const GDC_GROUPS = 6;
|
|
30
|
+
/**
|
|
31
|
+
* GameGroup field positions in read order (docs/WEB-PROTOCOL.md, Chat): colour, creator, moderator, game id, the group
|
|
32
|
+
* id, metadata, name, then the members as text (a JSON array of in-game ids, the creator included).
|
|
33
|
+
*/
|
|
34
|
+
const GG = { creator: 1, gameId: 3, id: 4, name: 6, members: 7 };
|
|
35
|
+
const GAME_GROUP_FIELDS = 8;
|
|
27
36
|
/**
|
|
28
37
|
* GameMessage field positions in read order, and its whole field count (protocol 243: the shared images,
|
|
29
38
|
* an optional list of strings, at 5, and the reactions, an optional list of records, at 7).
|
|
@@ -195,6 +204,70 @@ function applyReactionsDict(lines, dict) {
|
|
|
195
204
|
setReactions(line, byId.get(line.id));
|
|
196
205
|
}
|
|
197
206
|
}
|
|
207
|
+
/** The members text of a GameGroup: a JSON array of in-game ids, else `null`. */
|
|
208
|
+
function groupMembers(text) {
|
|
209
|
+
if (typeof text !== 'string')
|
|
210
|
+
return null;
|
|
211
|
+
try {
|
|
212
|
+
const v = JSON.parse(text);
|
|
213
|
+
if (!Array.isArray(v))
|
|
214
|
+
return null;
|
|
215
|
+
const ids = v.map((x) => (typeof x === 'string' && /^-?\d+$/.test(x) ? Number(x) : x));
|
|
216
|
+
return ids.every(isInt) ? ids : null;
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The game's groups from a GameRefresh reply (the body at the result-present flag): `[]` when the list is there and
|
|
224
|
+
* empty, `undefined` when it is absent, there is no result, or the shape is not understood. Never throws.
|
|
225
|
+
*/
|
|
226
|
+
export function readGameGroups(bundle, body) {
|
|
227
|
+
try {
|
|
228
|
+
if (!body.bool())
|
|
229
|
+
return undefined;
|
|
230
|
+
const ret = bundle.readClass(GAME_DETAILS_AND_CHAT, body);
|
|
231
|
+
if (!isObj(ret) || ret.$type !== GAME_DETAILS_AND_CHAT)
|
|
232
|
+
return undefined;
|
|
233
|
+
const list = fields(ret)[GDC_GROUPS];
|
|
234
|
+
if (!Array.isArray(list))
|
|
235
|
+
return undefined;
|
|
236
|
+
const out = [];
|
|
237
|
+
for (const g of list) {
|
|
238
|
+
if (g === null)
|
|
239
|
+
continue;
|
|
240
|
+
if (!isObj(g) || g.$type !== GAME_GROUP)
|
|
241
|
+
return undefined;
|
|
242
|
+
const f = fields(g);
|
|
243
|
+
if (f.length !== GAME_GROUP_FIELDS)
|
|
244
|
+
return undefined;
|
|
245
|
+
const [id, gameId, creator, name] = [f[GG.id], f[GG.gameId], f[GG.creator], f[GG.name]];
|
|
246
|
+
if (!isInt(id) || !isInt(gameId) || !isInt(creator) || typeof name !== 'string')
|
|
247
|
+
return undefined;
|
|
248
|
+
out.push({ id, gameId, creator, name, members: groupMembers(f[GG.members]) });
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The CreateGameGroup reply: a present flag, then the group's first message, whose game-group field is the new group's
|
|
258
|
+
* id. `undefined` when there is no result or no group id in it. Never throws.
|
|
259
|
+
*/
|
|
260
|
+
export function readCreateGameGroupReply(bundle, body) {
|
|
261
|
+
try {
|
|
262
|
+
if (!body.bool())
|
|
263
|
+
return undefined;
|
|
264
|
+
const line = webLine(bundle.readClass(GAME_MESSAGE, body));
|
|
265
|
+
return line?.group;
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
198
271
|
/**
|
|
199
272
|
* The SendChat reply: the updated message list, read straight after the result-present flag (each
|
|
200
273
|
* message behind its own present flag), oldest first; `undefined` when it cannot be read. The replies of
|
package/dist/mod-alliances.js
CHANGED
|
@@ -184,6 +184,20 @@ export function myAllies(state, me) {
|
|
|
184
184
|
}
|
|
185
185
|
return [...out].sort((x, y) => x - y);
|
|
186
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* #680: the alliances between two other players (in-game ids, each pair ascending, once), by pair — a pact
|
|
189
|
+
* between them, report only. Never a pair with `me` in it, nor a player with itself.
|
|
190
|
+
*/
|
|
191
|
+
export function othersAlliances(state, me) {
|
|
192
|
+
const out = new Map();
|
|
193
|
+
for (const a of state.alliances) {
|
|
194
|
+
if (a.proposer === a.accepter || a.proposer === me || a.accepter === me)
|
|
195
|
+
continue;
|
|
196
|
+
const pair = [a.proposer, a.accepter].sort((x, y) => x - y);
|
|
197
|
+
out.set(pair.join(','), pair);
|
|
198
|
+
}
|
|
199
|
+
return [...out.values()].sort((x, y) => x[0] - y[0] || x[1] - y[1]);
|
|
200
|
+
}
|
|
187
201
|
/** The alerts that report a break of an alliance with me (the mod's own wording). */
|
|
188
202
|
export const breakAlerts = (state) => state.alerts.filter((a) => / has broken their alliance with you$/.test(a.message));
|
|
189
203
|
/** Whether a game's mods (GameFeed settings, `modIds`) include Diplomacy 2. */
|
package/dist/protocol/codec.js
CHANGED
|
@@ -225,6 +225,10 @@ export class Reader {
|
|
|
225
225
|
get remaining() {
|
|
226
226
|
return this.buf.length - this.pos;
|
|
227
227
|
}
|
|
228
|
+
/** A second reader over the same bytes from this position, so one reply can be read two ways. */
|
|
229
|
+
fork() {
|
|
230
|
+
return new Reader(this.buf, this.pos);
|
|
231
|
+
}
|
|
228
232
|
need(n) {
|
|
229
233
|
if (this.pos + n > this.buf.length) {
|
|
230
234
|
throw new ProtocolError('truncated', `read past end: need ${n} byte(s) at offset ${this.pos} of ${this.buf.length}`);
|
package/dist/protocol/methods.js
CHANGED
|
@@ -103,6 +103,8 @@ export const Method = {
|
|
|
103
103
|
ForumVote: 204,
|
|
104
104
|
WatchForumThread: 328,
|
|
105
105
|
GetGroupForumThreads: 354,
|
|
106
|
+
/** A game group: a private conversation of several players in one game (the bundle's writer; UNVERIFIED live). */
|
|
107
|
+
CreateGameGroup: 356,
|
|
106
108
|
Subscribe: 243,
|
|
107
109
|
/** #633: the client's "Game Invite Code" screen (`GameInviteCode` reply); read from the bundle, UNVERIFIED live. */
|
|
108
110
|
GetGameByInviteCode: 295,
|
|
@@ -486,6 +488,23 @@ function checkIds(ids, what) {
|
|
|
486
488
|
function writeOptionalIntList(w, ids) {
|
|
487
489
|
w.bool(true).list(ids, (ww, id) => ww.int32(id));
|
|
488
490
|
}
|
|
491
|
+
/** The colour a new game group gets: the client's default, "Normal". */
|
|
492
|
+
export const GAME_GROUP_COLOUR = 13811880;
|
|
493
|
+
/**
|
|
494
|
+
* CreateGameGroup(auth, …) — the client's "Create Game Group": the int32 game id, the name (string), the int32 colour
|
|
495
|
+
* ({@link GAME_GROUP_COLOUR}), then a present, counted list of the other members' int32 in-game ids. Reply: a present
|
|
496
|
+
* flag and the group's first `GameMessage`, whose game-group field is the new group's id. Read from the bundle's
|
|
497
|
+
* writer; UNVERIFIED live.
|
|
498
|
+
*/
|
|
499
|
+
export function writeCreateGameGroupParams(w, p) {
|
|
500
|
+
if (p.members.length === 0)
|
|
501
|
+
throw new RangeError('no members');
|
|
502
|
+
checkIds(p.members, 'player id');
|
|
503
|
+
if (p.name.trim() === '')
|
|
504
|
+
throw new RangeError('a game group needs a name');
|
|
505
|
+
w.int32(p.gameId).string(p.name).int32(GAME_GROUP_COLOUR);
|
|
506
|
+
writeOptionalIntList(w, p.members);
|
|
507
|
+
}
|
|
489
508
|
/**
|
|
490
509
|
* CreatePrivateDiscussion(auth, …) — the client's "Send mail": the subject (string), a present list (int32 count) of
|
|
491
510
|
* the recipients' int32 ids, the body (string), an optional list of shared-image strings (sent absent), then the
|
|
@@ -829,20 +848,27 @@ export function writeRetrievePrivateMessageHistoryParams(w, p) {
|
|
|
829
848
|
/**
|
|
830
849
|
* SendChat(auth, …) per docs/WEB-PROTOCOL.md (read from the client's
|
|
831
850
|
* serializer; the owner verifies it live): game id, text, channel, optional
|
|
832
|
-
* private recipient, optional game-group id
|
|
851
|
+
* private recipient, optional game-group id, two marker strings
|
|
833
852
|
* (`-1`: seen nothing), two optional "last seen" dicts (absent). An optional int32 is a
|
|
834
853
|
* present flag then the value.
|
|
835
854
|
*/
|
|
836
855
|
export function writeSendChatParams(w, p) {
|
|
856
|
+
if (p.group !== undefined && p.recipient !== undefined)
|
|
857
|
+
throw new RangeError('a message cannot go both to a group and privately');
|
|
837
858
|
w.int32(p.gameId).string(p.text).int32(p.channel ?? CHAT_CHANNEL_PUBLIC);
|
|
838
859
|
if (p.recipient === undefined)
|
|
839
860
|
w.bool(false);
|
|
840
861
|
else
|
|
841
862
|
w.bool(true).int32(p.recipient);
|
|
863
|
+
if (p.group === undefined)
|
|
864
|
+
w.bool(false);
|
|
865
|
+
else {
|
|
866
|
+
checkIds([p.group], 'game group id');
|
|
867
|
+
w.bool(true).int32(p.group);
|
|
868
|
+
}
|
|
842
869
|
// The two chat markers are the last message seen on each channel: war.app parses them as numbers
|
|
843
870
|
// (ParseLongSafe), so a null one is refused (server error 1); a client that has seen nothing sends `-1`.
|
|
844
|
-
w.
|
|
845
|
-
.string(NO_CHAT_MARKER)
|
|
871
|
+
w.string(NO_CHAT_MARKER)
|
|
846
872
|
.string(NO_CHAT_MARKER)
|
|
847
873
|
.bool(false)
|
|
848
874
|
.bool(false)
|
package/dist/relations-store.js
CHANGED
|
@@ -20,13 +20,19 @@
|
|
|
20
20
|
// `intel` (#386): avoid targets and stated incomes from diplomacy (warapp_record_intel), applied by
|
|
21
21
|
// warapp_plan_turn and warapp_check_plan by default.
|
|
22
22
|
//
|
|
23
|
+
// `pacts` (#680): agreements among three or more with me, or between other players (`parties`, ids `p<n>`, a
|
|
24
|
+
// `source` of ledger or reported, each party's notice), kept apart from `agreements` so that every reader of
|
|
25
|
+
// the two-party truces reads exactly what it did; `ledgerTruces` adds the pacts I am in as one pairwise
|
|
26
|
+
// agreement per partner for the planner, the submit refusal and the checks. Files written before it have none.
|
|
27
|
+
//
|
|
23
28
|
// `usage` (part B2): per planned turn, what each honoured truce freed and what a break would have
|
|
24
29
|
// cost (rule T6: the armies and the income); warapp_plan_turn calls `recordTruceUsage`.
|
|
25
30
|
import { randomUUID } from 'node:crypto';
|
|
26
31
|
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
27
32
|
import { join } from 'node:path';
|
|
28
33
|
import { withPathLock } from './path-lock.js';
|
|
29
|
-
import { AGREEMENT_STATUSES, EXCEPTION_DIRECTIONS, MAX_CAPS, MAX_EXCEPTION_NOTE, MAX_EXCEPTIONS, MAX_NOTICE_TURNS, } from './analysis/relations.js';
|
|
34
|
+
import { AGREEMENT_STATUSES, EXCEPTION_DIRECTIONS, MAX_CAPS, MAX_EXCEPTION_NOTE, MAX_EXCEPTIONS, MAX_NOTICE_TURNS, MAX_PARTIES, } from './analysis/relations.js';
|
|
35
|
+
import { trucesWithMe } from './analysis/pacts.js';
|
|
30
36
|
import { COMMITMENT_KINDS } from './analysis/commitments.js';
|
|
31
37
|
import { coordinationOf, MAX_COORDINATION } from './analysis/coordination.js';
|
|
32
38
|
import { AVOID_REASONS, intelIsEmpty } from './analysis/intel.js';
|
|
@@ -45,8 +51,10 @@ export const MAX_INTEL_PLAYERS = 50;
|
|
|
45
51
|
export const relationsDir = (dir) => join(dir, 'relations');
|
|
46
52
|
export const relationsPath = (dir, gameId, playerId) => join(relationsDir(dir), `${gameId}-${playerId}.json`);
|
|
47
53
|
const AGREEMENT_ID = /^a(\d{1,9})$/;
|
|
48
|
-
/**
|
|
49
|
-
const
|
|
54
|
+
/** #680: a pact's id. */
|
|
55
|
+
const PACT_ID = /^p(\d{1,9})$/;
|
|
56
|
+
/** An agreement or pact id's number (`a7` and `p7` are 7); ids are made by {@link nextAgreementId} and {@link nextPactId}. */
|
|
57
|
+
const idNumber = (id) => Number((AGREEMENT_ID.exec(id) ?? PACT_ID.exec(id))?.[1] ?? 0);
|
|
50
58
|
function scopeOf(v) {
|
|
51
59
|
if (v === 'all')
|
|
52
60
|
return 'all';
|
|
@@ -76,14 +84,44 @@ function termsOf(v) {
|
|
|
76
84
|
t.against = v.against;
|
|
77
85
|
return t;
|
|
78
86
|
}
|
|
79
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* An agreement read back, or `undefined` when it is unusable. A two-party agreement (`a<n>`) reads exactly as
|
|
89
|
+
* before #680: any `parties`, `source` or `notices` on it is ignored.
|
|
90
|
+
*/
|
|
80
91
|
export function agreementOf(v) {
|
|
81
|
-
|
|
92
|
+
return agreementLike(v, AGREEMENT_ID);
|
|
93
|
+
}
|
|
94
|
+
/** #680: a pact read back (`p<n>`, 2–{@link MAX_PARTIES} distinct parties), or `undefined` when it is unusable. */
|
|
95
|
+
export function pactOf(v) {
|
|
96
|
+
const a = agreementLike(v, PACT_ID);
|
|
97
|
+
if (!a || !isRec(v))
|
|
98
|
+
return undefined;
|
|
99
|
+
const parties = ints(v.parties);
|
|
100
|
+
if (!parties || parties.length < 2 || parties.length > MAX_PARTIES || new Set(parties).size !== parties.length)
|
|
101
|
+
return undefined;
|
|
102
|
+
a.parties = [...parties].sort((x, y) => x - y);
|
|
103
|
+
if (v.source === 'reported')
|
|
104
|
+
a.source = 'reported';
|
|
105
|
+
else if (v.source === 'ledger')
|
|
106
|
+
a.source = 'ledger';
|
|
107
|
+
const notices = Array.isArray(v.notices)
|
|
108
|
+
? v.notices.filter((n) => isRec(n) && isInt(n.player) && isInt(n.turn) && a.parties.includes(n.player))
|
|
109
|
+
: [];
|
|
110
|
+
const restOf = (r) => (r === 'continue' || r === 'end' ? { rest: r } : {});
|
|
111
|
+
if (notices.length)
|
|
112
|
+
a.notices = [...new Map(notices.map((n) => [n.player, { player: n.player, turn: n.turn, ...restOf(n.rest) }])).values()];
|
|
113
|
+
return a;
|
|
114
|
+
}
|
|
115
|
+
function agreementLike(v, idPattern) {
|
|
116
|
+
if (!isRec(v) || typeof v.id !== 'string' || !idPattern.test(v.id) || !isInt(v.with) || v.kind !== 'truce' || !isStatus(v.status) || !isInt(v.createdTurn))
|
|
82
117
|
return undefined;
|
|
83
118
|
const terms = termsOf(v.terms);
|
|
84
119
|
if (!terms || !Array.isArray(v.history))
|
|
85
120
|
return undefined;
|
|
86
|
-
const
|
|
121
|
+
const pact = idPattern === PACT_ID;
|
|
122
|
+
const history = v.history
|
|
123
|
+
.filter((h) => isRec(h) && isInt(h.turn) && isStatus(h.status))
|
|
124
|
+
.map((h) => (pact && isInt(h.by) ? { turn: h.turn, status: h.status, by: h.by } : { turn: h.turn, status: h.status }));
|
|
87
125
|
const a = { id: v.id, with: v.with, kind: 'truce', status: v.status, createdTurn: v.createdTurn, terms, history };
|
|
88
126
|
if (isInt(v.agreedTurn))
|
|
89
127
|
a.agreedTurn = v.agreedTurn;
|
|
@@ -134,6 +172,11 @@ function exceptionOf(v) {
|
|
|
134
172
|
return e;
|
|
135
173
|
}
|
|
136
174
|
const COMMITMENT_ID = /^c(\d{1,9})$/;
|
|
175
|
+
/** #685: a pact commitment's other parties (2+ distinct ids, ascending); nothing when absent or unusable. */
|
|
176
|
+
function partiesOf(v) {
|
|
177
|
+
const ids = ints(v);
|
|
178
|
+
return ids && ids.length >= 2 && ids.length < MAX_PARTIES && new Set(ids).size === ids.length ? { parties: [...ids].sort((x, y) => x - y) } : {};
|
|
179
|
+
}
|
|
137
180
|
/** A commitment read back, or `undefined` when it is unusable. */
|
|
138
181
|
export function commitmentOf(v) {
|
|
139
182
|
if (!isRec(v) || typeof v.id !== 'string' || !COMMITMENT_ID.test(v.id) || !isInt(v.sentTurn))
|
|
@@ -155,11 +198,11 @@ export function commitmentOf(v) {
|
|
|
155
198
|
case 'notice':
|
|
156
199
|
if (typeof v.agreement !== 'string' || !isInt(v.with) || !isInt(v.endsTurn))
|
|
157
200
|
return undefined;
|
|
158
|
-
return { ...base, kind: 'notice', agreement: v.agreement, with: v.with, endsTurn: v.endsTurn };
|
|
201
|
+
return { ...base, kind: 'notice', agreement: v.agreement, with: v.with, ...partiesOf(v.parties), endsTurn: v.endsTurn };
|
|
159
202
|
default:
|
|
160
203
|
if (typeof v.agreement !== 'string' || !isInt(v.with))
|
|
161
204
|
return undefined;
|
|
162
|
-
return { ...base, kind: v.kind, agreement: v.agreement, with: v.with };
|
|
205
|
+
return { ...base, kind: v.kind, agreement: v.agreement, with: v.with, ...partiesOf(v.parties) };
|
|
163
206
|
}
|
|
164
207
|
}
|
|
165
208
|
/** The commitments of a ledger file, the unusable dropped; absent when none. */
|
|
@@ -169,6 +212,13 @@ function commitmentsOf(v) {
|
|
|
169
212
|
const list = v.map(commitmentOf).filter((c) => c !== undefined);
|
|
170
213
|
return list.length ? { commitments: list } : {};
|
|
171
214
|
}
|
|
215
|
+
/** #680: the pacts of a ledger file, the unusable dropped; absent when none. */
|
|
216
|
+
function pactsOf(v) {
|
|
217
|
+
if (!Array.isArray(v))
|
|
218
|
+
return {};
|
|
219
|
+
const list = v.map(pactOf).filter((a) => a !== undefined);
|
|
220
|
+
return list.length ? { pacts: list } : {};
|
|
221
|
+
}
|
|
172
222
|
/** The next commitment id in `ledger`: `c1`, `c2`, … */
|
|
173
223
|
export function nextCommitmentId(ledger) {
|
|
174
224
|
const n = (ledger.commitments ?? []).reduce((m, c) => Math.max(m, Number(COMMITMENT_ID.exec(c.id)?.[1] ?? 0)), 0);
|
|
@@ -230,7 +280,7 @@ export async function readLedger(dir, gameId, playerId) {
|
|
|
230
280
|
if (!isRec(c) || c.gameId !== gameId || c.playerId !== playerId || !Array.isArray(c.agreements))
|
|
231
281
|
return undefined;
|
|
232
282
|
const agreements = c.agreements.map(agreementOf).filter((a) => a !== undefined);
|
|
233
|
-
return { gameId, playerId, agreements, usage: usageOf(c.usage), ...commitmentsOf(c.commitments), ...intelOf(c.intel), ...coordinationsOf(c.coordination) };
|
|
283
|
+
return { gameId, playerId, agreements, ...pactsOf(c.pacts), usage: usageOf(c.usage), ...commitmentsOf(c.commitments), ...intelOf(c.intel), ...coordinationsOf(c.coordination) };
|
|
234
284
|
}
|
|
235
285
|
catch {
|
|
236
286
|
return undefined;
|
|
@@ -254,7 +304,7 @@ export async function readLedgerForWrite(dir, gameId, playerId) {
|
|
|
254
304
|
if (!isRec(c) || c.gameId !== gameId || c.playerId !== playerId || !Array.isArray(c.agreements))
|
|
255
305
|
return 'unreadable';
|
|
256
306
|
const agreements = c.agreements.map(agreementOf).filter((a) => a !== undefined);
|
|
257
|
-
return { gameId, playerId, agreements, usage: usageOf(c.usage), ...commitmentsOf(c.commitments), ...intelOf(c.intel), ...coordinationsOf(c.coordination) };
|
|
307
|
+
return { gameId, playerId, agreements, ...pactsOf(c.pacts), usage: usageOf(c.usage), ...commitmentsOf(c.commitments), ...intelOf(c.intel), ...coordinationsOf(c.coordination) };
|
|
258
308
|
}
|
|
259
309
|
catch {
|
|
260
310
|
return 'unreadable';
|
|
@@ -265,19 +315,24 @@ export async function readLedgerForWrite(dir, gameId, playerId) {
|
|
|
265
315
|
* dropped oldest first), and at most {@link MAX_USAGE_TURNS} usage turns.
|
|
266
316
|
*/
|
|
267
317
|
export function boundLedger(ledger) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
318
|
+
const bounded = (list) => {
|
|
319
|
+
let out = [...list].sort((a, b) => idNumber(a.id) - idNumber(b.id) || (a.id < b.id ? -1 : 1));
|
|
320
|
+
if (out.length > MAX_AGREEMENTS) {
|
|
321
|
+
const over = out.filter((a) => a.endedTurn !== undefined).sort((a, b) => a.endedTurn - b.endedTurn || (a.id < b.id ? -1 : 1));
|
|
322
|
+
const drop = new Set(over.slice(0, out.length - MAX_AGREEMENTS).map((a) => a.id));
|
|
323
|
+
out = out.filter((a) => !drop.has(a.id)).slice(-MAX_AGREEMENTS);
|
|
324
|
+
}
|
|
325
|
+
return out;
|
|
326
|
+
};
|
|
327
|
+
const agreements = bounded(ledger.agreements);
|
|
328
|
+
const pacts = ledger.pacts?.length ? { pacts: bounded(ledger.pacts) } : {};
|
|
274
329
|
const turns = Object.keys(ledger.usage)
|
|
275
330
|
.map(Number)
|
|
276
331
|
.sort((a, b) => b - a)
|
|
277
332
|
.slice(0, MAX_USAGE_TURNS)
|
|
278
333
|
.sort((a, b) => a - b);
|
|
279
334
|
const commitments = ledger.commitments?.length ? { commitments: ledger.commitments.slice(-MAX_COMMITMENTS) } : {};
|
|
280
|
-
const { commitments: _all, intel: _intel, coordination: _coord, ...rest } = ledger;
|
|
335
|
+
const { commitments: _all, intel: _intel, coordination: _coord, pacts: _pacts, ...rest } = ledger;
|
|
281
336
|
const coordination = ledger.coordination?.length ? { coordination: ledger.coordination.slice(-MAX_COORDINATION) } : {};
|
|
282
337
|
const intel = intelIsEmpty(ledger.intel)
|
|
283
338
|
? {}
|
|
@@ -288,7 +343,7 @@ export function boundLedger(ledger) {
|
|
|
288
343
|
incomes: ledger.intel.incomes.slice(-MAX_INTEL_PLAYERS),
|
|
289
344
|
},
|
|
290
345
|
};
|
|
291
|
-
return { ...rest, agreements, usage: Object.fromEntries(turns.map((t) => [String(t), ledger.usage[String(t)]])), ...commitments, ...intel, ...coordination };
|
|
346
|
+
return { ...rest, agreements, ...pacts, usage: Object.fromEntries(turns.map((t) => [String(t), ledger.usage[String(t)]])), ...commitments, ...intel, ...coordination };
|
|
292
347
|
}
|
|
293
348
|
/** Write the ledger (bounded), atomically, 0600. `true` when written; never throws. */
|
|
294
349
|
export async function writeLedger(dir, ledger) {
|
|
@@ -309,6 +364,16 @@ export function nextAgreementId(ledger) {
|
|
|
309
364
|
const n = ledger.agreements.reduce((m, a) => Math.max(m, idNumber(a.id)), 0);
|
|
310
365
|
return `a${n + 1}`;
|
|
311
366
|
}
|
|
367
|
+
/** #680: the next pact id in `ledger`: `p1`, `p2`, … */
|
|
368
|
+
export function nextPactId(ledger) {
|
|
369
|
+
const n = (ledger.pacts ?? []).reduce((m, a) => Math.max(m, idNumber(a.id)), 0);
|
|
370
|
+
return `p${n + 1}`;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* #680: every truce with me in `ledger` as the planner, the submit refusal and the checks read it: the two-party
|
|
374
|
+
* agreements, then each pact I am a party to as one pairwise agreement per partner (`trucesWithMe`). `[]` without a ledger.
|
|
375
|
+
*/
|
|
376
|
+
export const ledgerTruces = (ledger) => ledger ? trucesWithMe(ledger.agreements, ledger.pacts, ledger.playerId) : [];
|
|
312
377
|
/**
|
|
313
378
|
* Part B2: record what each truce freed and would cost on `turn`. Writes only when a ledger
|
|
314
379
|
* exists for the game. `true` when written; never throws.
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { profileFormatOf } from '../analysis/opponent-profile.js';
|
|
7
7
|
import { trackGame } from '../analysis/opponents.js';
|
|
8
8
|
import { applyBonusOverrides } from '../analysis/overrides.js';
|
|
9
|
-
import { aggregateBenchmarks, aggregateEstimates, benchmarkByEra, estimateSamples, openingBenchmark, openingEra, openingFlags, ratioSummary, } from '../analysis/calibrate.js';
|
|
9
|
+
import { aggregateBenchmarks, aggregateEstimates, benchmarkByEra, earlyLossesByEra, estimateSamples, openingBenchmark, openingEra, openingFlags, openingLosses, ratioSummary, } from '../analysis/calibrate.js';
|
|
10
10
|
import { addCalibration } from '../analysis/game-lessons.js';
|
|
11
11
|
import { calibrationSources } from '../calibration-source.js';
|
|
12
12
|
import { holdsSeat } from '../player-states.js';
|
|
@@ -32,6 +32,7 @@ export async function calibrationHarness(dir, opts = {}) {
|
|
|
32
32
|
const byFormat = {};
|
|
33
33
|
const benchByFormat = {};
|
|
34
34
|
const eraEntries = [];
|
|
35
|
+
const lossEntries = [];
|
|
35
36
|
let done = 0;
|
|
36
37
|
for (const input of src.inputs) {
|
|
37
38
|
opts.signal?.throwIfAborted();
|
|
@@ -76,13 +77,28 @@ export async function calibrationHarness(dir, opts = {}) {
|
|
|
76
77
|
winners: seated.filter((p) => p.state === 'Won').map((p) => p.id),
|
|
77
78
|
...(opts.turns ? { turns: opts.turns } : {}),
|
|
78
79
|
});
|
|
80
|
+
// #503: the era (turn-1 time where known — no record keeps one yet — else the game id).
|
|
81
|
+
const o = input.opening;
|
|
82
|
+
const { era, basis } = openingEra({ planned: o.planned, ...(o.gameId !== undefined ? { gameId: o.gameId } : {}) });
|
|
83
|
+
// #506: land lost to a player in the opening, mine against the winner's (a teammate's capture is no loss).
|
|
84
|
+
const allies = (id) => {
|
|
85
|
+
const team = seated.find((p) => p.id === id)?.team;
|
|
86
|
+
return typeof team === 'number' && team >= 0 ? seated.filter((p) => p.id !== id && p.team === team).map((p) => p.id) : [];
|
|
87
|
+
};
|
|
88
|
+
const won = seated.filter((p) => p.state === 'Won').map((p) => p.id);
|
|
89
|
+
const iWon = won.includes(me);
|
|
90
|
+
const winner = iWon ? me : won[0];
|
|
91
|
+
lossEntries.push({
|
|
92
|
+
era,
|
|
93
|
+
format,
|
|
94
|
+
mine: openingLosses({ player: me, history: h.boards, allies: allies(me) }),
|
|
95
|
+
winner: winner === undefined ? null : openingLosses({ player: winner, history: h.boards, allies: allies(winner) }),
|
|
96
|
+
iWon,
|
|
97
|
+
});
|
|
79
98
|
if (bench.turns.length) {
|
|
80
99
|
counts.benchmarked++;
|
|
81
100
|
(benchByFormat[format] ??= []).push(bench);
|
|
82
|
-
// #503: the
|
|
83
|
-
// opening window's flags (turns with no orders of mine, a boot of my seat).
|
|
84
|
-
const o = input.opening;
|
|
85
|
-
const { era, basis } = openingEra({ planned: o.planned, ...(o.gameId !== undefined ? { gameId: o.gameId } : {}) });
|
|
101
|
+
// #503: the opening window's flags (turns with no orders of mine, a boot of my seat).
|
|
86
102
|
const flags = openingFlags({
|
|
87
103
|
me,
|
|
88
104
|
history: h.boards,
|
|
@@ -105,5 +121,5 @@ export async function calibrationHarness(dir, opts = {}) {
|
|
|
105
121
|
for (const [format, list] of Object.entries(benchByFormat))
|
|
106
122
|
benchmark[format] = aggregateBenchmarks(list);
|
|
107
123
|
benchmark.all = aggregateBenchmarks(Object.values(benchByFormat).flat());
|
|
108
|
-
return { counts, calibration, benchmark, byEra: benchmarkByEra(eraEntries) };
|
|
124
|
+
return { counts, calibration, benchmark, byEra: benchmarkByEra(eraEntries), earlyLosses: earlyLossesByEra(lossEntries) };
|
|
109
125
|
}
|
|
@@ -11,7 +11,7 @@ import { chatBlocks } from '../analysis/chat-blocks.js';
|
|
|
11
11
|
import { playersByInGameId } from '../live.js';
|
|
12
12
|
import { carriesTeam } from '../teams.js';
|
|
13
13
|
import { readOutlookLog } from '../outlook-log.js';
|
|
14
|
-
import { readLedger } from '../relations-store.js';
|
|
14
|
+
import { ledgerTruces, readLedger } from '../relations-store.js';
|
|
15
15
|
import { shown } from './chat.js';
|
|
16
16
|
import { currentTurn } from './common.js';
|
|
17
17
|
/** What every view carrying `intent` / `advice` says beside them. */
|
|
@@ -91,7 +91,7 @@ export async function chatAdviceContext(ctx, game, me) {
|
|
|
91
91
|
try {
|
|
92
92
|
const [log, ledger] = await Promise.all([readOutlookLog(ctx.dataDir, game.id, meId), readLedger(ctx.dataDir, game.id, meId)]);
|
|
93
93
|
const standing = standingFromOutlook(log);
|
|
94
|
-
const truces = chatTruces(ledger
|
|
94
|
+
const truces = chatTruces(ledger ? ledgerTruces(ledger) : undefined, currentTurn(game), game.players);
|
|
95
95
|
return { ...base, ...(standing ? { standing } : {}), ...(truces.size ? { truces } : {}) };
|
|
96
96
|
}
|
|
97
97
|
catch {
|