Skip to content

Commit

Permalink
Add Player:onReconnect event
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo committed Nov 17, 2024
1 parent 173ab18 commit 1d9119a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/cpplinter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ TalkAction = {}
---@field register fun(self:CreatureEvent):boolean
---@field onLogin fun(player:Player):boolean
---@field onLogout fun(player:Player):boolean
---@field onReconnect fun(player:Player)
---@field onThink fun(creature:Creature, interval:integer):boolean
---@field onPrepareDeath fun(creature:Creature, killer:Creature):boolean
---@field onDeath fun(creature:Creature, corpse:Item, killer:Creature, mostDamageKiller:Creature, lastHitUnjustified:boolean, mostDamageUnjustified:boolean):boolean
Expand Down
4 changes: 4 additions & 0 deletions data/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ do
self:type("logout")
self:onLogout(value)
return
elseif key == "onReconnect" then
self:type("reconnect")
self:onReconnect(value)
return
elseif key == "onThink" then
self:type("think")
self:onThink(value)
Expand Down
35 changes: 35 additions & 0 deletions src/creatureevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,19 @@ bool CreatureEvents::playerLogout(Player* player) const
return true;
}

void CreatureEvents::playerReconnect(Player* player) const
{
// fire global event if is registered
for (const auto& it : creatureEvents) {
if (it.second.getEventType() == CREATURE_EVENT_RECONNECT) {
it.second.executeOnReconnect(player);
}
}
}

bool CreatureEvents::playerAdvance(Player* player, skills_t skill, uint32_t oldLevel, uint32_t newLevel)
{
// fire global event if is registered
for (auto& it : creatureEvents) {
if (it.second.getEventType() == CREATURE_EVENT_ADVANCE) {
if (!it.second.executeAdvance(player, skill, oldLevel, newLevel)) {
Expand Down Expand Up @@ -166,6 +177,8 @@ bool CreatureEvent::configureEvent(const pugi::xml_node& node)
type = CREATURE_EVENT_LOGIN;
} else if (tmpStr == "logout") {
type = CREATURE_EVENT_LOGOUT;
} else if (tmpStr == "reconnect") {
type = CREATURE_EVENT_RECONNECT;
} else if (tmpStr == "think") {
type = CREATURE_EVENT_THINK;
} else if (tmpStr == "preparedeath") {
Expand Down Expand Up @@ -206,6 +219,9 @@ std::string_view CreatureEvent::getScriptEventName() const
case CREATURE_EVENT_LOGOUT:
return "onLogout";

case CREATURE_EVENT_RECONNECT:
return "onReconnect";

case CREATURE_EVENT_THINK:
return "onThink";

Expand Down Expand Up @@ -296,6 +312,25 @@ bool CreatureEvent::executeOnLogout(Player* player) const
return scriptInterface->callFunction(1);
}

void CreatureEvent::executeOnReconnect(Player* player) const
{
// onReconnect(player)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeOnReconnect] Call stack overflow" << std::endl;
return;
}

ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(scriptId, scriptInterface);

lua_State* L = scriptInterface->getLuaState();

scriptInterface->pushFunction(scriptId);
tfs::lua::pushUserdata(L, player);
tfs::lua::setMetatable(L, -1, "Player");
scriptInterface->callFunction(1);
}

bool CreatureEvent::executeOnThink(Creature* creature, uint32_t interval)
{
// onThink(creature, interval)
Expand Down
3 changes: 3 additions & 0 deletions src/creatureevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum CreatureEventType_t
CREATURE_EVENT_NONE,
CREATURE_EVENT_LOGIN,
CREATURE_EVENT_LOGOUT,
CREATURE_EVENT_RECONNECT,
CREATURE_EVENT_THINK,
CREATURE_EVENT_PREPAREDEATH,
CREATURE_EVENT_DEATH,
Expand Down Expand Up @@ -48,6 +49,7 @@ class CreatureEvent final : public Event
// scripting
bool executeOnLogin(Player* player) const;
bool executeOnLogout(Player* player) const;
void executeOnReconnect(Player* player) const;
bool executeOnThink(Creature* creature, uint32_t interval);
bool executeOnPrepareDeath(Creature* creature, Creature* killer);
bool executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller,
Expand Down Expand Up @@ -81,6 +83,7 @@ class CreatureEvents final : public BaseEvents
// global events
bool playerLogin(Player* player) const;
bool playerLogout(Player* player) const;
void playerReconnect(Player* player) const;
bool playerAdvance(Player* player, skills_t, uint32_t, uint32_t);

CreatureEvent* getEventByName(const std::string& name, bool forceLoaded = true);
Expand Down
4 changes: 4 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,7 @@ void LuaScriptInterface::registerFunctions()
registerEnum(L, CREATURE_EVENT_NONE);
registerEnum(L, CREATURE_EVENT_LOGIN);
registerEnum(L, CREATURE_EVENT_LOGOUT);
registerEnum(L, CREATURE_EVENT_RECONNECT);
registerEnum(L, CREATURE_EVENT_THINK);
registerEnum(L, CREATURE_EVENT_PREPAREDEATH);
registerEnum(L, CREATURE_EVENT_DEATH);
Expand Down Expand Up @@ -3493,6 +3494,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod(L, "CreatureEvent", "register", LuaScriptInterface::luaCreatureEventRegister);
registerMethod(L, "CreatureEvent", "onLogin", LuaScriptInterface::luaCreatureEventOnCallback);
registerMethod(L, "CreatureEvent", "onLogout", LuaScriptInterface::luaCreatureEventOnCallback);
registerMethod(L, "CreatureEvent", "onReconnect", LuaScriptInterface::luaCreatureEventOnCallback);
registerMethod(L, "CreatureEvent", "onThink", LuaScriptInterface::luaCreatureEventOnCallback);
registerMethod(L, "CreatureEvent", "onPrepareDeath", LuaScriptInterface::luaCreatureEventOnCallback);
registerMethod(L, "CreatureEvent", "onDeath", LuaScriptInterface::luaCreatureEventOnCallback);
Expand Down Expand Up @@ -17758,6 +17760,8 @@ int LuaScriptInterface::luaCreatureEventType(lua_State* L)
creature->setEventType(CREATURE_EVENT_LOGIN);
} else if (tmpStr == "logout") {
creature->setEventType(CREATURE_EVENT_LOGOUT);
} else if (tmpStr == "reconnect") {
creature->setEventType(CREATURE_EVENT_RECONNECT);
} else if (tmpStr == "think") {
creature->setEventType(CREATURE_EVENT_THINK);
} else if (tmpStr == "preparedeath") {
Expand Down
3 changes: 3 additions & 0 deletions src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ void ProtocolGame::login(uint32_t characterId, uint32_t accountId, OperatingSyst

void ProtocolGame::connect(uint32_t playerId, OperatingSystem_t operatingSystem)
{
// dispatcher thread
eventConnect = 0;

Player* foundPlayer = g_game.getPlayerByID(playerId);
Expand Down Expand Up @@ -287,6 +288,8 @@ void ProtocolGame::connect(uint32_t playerId, OperatingSystem_t operatingSystem)
player->lastLoginSaved = std::max<time_t>(time(nullptr), player->lastLoginSaved + 1);
player->resetIdleTime();
acceptPackets = true;

g_creatureEvents->playerReconnect(player);
}

void ProtocolGame::logout(bool displayEffect, bool forced)
Expand Down

0 comments on commit 1d9119a

Please sign in to comment.