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

Move players record to lua #4840

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion data/cpplinter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,6 @@ MoveEvent = {}
---@field interval fun(self:GlobalEvent, interval:integer):boolean
---@field onStartup fun():boolean
---@field onShutdown fun():boolean
---@field onRecord fun(current:integer, old:integer):boolean
---@field onThink fun(interval:integer):boolean
---@field onTime fun(interval:integer):boolean
---@operator call(string):GlobalEvent
Expand Down
1 change: 0 additions & 1 deletion data/globalevents/globalevents.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<globalevents>
<globalevent type="startup" name="ServerStartup" script="startup.lua" />
<globalevent type="record" name="PlayerRecord" script="record.lua" />
<globalevent name="Server Save" time="09:55:00" script="server_save.lua" />
<!--
<globalevent name="timer_example" time="12:00:00" script="my_script.lua" />
Expand Down
4 changes: 0 additions & 4 deletions data/globalevents/scripts/record.lua

This file was deleted.

1 change: 1 addition & 0 deletions data/lib/core/game/lib.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dofile('data/lib/core/game/account_storage.lua')
dofile('data/lib/core/game/global_storage.lua')
dofile('data/lib/core/game/players_record.lua')
dofile('data/lib/core/game/quest.lua')
dofile('data/lib/core/game/world_light.lua')
dofile('data/lib/core/game/world_time.lua')
Expand Down
15 changes: 15 additions & 0 deletions data/lib/core/game/players_record.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
do
local playersRecord = 0

function Game.getPlayersRecord()
return playersRecord
end

function Game.setPlayersRecord(value)
playersRecord = value
end

function Game.savePlayersRecord()
db.query("UPDATE `server_config` SET `value` = '" .. Game.getPlayersRecord() .. "' WHERE `config` = 'players_record'")
end
end
13 changes: 13 additions & 0 deletions data/scripts/creaturescripts/player/players_record.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local event = CreatureEvent("PlayersRecord")

function event.onLogin(player)
local players = #Game.getPlayers()
local record = Game.getPlayersRecord()
if players > record then
Game.setPlayersRecord(players)
addEvent(Game.broadcastMessage, 150, "New record: " .. players .. " players are logged in.", MESSAGE_STATUS_DEFAULT)
end
return true
end

event:register()
28 changes: 28 additions & 0 deletions data/scripts/globalevents/players_record.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
do
-- Load players record
local event = GlobalEvent("LoadPlayersRecord")

function event.onStartup()
local resultId = db.storeQuery("SELECT `value` FROM `server_config` WHERE `config` = 'players_record'")
if resultId then
Game.setPlayersRecord(result.getNumber(resultId, "value"))
else
db.query("INSERT INTO `server_config` (`config`, `value`) VALUES ('players_record', '0')")
end
return true
end

event:register()
end

do
-- Save players record
local event = GlobalEvent("SavePlayersRecord")

function event.onSave()
Game.savePlayersRecord()
return true
end

event:register()
end
51 changes: 15 additions & 36 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ extern MoveEvents* g_moveEvents;
extern Weapons* g_weapons;
extern Scripts* g_scripts;

Game::Game()
Game::Game() : scriptInterface("Game Interface")
{
scriptInterface.initState();

offlineTrainingWindow.defaultEnterButton = 0;
offlineTrainingWindow.defaultEscapeButton = 1;
offlineTrainingWindow.choices.emplace_back("Sword Fighting and Shielding", SKILL_SWORD);
Expand Down Expand Up @@ -93,8 +95,6 @@ void Game::setGameState(GameState_t newState)

mounts.loadFromXml();

loadPlayersRecord();

g_globalEvents->startup();
break;
}
Expand Down Expand Up @@ -770,6 +770,18 @@ void Game::playerMoveCreature(Player* player, Creature* movingCreature, const Po
}
}

