• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[TFS 1.3] Commands: /restart & /stopRestart - to manual restart server with timer/countdown and broadcast notification to online players

Fresh

Quack!
Joined
Oct 21, 2009
Messages
1,837
Solutions
18
Reaction score
615
Location
Poland
Commands: /restart and /stopRestart
Useful manual restart server command that countdown with timer and broadcast notification (reason of restart + timer) to online players (at bottom of screen).

🎬PREVIEW



U S A G E
/restart time_in_miliseconds , reason
example:
/restart 15 , Maintenance restart please hold your chairs bitches!
/stopRestart
If "restart command" was executed by someone before, you can stop it by saying above command.



First of all, please add this inside your global variables like somewhere in global.lua or something:
Lua:
STORAGE_REBOOT = insert_your_free_global_storage

/restart (talkactions/scripts/restart.lua)
Lua:
<talkaction words="/restart" separator=" " script="restart.lua" />
Script:
Lua:
function onSay(player, words, param)

  local restartGlobalStorage = Game.getStorageValue(STORAGE_REBOOT)
  local split = param:split(",")
  local server = "Server"

  local time = tonumber(split[1])
  local reason = split[2]

  if not player:getGroup():getAccess() then
    return true
  end

  if player:getAccountType() < ACCOUNT_TYPE_GOD then
    return false
  end

  if time == nil or time <= 0 then
    player:sendCancelMessage("ERROR: Argument - time, not found! You need to use it properly ex. /restart time, reason")
    return false
  end
  if time >= 1800 then
    player:sendCancelMessage("Time to restart cannot be longer than 30 minutes (1800).")
    return false
  end
  if reason == nil then
    player:sendCancelMessage("ERROR: Argument - reason, not found! You need to use it properly ex. /restart time, reason")
    return false
  end

  if not Game.getStorageValue(STORAGE_REBOOT) then
    for _, tmpPlayer in ipairs(Game.getPlayers()) do
      broadcastMessage("Game World is going to perform maintenance reboot in ".. time .." seconds, because of following reason: ".. reason ..".", MESSAGE_STATUS_WARNING)
    end
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Executed Game World Rebooting! To stop this action, write: /stopRestart")
    Game.setStorageValue(STORAGE_REBOOT, time)
    addEvent(minusTime,1000,tmpPlayer)
    return true
  else
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Rebooting Progress is already running, to Stop this action, write: /stopRestart")
  return true
  end
end

function minusTime()
  local restartGlobalStorage = Game.getStorageValue(STORAGE_REBOOT)
    if restartGlobalStorage == nil then
      return true
    end
    if restartGlobalStorage > 0 then
      for _, tmpPlayer in ipairs(Game.getPlayers()) do
        tmpPlayer:sendTextMessage(MESSAGE_STATUS_SMALL,"Game World is going to reboot in: "..restartGlobalStorage.." seconds.")
      end
      Game.setStorageValue(STORAGE_REBOOT,restartGlobalStorage - 1)
      addEvent(minusTime,1000)
    elseif restartGlobalStorage == 0 then
      for _, tmpPlayer in ipairs(Game.getPlayers()) do
        tmpPlayer:sendTextMessage(MESSAGE_STATUS_SMALL,"Rebooting Game World!")
      end
      saveServer()
      addEvent(rebootServer(), 1000)
    end

    end

/stopRestart (talkactions/scripts/stopRestart.lua)
Lua:
<talkaction words="/stopRestart" separator=" " script="stopRestart.lua" />
Script:
Lua:
function onSay(cid, words, param, channel)
local restartGlobalStorage = Game.getStorageValue(STORAGE_REBOOT)
    if restartGlobalStorage ~= nil then
        broadcastMessage("Game World reboot has been canceled by Game Creator.")
        Game.setStorageValue(STORAGE_REBOOT)
        cid:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You canceled Game World reboot progress.")
    else
        cid:sendTextMessage(MESSAGE_STATUS_SMALL,"First you must call Game World reboot by using: /restart time, reason!")

           return true
    end
end
 
Last edited:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/talkactions/scripts/restart.lua:61: attempt to call global 'rebootServer' (a nil value)
stack traceback:
[C]: in function 'rebootServer'
data/talkactions/scripts/restart.lua:61: in function <data/talkactions/scripts/restart.lua:45>
 
@gicu0770
Yeah i forgot to post code from C++ , so:

game.cpp
C++:
void Game::RebootServer() {
std::cout << "Rebooting server..." << std::endl;
for (const auto& it : players) {  
it.second->loginPosition = it.second->getPosition();  
IOLoginData::savePlayer(it.second);
}
Map::save();
exit(0);
}

game.h
C++:
void RebootServer();

luascript.cpp
C++:
    //rebootServer()
lua_register(m_luaState, "rebootServer", LuaScriptInterface::luaRebootServer);
C++:
int32_t LuaScriptInterface::luaRebootServer(lua_State* L)
{
g_game.RebootServer();   
pushBoolean(L, true);
return 1;
}

