-
Notifications
You must be signed in to change notification settings - Fork 7
/
gameAnalyzer.ts
executable file
·320 lines (304 loc) · 9.49 KB
/
gameAnalyzer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!./node_modules/.bin/ts-node
import assert from "assert";
import { Root } from "protobufjs";
import { readFileSync } from "fs";
import { lq } from "./majsoulPb";
import { calcShanten } from "./shanten";
import { Hai, HaiArr, hairi, syantenAll } from "syanten";
import { wrappedRun } from "./entryPoint";
type SuitTileType = "m" | "p" | "s";
type HonorTileType = "z";
type HonorTileNumber = "1" | "2" | "3" | "4" | "5" | "6" | "7";
type SuitTileNumber = HonorTileNumber | "8" | "9" | "0";
type Tile = `${HonorTileNumber}${HonorTileType}` | `${SuitTileNumber}${SuitTileType}`;
type OpenedMeld = {
hand: Tile[];
discard?: Tile;
};
const TILE_RE = /^([0-9][mps]|[1-7]z)$/;
const KITA = "4z";
function isValidTile(tile: string): tile is Tile {
return TILE_RE.test(tile);
}
function validateTile(tile: string): Tile {
if (!isValidTile(tile)) {
throw new Error(`Invalid tile: ${tile}`);
}
return tile;
}
function isEquivantTile(a: Tile, b: Tile): boolean {
if (a === b) {
return true;
}
if (a.charAt(1) !== b.charAt(1)) {
return false;
}
return ["0", "5"].includes(a.charAt(0)) && ["0", "5"].includes(b.charAt(0));
}
function tilesToHaiArr(tiles: Tile[]): HaiArr {
const ret: HaiArr = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
];
const INDEXER = "mpsz";
for (const tile of tiles) {
let n = parseInt(tile.charAt(0), 10);
const type = tile.charAt(1);
if (type === "z") {
assert(n >= 1 && n <= 7);
} else {
if (n === 0) {
n = 5;
}
assert(n >= 1 && n <= 9);
}
const typeIndex = INDEXER.indexOf(type);
assert(typeIndex >= 0);
ret[typeIndex][n - 1]++;
assert(ret[typeIndex][n - 1] <= 4);
}
return ret;
}
class TileBin {
_tiles: { [tile: string]: number };
constructor() {
this._tiles = {};
}
put(tile: Tile): void {
if (/^0[mps]$/.test(tile)) {
tile = ("5" + tile.charAt(1)) as Tile;
}
this._tiles[tile] = (this._tiles[tile] || 0) + 1;
assert(this._tiles[tile] <= 4);
}
getNum(tile: Tile): number {
if (/^0[mps]$/.test(tile)) {
tile = ("5" + tile.charAt(1)) as Tile;
}
return this._tiles[tile] || 0;
}
}
class Player {
_hand: Tile[];
_opened: OpenedMeld[];
_discarded: Tile[];
constructor(hand: Tile[]) {
assert(hand.length === 13 || hand.length === 14);
this._hand = hand;
this._opened = [];
this._discarded = [];
}
deal(tile: Tile): void {
assert.equal(this._hand.length % 3, 1);
this._hand.push(tile);
}
discard(tile: Tile): void {
assert.equal(this._hand.length % 3, 2);
const index = this._hand.indexOf(tile);
if (index === -1) {
throw new Error(`Not in hand: ${tile}`);
}
this._discarded.push(tile);
this._hand.splice(index, 1);
}
kita(): void {
const tile = KITA;
assert.equal(this._hand.length % 3, 2);
const index = this._hand.indexOf(tile);
if (index === -1) {
throw new Error(`Not in hand: ${tile}`);
}
this._opened.push({ hand: [tile] });
this._hand.splice(index, 1);
}
open(tile: Tile, handTiles: Tile[]): void {
assert.equal(this._hand.length % 3, 1);
assert(handTiles.length === 2 || handTiles.length === 3);
for (const handTile of handTiles) {
const index = this._hand.indexOf(handTile);
if (index === -1) {
throw new Error(`Not in hand: ${handTile}`);
}
this._hand.splice(index, 1);
}
this._opened.push({ hand: handTiles, discard: tile });
}
kan(tile: Tile) {
assert.equal(this._hand.length % 3, 2);
let meld = this._opened.find(
(x) =>
x.hand.length === 2 &&
x.discard &&
isEquivantTile(x.discard, tile) &&
x.hand.every((t) => isEquivantTile(t, tile))
);
if (meld) {
const index = this._hand.indexOf(tile);
if (index === -1) {
throw new Error(`Not in hand: ${tile}`);
}
meld.hand.push(tile);
this._hand.splice(index, 1);
} else {
const meld: Tile[] = [];
for (let i = 0; i < 4; i++) {
const index = this._hand.findIndex((x) => isEquivantTile(x, tile));
if (index === -1) {
throw new Error(`Not in hand: ${tile}`);
}
meld.push(this._hand[index]);
this._hand.splice(index, 1);
}
this._opened.push({ hand: meld });
}
}
syanten(): number {
return calcShanten(this._hand);
}
isKokushiTenpai(): boolean {
if (this._hand.length !== 13) {
return false;
}
const tiles = {} as { [key: string]: number };
for (const tile of this._hand) {
if (!/^([19][mps]|.z)$/.test(tile)) {
return false;
}
tiles[tile] = (tiles[tile] || 0) + 1;
if (tiles[tile] > 2) {
return false;
}
}
const entries = Object.entries(tiles);
return entries.filter(([, count]) => count === 2).length <= 1;
}
}
const ACCEPTED_RECORD_TYPES = {
".lq.RecordDealTile": new lq.RecordDealTile(),
".lq.RecordChiPengGang": new lq.RecordChiPengGang(),
".lq.RecordDiscardTile": new lq.RecordDiscardTile(),
".lq.RecordNoTile": new lq.RecordNoTile(),
".lq.RecordHule": new lq.RecordHule(),
".lq.RecordBaBei": new lq.RecordBaBei(),
".lq.RecordAnGangAddGang": new lq.RecordAnGangAddGang(),
".lq.RecordLiuJu": new lq.RecordLiuJu(),
};
export class MajsoulGameAnalyzer {
_players: Player[];
_pendingTile: Tile | undefined;
constructor(newRoundRecord: lq.RecordNewRound) {
assert([3, 4].includes(newRoundRecord.scores.length));
const { tiles0, tiles1, tiles2, tiles3 } = newRoundRecord;
const tiles = [tiles0, tiles1, tiles2, tiles3].slice(0, newRoundRecord.scores.length);
this._players = tiles.map((t) => new Player(t.map(validateTile)));
}
getRemainingNumTiles(seat: number, tiles: (Tile | string)[]): number {
const bin = new TileBin();
for (const player of this._players) {
player._discarded.forEach((x) => bin.put(x));
player._opened.forEach((x) => x.hand.forEach((t) => bin.put(t)));
}
this._players[seat]._hand.forEach((x) => bin.put(x));
let ret = 0;
for (const tile of tiles) {
ret += 4 - bin.getNum(validateTile(tile));
}
return ret;
}
processRecord<T extends keyof typeof ACCEPTED_RECORD_TYPES>(
recordName: keyof typeof ACCEPTED_RECORD_TYPES,
record: typeof ACCEPTED_RECORD_TYPES[T]
): void {
if (!(recordName in ACCEPTED_RECORD_TYPES)) {
throw new Error(`Unknown record: ${recordName}`);
}
switch (recordName) {
case ".lq.RecordDealTile": {
const r = record as lq.RecordDealTile;
this._players[r.seat].deal(validateTile(r.tile));
this._pendingTile = undefined;
break;
}
case ".lq.RecordDiscardTile": {
const r = record as lq.RecordDiscardTile;
const tile = validateTile(r.tile);
this._players[r.seat].discard(tile);
this._pendingTile = tile;
if (r.tingpais?.length) {
assert(this._players[r.seat].syanten() === 0 || this._players[r.seat].isKokushiTenpai());
}
if (r.is_liqi) {
assert(r.zhenting.length === this._players.length);
assert(
r.zhenting[r.seat] ===
this._players[r.seat]._discarded.some((x) =>
r.tingpais.some((t) => isEquivantTile(validateTile(t.tile!), validateTile(x)))
)
);
this.getRemainingNumTiles(
r.seat,
r.tingpais.map((x) => x.tile!)
);
}
break;
}
case ".lq.RecordChiPengGang": {
const r = record as lq.RecordChiPengGang;
const tiles = r.tiles.map(validateTile);
if (!this._pendingTile) {
throw new Error("No pending tile");
}
if (tiles.length < 3) {
throw new Error("Unexpected number of tiles: " + tiles.length);
}
const index = tiles.indexOf(this._pendingTile);
assert(index !== -1);
tiles.splice(index, 1);
this._players[r.seat].open(this._pendingTile, tiles);
this._pendingTile = undefined;
break;
}
case ".lq.RecordBaBei": {
const r = record as lq.RecordBaBei;
this._players[r.seat].kita();
this._pendingTile = KITA;
break;
}
case ".lq.RecordAnGangAddGang": {
const r = record as lq.RecordAnGangAddGang;
const tile = validateTile(r.tiles);
this._players[r.seat].kan(tile);
this._pendingTile = tile;
break;
}
}
}
}
if (require.main === module) {
wrappedRun(async () => {
console.log(process.argv[2]);
const root = Root.fromJSON(JSON.parse(readFileSync("majsoulPb.proto.json", "utf8")));
for (const file of process.argv.slice(2)) {
const wrappedData = lq.Wrapper.decode(readFileSync(file));
const type = root.lookupType(wrappedData.name);
const msg = type.decode(wrappedData.data) as lq.IGameDetailRecords;
let gameAnalyzer: MajsoulGameAnalyzer;
for (const actionData of msg?.actions || []) {
if (!actionData.result?.length) {
continue;
}
const wrappedResult = lq.Wrapper.decode(actionData.result);
const type = root.lookupType(wrappedResult.name);
const record = type.decode(wrappedResult.data);
if (wrappedResult.name === ".lq.RecordNewRound") {
gameAnalyzer = new MajsoulGameAnalyzer(record as unknown as lq.RecordNewRound);
} else {
assert(gameAnalyzer!);
gameAnalyzer!.processRecord(wrappedResult.name as any, record as any);
}
}
}
});
}