Differential D5006 Diff 21823 binaries/data/mods/public/simulation/components/tests/test_ResourceTrickle.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_ResourceTrickle.js
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ||||
if (valueName == "ResourceTrickle/Rates/food") | if (valueName == "ResourceTrickle/Rates/food") | ||||
return currentValue + 1; | return currentValue + 1; | ||||
return currentValue; | return currentValue; | ||||
}; | }; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
// Calling OnValueModification will reset the timer, which can then be called, thus increasing the resources of the player. | // Calling OnValueModification will reset the timer, which can then be called, thus increasing the resources of the player. | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpResourceTrickle.GetRates(), { "food": 1 }); | TS_ASSERT_UNEVAL_EQUALS(cmpResourceTrickle.GetRates(), { "food": 1 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 301, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 301, "metal": 300 }); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.ComputeRates(), true); | TS_ASSERT_EQUALS(cmpResourceTrickle.ComputeRates(), true); | ||||
// Reset the trickle modification. | // Reset the trickle modification. | ||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => currentValue; | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => currentValue; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpResourceTrickle.GetRates(), {}); | TS_ASSERT_UNEVAL_EQUALS(cmpResourceTrickle.GetRates(), {}); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.ComputeRates(), false); | TS_ASSERT_EQUALS(cmpResourceTrickle.ComputeRates(), false); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 301, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 301, "metal": 300 }); | ||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ||||
if (valueName == "ResourceTrickle/Interval") | if (valueName == "ResourceTrickle/Interval") | ||||
return currentValue + 200; | return currentValue + 200; | ||||
if (valueName == "ResourceTrickle/Rates/food") | if (valueName == "ResourceTrickle/Rates/food") | ||||
return currentValue + 1; | return currentValue + 1; | ||||
return currentValue; | return currentValue; | ||||
}; | }; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 400); | TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 400); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 301, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 301, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 302, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 302, "metal": 300 }); | ||||
// Interval becomes a normal timer, thus cancelled after the first execution. | // Interval becomes a normal timer, thus cancelled after the first execution. | ||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ||||
if (valueName == "ResourceTrickle/Interval") | if (valueName == "ResourceTrickle/Interval") | ||||
return currentValue - 200; | return currentValue - 200; | ||||
if (valueName == "ResourceTrickle/Rates/food") | if (valueName == "ResourceTrickle/Rates/food") | ||||
return currentValue + 1; | return currentValue + 1; | ||||
return currentValue; | return currentValue; | ||||
}; | }; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 0); | TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 0); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 302, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 302, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 303, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 303, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 303, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 303, "metal": 300 }); | ||||
// Timer became invalidated, check whether it's recreated properly after that. | // Timer became invalidated, check whether it's recreated properly after that. | ||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ||||
if (valueName == "ResourceTrickle/Interval") | if (valueName == "ResourceTrickle/Interval") | ||||
return currentValue - 100; | return currentValue - 100; | ||||
if (valueName == "ResourceTrickle/Rates/food") | if (valueName == "ResourceTrickle/Rates/food") | ||||
return currentValue + 1; | return currentValue + 1; | ||||
return currentValue; | return currentValue; | ||||
}; | }; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 100); | TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 100); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 305, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 305, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 307, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 307, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 309, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 309, "metal": 300 }); | ||||
// Value is now invalid, timer should be cancelled. | // Value is now invalid, timer should be cancelled. | ||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ||||
if (valueName == "ResourceTrickle/Interval") | if (valueName == "ResourceTrickle/Interval") | ||||
return currentValue - 201; | return currentValue - 201; | ||||
if (valueName == "ResourceTrickle/Rates/food") | if (valueName == "ResourceTrickle/Rates/food") | ||||
return currentValue + 1; | return currentValue + 1; | ||||
return currentValue; | return currentValue; | ||||
}; | }; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), -1); | TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), -1); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 309, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 309, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 309, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 309, "metal": 300 }); | ||||
// Timer became invalidated, check whether it's recreated properly after that. | // Timer became invalidated, check whether it's recreated properly after that. | ||||
ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ApplyValueModificationsToEntity = (valueName, currentValue, entity) => { | ||||
if (valueName == "ResourceTrickle/Rates/food") | if (valueName == "ResourceTrickle/Rates/food") | ||||
return currentValue + 1; | return currentValue + 1; | ||||
return currentValue; | return currentValue; | ||||
}; | }; | ||||
Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | Engine.RegisterGlobal("ApplyValueModificationsToEntity", ApplyValueModificationsToEntity); | ||||
cmpResourceTrickle.OnValueModification({ "component": "ResourceTrickle" }); | cmpResourceTrickle.OnValueModification(); | ||||
TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 200); | TS_ASSERT_EQUALS(cmpResourceTrickle.GetInterval(), 200); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 310, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 310, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 311, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 311, "metal": 300 }); | ||||
cmpTimer.OnUpdate({ "turnLength": turnLength }); | cmpTimer.OnUpdate({ "turnLength": turnLength }); | ||||
TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 312, "metal": 300 }); | TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetResourceCounts(), { "food": 312, "metal": 300 }); |
Wildfire Games · Phabricator