Skip to content

Commit

Permalink
refactor: Adds recycleInstanceEvents and recycleInstanceOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
jcowman2 committed Nov 4, 2018
1 parent ff0dd99 commit 1115a37
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/events/index.ts
Expand Up @@ -21,4 +21,4 @@ export {
DEFAULT_EVENT_ID,
DEFAULT_EVENT_NAME
} from "./event-record";
export { default as InstanceEvents } from "./instance-events";
export { InstanceEvents, recycleInstanceEvents } from "./instance-events";
14 changes: 13 additions & 1 deletion src/events/instance-events.ts
Expand Up @@ -14,10 +14,22 @@ import {
} from "./event-model";
import { DEFAULT_EVENT_ID, EventRecord } from "./event-record";

/**
* Creates a new `InstanceEvents` for the new game cycle, clearing all
* old events but preserving the last event ID.
*
* @param oldEvents The previous `InstanceEvents`.
* @param newInstance The new `GameInstance` that will own this `InstanceEvents`.
*/
export const recycleInstanceEvents = (
oldEvents: InstanceEvents,
newInstance: GameInstance
) => new InstanceEvents(newInstance, oldEvents.lastEventId);

/**
* Manager for all events in a `GameInstance`.
*/
export default class InstanceEvents {
export class InstanceEvents {
/** The current `EventRecord`. */
get current(): EventRecord {
let event = this._queue[0];
Expand Down
8 changes: 4 additions & 4 deletions src/game-instance.ts
Expand Up @@ -22,8 +22,8 @@ import {
import { GameOptions, InstanceOptions } from "./config";
import { ContextManager } from "./context-manager";
import { RegalError } from "./error";
import { InstanceEvents } from "./events";
import { InstanceOutput } from "./output";
import { InstanceEvents, recycleInstanceEvents } from "./events";
import { InstanceOutput, recycleInstanceOutput } from "./output";

/**
* The current state of the game, unique to each player.
Expand Down Expand Up @@ -86,9 +86,9 @@ export default class GameInstance {
newOptions === undefined ? this.options.overrides : newOptions;

const newGame = new GameInstance(opts);
newGame.events = new InstanceEvents(newGame, this.events.lastEventId);
newGame.events = recycleInstanceEvents(this.events, newGame);
newGame.agents = recycleInstanceAgents(this.agents, newGame);
newGame.output = new InstanceOutput(newGame, this.output.lineCount);
newGame.output = recycleInstanceOutput(this.output, newGame);

return newGame;
}
Expand Down
12 changes: 12 additions & 0 deletions src/output.ts
Expand Up @@ -62,6 +62,18 @@ export interface OutputLine {
type: OutputLineType;
}

/**
* Creates a new `InstanceOutput` for the new game cycle, clearing all
* old output but preserving the line count.
*
* @param oldOutput The previous `InstanceOutput`.
* @param newInstance The new `GameInstance` that will own this `InstanceOutput`.
*/
export const recycleInstanceOutput = (
oldOutput: InstanceOutput,
newInstance: GameInstance
) => new InstanceOutput(newInstance, oldOutput.lineCount);

/**
* Manager for output in a `GameInstance`.
*
Expand Down
13 changes: 7 additions & 6 deletions test/unit/events.test.ts
Expand Up @@ -11,7 +11,8 @@ import {
nq,
isEventQueue,
enqueue,
InstanceEvents
InstanceEvents,
recycleInstanceEvents
} from "../../src/events";
import { log, getDemoMetadata } from "../test-utils";
import {
Expand Down Expand Up @@ -907,8 +908,8 @@ describe("Events", function() {
expect(myGame.events.lastEventId).to.equal(13);
});

it.skip("InstanceEvents.cycle creates a new InstanceEvents with the previous instance's lastEventId", function() {
/*const spam = on("SPAM", game => {
it("recycleInstanceEvents creates a new InstanceEvents with the previous instance's lastEventId", function() {
const spam = on("SPAM", game => {
game.output.write("Get spammed.");
});

Expand All @@ -917,7 +918,7 @@ describe("Events", function() {
const game1 = new GameInstance();

const game2 = new GameInstance();
const events2 = game1.events.cycle(game2);
const events2 = recycleInstanceEvents(game1.events, game2);

expect(events2.lastEventId).to.equal(0);
expect(events2.game).to.equal(game2);
Expand All @@ -926,11 +927,11 @@ describe("Events", function() {
events2.invoke(spam.then(spam).thenq(spam));

const game3 = new GameInstance();
const events3 = events2.cycle(game3);
const events3 = recycleInstanceEvents(events2, game3);

expect(events3.lastEventId).to.equal(3);
expect(events3.game).to.equal(game3);
expect(events3.history).to.be.empty;*/
expect(events3.history).to.be.empty;
});
});
});
Expand Down
16 changes: 10 additions & 6 deletions test/unit/output.test.ts
Expand Up @@ -2,7 +2,11 @@ import { expect } from "chai";
import "mocha";

import GameInstance from "../../src/game-instance";
import { OutputLineType, InstanceOutput } from "../../src/output";
import {
OutputLineType,
InstanceOutput,
recycleInstanceOutput
} from "../../src/output";
import { MetadataManager } from "../../src/config";
import { getDemoMetadata } from "../test-utils";
import { Game } from "../../src/game-api";
Expand Down Expand Up @@ -207,11 +211,11 @@ describe("Output", function() {
expect(myGame.output.lineCount).to.equal(11);
});

it.skip("InstanceOutput.cycle creates a new InstanceOutput with the previous instance's lineCount", function() {
/*const game1 = new GameInstance();
it("recycleInstanceOutput creates a new InstanceOutput with the previous instance's lineCount", function() {
const game1 = new GameInstance();

const game2 = new GameInstance();
const output2 = game1.output.cycle(game2);
const output2 = recycleInstanceOutput(game1.output, game2);

expect(output2.lineCount).to.equal(0);
expect(output2.game).to.equal(game2);
Expand All @@ -220,11 +224,11 @@ describe("Output", function() {
output2.write("Foo", "Bar", "Baz");

const game3 = new GameInstance();
const output3 = output2.cycle(game3);
const output3 = recycleInstanceOutput(output2, game3);

expect(output3.lineCount).to.equal(3);
expect(output3.game).to.equal(game3);
expect(output3.lines).to.be.empty;*/
expect(output3.lines).to.be.empty;
});
});
});

0 comments on commit 1115a37

Please sign in to comment.