Removed some numeric index access, removed duplicated API call in server

This commit is contained in:
Ivan Savenko
2026-06-04 01:56:31 +03:00
parent 62adfad49d
commit 7b68fa0503
13 changed files with 20 additions and 39 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ This page describes the internal working of the Lua scripting module. For usage
- reconsider approach to mutable methods (like BattleHexArrayProxy). Either remove or provide better API bindings approach for such cases. Or convert it to pure Lua class
- consider removing excessive namespace from scripting API, have all API classes directly in scripting::api namespace
- try to remove remaining hardcoded bits of SpellID's CLONE, TELEPORT, SACRIFICE, STONE_GAZE, SLAYER, AIR_SHIELD, POISON, RESURRECTION, FIRE_SHIELD, DEATH_STARE, as well as some entries in .lua
- implement comparison operator of exposed API classes by auto-implementing `__eq` Lua field for all exported classes
- Actually use comparison operator of exposed API classes - currently hard to change without breaking tests
- consider wrapping Lua userdata into std::any for better type safety
- check if there is a way to wrap Lua function into C++ wrapper and pass it into LuaFunctionWrapper, or even LuaMethodWrapper
- decide how to handle MetaString in Lua API. Make it Lua serializeable?
+5 -23
View File
@@ -55,7 +55,6 @@ const std::vector<ServerCallbackProxy::CustomRegType> ServerCallbackProxy::REGIS
{ "addUnitBonus", LuaFunctionWrapper<&ServerCallbackProxy::addUnitBonus>::invoke, false },
{ "addBattleBonus", LuaFunctionWrapper<&ServerCallbackProxy::addBattleBonus>::invoke, false },
{ "addObstacle", LuaFunctionWrapper<&ServerCallbackProxy::addObstacle>::invoke, false },
{ "applyUnitBonuses", LuaFunctionWrapper<&ServerCallbackProxy::applyUnitBonuses>::invoke, false },
{ "catapultAttack", LuaFunctionWrapper<&ServerCallbackProxy::catapultAttack>::invoke, false },
{ "rngInt", LuaCallWrapper<&ServerCallbackProxy::rngInt>::invoke, false },
};
@@ -80,14 +79,17 @@ void ServerCallbackProxy::removeUnitBonuses(ServerCallback * object, BattleID ba
object->apply(sse);
}
void ServerCallbackProxy::addUnitBonus(ServerCallback * object, BattleID battleID, uint32_t unitId, const JsonNode & data)
void ServerCallbackProxy::addUnitBonus(ServerCallback * object, BattleID battleID, const battle::Unit * unit, const JsonNode & data, bool cumulative)
{
Bonus b;
JsonUtils::parseBonus(data, &b);
SetStackEffect sse;
sse.battleID = battleID;
sse.toAdd.emplace_back(unitId, std::vector<Bonus>{b});
if(cumulative)
sse.toAdd.emplace_back(unit->unitId(), std::vector<Bonus>{b});
else
sse.toUpdate.emplace_back(unit->unitId(), std::vector<Bonus>{b});
object->apply(sse);
}
@@ -161,26 +163,6 @@ void ServerCallbackProxy::addObstacle(ServerCallback * object, BattleID battleID
object->apply(pack);
}
void ServerCallbackProxy::applyUnitBonuses(ServerCallback * object, BattleID battleID, const battle::Unit * unit, const JsonNode & bonuses, bool cumulative)
{
std::vector<Bonus> buffer;
for(const auto & [name, bonusJson] : bonuses.Struct())
{
auto b = JsonUtils::parseBonus(bonusJson);
if(b)
buffer.push_back(*b);
}
if(buffer.empty())
return;
SetStackEffect sse;
sse.battleID = battleID;
if(cumulative)
sse.toAdd.emplace_back(unit->unitId(), buffer);
else
sse.toUpdate.emplace_back(unit->unitId(), buffer);
object->apply(sse);
}
void ServerCallbackProxy::createUnit(ServerCallback * object, BattleID battleID, uint32_t id, JsonNode data)
{
+1 -2
View File
@@ -43,10 +43,9 @@ public:
static void appendLog(ServerCallback * object, BattleID battleID, const JsonNode & config);
static bool describeChanges(ServerCallback * object);
static void removeUnitBonuses(ServerCallback * object, BattleID battleID, const battle::Unit * unit, const BonusList & bonusList);
static void addUnitBonus(ServerCallback * object, BattleID battleID, uint32_t unitId, const JsonNode & data);
static void addUnitBonus(ServerCallback * object, BattleID battleID, const battle::Unit * unit, const JsonNode & data, bool cumulative);
static void addBattleBonus(ServerCallback * object, BattleID battleID, const JsonNode & data);
static void addObstacle(ServerCallback * object, BattleID battleID, const JsonNode & descriptor);
static void applyUnitBonuses(ServerCallback * object, BattleID battleID, const battle::Unit * unit, const JsonNode & bonuses, bool cumulative);
static void catapultAttack(ServerCallback * object, BattleID battleID, const battle::Unit * attacker, EWallPart attackedPart, BattleHex destinationTile, int32_t damageDealt, int32_t killedTowerShooter);
static int rngInt(lua_State * L); // args: low, high; returns: int in [low, high]
static int healUnit(lua_State * L);
-1
View File
@@ -25,7 +25,6 @@ namespace scripting::api::library
const std::vector<ArtifactProxy::CustomRegType> ArtifactProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, Artifact>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, Artifact>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, Artifact>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, Artifact>::invoke, false},
-1
View File
@@ -25,7 +25,6 @@ namespace scripting::api::library
const std::vector<CreatureProxy::CustomRegType> CreatureProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, Creature>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, Creature>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, Creature>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, Creature>::invoke, false},
-1
View File
@@ -23,7 +23,6 @@ namespace scripting::api::library
const std::vector<FactionProxy::CustomRegType> FactionProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, Faction>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, Faction>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, Faction>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, Faction>::invoke, false},
-1
View File
@@ -23,7 +23,6 @@ namespace scripting::api::library
const std::vector<HeroClassProxy::CustomRegType> HeroClassProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, HeroClass>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, HeroClass>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, HeroClass>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, HeroClass>::invoke, false},
-1
View File
@@ -23,7 +23,6 @@ namespace scripting::api::library
const std::vector<HeroTypeProxy::CustomRegType> HeroTypeProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, HeroType>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, HeroType>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, HeroType>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, HeroType>::invoke, false},
-1
View File
@@ -23,7 +23,6 @@ namespace scripting::api::library
const std::vector<SkillProxy::CustomRegType> SkillProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, Skill>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, Skill>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, Skill>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, Skill>::invoke, false},
-1
View File
@@ -26,7 +26,6 @@ using ::spells::Spell;
const std::vector<SpellProxy::CustomRegType> SpellProxy::REGISTER_CUSTOM =
{
{"getIconIndex", LuaMethodWrapper<&Entity::getIconIndex, Spell>::invoke, false},
{"getIndex", LuaMethodWrapper<&Entity::getIndex, Spell>::invoke, false},
{"getJsonKey", LuaMethodWrapper<&Entity::getJsonKey, Spell>::invoke, false},
{"getName", LuaMethodWrapper<&Entity::getNameTranslated, Spell>::invoke, false},
+2 -2
View File
@@ -48,14 +48,14 @@ function Script:apply(mechanics, server, target)
originalState:setCloneID(cloneId)
server:changeUnit(battleID, originalState)
server:addUnitBonus(battleID, cloneId, {
server:addUnitBonus(battleID, cloneUnit, {
duration = ENUM.BonusDuration.nTurns,
type = "NONE",
sourceType = ENUM.BonusSource.spellEffect,
val = 0,
sourceID = mechanics:getSpell():getJsonKey(),
turns = mechanics:getEffectDuration()
})
}, true)
::continue::
end
+3 -1
View File
@@ -153,7 +153,9 @@ function Script:apply(mechanics, server, target)
self:describeEffect(server, battleID, unit, buffer, singular, plural)
end
server:applyUnitBonuses(battleID, unit, buffer, self.cumulative or false)
for _, nb in pairs(buffer) do
server:addUnitBonus(battleID, unit, nb, self.cumulative or false)
end
::continue::
end
+8 -3
View File
@@ -95,18 +95,23 @@ TEST_P(TimedApplyTest, ChangesBonuses)
bonus.sid = BonusSourceID(testSpellId);
}
auto accumulate = [&actualBonus](uint32_t, const std::vector<Bonus> & b)
{
actualBonus.insert(actualBonus.end(), b.begin(), b.end());
};
if(cumulative)
{
EXPECT_CALL(*battleFake, addUnitBonus(Eq(unitId), _)).WillOnce(SaveArg<1>(&actualBonus));
EXPECT_CALL(*battleFake, addUnitBonus(Eq(unitId), _)).Times(2).WillRepeatedly(Invoke(accumulate));
}
else
{
EXPECT_CALL(*battleFake, updateUnitBonus(Eq(unitId), _)).WillOnce(SaveArg<1>(&actualBonus));
EXPECT_CALL(*battleFake, updateUnitBonus(Eq(unitId), _)).Times(2).WillRepeatedly(Invoke(accumulate));
}
setDefaultExpectations();
EXPECT_CALL(serverMock, apply(Matcher<SetStackEffect &>(_))).Times(1);
EXPECT_CALL(serverMock, apply(Matcher<SetStackEffect &>(_))).Times(2);
subject->apply(&serverMock, &mechanicsMock, target);