luascript.h
C++:
static int32_t luaRebootServer(lua_State* L);

Also this feature NOT tested on Linux,
I only use it on Windows for "test server" purposes, with bash handling (to run .sh files), to use this feature properly u need to "run" server from .sh file that includes inside:
Code:
#!/bin/bash
ulimit -c unlimited
while true; do ./dbuEngine; done
#!/bin/bash directory to executable file
while true; do ./dbuEngine; done /dbuEngine - name of your compiled .exe TFS.
 
why source edits? TFS has the needed functions already
Code:
saveServer()
Game.setGameState(GAME_STATE_SHUTDOWN)
 
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/talkactions/scripts/restart.lua:61: attempt to call global 'rebootServer' (a nil value)
stack traceback:
[C]: in function 'rebootServer'
data/talkactions/scripts/restart.lua:61: in function <data/talkactions/scripts/restart.lua:45>
 
Since I'm back kinda to TFS, I little update this and now it's without source edits, Thanks @zbizu !
If you want to use this, still remember to declare STORAGE_REBOOT somewhere in LIBs.

restart.lua
Lua:
local SERVER_NAME = "Gorgonzola Global"
local RESTART_MAX_TIME = 1800 -- in seconds, 1800 = 30 minutes, max time allowed to pass in talkaction (time after server will be restarted)

local function broadcastRestart(message, messageType)
  for _, tmpPlayer in ipairs(Game.getPlayers()) do
    tmpPlayer:sendTextMessage(messageType, message)
  end
  return TRUE
end

local function restartServer()
  return Game.setGameState(GAME_STATE_SHUTDOWN)
end

local function restartTimer()
  -- If restart is not in progress, not execute timer
  if not Game.getStorageValue(STORAGE_REBOOT) then return TRUE end
  -- If countdown completely elapsed
  if Game.getStorageValue(STORAGE_REBOOT) == 0 then
    broadcastRestart("Rebooting server..", MESSAGE_STATUS_SMALL)
    saveServer()
    Game.setGameState(GAME_STATE_CLOSED)
    addEvent(restartServer, 1000)
    return FALSE
  end
  -- Continue
  Game.setStorageValue(STORAGE_REBOOT,Game.getStorageValue(STORAGE_REBOOT) - 1)
  broadcastRestart(string.format("%s world is going to reboot in %d seconds. Stay safe!", SERVER_NAME, Game.getStorageValue(STORAGE_REBOOT)), MESSAGE_STATUS_SMALL)
  addEvent(restartTimer, 1000)
end

function onSay(player, words, param)

  local split = param:split(",")
  local time = tonumber(split[1])
  local reason = split[2]

  -- Only ACCOUNT_TYPE_GOD can execute this
  if not (player:getAccountType() == ACCOUNT_TYPE_GOD) then return FALSE end
  -- Missing variables (early returns)
  if not time or time == nil or not reason or reason == nil or reason == '' or reason:match("^%s*$") then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "> Failed to execute talkaction /restart - arguments missing!.. Syntax: '/restart time, reason'")
    return FALSE
  end
  if time < 0 then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "> Failed to execute talkaction /restart - `time` cant be lower than 0!")
    return FALSE
  end
  if time >= RESTART_MAX_TIME then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "> Failed to execute talkaction /restart - `time` cant be longer than ".. RESTART_MAX_TIME / 60 .." minutes [".. RESTART_MAX_TIME .. " seconds]")
    return FALSE
  end
  -- If restart is in progress
  if Game.getStorageValue(STORAGE_REBOOT) then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Rebooting is already running. Can't start another one. To stop current, write: /stoprestart")
    return FALSE
  end
  -- Broadcast Message to Online Players
  broadcastRestart(string.format("%s world is going to perform maintenance reboot in %d seconds, due to following reason: %s", SERVER_NAME, time, reason), MESSAGE_STATUS_WARNING)
  -- Launch reboot
  Game.setStorageValue(STORAGE_REBOOT, time)
  addEvent(restartTimer, 1000, tmpPlayer)
  -- For Developers
  player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "> Restart > Executed!.. To stop, write: /stoprestart)")
  print("> ".. player:getName() .." executed talkaction: /restart")
  return TRUE
end

stoprestart.lua
Lua:
function onSay(player, words, param)
  if not (player:getAccountType() == ACCOUNT_TYPE_GOD) then return FALSE end
  if not Game.getStorageValue(STORAGE_REBOOT) then 
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_RED, "There is no reboot in progress! To restart, write: /restart time, reason!")
    return FALSE
  end
  broadcastMessage("Server reboot has been aborted by developers.")
  Game.setStorageValue(STORAGE_REBOOT)
  -- For Developers
  print("> ".. player:getName() .." executed talkaction: /stoprestart")
  player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "> Restart > Aborted!")
  return TRUE
end
 
Last edited:
Back
Top