Skip to content

Commit

Permalink
refactor: Move simulateRevert from InstanceAgentsInternal to GameInst…
Browse files Browse the repository at this point in the history
…anceInternal

Commit 400 :D
  • Loading branch information
jcowman2 committed Nov 27, 2018
1 parent 64a0bf3 commit b59e833
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 67 deletions.
49 changes: 1 addition & 48 deletions src/agents/impl/instance-agents-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

import { RegalError } from "../../error";
import { on } from "../../events";
import { GameInstance, GameInstanceInternal } from "../../state";
import { GameInstanceInternal } from "../../state";
import { isAgent } from "../agent";
import {
AgentArrayReference,
Expand Down Expand Up @@ -283,50 +282,4 @@ class InstanceAgentsImpl implements InstanceAgentsInternal {
delete this[id];
}
}

public simulateRevert(
source: InstanceAgentsInternal,
revertTo: number = 0
): void {
// Build revert function
on("REVERT", (game: GameInstanceInternal) => {
const target = game.agents;

for (const am of source.agentManagers()) {
const id = am.id;

const props = Object.keys(am).filter(
key => key !== "game" && key !== "id"
);

for (const prop of props) {
const history = am.getPropertyHistory(prop);
const lastChangeIdx = history.findIndex(
change => change.eventId <= revertTo
);

if (lastChangeIdx === -1) {
// If all changes to the property happened after the target event, delete/reset it
if (StaticAgentRegistry.hasAgentProperty(id, prop)) {
const newVal = StaticAgentRegistry.getAgentProperty(
id,
prop
);
target.setAgentProperty(id, prop, newVal);
} else {
target.deleteAgentProperty(id, prop);
}
} else {
// Otherwise, set the property to its value right after the target event
const targetVal = history[lastChangeIdx].final;
const currentVal = target.getAgentProperty(id, prop);

if (targetVal !== currentVal) {
target.setAgentProperty(id, prop, targetVal);
}
}
}
}
})(this.game); // Execute the revert function on this instance
}
}
9 changes: 0 additions & 9 deletions src/agents/instance-agents-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ export interface InstanceAgentsInternal {
* instance reverting. Make sure to use this correctly.
*/
scrubAgents(): void;

/**
* Builds and executes a `TrackedEvent` that reverts all changes in this
* `InstanceAgentsInternal` since a specified event.
*
* @param source The agent history on which the revert function will be based. Is not modified.
* @param revertTo The id of the `TrackedEvent` to which the state will be reverted.
*/
simulateRevert(source: InstanceAgentsInternal, revertTo?: number): void;
}

/** Whether the property is a positive integer, meaning its a valid agent id. */
Expand Down
2 changes: 1 addition & 1 deletion src/api/game-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class Game {
}

newInstance = oldInstance.recycle();
newInstance.agents.simulateRevert(oldInstance.agents);
newInstance.simulateRevert(oldInstance);
} catch (error) {
err = wrapApiErrorAsRegalError(error);
}
Expand Down
10 changes: 10 additions & 0 deletions src/state/game-instance-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ export interface GameInstanceInternal extends GameInstance {
* @returns The new `GameInstanceInternal`, with each manager cycled.
*/
recycle(newOptions?: Partial<GameOptions>): GameInstanceInternal;

/**
* Builds and executes a `TrackedEvent` that reverts all changes in this
* `GameInstanceInternal` since a specified event.
*
* @param source The instance containing agent history on which the revert function
* will be based. Will not be modified.
* @param revertTo The id of the `TrackedEvent` to which the state will be reverted.
*/
simulateRevert(source: GameInstanceInternal, revertTo?: number): void;
}
48 changes: 46 additions & 2 deletions src/state/impl/game-instance-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import {
buildActiveAgentProxy,
buildInstanceAgents,
InstanceAgentsInternal,
isAgent
isAgent,
StaticAgentRegistry
} from "../../agents";
import {
buildInstanceOptions,
GameOptions,
InstanceOptionsInternal
} from "../../config";
import { RegalError } from "../../error";
import { buildInstanceEvents, InstanceEventsInternal } from "../../events";
import { buildInstanceEvents, InstanceEventsInternal, on } from "../../events";
import { buildInstanceOutput, InstanceOutputInternal } from "../../output";
import { buildInstanceRandom, InstanceRandomInternal } from "../../random";
import { ContextManager } from "../context-manager";
Expand Down Expand Up @@ -109,4 +110,47 @@ class GameInstanceImpl implements GameInstanceInternal {

return returnObj;
}

