Differential D1718 Diff 8477 binaries/data/mods/public/simulation/components/tests/test_ResourceSupply.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_ResourceSupply.js
Resources = { | Resources = { | ||||
"BuildChoicesSchema": () => { | "BuildChoicesSchema": () => { | ||||
let schema = ""; | let schema = ""; | ||||
for (let res of ["food", "metal"]) | for (let res of ["food", "metal"]) | ||||
{ | { | ||||
for (let subtype in ["meat", "grain"]) | for (let subtype in ["meat", "grain"]) | ||||
schema += "<value>" + res + "." + subtype + "</value>"; | schema += "<value>" + res + "." + subtype + "</value>"; | ||||
schema += "<value> treasure." + res + "</value>"; | schema += "<value> treasure." + res + "</value>"; | ||||
} | } | ||||
return "<choice>" + schema + "</choice>"; | return "<choice>" + schema + "</choice>"; | ||||
} | } | ||||
}; | }; | ||||
Engine.LoadHelperScript("Player.js"); | |||||
Engine.LoadHelperScript("ValueModification.js"); | |||||
Engine.LoadComponentScript("interfaces/TechnologyManager.js"); | |||||
Engine.LoadComponentScript("interfaces/AuraManager.js"); | |||||
Engine.LoadComponentScript("interfaces/ResourceSupply.js"); | Engine.LoadComponentScript("interfaces/ResourceSupply.js"); | ||||
Engine.LoadComponentScript("interfaces/Health.js"); | |||||
Engine.LoadComponentScript("interfaces/Timer.js"); | |||||
Engine.LoadComponentScript("ResourceSupply.js"); | Engine.LoadComponentScript("ResourceSupply.js"); | ||||
Engine.LoadComponentScript("Timer.js"); | |||||
const entity = 60; | |||||
let entity = 60; | |||||
AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | ||||
"GetNumPlayers": () => 3 | "GetNumPlayers": () => 3 | ||||
}); | }); | ||||
AddMock(entity, IID_Fogging, { | AddMock(entity, IID_Fogging, { | ||||
"Activate": () => {} | "Activate": () => {} | ||||
}); | }); | ||||
let template = { | let template = { | ||||
"Amount": 1000, | "Amount": 1000, | ||||
Stan: Use strings. | |||||
"Type": "food.meat", | "Type": "food.meat", | ||||
"KillBeforeGather": false, | "KillBeforeGather": false, | ||||
"MaxGatherers": 2 | "MaxGatherers": 2 | ||||
}; | }; | ||||
let cmpResourceSupply = ConstructComponent(entity, "ResourceSupply", template); | let cmpResourceSupply = ConstructComponent(entity, "ResourceSupply", template); | ||||
TS_ASSERT(!cmpResourceSupply.IsInfinite()); | TS_ASSERT(!cmpResourceSupply.IsInfinite()); | ||||
Show All 30 Lines | |||||
TS_ASSERT_UNEVAL_EQUALS(cmpResourceSupply.TakeResources(300), { "amount": 300, "exhausted": false }); | TS_ASSERT_UNEVAL_EQUALS(cmpResourceSupply.TakeResources(300), { "amount": 300, "exhausted": false }); | ||||
TS_ASSERT_EQUALS(cmpResourceSupply.GetCurrentAmount(), 700); | TS_ASSERT_EQUALS(cmpResourceSupply.GetCurrentAmount(), 700); | ||||
TS_ASSERT(cmpResourceSupply.IsAvailable(1, 70)); | TS_ASSERT(cmpResourceSupply.IsAvailable(1, 70)); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpResourceSupply.TakeResources(800), { "amount": 700, "exhausted": true }); | TS_ASSERT_UNEVAL_EQUALS(cmpResourceSupply.TakeResources(800), { "amount": 700, "exhausted": true }); | ||||
TS_ASSERT_EQUALS(cmpResourceSupply.GetCurrentAmount(), 0); | TS_ASSERT_EQUALS(cmpResourceSupply.GetCurrentAmount(), 0); | ||||
// The resource is not available when exhausted | // The resource is not available when exhausted | ||||
TS_ASSERT(!cmpResourceSupply.IsAvailable(1, 70)); | TS_ASSERT(!cmpResourceSupply.IsAvailable(1, 70)); | ||||
/** | |||||
* @param {object} temp the resourceSupply template | |||||
* @param {number} ent the entity's entity_id_t | |||||
* @param {number[]} results the expect results. | |||||
*/ | |||||
function TestChangeModifiers(temp, ent, results) | |||||
{ | |||||
let turnLength = 500; | |||||
let cmpTimer = ConstructComponent(SYSTEM_ent, "Timer"); | |||||
let resourceSupply = ConstructComponent(ent, "ResourceSupply", temp); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetMaxAmount(), results[0]); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[1]); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetNumGatherers(), results[2]); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[3]); | |||||
Done Inline ActionsUse strings. Stan: Use strings. | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[4]); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[5]); | |||||
TS_ASSERT(resourceSupply.AddGatherer(2, 72)); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[6]); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[7]); | |||||
resourceSupply.RemoveGatherer(72, 2); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[8]); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[9]); | |||||
AddMock(ent, IID_Health, {}); | |||||
TS_ASSERT_EQUALS(!!Engine.QueryInterface(ent, IID_Health), true); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[10]); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[11]); | |||||
Done Inline ActionsUse strings. Stan: Use strings. | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
DeleteMock(ent, IID_Health, {}); | |||||
TS_ASSERT_EQUALS(!!Engine.QueryInterface(ent, IID_Health), false); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[12]); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
TS_ASSERT_EQUALS(resourceSupply.GetCurrentAmount(), results[13]); | |||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | |||||
} | |||||
// Decay when it has no gatherers | |||||
template = { | |||||
"Amount": 1000, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": false, | |||||
"Change": { | |||||
"Rotting": { | |||||
"NotBeingGathered": void (0), | |||||
"Value": -1, | |||||
"Interval": 500 | |||||
} | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
Done Inline ActionsUse strings. Stan: Use strings. | |||||
TestChangeModifiers(template, 29, [1000, 1000, 0, 1000, 999, 998, 998, 998, 997, 996, 995, 994, 993, 992]); | |||||
// Growing | |||||
template = { | |||||
"Amount": 1000, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": true, | |||||
"Change": { | |||||
"Growth": { | |||||
"Alive": void (0), | |||||
"Value": 5, | |||||
"Interval": 500, | |||||
"ChangeLimit": 1010 | |||||
} | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
TestChangeModifiers(template, 28, [1010, 1000, 0, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1005, 1010, 1010, 1010]); | |||||
Done Inline ActionsUse strings. Stan: Use strings. | |||||
// Decaying and growing. | |||||
template = { | |||||
"Amount": 1000, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": {}, | |||||
"Change": { | |||||
"Growth": { | |||||
"Alive": void (0), | |||||
"Value": 5, | |||||
"Interval": 500, | |||||
"ChangeLimit": 2000 | |||||
}, | |||||
"Decay": { | |||||
"Dead": void (0), | |||||
"Value": -3, | |||||
"Interval": 500, | |||||
"ChangeLimit": 994 | |||||
} | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
Done Inline ActionsUse strings. Stan: Use strings. | |||||
Done Inline ActionsYou *could* have stopped after the first one ;) Freagarach: You *could* have stopped after the first one ;) | |||||
TestChangeModifiers(template, 27, [2000, 1000, 0, 1000, 997, 994, 994, 994, 994, 994, 999, 1004, 1009, 1006]); | |||||
// Test infinity | |||||
template = { | |||||
"Amount": Infinity, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": true, | |||||
"Change": { | |||||
"Growth": { | |||||
"Alive": void (0), | |||||
"Value": 5, | |||||
"Interval": 500, | |||||
"ChangeLimit": 2000 | |||||
}, | |||||
"Decay": { | |||||
"Dead": void (0), | |||||
"Value": -3, | |||||
"Interval": 500, | |||||
} | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
TestChangeModifiers(template, 26, [Infinity, Infinity, 0, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]); | |||||
// Test combined changes Alive + Dead | |||||
template = { | |||||
"Amount": 500, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": true, | |||||
"Change": { | |||||
"Growth": { | |||||
"Alive": void (0), | |||||
"Dead": void (0), | |||||
"Value": 5, | |||||
"Interval": 1000, | |||||
"ChangeLimit": 2000 | |||||
}, | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
TestChangeModifiers(template, 25, [2000, 500, 0, 500, 505, 510, 515, 520, 525, 530, 535, 540, 545, 550]); | |||||
// Test combined changes Alive + NotBeingGathered | |||||
template = { | |||||
"Amount": 500, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": true, | |||||
"Change": { | |||||
"Growth": { | |||||
"Alive": void (0), | |||||
"NotBeingGathered": void (0), | |||||
"Value": 5, | |||||
"Interval": 1000, | |||||
"ChangeLimit": 2000 | |||||
}, | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
TestChangeModifiers(template, 24, [2000, 500, 0, 500, 500, 500, 500, 500, 500, 500, 505, 510, 515, 515]); | |||||
// Test combined changes Alive + NotBeingGathered and just NotBeingGathered | |||||
template = { | |||||
"Amount": 500, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": true, | |||||
"Change": { | |||||
"Growth": { | |||||
"Alive": void (0), | |||||
"NotBeingGathered": void (0), | |||||
"Value": 5, | |||||
"Interval": 1000, | |||||
"ChangeLimit": 2000 | |||||
}, | |||||
"Regen": { | |||||
"NotBeingGathered": void (0), | |||||
"Value": 5, | |||||
"Interval": 1000, | |||||
"ChangeLimit": 2000 | |||||
}, | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
TestChangeModifiers(template, 23, [2000, 500, 0, 500, 505, 510, 510, 510, 515, 520, 530, 540, 550, 555]); | |||||
// Test combined changes Dead + NotBeingGathered | |||||
template = { | |||||
"Amount": 500, | |||||
"Type": "food.meat", | |||||
"KillBeforeGather": true, | |||||
"Change": { | |||||
"Decay": { | |||||
"Dead": void (0), | |||||
"NotBeingGathered": void (0), | |||||
"Value": -5, | |||||
"Interval": 1000, | |||||
"ChangeLimit": 492 | |||||
}, | |||||
}, | |||||
"MaxGatherers": 2 | |||||
}; | |||||
TestChangeModifiers(template, 22, [500, 500, 0, 500, 495, 492, 492, 492, 492, 492, 492, 492, 492, 492]); | |||||
Done Inline Actions-cmp Freagarach: -`cmp` | |||||
Done Inline Actions-cmp Freagarach: -`cmp` | |||||
Done Inline Actions-cmp Freagarach: -`cmp` | |||||
Done Inline Actions-cmp Freagarach: -`cmp` | |||||
Done Inline Actions-cmp Freagarach: -`cmp` |
Wildfire Games · Phabricator
Use strings.