@pkmn/stats 0.0.1 → 0.1.1

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.
Files changed (77) hide show
  1. package/CHANGES.md +1 -1
  2. package/README.md +11 -7
  3. package/build/aliases.json +1 -0
  4. package/build/binary.d.ts +138 -0
  5. package/build/binary.js +386 -0
  6. package/build/binary.js.map +1 -0
  7. package/build/classifier.d.ts +2 -2
  8. package/build/classifier.js +48 -42
  9. package/build/classifier.js.map +1 -1
  10. package/build/data.d.ts +39 -0
  11. package/build/data.js +126 -0
  12. package/build/data.js.map +1 -0
  13. package/build/display.d.ts +1 -1
  14. package/build/display.js +24 -14
  15. package/build/display.js.map +1 -1
  16. package/build/index.d.ts +1 -0
  17. package/build/index.js +3 -1
  18. package/build/index.js.map +1 -1
  19. package/build/parser.d.ts +2 -2
  20. package/build/parser.js +59 -41
  21. package/build/parser.js.map +1 -1
  22. package/build/pkmn/data.d.ts +39 -0
  23. package/build/pkmn/data.js +126 -0
  24. package/build/pkmn/data.js.map +1 -0
  25. package/build/pkmn/index.d.ts +1 -0
  26. package/build/pkmn/index.js +18 -0
  27. package/build/pkmn/index.js.map +1 -0
  28. package/build/reports.d.ts +12 -3
  29. package/build/reports.js +30 -23
  30. package/build/reports.js.map +1 -1
  31. package/build/smogon/aliases.json +227 -0
  32. package/build/smogon/classifier.d.ts +12 -0
  33. package/build/smogon/classifier.js +556 -0
  34. package/build/smogon/classifier.js.map +1 -0
  35. package/build/smogon/display.d.ts +96 -0
  36. package/build/smogon/display.js +412 -0
  37. package/build/smogon/display.js.map +1 -0
  38. package/build/smogon/index.d.ts +6 -0
  39. package/build/smogon/index.js +19 -0
  40. package/build/smogon/index.js.map +1 -0
  41. package/build/smogon/parser.d.ts +57 -0
  42. package/build/smogon/parser.js +358 -0
  43. package/build/smogon/parser.js.map +1 -0
  44. package/build/smogon/reports.d.ts +51 -0
  45. package/build/smogon/reports.js +652 -0
  46. package/build/smogon/reports.js.map +1 -0
  47. package/build/smogon/stats.d.ts +119 -0
  48. package/build/smogon/stats.js +521 -0
  49. package/build/smogon/stats.js.map +1 -0
  50. package/build/smogon/test/integration.d.ts +30 -0
  51. package/build/smogon/test/integration.js +179 -0
  52. package/build/smogon/test/integration.js.map +1 -0
  53. package/build/smogon/test/integration.test.d.ts +1 -0
  54. package/build/smogon/test/integration.test.js +38 -0
  55. package/build/smogon/test/integration.test.js.map +1 -0
  56. package/build/smogon/test/parser.test.d.ts +1 -0
  57. package/build/smogon/test/parser.test.js +91 -0
  58. package/build/smogon/test/parser.test.js.map +1 -0
  59. package/build/smogon/util.d.ts +63 -0
  60. package/build/smogon/util.js +400 -0
  61. package/build/smogon/util.js.map +1 -0
  62. package/build/stats.d.ts +3 -3
  63. package/build/stats.js +34 -34
  64. package/build/stats.js.map +1 -1
  65. package/build/util.d.ts +7 -7
  66. package/build/util.js +45 -34
  67. package/build/util.js.map +1 -1
  68. package/package.json +11 -14
  69. package/src/aliases.json +1 -0
  70. package/src/binary.ts +514 -0
  71. package/src/classifier.ts +38 -37
  72. package/src/display.ts +12 -14
  73. package/src/index.ts +6 -17
  74. package/src/parser.ts +39 -22
  75. package/src/reports.ts +22 -21
  76. package/src/stats.ts +27 -24
  77. package/src/util.ts +40 -39
