Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new global event OnSave #2025

Merged
merged 27 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
199a638
update
luanluciano93 Dec 14, 2023
afcf482
update
luanluciano93 Dec 14, 2023
9c05a2f
Update signals.cpp
luanluciano93 Dec 14, 2023
46f80a9
update
luanluciano93 Dec 14, 2023
3a6f9e6
Merge branch 'main' into save-event
luanluciano93 Jan 16, 2024
4d5333a
Merge branch 'main' into save-event
luanluciano93 Feb 2, 2024
0ed898e
Merge branch 'main' into save-event
dudantas Feb 6, 2024
10b47aa
Merge branch 'main' into save-event
luanluciano93 Feb 14, 2024
0e67fd8
Merge branch 'main' into save-event
elsongabriel Feb 19, 2024
726c7ad
Merge branch 'main' into save-event
elsongabriel Feb 29, 2024
21aa7c0
Merge branch 'main' into save-event
luanluciano93 Mar 10, 2024
cee0e39
Merge branch 'main' into save-event
luanluciano93 Mar 11, 2024
36f26a0
Merge branch 'main' into save-event
luanluciano93 Mar 19, 2024
2ccc61d
Merge branch 'main' into save-event
luanluciano93 Apr 19, 2024
219d802
Merge branch 'main' into save-event
luanluciano93 Apr 29, 2024
4f953b3
Code format - (Clang-format)
github-actions[bot] Apr 29, 2024
80489e4
Merge branch 'main' into save-event
luanluciano93 May 9, 2024
f969ddb
Merge branch 'main' into save-event
luanluciano93 May 14, 2024
2da51ae
Merge branch 'main' into save-event
luanluciano93 Jun 7, 2024
67e8d7a
Merge branch 'main' into save-event
elsongabriel Jun 11, 2024
a57431d
Merge branch 'main' into save-event
luanluciano93 Jun 12, 2024
6b6a9dd
Merge branch 'main' into save-event
luanluciano93 Jun 13, 2024
17eb056
Merge branch 'main' into save-event
luanluciano93 Jun 17, 2024
95f71a4
Merge branch 'main' into save-event
luanluciano93 Jun 23, 2024
c09d5ce
Merge branch 'main' into save-event
luanluciano93 Jun 24, 2024
9e28f61
Merge branch 'main' into save-event
luanluciano93 Jul 3, 2024
1c77eb6
Merge branch 'main' into save-event
luanluciano93 Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -350,7 +350,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 @@ -368,6 +369,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
1 change: 1 addition & 0 deletions src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@
}

int GlobalFunctions::luaSaveServer(lua_State* L) {
g_globalEvents().save();

Check failure on line 726 in src/lua/functions/core/game/global_functions.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

‘g_globalEvents’ was not declared in this scope

Check failure on line 726 in src/lua/functions/core/game/global_functions.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘g_globalEvents’ was not declared in this scope
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 @@ -64,6 +64,14 @@
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 @@ -159,7 +167,8 @@
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 @@ -190,6 +199,8 @@
return "onPeriodChange";
case GLOBALEVENT_ON_THINK:
return "onThink";
case GLOBALEVENT_ON_SAVE:

Check failure on line 202 in src/lua/global/globalevent.cpp

View workflow job for this annotation

GitHub Actions / windows-2022-windows-release

'GLOBALEVENT_ON_SAVE': undeclared identifier

Check failure on line 202 in src/lua/global/globalevent.cpp

View workflow job for this annotation

GitHub Actions / windows-2022-windows-release

expression did not evaluate to a constant

Check failure on line 202 in src/lua/global/globalevent.cpp

View workflow job for this annotation

GitHub Actions / windows-2022-windows-release

case expression not constant

Check failure on line 202 in src/lua/global/globalevent.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-release

‘GLOBALEVENT_ON_SAVE’ was not declared in this scope; did you mean ‘GLOBALEVENT_SAVE’?

Check failure on line 202 in src/lua/global/globalevent.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-release

‘GLOBALEVENT_ON_SAVE’ was not declared in this scope; did you mean ‘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
Loading