Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): Start implementing new Agent model [wip]
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 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,56 @@ | ||
import { RegalError } from "../error"; | ||
import GameInstance from "../game-instance"; | ||
import { | ||
addAgentToStaticRegistry, | ||
getNextStaticAgentId, | ||
getStaticAgentProperty, | ||
isContextStatic | ||
} from "./todo"; | ||
|
||
const agentReservedProperties = ["id", "game", "isActivated"]; | ||
|
||
const AGENT_PROXY_HANDLER = { | ||
get(target: Agent, property: PropertyKey) { | ||
if (agentReservedProperties.includes(property.toString())) { | ||
return Reflect.get(target, property); | ||
} | ||
|
||
if (!target.isActivated) { | ||
throw new RegalError( | ||
"The agent has not been activated; that property is not accessible." | ||
); | ||
} | ||
|
||
let value = target.game.agents.getAgentProperty(target.id, property); | ||
|
||
if ( | ||
value === undefined && | ||
!target.game.agents.agentPropertyWasDeleted(target.id, property) | ||
) { | ||
value = getStaticAgentProperty(target.id, property); | ||
} | ||
|
||
return value; | ||
}, | ||
|
||
set(target: Agent, property: PropertyKey, value: any): boolean { | ||
// todo | ||
return true; | ||
} | ||
}; | ||
|
||
export class Agent { | ||
public id: number; | ||
public game: GameInstance; | ||
public isActivated: boolean; | ||
|
||
constructor() { | ||
this.isActivated = false; | ||
|
||
if (isContextStatic()) { | ||
this.id = getNextStaticAgentId(); | ||
addAgentToStaticRegistry(this); | ||
} | ||
return new Proxy({} as Agent, AGENT_PROXY_HANDLER); | ||
} | ||
} |
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,20 @@ | ||
import { Agent } from "./agent-model"; | ||
|
||
export const isContextStatic = () => { | ||
return true; | ||
}; | ||
|
||
export const getNextStaticAgentId = () => { | ||
return 1; | ||
}; | ||
|
||
export const addAgentToStaticRegistry = (agent: Agent) => { | ||
return; | ||
}; | ||
|
||
export const getStaticAgentProperty = ( | ||
id: number, | ||
property: PropertyKey | ||
): any => { | ||
return; | ||
}; |