-
Notifications
You must be signed in to change notification settings - Fork 10
Policy
Sriep edited this page Aug 6, 2016
·
13 revisions
Policies are code snippets that are run each tick. The code that handels the economy of each room is a policy. This where user code can be put. You can create them using the policy.framework object.
In the main method we call
policy.enactPolicies();
If we look at policy.enactPolicies()
enactPolicies: function()
{
this.checkRoomPolicies();
for (var i in Memory.policies) {
var oldPolicyModule = this.getModuleFromPolicy(Memory.policies[i]);
var newPolicyDetails = oldPolicyModule.draftNewPolicyId(Memory.policies[i]);
if (newPolicyDetails === null) {
delete Memory.policies[i];
} else if ( newPolicyDetails.id != Memory.policies[i].id) {
var newPolicyModule = this.getModuleFromPolicy(newPolicyDetails);
this.activatePolicy(newPolicyDetails);
newPolicyModule.switchPolicy(Memory.policies[i], newPolicyDetails);
delete Memory.policies[i];
newPolicyModule.enactPolicy(newPolicyDetails);
} else {
oldPolicyModule.enactPolicy(Memory.policies[i]);
}
}
},
Details of all policies are storted in Memory.polices. We can see that each policy object is expected to proved several methods to be called by the base object.
- initialisePolicy(policy) This performs any tasks that a policy needs to do once only at startup. It is called from the activatePolicy method.
- draftNewPolicyId(policy) This is called to see if the policy whishes to end. If it returns null, it is delted. If it returns details of a new policy, then the old policy is delted and replaced by the new one. This is really useful for developing finte state machines.
- switchPolicy(oldPolicy, newPolicy) Called from a new polocy that has just been created after from the draftNewPolicyId method. It is where you put any initialise details that are dependant on the prevous policy. Atain useful for developing finite state machines.
- enactPolicy(policy) This is where custom code to be enacted each tick is put.
- The policy filename needs to start "policy."
- A plolicy must create a taskType member with a value equal to the part of the filename after "policy.". Ideally there should be a matching POLICY_ entry in gc.js
- A method should be added to "policy.framework" to allow the policy to be started.