Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from regal/fix/47-scrub-agent
fix(agent): All inaccessible data is scrubbed from InstanceAgents before each player command
- Loading branch information
Showing
7 changed files
with
359 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Contains `scrubAgents` function, which deletes all agents in an | ||
* `InstanceAgents` that are not accessible from the instance's state. | ||
* | ||
* Copyright (c) 2018 Joseph R Cowman | ||
* Licensed under MIT License (see https://github.com/regal/regal) | ||
*/ | ||
|
||
import { isAgent } from "./agent-model"; | ||
import { InstanceAgents, propertyIsAgentId } from "./instance-agents"; | ||
|
||
/** | ||
* Traverses all agents that are accessible from the `GameInstance`'s | ||
* state via breadth-first search. All remaining agents (the ones with | ||
* no references to them) are deleted from the `InstanceAgents`. | ||
* | ||
* If run at the improper time, this will break event sourcing and/or | ||
* instance reverting. Make sure to use this correctly. | ||
* | ||
* @param agents The `InstanceAgents` to be scrubbed. | ||
*/ | ||
export const scrubAgents = (agents: InstanceAgents): void => { | ||
const seen = new Set<number>(); | ||
const q = [0]; // Start at the state, which always has an id of zero | ||
|
||
while (q.length > 0) { | ||
const id = q.shift(); | ||
seen.add(id); | ||
|
||
for (const prop of agents.getAgentPropertyKeys(id)) { | ||
const val = agents.getAgentProperty(id, prop); | ||
if (isAgent(val) && !seen.has(val.id)) { | ||
q.push(val.id); | ||
} | ||
} | ||
} | ||
|
||
const waste = Object.keys(agents) | ||
.filter(propertyIsAgentId) | ||
.filter(id => !seen.has(Number(id))); | ||
|
||
for (const id of waste) { | ||
delete agents[id]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.