public simulateRevert(source: GameInstanceInternal, revertTo: number = 0) {
// Build function to revert agents
on("REVERT", (game: GameInstanceInternal) => {
const target = game.agents;

for (const am of source.agents.agentManagers()) {
const id = am.id;

const props = Object.keys(am).filter(
key => key !== "game" && key !== "id"
);

for (const prop of props) {
const history = am.getPropertyHistory(prop);
const lastChangeIdx = history.findIndex(
change => change.eventId <= revertTo
);

if (lastChangeIdx === -1) {
// If all changes to the property happened after the target event, delete/reset it
if (StaticAgentRegistry.hasAgentProperty(id, prop)) {
const newVal = StaticAgentRegistry.getAgentProperty(
id,
prop
);
target.setAgentProperty(id, prop, newVal);
} else {
target.deleteAgentProperty(id, prop);
}
} else {
// Otherwise, set the property to its value right after the target event
const targetVal = history[lastChangeIdx].final;
const currentVal = target.getAgentProperty(id, prop);

if (targetVal !== currentVal) {
target.setAgentProperty(id, prop, targetVal);
}
}
}
}
})(this); // Execute the revert function on this instance
}
}
14 changes: 7 additions & 7 deletions test/unit/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2682,7 +2682,7 @@ describe("Agents", function() {
expect(myGame.state[5]).to.equal("Yo 5");
expect(myGame.state[9]).to.equal("Yo 9");

myGame.agents.simulateRevert(myGame.agents, 1);
myGame.simulateRevert(myGame, 1);

expect(myGame.state.foo).to.equal("Hello, world!");
expect(myGame.state[0]).to.be.undefined;
Expand Down Expand Up @@ -2717,28 +2717,28 @@ describe("Agents", function() {
expect(myGame.state[5]).to.equal("Yo 5");
expect(myGame.state[9]).to.equal("Yo 9");

myGame.agents.simulateRevert(myGame.agents, 8);
myGame.simulateRevert(myGame, 8);

expect(myGame.state.foo).to.equal("Hello, world!-0-1-2-3-4-5-6");
expect(myGame.state[0]).to.equal("Yo 0");
expect(myGame.state[5]).to.equal("Yo 5");
expect(myGame.state[9]).to.be.undefined;

myGame.agents.simulateRevert(myGame.agents, 6);
myGame.simulateRevert(myGame, 6);

expect(myGame.state.foo).to.equal("Hello, world!-0-1-2-3-4");
expect(myGame.state[0]).to.equal("Yo 0");
expect(myGame.state[5]).to.be.undefined;
expect(myGame.state[9]).to.be.undefined;

myGame.agents.simulateRevert(myGame.agents, 4);
myGame.simulateRevert(myGame, 4);

expect(myGame.state.foo).to.equal("Hello, world!-0-1-2");
expect(myGame.state[0]).to.equal("Yo 0");
expect(myGame.state[5]).to.be.undefined;
expect(myGame.state[9]).to.be.undefined;

myGame.agents.simulateRevert(myGame.agents, 1);
myGame.simulateRevert(myGame, 1);

expect(myGame.state.foo).to.equal("Hello, world!");
expect(myGame.state[0]).to.be.undefined;
Expand All @@ -2756,7 +2756,7 @@ describe("Agents", function() {

const myGame = buildGameInstance();
init(myGame);
myGame.agents.simulateRevert(myGame.agents, 0);
myGame.simulateRevert(myGame, 0);

expect(myGame.state.foo).to.be.undefined;
expect(myGame.state.dummy).to.be.undefined;
Expand All @@ -2782,7 +2782,7 @@ describe("Agents", function() {
"boppity"
);

myGame.agents.simulateRevert(myGame.agents);
myGame.simulateRevert(myGame);

expect(myGame.agents.getAgentProperty(1, "name")).to.equal("Lars");
expect(myGame.agents.getAgentProperty(1, "health")).to.equal(15);
Expand Down

0 comments on commit b59e833

Please sign in to comment.