File size: 3,528 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var multi_random_runner_exports = {};
__export(multi_random_runner_exports, {
  MultiRandomRunner: () => MultiRandomRunner
});
module.exports = __toCommonJS(multi_random_runner_exports);
var import_prng = require("../prng");
var import_runner = require("./runner");
/**
 * Battle Simulator multi random runner.
 * Pokemon Showdown - http://pokemonshowdown.com/
 *
 * @license MIT
 */
const _MultiRandomRunner = class {
  constructor(options) {
    this.options = { ...options };
    this.totalGames = options.totalGames;
    this.prng = import_prng.PRNG.get(options.prng);
    this.options.prng = this.prng;
    this.format = options.format;
    this.cycle = !!options.cycle;
    this.all = !!options.all;
    this.isAsync = !!options.async;
    this.formatIndex = 0;
    this.numGames = 0;
  }
  async run() {
    let games = [];
    let format;
    let lastFormat = false;
    let failures = 0;
    while (format = this.getNextFormat()) {
      if (this.all && lastFormat && format !== lastFormat) {
        if (this.isAsync)
          await Promise.all(games);
        games = [];
      }
      const seed = this.prng.getSeed();
      const game = new import_runner.Runner({ format, ...this.options }).run().catch((err) => {
        failures++;
        console.error(
          `Run \`node tools/simulate multi 1 --format=${format} --seed=${seed}\` to debug (optionally with \`--output\` and/or \`--input\` for more info):
`,
          err
        );
      });
      if (!this.isAsync)
        await game;
      games.push(game);
      lastFormat = format;
    }
    if (this.isAsync)
      await Promise.all(games);
    return failures;
  }
  getNextFormat() {
    const FORMATS = _MultiRandomRunner.FORMATS;
    if (this.formatIndex > FORMATS.length)
      return false;
    if (this.numGames++ < this.totalGames) {
      if (this.format) {
        return this.format;
      } else if (this.all) {
        return FORMATS[this.formatIndex];
      } else if (this.cycle) {
        const format = FORMATS[this.formatIndex];
        this.formatIndex = (this.formatIndex + 1) % FORMATS.length;
        return format;
      } else {
        return this.prng.sample(FORMATS);
      }
    } else if (this.all) {
      this.numGames = 1;
      this.formatIndex++;
      return FORMATS[this.formatIndex];
    }
    return false;
  }
};
let MultiRandomRunner = _MultiRandomRunner;
MultiRandomRunner.FORMATS = [
  "gen8randombattle",
  "gen8randomdoublesbattle",
  "gen8battlefactory",
  "gen7randombattle",
  "gen7battlefactory",
  "gen6randombattle",
  "gen6battlefactory",
  "gen5randombattle",
  "gen4randombattle",
  "gen3randombattle",
  "gen2randombattle",
  "gen1randombattle"
];
//# sourceMappingURL=multi-random-runner.js.map