Skip to content

Commit

Permalink
feat: new global event OnSave (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luan Luciano authored Jul 11, 2024
1 parent b799436 commit 3f12a85
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions data/libs/functions/revscriptsys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ do
self:type("periodchange")
self:onPeriodChange(value)
return
elseif key == "onSave" then
self:type("save")
self:onSave(value)
return
end
rawset(self, key, value)
end
Expand Down
5 changes: 4 additions & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ void Game::setGameState(GameState_t newState) {
}

case GAME_STATE_SHUTDOWN: {
g_globalEvents().execute(GLOBALEVENT_SHUTDOWN);
g_globalEvents().save();
g_globalEvents().shutdown();

// kick all players that are still online
auto it = players.begin();
Expand All @@ -611,6 +612,8 @@ void Game::setGameState(GameState_t newState) {
}

case GAME_STATE_CLOSED: {
g_globalEvents().save();

/* kick all players without the CanAlwaysLogin flag */
auto it = players.begin();
while (it != players.end()) {
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lua/functions/core/game/global_functions.hpp"
#include "lua/scripts/lua_environment.hpp"
#include "lua/scripts/script_environment.hpp"
#include "lua/global/globalevent.hpp"
#include "server/network/protocol/protocolstatus.hpp"
#include "creatures/players/wheel/player_wheel.hpp"
#include "lua/global/lua_timer_event_descr.hpp"
Expand Down Expand Up @@ -706,6 +707,7 @@ int GlobalFunctions::luaStopEvent(lua_State* L) {
}

int GlobalFunctions::luaSaveServer(lua_State* L) {
g_globalEvents().save();
g_saveManager().scheduleAll();
pushBoolean(L, true);
return 1;
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/events/global_event_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ int GlobalEventFunctions::luaGlobalEventType(lua_State* L) {
global->setEventType(GLOBALEVENT_PERIODCHANGE);
} else if (tmpStr == "onthink") {
global->setEventType(GLOBALEVENT_ON_THINK);
} else if (tmpStr == "save") {
global->setEventType(GLOBALEVENT_SAVE);
} else {
g_logger().error("[GlobalEventFunctions::luaGlobalEventType] - "
"Invalid type for global event: {}");
Expand Down
1 change: 1 addition & 0 deletions src/lua/functions/events/global_event_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GlobalEventFunctions final : LuaScriptInterface {
registerMethod(L, "GlobalEvent", "onShutdown", GlobalEventFunctions::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onRecord", GlobalEventFunctions::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onPeriodChange", GlobalEventFunctions::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onSave", GlobalEventFunctions::luaGlobalEventOnCallback);
}

private:
Expand Down
13 changes: 12 additions & 1 deletion src/lua/global/globalevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ void GlobalEvents::startup() const {
execute(GLOBALEVENT_STARTUP);
}

void GlobalEvents::shutdown() const {
execute(GLOBALEVENT_SHUTDOWN);
}

void GlobalEvents::save() const {
execute(GLOBALEVENT_SAVE);
}

void GlobalEvents::timer() {
time_t now = time(nullptr);

Expand Down Expand Up @@ -165,7 +173,8 @@ GlobalEventMap GlobalEvents::getEventMap(GlobalEvent_t type) {
case GLOBALEVENT_PERIODCHANGE:
case GLOBALEVENT_STARTUP:
case GLOBALEVENT_SHUTDOWN:
case GLOBALEVENT_RECORD: {
case GLOBALEVENT_RECORD:
case GLOBALEVENT_SAVE: {
GlobalEventMap retMap;
for (const auto &it : serverMap) {
if (it.second->getEventType() == type) {
Expand Down Expand Up @@ -196,6 +205,8 @@ std::string GlobalEvent::getScriptTypeName() const {
return "onPeriodChange";
case GLOBALEVENT_ON_THINK:
return "onThink";
case GLOBALEVENT_SAVE:
return "onSave";
default:
g_logger().error("[GlobalEvent::getScriptTypeName] - Invalid event type");
return std::string();
Expand Down
2 changes: 2 additions & 0 deletions src/lua/global/globalevent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class GlobalEvents final : public Scripts {
}

void startup() const;
void shutdown() const;
void save() const;

void timer();
void think();
Expand Down
1 change: 1 addition & 0 deletions src/lua/lua_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ enum GlobalEvent_t {
GLOBALEVENT_RECORD,
GLOBALEVENT_PERIODCHANGE,
GLOBALEVENT_ON_THINK,
GLOBALEVENT_SAVE,
};

enum ModuleType_t {
Expand Down
2 changes: 2 additions & 0 deletions src/server/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lib/thread/thread_pool.hpp"
#include "lua/creature/events.hpp"
#include "lua/scripts/lua_environment.hpp"
#include "lua/global/globalevent.hpp"
#include "server/signals.hpp"

Signals::Signals(asio::io_service &service) :
Expand Down Expand Up @@ -92,6 +93,7 @@ void Signals::sigtermHandler() {
void Signals::sigusr1Handler() {
// Dispatcher thread
g_logger().info("SIGUSR1 received, saving the game state...");
g_globalEvents().save();
g_saveManager().scheduleAll();
}

Expand Down

0 comments on commit 3f12a85

Please sign in to comment.