Skip to content

Commit

Permalink
feat(random): InstanceRandom value generation methods add records to …
Browse files Browse the repository at this point in the history
…the EventRecord
  • Loading branch information
jcowman2 committed Nov 27, 2018
1 parent cf121c2 commit 18eee93
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/random/impl/instance-random-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ class InstanceRandomImpl implements InstanceRandomInternal {
);
}

const value = this._generator.nextInt(min, max);
this.trackRandom(value);

this._numGenerations++;
return this._generator.nextInt(min, max);
return value;
}

public decimal(): number {
const value = this._generator.next(0, 1);
this.trackRandom(value);
this._numGenerations++;
return this._generator.next(0, 1);
return value;
}

public string(length: number, charset: string = EXPANDED_CHARSET) {
Expand All @@ -79,21 +84,37 @@ class InstanceRandomImpl implements InstanceRandomInternal {
);
}

const value = this._generator.nextString(length, charset);
this.trackRandom(value);

this._numGenerations++;
return this._generator.nextString(length, charset);
return value;
}

public choice<T>(array: T[]): T {
if (array === undefined) {
throw new RegalError("Array must be defined.");
}

const idx = this._generator.nextInt(0, array.length - 1);
this.trackRandom(idx); // Track the index of the selected element, rather than the element itself

this._numGenerations++;
return this._generator.nextArrayItem(array);
return array[idx];
}

public boolean(): boolean {
const value = this._generator.nextBoolean();
this.trackRandom(value);
this._numGenerations++;
return this._generator.nextBoolean();
return value;
}

/** Internal helper method to add the `RandomRecord` to the current `EventRecord`. */
private trackRandom(value: string | number | boolean): void {
this.game.events.current.trackRandom({
id: this.numGenerations,
value
});
}
}
50 changes: 50 additions & 0 deletions test/unit/random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { RegalError } from "../../src/error";
import { log, getDemoMetadata } from "../test-utils";
import { Game } from "../../src/api";
import { Agent, isAgent } from "../../src/agents";
import { on } from "../../src/events";

describe("Random", function() {
beforeEach(function() {
Expand Down Expand Up @@ -319,5 +320,54 @@ describe("Random", function() {
}
});
});

it("Generated values are recorded in InstanceEvent's EventRecord history", function() {
const rand1 = on("RAND1", game => {
game.state.randos = [];
const randos = game.state.randos as any[];

randos.push(game.random.boolean());
randos.push(game.random.decimal());
});

const rand2 = on("RAND2", game => {
const randos = game.state.randos as any[];

randos.push(game.random.int(1, 10));
randos.push(game.random.string(5));
randos.push(game.random.choice(randos));
});

const myGame = buildGameInstance({ seed: "wooof" });
rand1.then(rand2)(myGame);

expect(myGame.state.randos).to.deep.equal([
false,
0.0217038414025921,
10,
"IcR*G",
false
]);

expect(myGame.events.history).to.deep.equal([
{
id: 2,
name: "RAND2",
randoms: [
{ id: 2, value: 10 },
{ id: 3, value: "IcR*G" },
{ id: 4, value: 0 } // InstanceRandom.choice records the index of the selected element, not the element itself
]
},
{
id: 1,
name: "RAND1",
randoms: [
{ id: 0, value: false },
{ id: 1, value: 0.0217038414025921 }
]
}
]);
});
});
});

0 comments on commit 18eee93

Please sign in to comment.