package/src/binary.ts ADDED
@@ -0,0 +1,514 @@
1
+ import {Generation, toID, ID, PokemonSet, TypeName, Dex, StatsTable} from '@pkmn/data';
2
+
3
+ import * as parser from './parser';
4
+ import {weighting} from './util';
5
+
6
+ const enum EndType {
7
+ NORMAL = 0,
8
+ TIE = 1,
9
+ FORFEIT = 2,
10
+ FORCED_WIN = 3,
11
+ FORCED_TIE = 4,
12
+ }
13
+
14
+ const LE = (() => {
15
+ const u8 = new Uint8Array(4);
16
+ const u16 = new Uint16Array(u8.buffer);
17
+ return !!((u16[0] = 1) & u16[0]);
18
+ })();
19
+
20
+ const Read = new class {
21
+ u8(this: void, buf: Buffer, offset: number) {
22
+ return buf.readUInt8(offset);
23
+ }
24
+
25
+ u16(this: void, buf: Buffer, offset: number) {
26
+ return LE ? buf.readUInt16LE(offset) : buf.readUInt16BE(offset);
27
+ }
28
+
29
+ u32(this: void, buf: Buffer, offset: number) {
30
+ return LE ? buf.readUInt32LE(offset) : buf.readUInt32BE(offset);
31
+ }
32
+
33
+ u64(this: void, buf: Buffer, offset: number) {
34
+ return LE ? buf.readBigInt64LE(offset) : buf.readBigInt64BE(offset);
35
+ }
36
+ };
37
+
38
+ const Write = new class {
39
+ u8(this: void, buf: Buffer, value: number, offset: number) {
40
+ return buf.writeUInt8(offset);
41
+ }
42
+
43
+ u16(this: void, buf: Buffer, value: number, offset: number) {
44
+ return LE ? buf.writeUInt16LE(value, offset) : buf.writeUInt16BE(value, offset);
45
+ }
46
+
47
+ u32(this: void, buf: Buffer, value: number, offset: number) {
48
+ return LE ? buf.writeUInt32LE(value, offset) : buf.writeUInt32BE(value, offset);
49
+ }
50
+
51
+ u64(this: void, buf: Buffer, value: number, offset: number) {
52
+ return LE ? buf.writeBigInt64LE(BigInt(value), offset)
53
+ : buf.writeBigInt64BE(BigInt(value), offset);
54
+ }
55
+ };
56
+
57
+ const Sizes = {1: 5, 2: 7, 3: 24, 4: 24, 5: 24, 6: 25, 7: 26, 8: 25} as const;
58
+
59
+ const Team = new class {
60
+ encode(
61
+ gen: Generation,
62
+ lookup: Binary.Lookup,
63
+ team: Partial<PokemonSet>[],
64
+ buf: Buffer,
65
+ offset = 0
66
+ ) {
67
+ switch (gen.num) {
68
+ case 1: {
69
+ for (const set of team) {
70
+ Write.u8(buf, lookup.speciesByID(set.species as ID), offset++);
71
+ for (let i = 0; i < 4; i++) {
72
+ Write.u8(buf, i < set.moves!.length ? lookup.moveByID(set.moves![i] as ID) : 0, offset++);
73
+ }
74
+ }
75
+ return offset;
76
+ }
77
+ case 2: {
78
+ for (const set of team) {
79
+ Write.u8(buf, lookup.speciesByID(set.species as ID), offset++);
80
+ Write.u8(buf, set.item ? lookup.itemByID(set.item as ID) : 0, offset++);
81
+ let type: TypeName = 'Normal';
82
+ for (let i = 0; i < 4; i++) {
83
+ let move = set.moves![i];
84
+ if (set.moves![i].startsWith('hiddenpower')) {
85
+ move = 'hiddenpower';
86
+ type = (move.charAt(11).toUpperCase() + move.slice(12)) as TypeName;
87
+ }
88
+ Write.u8(buf, i < set.moves!.length ? lookup.moveByID(move as ID) : 0, offset++);
89
+ }
90
+ Write.u8(buf, lookup.typeByName(type), offset++);
91
+ }
92
+ return offset;
93
+ }
94
+ default: throw new Error(`Unsupported gen ${gen.num}}`);
95
+ }
96
+ }
97
+
98
+ decode(gen: Generation, lookup: Binary.Lookup, buf: Buffer, offset = 0) {
99
+ const team: Partial<PokemonSet>[] = [];
100
+ const N = Sizes[gen.num as keyof typeof Sizes];
101
+
102
+ let byte = 0;
103
+ switch (gen.num) {
104
+ case 1: {
105
+ for (let i = offset; i < offset + 6 * N; i += N) {
106
+ byte = Read.u8(buf, i);
107
+ if (!byte) return team;
108
+ const set: Partial<PokemonSet> = {};
109
+ set.species = lookup.speciesByNum(byte);
110
+ set.moves = [];
111
+ for (let j = 0; j < 4; j++) {
112
+ byte = Read.u8(buf, i + 1 + j);
113
+ if (!byte) break;
114
+ set.moves.push(lookup.moveByNum(byte));
115
+ }
116
+ team.push(set);
117
+ }
118
+ return team;
119
+ }
120
+ case 2: {
121
+ for (let i = offset; i < offset + 6 * N; i += N) {
122
+ byte = Read.u8(buf, i);
123
+ if (!byte) return team;
124
+ const set: Partial<PokemonSet> = {};
125
+ set.species = lookup.speciesByNum(byte);
126
+ byte = Read.u8(buf, i + 1);
127
+ set.item = byte ? lookup.itemByNum(byte) : undefined;
128
+ set.moves = [];
129
+ for (let j = 0; j < 4; j++) {
130
+ byte = Read.u8(buf, i + 2 + j);
131
+ if (!byte) break;
132
+ const move = lookup.moveByNum(byte);
133
+ set.moves.push(move === 'hiddenpower'
134
+ ? `${move}${toID(lookup.typeByNum(Read.u8(buf, i + 6)))}`
135
+ : move);
136
+ }
137
+ team.push(set);
138
+ }
139
+ return team;
140
+ }
141
+ default: throw new Error(`Unsupported gen ${gen.num}`);
142
+ }
143
+ }
144
+ };
145
+
146
+ const Log = new class {
147
+ encode(
148
+ gen: Generation,
149
+ lookup: Binary.Lookup,
150
+ canonicalize: (team: Partial<PokemonSet>[], dex: Dex) => Partial<PokemonSet>[],
151
+ log: parser.Log,
152
+ buf: Buffer,
153
+ offset = 0
154
+ ) {
155
+ if (gen.num >= 3) throw new Error(`Unsupported gen ${gen.num}`); // TODO
156
+
157
+ Write.u64(buf, new Date(log.timestamp).getTime(), offset);
158
+ Write.u16(buf, log.turns, offset + 8);
159
+
160
+ const winner: 'p1' | 'p2' = log.winner === log.p2 ? 'p2' : 'p1';
161
+ const loser: 'p1' | 'p2' = winner === 'p1' ? 'p2' : 'p1';
162
+ let endType: typeof EndType[keyof typeof EndType] = EndType.NORMAL;
163
+ if (!log.winner || log.winner === 'tie') {
164
+ endType = EndType.TIE;
165
+ }
166
+ if (log.endType === 'forced') {
167
+ endType = endType === EndType.NORMAL ? EndType.FORCED_WIN : EndType.FORCED_TIE;
168
+ } else if (log.endType === 'forfeit') {
169
+ endType = EndType.FORFEIT;
170
+ }
171
+ Write.u8(buf, endType, offset + 10);
172
+
173
+ if (log[`${winner}rating`]) {
174
+ Write.u16(buf, Math.round(log[`${winner}rating`]!.rpr), offset + 11);
175
+ Write.u8(buf, Math.round(log[`${winner}rating`]!.rprd), offset + 13);
176
+ }
177
+ if (log[`${loser}rating`]) {
178
+ Write.u16(buf, Math.round(log[`${loser}rating`]!.rpr), offset + 14);
179
+ Write.u8(buf, Math.round(log[`${loser}rating`]!.rprd), offset + 16);
180
+ }
181
+
182
+ const N = 6 * Sizes[gen.num as keyof typeof Sizes];
183
+ Team.encode(gen, lookup, canonicalize(log[`${winner}team`], gen.dex), buf, offset + 17);
184
+ Team.encode(gen, lookup, canonicalize(log[`${loser}team`], gen.dex), buf, offset + 17 + N);
185
+
186
+ return buf;
187
+ }
188
+
189
+ decode(gen: Generation, lookup: Binary.Lookup, buf: Buffer, offset = 0) {
190
+ if (gen.num >= 3) throw new Error(`Unsupported gen ${gen.num}`); // TODO
191
+
192
+ const N = 6 * Sizes[gen.num as keyof typeof Sizes];
193
+
194
+ const data: Binary.Data = {
195
+ timestamp: BigInt(0),
196
+ turns: 0,
197
+ endType: EndType.NORMAL,
198
+ winner: {
199
+ team: undefined!,
200
+ rating: undefined,
201
+ },
202
+ loser: {
203
+ team: undefined!,
204
+ rating: undefined,
205
+ },
206
+ };
207
+
208
+ data.timestamp = Read.u64(buf, offset);
209
+ data.turns = Read.u16(buf, offset + 8);
210
+ data.endType = Read.u8(buf, offset + 10);
211
+ let byte = Read.u16(buf, offset + 11);
212
+ if (byte) data.winner.rating = {rpr: byte, rprd: Read.u8(buf, offset + 13)};
213
+ byte = Read.u16(buf, offset + 14);
214
+ if (byte) data.loser.rating = {rpr: byte, rprd: Read.u8(buf, offset + 16)};
215
+
216
+ data.winner.team = Team.decode(gen, lookup, buf, offset + 17);
217
+ data.loser.team = Team.decode(gen, lookup, buf, offset + 17 + N);
218
+
219
+ return data;
220
+ }
221
+ };
222
+
223
+ const HP_TYPE_TO_NUM = {
224
+ fighting: 0, flying: 1, poison: 2, ground: 3, rock: 4, bug: 5, ghost: 6, steel: 7,
225
+ fire: 8, water: 9, grass: 10, electric: 11, psychic: 12, ice: 13, dragon: 14, dark: 15,
226
+ } as const;
227
+ const NUM_TO_HP_TYPE = Object.values(HP_TYPE_TO_NUM);
228
+
229
+ const Stats = new class {
230
+ sizes(gen: Generation, lookup: Binary.Lookup, options?: {moves?: number; items?: number}) {
231
+ const species = lookup.sizes.species;
232
+ const moves = lookup.sizes.moves + (gen.num < 2 ? 0 : 16);
233
+ const items = gen.num < 2 ? 0 : lookup.sizes.items + 1;
234
+ const stats = options
235
+ ? (species * 2) + (species * 2) +
236
+ (species * options.moves! * 3) +
237
+ (gen.num >= 2 ? species * options.items! * 3 : 0)
238
+ // (species * species * 2);
239
+ : -1;
240
+ return {species, moves, items, stats};
241
+ }
242
+
243
+ compute(gen: Generation, lookup: Binary.Lookup, db: Buffer, options: {cutoff: number}) {
244
+ const sizes = this.sizes(gen, lookup);
245
+ const stats: Binary.Statistics = {
246
+ total: {lead: 0, usage: 0},
247
+ species: new Array(sizes.species),
248
+ species_lead: new Array(sizes.species),
249
+ move_species: new Array(sizes.species),
250
+ item_species: new Array(sizes.species),
251
+ species_species: new Array(sizes.species),
252
+ move_move: new Array(sizes.moves),
253
+ };
254
+
255
+ for (let i = 0; i < sizes.species; i++) {
256
+ stats.species[i] = 0;
257
+ stats.species_lead[i] = 0;
258
+ stats.move_species[i] = {};
259
+ stats.item_species[i] = {};
260
+ stats.species_species[i] = new Array(sizes.species);
261
+ for (let j = 0; j < sizes.species; j++) {
262
+ stats.species_species[i][j] = 0;
263
+ }
264
+ }
265
+
266
+ for (let i = 0; i < sizes.moves; i++) {
267
+ stats.move_move[i] = new Array(sizes.moves);
268
+ for (let j = 0; j < sizes.moves; j++) {
269
+ stats.move_move[i][j] = 0;
270
+ }
271
+ }
272
+
273
+ const N = 6 * Sizes[gen.num as keyof typeof Sizes];
274
+ const row = 17 + 2 * N;
275
+ if (db.length % row !== 0) {
276
+ throw new Error(`Corrupted logs database of size ${db.length} (${row})`);
277
+ }
278
+
279
+ for (let offset = 0; offset < db.length; offset += row) {
280
+ const data = Log.decode(gen, lookup, db, offset);
281
+ for (const player of [data.winner, data.loser] as const) {
282
+ if (!player.rating) continue;
283
+ const weight = weighting(player.rating.rpr, player.rating.rprd, options.cutoff);
284
+ if (!weight) continue;
285
+
286
+ for (const [index, set] of player.team.entries()) {
287
+ const s = lookup.speciesByID(set.species as ID) - 1;
288
+
289
+ stats.species[s] += weight;
290
+ stats.total.usage += weight;
291
+
292
+ // FIXME track average team size so dont just naively multiply by 6
293
+
294
+ if (index === 0) {
295
+ stats.species_lead[s] += weight;
296
+ stats.total.lead += weight;
297
+ }
298
+ // FIXME non lead
299
+
300
+ for (let j = 0; j < index; j++) {
301
+ const t = lookup.speciesByID(player.team[j].species as ID) - 1;
302
+ stats.species_species[s][t] = (stats.species_species[t][s] += weight);
303
+ }
304
+
305
+ for (const move of set.moves!) {
306
+ const m = this.moveByID(lookup, move as ID);
307
+ stats.move_species[s][m] = (stats.move_species[s][m] || 0) + weight;
308
+ }
309
+
310
+ if (gen.num >= 2) {
311
+ const i = set.item ? lookup.itemByID(set.item as ID) : 0;
312
+ stats.item_species[s][i] = (stats.item_species[s][i] || 0) + weight;
313
+ }
314
+ }
315
+ }
316
+ }
317
+ }
318
+
319
+ encode(
320
+ gen: Generation,
321
+ lookup: Binary.Lookup,
322
+ stats: Binary.Statistics,
323
+ options: {moves: number; items: number},
324
+ ) {
325
+ const sizes = this.sizes(gen, lookup);
326
+ const BY_VAL = (a: [string, number], b: [string, number]) => b[1] - a[1];
327
+
328
+ let cursor = 0;
329
+ const buf = Buffer.alloc(sizes.stats);
330
+ for (let i = cursor; i < stats.species_lead.length; i++) {
331
+ Write.u16(buf, round(stats.species_lead[i] / stats.total.lead), i * 2);
332
+ }
333
+ cursor += sizes.species * 2;
334
+
335
+ // FIXME: want to track only NON lead statistics for other pokemon!
336
+ for (let i = cursor; i < stats.species.length; i++) {
337
+ Write.u16(buf, round((stats.species[i] / stats.total.usage) * 6), i * 2);
338
+ }
339
+ cursor += sizes.species * 2;
340
+
341
+ const n = gen.num < 2 ? 3 : 4;
342
+ const write = gen.num < 2 ? Write.u8 : Write.u16;
343
+ for (let i = cursor; i < stats.move_species.length; i++) {
344
+ const moves = Object.entries(stats.move_species[i]).sort(BY_VAL);
345
+ for (let j = 0; j < Math.min(moves.length, options.moves); j++) {
346
+ const [key, weight] = moves[j];
347
+ const offset = (i * options.moves * n) + (j * n);
348
+ write(buf, +key, offset);
349
+ Write.u16(buf, round(weight / stats.species[i]), offset + 1);
350
+ }
351
+ }
352
+ cursor += sizes.species * options.moves * n;
353
+
354
+ if (gen.num >= 2) {
355
+ for (let i = cursor; i < stats.item_species.length; i++) {
356
+ const items = Object.entries(stats.item_species[i]).sort(BY_VAL);
357
+ for (let j = 0; j < Math.min(items.length, options.items); j++) {
358
+ const [key, weight] = items[j];
359
+ const offset = (i * options.items * 3) + (j * 3);
360
+ Write.u8(buf, +key, offset);
361
+ Write.u16(buf, round(weight / stats.species[i]), offset + 1);
362
+ }
363
+ }
364
+ cursor += sizes.species * options.items * 3;
365
+ }
366
+
367
+ for (let i = cursor; i < sizes.species; i++) {
368
+ for (let j = 0; j < sizes.species; j++) {
369
+ const offset = (i * sizes.species) + (j * 2);
370
+ const weight = stats.species[i];
371
+ const w = stats.species_species[i][j];
372
+ const usage = (stats.species[j] / stats.total.usage) * 6;
373
+ Write.u16(buf, round((w - weight * usage) / weight), offset);
374
+ }
375
+ }
376
+ cursor += sizes.species * sizes.species * 2;
377
+
378
+ return buf;
379
+ }
380
+
381
+ moveByID(lookup: Binary.Lookup, move: ID) {
382
+ return (move.startsWith('hiddenpower')
383
+ ? lookup.sizes.moves + HP_TYPE_TO_NUM[move.slice(11) as keyof typeof HP_TYPE_TO_NUM]
384
+ : lookup.moveByID(move));
385
+ }
386
+
387
+ moveByNum(lookup: Binary.Lookup, num: number) {
388
+ return (num >= lookup.sizes.moves
389
+ ? `hiddenpower${NUM_TO_HP_TYPE[num]}` as ID
390
+ : lookup.moveByNum(num));
391
+ }
392
+ };
393
+
394
+ const Display = new class {
395
+ pokemon(
396
+ gen: Generation,
397
+ lookup: Binary.Lookup,
398
+ db: Buffer,
399
+ options: {moves: number; items: number},
400
+ ) {
401
+ const sizes = Stats.sizes(gen, lookup, options);
402
+ if (db.length !== sizes.stats) {
403
+ throw new Error(`Corrupted stats.db of size ${db.length} (${sizes.stats})`);
404
+ }
405
+
406
+ const pokemon: [ID, Binary.DisplayStatistics[keyof Binary.DisplayStatistics]][] = [];
407
+ for (let i = 0; i < sizes.species; i++) {
408
+ let offset = 0;
409
+ const id = lookup.speciesByNum(i + 1);
410
+
411
+ const lead = Read.u16(db, offset + (i * 2)) / 100;
412
+ offset += sizes.species * 2;
413
+
414
+ const nonlead = Read.u16(db, offset + (i * 2)) / 100;
415
+ offset += sizes.species * 2;
416
+
417
+ const usage = nonlead; // TODO: compute based on nonlead and lead!
418
+
419
+ const moves: {[id: string]: number} = {};
420
+ for (let j = 0; j < options.moves; j++) {
421
+ const off = offset + (i * options.moves * 3) + (j * 3);
422
+ const move = Read.u8(db, off);
423
+ if (move === 0) break;
424
+ moves[Stats.moveByNum(lookup, move)] = Read.u16(db, off + 1) / 100;
425
+ }
426
+ offset += sizes.species * options.moves * 3;
427
+
428
+ let items: {[id: string]: number} | undefined = undefined;
429
+ if (gen.num >= 2) {
430
+ items = {};
431
+ for (let j = 0; j < options.moves; j++) {
432
+ const off = offset + (i * options.moves * 3) + (j * 3);
433
+ const item = Read.u8(db, off);
434
+ const val = Read.u16(db, off + 1);
435
+ if (item === 0 && val === 0) break;
436
+ moves[lookup.itemByNum(item)] = val / 100;
437
+ }
438
+ offset += sizes.species * options.items * 3;
439
+ }
440
+
441
+ pokemon.push([id, {usage, lead, moves, items}]);
442
+ }
443
+
444
+ const stats: Binary.DisplayStatistics = {};
445
+ for (const p of pokemon.sort((a, b) => b[1].usage - a[1].usage)) {
446
+ stats[p[0]] = p[1];
447
+ }
448
+ return stats;
449
+ }
450
+ };
451
+
452
+ function round(v: number, p = 1e4) {
453
+ return Math.round(v * p);
454
+ }
455
+
456
+ function bias(stats: StatsTable) {
457
+ const [first, second] = Object.entries(stats).sort((a, b) => b[1] - a[1]);
458
+ // TODO: convert this pair (eg. 'atkhp') to a number
459
+ return first[0] > second[0] ? [first[0], second[0]] : [second[0], first[0]];
460
+ }
461
+
462
+ export const Binary = {Read, Write, Sizes, Log, Team, Stats, Display};
463
+
464
+ export namespace Binary {
465
+ export interface Lookup {
466
+ sizes: {
467
+ types: number;
468
+ species: number;
469
+ moves: number;
470
+ items: number;
471
+ };
472
+ typeByNum(num: number): TypeName;
473
+ typeByName(name: TypeName): number;
474
+ speciesByNum(num: number): ID;
475
+ speciesByID(id: ID | undefined): number;
476
+ moveByNum(num: number): ID;
477
+ moveByID(id: ID | undefined): number;
478
+ itemByNum(num: number): ID;
479
+ itemByID(id: ID | undefined): number;
480
+ }
481
+
482
+ export interface Data {
483
+ timestamp: bigint;
484
+ turns: number;
485
+ endType: EndType;
486
+ winner: {
487
+ team: Array<Partial<PokemonSet>>;
488
+ rating?: {rpr: number; rprd: number};
489
+ };
490
+ loser: {
491
+ team: Array<Partial<PokemonSet>>;
492
+ rating?: {rpr: number; rprd: number};
493
+ };
494
+ }
495
+
496
+ export interface Statistics {
497
+ total: {lead: number; usage: number};
498
+ species: number[];
499
+ species_lead: number[];
500
+ move_species: {[num: number]: number}[];
501
+ item_species: {[num: number]: number}[];
502
+ species_species: number[][];
503
+ move_move: number[][];
504
+ }
505
+
506
+ export interface DisplayStatistics {
507
+ [id: string]: {
508
+ usage: number;
509
+ lead: number;
510
+ moves: {[id: string]: number};
511
+ items?: {[id: string]: number};
512
+ };
513
+ }
514
+ }
package/src/classifier.ts CHANGED
@@ -5,17 +5,17 @@ import * as util from './util';
5
5
  const LOG3_LOG2 = Math.log(3) / Math.log(2);
