mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-08-15 14:43:32 -07:00
Fix a ModifiersManager bug when changing player entities
The cache invalidation was incorrectly using forEach, which failed to invalidate the cache. This never actually caused a bug because the game currently does not silently swap active player entities.
This commit is contained in:
parent
40ab70b804
commit
a406660e78
2 changed files with 51 additions and 1 deletions
|
|
@ -198,7 +198,8 @@ ModifiersManager.prototype.OnGlobalPlayerEntityChanged = function(msg)
|
|||
|
||||
if (msg.from != INVALID_PLAYER && this.playerEntitiesCached.has(msg.from))
|
||||
{
|
||||
this.playerEntitiesCached.get(msg.from).forEach(propName => this.InvalidateCache(propName, msg.from));
|
||||
const playerCache = this.playerEntitiesCached.get(msg.from);
|
||||
playerCache.forEach((_, propName) => this.InvalidateCache(propName, msg.from, playerCache));
|
||||
this.playerEntitiesCached.delete(msg.from);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -248,3 +248,52 @@ TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Test_D", 10, 5), 16);
|
|||
Engine.PostMessage = oldPostMessage;
|
||||
Engine.BroadcastMessage = oldBroadcastMessage;
|
||||
})();
|
||||
|
||||
(function Test_PlayerEntityChangeInvalidatesCachedEntities()
|
||||
{
|
||||
const PLAYER_ID = 1;
|
||||
const OLD_PLAYER_ENTITY = 40;
|
||||
const NEW_PLAYER_ENTITY = 41;
|
||||
const TEST_ENTITY = 42;
|
||||
const PROPERTY_NAME = "Test_PlayerEntityChange";
|
||||
|
||||
let playerEntity = OLD_PLAYER_ENTITY;
|
||||
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
|
||||
"GetPlayerByID": () => playerEntity
|
||||
});
|
||||
AddMock(OLD_PLAYER_ENTITY, IID_Player, {
|
||||
"GetPlayerID": () => PLAYER_ID
|
||||
});
|
||||
AddMock(NEW_PLAYER_ENTITY, IID_Player, {
|
||||
"GetPlayerID": () => PLAYER_ID
|
||||
});
|
||||
AddMock(TEST_ENTITY, IID_Ownership, {
|
||||
"GetOwner": () => PLAYER_ID
|
||||
});
|
||||
AddMock(TEST_ENTITY, IID_Identity, {
|
||||
"GetClassesList": () => "Unit"
|
||||
});
|
||||
|
||||
const cmp = ConstructComponent(SYSTEM_ENTITY, "ModifiersManager", {});
|
||||
cmp.Init();
|
||||
cmp.OnGlobalPlayerEntityChanged({
|
||||
"player": PLAYER_ID,
|
||||
"from": INVALID_ENTITY,
|
||||
"to": OLD_PLAYER_ENTITY
|
||||
});
|
||||
cmp.AddModifier(PROPERTY_NAME, "old player modifier", [{
|
||||
"affects": ["Unit"],
|
||||
"add": 10
|
||||
}], OLD_PLAYER_ENTITY);
|
||||
|
||||
TS_ASSERT_EQUALS(cmp.ApplyModifiers(PROPERTY_NAME, 5, TEST_ENTITY), 15);
|
||||
|
||||
playerEntity = NEW_PLAYER_ENTITY;
|
||||
cmp.OnGlobalPlayerEntityChanged({
|
||||
"player": PLAYER_ID,
|
||||
"from": OLD_PLAYER_ENTITY,
|
||||
"to": NEW_PLAYER_ENTITY
|
||||
});
|
||||
|
||||
TS_ASSERT_EQUALS(cmp.ApplyModifiers(PROPERTY_NAME, 5, TEST_ENTITY), 5);
|
||||
})();
|
||||
|
|
|
|||
Loading…
Reference in a new issue