uint32_t Game::getPlayersRecord()
{
lua_State* L = scriptInterface.getLuaState();
lua_getglobal(L, "Game");
lua_getfield(L, -1, "getPlayersRecord");
lua_call(L, 0, 1);

uint32_t playersRecord = lua_tointeger(L, -1);
lua_pop(L, 1);
return playersRecord;
}

ReturnValue Game::internalMoveCreature(Creature* creature, Direction direction, uint32_t flags /*= 0*/)
{
creature->setLastPosition(creature->getPosition());
Expand Down Expand Up @@ -4894,39 +4906,6 @@ void Game::updatePlayerShield(Player* player)
}
}

void Game::checkPlayersRecord()
{
const size_t playersOnline = getPlayersOnline();
if (playersOnline > playersRecord) {
uint32_t previousRecord = playersRecord;
playersRecord = playersOnline;

for (auto& it : g_globalEvents->getEventMap(GLOBALEVENT_RECORD)) {
it.second.executeRecord(playersRecord, previousRecord);
}
updatePlayersRecord();
}
}

void Game::updatePlayersRecord() const
{
Database& db = Database::getInstance();
db.executeQuery(
fmt::format("UPDATE `server_config` SET `value` = '{:d}' WHERE `config` = 'players_record'", playersRecord));
}

void Game::loadPlayersRecord()
{
Database& db = Database::getInstance();

DBResult_ptr result = db.storeQuery("SELECT `value` FROM `server_config` WHERE `config` = 'players_record'");
if (result) {
playersRecord = result->getNumber<uint32_t>("value");
} else {
db.executeQuery("INSERT INTO `server_config` (`config`, `value`) VALUES ('players_record', '0')");
}
}

void Game::playerInviteToParty(uint32_t playerId, uint32_t invitedId)
{
if (playerId == invitedId) {
Expand Down
10 changes: 3 additions & 7 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Game
size_t getPlayersOnline() const { return players.size(); }
size_t getMonstersOnline() const { return monsters.size(); }
size_t getNpcsOnline() const { return npcs.size(); }
uint32_t getPlayersRecord() const { return playersRecord; }
uint32_t getPlayersRecord();

ReturnValue internalMoveCreature(Creature* creature, Direction direction, uint32_t flags = 0);
ReturnValue internalMoveCreature(Creature& creature, Tile& toTile, uint32_t flags = 0);
Expand Down Expand Up @@ -290,9 +290,6 @@ class Game
bool internalCreatureSay(Creature* creature, SpeakClasses type, const std::string& text, bool ghostMode,
SpectatorVec* spectatorsPtr = nullptr, const Position* pos = nullptr, bool echo = false);

void loadPlayersRecord();
void checkPlayersRecord();

void sendGuildMotd(uint32_t playerId);
void kickPlayer(uint32_t playerId, bool displayEffect);
void playerDebugAssert(uint32_t playerId, const std::string& assertLine, const std::string& date,
Expand Down Expand Up @@ -502,6 +499,8 @@ class Game
void checkDecay();
void internalDecayItem(Item* item);

LuaScriptInterface scriptInterface;

std::unordered_map<uint32_t, Player*> players;
std::unordered_map<std::string, Player*> mappedPlayerNames;
std::unordered_map<uint32_t, Player*> mappedPlayerGuids;
Expand Down Expand Up @@ -534,9 +533,6 @@ class Game
WorldType_t worldType = WORLD_TYPE_PVP;

ServiceManager* serviceManager = nullptr;

void updatePlayersRecord() const;
uint32_t playersRecord = 0;
};

#endif // FS_GAME_H
24 changes: 0 additions & 24 deletions src/globalevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ GlobalEventMap GlobalEvents::getEventMap(GlobalEvent_t type)
return timerMap;
case GLOBALEVENT_STARTUP:
case GLOBALEVENT_SHUTDOWN:
case GLOBALEVENT_RECORD:
case GLOBALEVENT_SAVE: {
GlobalEventMap retMap;
for (const auto& it : serverMap) {
Expand Down Expand Up @@ -288,8 +287,6 @@ bool GlobalEvent::configureEvent(const pugi::xml_node& node)
eventType = GLOBALEVENT_STARTUP;
} else if (caseInsensitiveEqual(value, "shutdown")) {
eventType = GLOBALEVENT_SHUTDOWN;
} else if (caseInsensitiveEqual(value, "record")) {
eventType = GLOBALEVENT_RECORD;
} else if (caseInsensitiveEqual(value, "save")) {
eventType = GLOBALEVENT_SAVE;
} else {
Expand All @@ -315,8 +312,6 @@ std::string_view GlobalEvent::getScriptEventName() const
return "onStartup";
case GLOBALEVENT_SHUTDOWN:
return "onShutdown";
case GLOBALEVENT_RECORD:
return "onRecord";
case GLOBALEVENT_SAVE:
return "onSave";
case GLOBALEVENT_TIMER:
Expand All @@ -326,25 +321,6 @@ std::string_view GlobalEvent::getScriptEventName() const
}
}

bool GlobalEvent::executeRecord(uint32_t current, uint32_t old)
{
// onRecord(current, old)
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - GlobalEvent::executeRecord] Call stack overflow" << std::endl;
return false;
}

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

lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(scriptId);

lua_pushnumber(L, current);
lua_pushnumber(L, old);
return scriptInterface->callFunction(2);
}