6
6
 
7
7
  export const Classifier = new class {
8
- classifyTeam(gen: Generation, team: Array<PokemonSet<ID>>) {
8
+ classifyTeam(gen: Generation, team: Array<PokemonSet<ID>>, legacy = false) {
9
9
  let teamBias = 0;
10
10
  const teamStalliness = [];
11
11
  for (const pokemon of team) {
12
- const {bias, stalliness} = this.classifyPokemon(gen, pokemon);
12
+ const {bias, stalliness} = this.classifyPokemon(gen, pokemon, legacy);
13
13
  teamBias += bias;
14
14
  teamStalliness.push(stalliness);
15
15
  }
16
16
 
17
17
  const stalliness = teamStalliness.reduce((a, b) => a + b) / teamStalliness.length;
18
- const tags = tag(gen, team, stalliness);
18
+ const tags = tag(gen, team, stalliness, legacy);
19
19
 
20
20
  return {bias: teamBias, stalliness, tags};
21
21
  }
@@ -23,13 +23,13 @@ export const Classifier = new class {
23
23
  // For stats and moveset purposes we're now counting Mega Pokemon seperately,
24
24
  // but for team analysis we still want to consider the base (which presumably
25
25
  // breaks for Hackmons, but we're OK with that).
26
- classifyPokemon(gen: Generation, pokemon: PokemonSet<ID>) {
26
+ classifyPokemon(gen: Generation, pokemon: PokemonSet<ID>, legacy = false) {
27
27
  const originalSpecies = pokemon.species;
28
28
  const originalAbility = pokemon.ability;
29
29
 
30
- const species = util.getSpecies(gen, pokemon.species);
30
+ const species = util.getSpecies(gen, pokemon.species, legacy);
31
31
  let mega: { species: ID; ability: ID } | undefined;
32
- if (util.isMega(species)) {
32
+ if (util.isMega(species, legacy)) {
33
33
  mega = {
34
34
  species: toID(species.name),
35
35
  ability: toID(species.abilities['0']),
@@ -37,26 +37,27 @@ export const Classifier = new class {
37
37
  pokemon.species = toID(species.baseSpecies);
38
38
  }
39
39
 
40
- let {bias, stalliness} = classifyForme(gen, pokemon);
41
- // FIXME: Intended behavior, but not used for compatibility:
42
- // if (pokemon.species === 'meloetta' && pokemon.moves.includes('relicsong')) {
43
- // pokemon.species = 'meloettapirouette';
44
- // stalliness = (stalliness + classifyForme(gen, pokemon).stalliness) / 2;
45
- // } else if (
46
- // pokemon.species === 'darmanitan' && pokemon.ability === 'zenmode') {
47
- // pokemon.species = 'darmanitanzen' ;
48
- // stalliness = (stalliness + classifyForme(gen, pokemon).stalliness) / 2;
49
- // } else if (
50
- // pokemon.species === 'rayquaza' &&
51
- // pokemon.moves.includes('dragonascent')) {
52
- // pokemon.species = 'rayquazamega';
53
- // pokemon.ability = 'deltastream';
54
- // stalliness = (stalliness + classifyForme(gen, pokemon).stalliness) / 2;
55
- // } else {
40
+ let {bias, stalliness} = classifyForme(gen, pokemon, legacy);
41
+ if (!legacy) {
42
+ if (pokemon.species === 'meloetta' && pokemon.moves.includes('relicsong' as ID)) {
43
+ pokemon.species = 'meloettapirouette' as ID;
44
+ stalliness = (stalliness + classifyForme(gen, pokemon, legacy).stalliness) / 2;
45
+ } else if (
46
+ pokemon.species === 'darmanitan' && pokemon.ability === 'zenmode') {
47
+ pokemon.species = 'darmanitanzen' as ID;
48
+ stalliness = (stalliness + classifyForme(gen, pokemon, legacy).stalliness) / 2;
49
+ } else if (
50
+ pokemon.species === 'rayquaza' &&
51
+ pokemon.moves.includes('dragonascent' as ID)) {
52
+ pokemon.species = 'rayquazamega' as ID;
53
+ pokemon.ability = 'deltastream' as ID;
54
+ stalliness = (stalliness + classifyForme(gen, pokemon, legacy).stalliness) / 2;
55
+ }
56
+ }
56
57
  if (mega) {
57
- // pokemon.species = mega.species; FIXME see above
58
+ if (!legacy) pokemon.species = mega.species;
58
59
  pokemon.ability = mega.ability;
59
- stalliness = (stalliness + classifyForme(gen, pokemon).stalliness) / 2;
60
+ stalliness = (stalliness + classifyForme(gen, pokemon, legacy).stalliness) / 2;
60
61
  }
61
62
 
62
63
  // Make sure to revert back to the original values
@@ -71,8 +72,8 @@ const TRAPPING_ABILITIES = new Set(['arenatrap', 'magnetpull', 'shadowtag']);
71
72
 
72
73
  const TRAPPING_MOVES = new Set(['block', 'meanlook', 'spiderweb', 'pursuit']);
73
74
 
74
- function classifyForme(gen: Generation, pokemon: PokemonSet<ID>) {
75
- let stalliness = baseStalliness(gen, pokemon);
75
+ function classifyForme(gen: Generation, pokemon: PokemonSet<ID>, legacy: boolean) {
76
+ let stalliness = baseStalliness(gen, pokemon, legacy);
76
77
  stalliness += abilityStallinessModifier(pokemon);
77
78
  stalliness += itemStallinessModifier(pokemon);
78
79
  stalliness += movesStallinessModifier(pokemon);
@@ -99,11 +100,11 @@ function classifyForme(gen: Generation, pokemon: PokemonSet<ID>) {
99
100
  return {bias, stalliness};
100
101
  }
101
102
 
102
- function baseStalliness(gen: Generation, pokemon: PokemonSet<ID>) {
103
+ function baseStalliness(gen: Generation, pokemon: PokemonSet<ID>, legacy: boolean) {
103
104
  if (pokemon.species === 'shedinja') return 0;
104
105
  // TODO: replace this with mean stalliness for the tier
105
106
  if (pokemon.species === 'ditto') return LOG3_LOG2;
106
- const stats = calcStats(gen, pokemon);
107
+ const stats = calcStats(gen, pokemon, legacy);
107
108
  return (
108
109
  -Math.log(
109
110
  (((((Math.floor(2.0 * pokemon.level + 10) / 250) * Math.max(stats.atk, stats.spa)) /
@@ -116,11 +117,11 @@ function baseStalliness(gen: Generation, pokemon: PokemonSet<ID>) {
116
117
  );
117
118
  }
118
119
 
119
- function calcStats(gen: Generation, pokemon: PokemonSet<ID>) {
120
- const stats = calcFormeStats(gen, pokemon);
120
+ function calcStats(gen: Generation, pokemon: PokemonSet<ID>, legacy: boolean) {
121
+ const stats = calcFormeStats(gen, pokemon, legacy);
121
122
  if (pokemon.species === 'aegislash' && pokemon.ability === 'stancechange') {
122
123
  pokemon.species = 'aegislashblade' as ID;
123
- const blade = calcFormeStats(gen, pokemon);
124
+ const blade = calcFormeStats(gen, pokemon, legacy);
124
125
  pokemon.species = 'aegislash' as ID;
125
126
  blade.def = Math.floor((blade.def + stats.def) / 2);
126
127
  blade.spd = Math.floor((blade.spd + stats.spd) / 2);
@@ -129,9 +130,9 @@ function calcStats(gen: Generation, pokemon: PokemonSet<ID>) {
129
130
  return stats;
130
131
  }
131
132
 
132
- function calcFormeStats(gen: Generation, pokemon: PokemonSet<ID>) {
133
- const species = util.getSpecies(gen, pokemon.species);
134
- const nature = util.ignoreGen(gen).natures.get(pokemon.nature)!;
133
+ function calcFormeStats(gen: Generation, pokemon: PokemonSet<ID>, legacy: boolean) {
134
+ const species = util.getSpecies(gen, pokemon.species, legacy);
135
+ const nature = util.ignoreGen(gen, legacy).natures.get(pokemon.nature)!;
135
136
  const stats = {hp: 0, atk: 0, def: 0, spa: 0, spd: 0, spe: 0};
136
137
  let stat: StatID;
137
138
  for (stat in stats) {
@@ -178,7 +179,7 @@ const GRAVITY_MOVES = new Set([
178
179
  'earthquake', 'magnitude', 'mudbomb', 'mudshot', 'mudslap', 'sandattack', 'spikes', 'toxicspikes',
179
180
  ]);
180
181
 
181
- function tag(gen: Generation, team: Array<PokemonSet<ID>>, stalliness: number) {
182
+ function tag(gen: Generation, team: Array<PokemonSet<ID>>, stalliness: number, legacy: boolean) {
182
183
  const weather = {rain: 0, sun: 0, sand: 0, hail: 0};
183
184
  const style = {
184
185
  batonpass: 0, tailwind: 0, trickroom: 0, slow: 0, gravityMoves: 0, gravity: 0, voltturn: 0,
@@ -187,8 +188,8 @@ function tag(gen: Generation, team: Array<PokemonSet<ID>>, stalliness: number) {
187
188
 
188
189
  let possibleTypes: TypeName[] | undefined;
189
190
  for (const pokemon of team) {
190
- let species = util.getSpecies(gen, pokemon.species);
191
- if (util.isMega(species)) species = util.getBaseSpecies(gen, species.id);
191
+ let species = util.getSpecies(gen, pokemon.species, legacy);
192
+ if (util.isMega(species, legacy)) species = util.getBaseSpecies(gen, species.id, legacy);
192
193
 
193
194
  const moves = new Set(pokemon.moves as string[]);
194
195
  possibleTypes = possibleTypes