Skip to content

Commit

Permalink
feat(agent): InstanceAgents are scrubbed on Game API calls to postPla…
Browse files Browse the repository at this point in the history
…yerCommand and postUndoCommand
  • Loading branch information
jcowman2 committed Oct 28, 2018
1 parent de77399 commit 70058da
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
3 changes: 0 additions & 3 deletions src/agents/instance-agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
isAgent
} from "./agent-model";
import { AgentReference, isAgentReference } from "./agent-reference";
import { scrubAgents } from "./agent-scrub";
import { StaticAgentRegistry } from "./static-agent-registry";

/**
Expand Down Expand Up @@ -158,8 +157,6 @@ export const recycleInstanceAgents = (
});
}

scrubAgents(newAgents);

return newAgents;
};

Expand Down
8 changes: 7 additions & 1 deletion src/game-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { buildRevertFunction, StaticAgentRegistry } from "./agents";
import {
buildRevertFunction,
scrubAgents,
StaticAgentRegistry
} from "./agents";
import { HookManager } from "./api-hooks";
import { GameMetadata, GameOptions, MetadataManager } from "./config";
import { ContextManager } from "./context-manager";
Expand Down Expand Up @@ -191,6 +195,7 @@ export class Game {
}

newInstance = instance.recycle();
scrubAgents(newInstance.agents);

const activatedEvent = HookManager.playerCommandHook(command);
newInstance.events.invoke(activatedEvent);
Expand Down Expand Up @@ -264,6 +269,7 @@ export class Game {
}

newInstance = instance.recycle();
scrubAgents(newInstance.agents);

const revert = buildRevertFunction(instance.agents);
revert(newInstance);
Expand Down
41 changes: 40 additions & 1 deletion test/unit/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2882,7 +2882,46 @@ describe("Agents", function() {
}
});

expect(() => myGame.agents.getAgentProperty(3, "name")).to.throw(RegalError, "No agent with the id <3> exists.");
expect(() => myGame.agents.getAgentProperty(3, "name")).to.throw(
RegalError,
"No agent with the id <3> exists."
);
});

it("Scrubbing an InstanceAgents finds agent array references", function() {
Game.init();
const myGame = new GameInstance();

const p = myGame.using(
new MultiParent([new Dummy("D1", 1), new Dummy("D2", 2)])
);
myGame.state.arr = [true, new Dummy("D3", 3), p, p.children[0]];

expect(
myGame.agents.agentManagers().map(am => am.id)
).to.deep.equal([0, 1, 2, 3, 4, 5, 6]);
expect(myGame.state.arr[3]).to.deep.equal({
id: 3,
name: "D1",
health: 1
});

scrubAgents(myGame.agents);
expect(
myGame.agents.agentManagers().map(am => am.id)
).to.deep.equal([0, 1, 2, 3, 4, 5, 6]);

(myGame.state.arr as any[]).splice(2, 1);
scrubAgents(myGame.agents);

expect(
myGame.agents.agentManagers().map(am => am.id)
).to.deep.equal([0, 3, 5, 6]);
expect(myGame.state.arr[2]).to.deep.equal({
id: 3,
name: "D1",
health: 1
});
});
});
});

0 comments on commit 70058da

Please sign in to comment.