@civ-clone/core-strategy 0.1.2 → 0.1.3

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.
@@ -2,7 +2,8 @@ import EntityRegistry from '@civ-clone/core-registry/EntityRegistry';
2
2
  import PlayerAction from '@civ-clone/core-player/PlayerAction';
3
3
  import Strategy from './Strategy';
4
4
  export declare class StrategyRegistry extends EntityRegistry<Strategy> {
5
- constructor();
5
+ private _randomNumberGenerator;
6
+ constructor(randomNumberGenerator?: () => number);
6
7
  /**
7
8
  * Iterates all `Strategy`s ordered first by `Priority`, then by a random number so that alternative strategies are
8
9
  * tried.
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.instance = exports.StrategyRegistry = void 0;
4
4
  const EntityRegistry_1 = require("@civ-clone/core-registry/EntityRegistry");
5
5
  const Strategy_1 = require("./Strategy");
6
+ const core_random_1 = require("@civ-clone/core-random");
6
7
  class StrategyRegistry extends EntityRegistry_1.default {
7
- constructor() {
8
+ constructor(randomNumberGenerator = core_random_1.instance) {
8
9
  super(Strategy_1.default);
10
+ this._randomNumberGenerator = randomNumberGenerator;
9
11
  }
10
12
  /**
11
13
  * Iterates all `Strategy`s ordered first by `Priority`, then by a random number so that alternative strategies are
@@ -14,7 +16,7 @@ class StrategyRegistry extends EntityRegistry_1.default {
14
16
  attempt(action) {
15
17
  return this.entries()
16
18
  .sort((a, b) => a.priority(action).value() - b.priority(action).value() ||
17
- Math.floor(Math.random() * 3 - 1))
19
+ Math.floor(this._randomNumberGenerator() * 3 - 1))
18
20
  .some((strategy) => strategy.attempt(action));
19
21
  }
20
22
  }
@@ -1 +1 @@
1
- {"version":3,"file":"StrategyRegistry.js","sourceRoot":"","sources":["StrategyRegistry.ts"],"names":[],"mappings":";;;AAAA,4EAAqE;AAErE,yCAAkC;AAElC,MAAa,gBAAiB,SAAQ,wBAAwB;IAC5D;QACE,KAAK,CAAC,kBAAQ,CAAC,CAAC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AE,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,CAAC;CACF;AAtBD,4CAsBC;AAEY,QAAA,QAAQ,GAAG,IAAI,gBAAgB,EAAE,CAAC;AAE/C,kBAAe,gBAAgB,CAAC"}
@@ -1,10 +1,15 @@
1
1
  import EntityRegistry from '@civ-clone/core-registry/EntityRegistry';
2
2
  import PlayerAction from '@civ-clone/core-player/PlayerAction';
3
3
  import Strategy from './Strategy';
4
+ import { instance as rngInstance } from '@civ-clone/core-random';
4
5
 
5
6
  export class StrategyRegistry extends EntityRegistry<Strategy> {
6
- constructor() {
7
+ private _randomNumberGenerator: () => number;
8
+
9
+ constructor(randomNumberGenerator: () => number = rngInstance) {
7
10
  super(Strategy);
11
+
12
+ this._randomNumberGenerator = randomNumberGenerator;
8
13
  }
9
14
 
10
15
  /**
@@ -16,7 +21,7 @@ export class StrategyRegistry extends EntityRegistry<Strategy> {
16
21
  .sort(
17
22
  (a, b) =>
18
23
  a.priority(action).value() - b.priority(action).value() ||
19
- Math.floor(Math.random() * 3 - 1)
24
+ Math.floor(this._randomNumberGenerator() * 3 - 1)
20
25
  )
21
26
  .some((strategy) => strategy.attempt(action));
22
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@civ-clone/core-strategy",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "repository": "git@github.com:civ-clone/core-strategy.git",
5
5
  "keywords": [
6
6
  "typescript",
@@ -34,6 +34,7 @@
34
34
  "@civ-clone/core-civilization": "^0.1.0",
35
35
  "@civ-clone/core-data-object": "^0.1.0",
36
36
  "@civ-clone/core-player": "^0.1.2",
37
+ "@civ-clone/core-random": "^0.1.0",
37
38
  "@civ-clone/core-registry": "^0.1.0",
38
39
  "@civ-clone/core-rule": "^0.1.1"
39
40
  }