bool GlobalEvent::executeEvent() const
{
if (!tfs::lua::reserveScriptEnv()) {
Expand Down
2 changes: 0 additions & 2 deletions src/globalevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ enum GlobalEvent_t

GLOBALEVENT_STARTUP,
GLOBALEVENT_SHUTDOWN,
GLOBALEVENT_RECORD,
GLOBALEVENT_SAVE,
};

Expand Down Expand Up @@ -66,7 +65,6 @@ class GlobalEvent final : public Event

bool configureEvent(const pugi::xml_node& node) override;

bool executeRecord(uint32_t current, uint32_t old);
bool executeEvent() const;

GlobalEvent_t getEventType() const { return eventType; }
Expand Down
5 changes: 1 addition & 4 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,6 @@ void LuaScriptInterface::registerFunctions()
registerMethod(L, "GlobalEvent", "onTime", LuaScriptInterface::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onStartup", LuaScriptInterface::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onShutdown", LuaScriptInterface::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onRecord", LuaScriptInterface::luaGlobalEventOnCallback);
registerMethod(L, "GlobalEvent", "onSave", LuaScriptInterface::luaGlobalEventOnCallback);

// Weapon
Expand Down Expand Up @@ -18166,8 +18165,6 @@ int LuaScriptInterface::luaGlobalEventType(lua_State* L)
global->setEventType(GLOBALEVENT_STARTUP);
} else if (tmpStr == "shutdown") {
global->setEventType(GLOBALEVENT_SHUTDOWN);
} else if (tmpStr == "record") {
global->setEventType(GLOBALEVENT_RECORD);
} else if (tmpStr == "timer") {
global->setEventType(GLOBALEVENT_TIMER);
} else if (tmpStr == "save") {
Expand Down Expand Up @@ -18210,7 +18207,7 @@ int LuaScriptInterface::luaGlobalEventRegister(lua_State* L)

int LuaScriptInterface::luaGlobalEventOnCallback(lua_State* L)
{
// globalevent:onThink / record / etc. (callback)
// globalevent:onThink / etc. (callback)
GlobalEvent* globalevent = tfs::lua::getUserdata<GlobalEvent>(L, 1);
if (globalevent) {
if (!globalevent->loadCallback()) {
Expand Down
1 change: 0 additions & 1 deletion src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,6 @@ void Player::onCreatureAppear(Creature* creature, bool isLogin)
}
}

g_game.checkPlayersRecord();
IOLoginData::updateOnlineStatus(guid, true);
}
}
Expand Down
Loading