• 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 0.X Protect players from LAG

warriorfrog

Active Member
Joined
Jul 29, 2015
Messages
334
Reaction score
35
I'm trying to create 2 scripts that protect players with lag

1 player protect
when a player is lagged and he is not attacking anyone and is not being attacked -> set a storage that set deathloss to 0 (items,xp,skills)

2 server
when server get lagged -> set a storage that set deathloss to 0 (items,xp,skills)


But i need two helps
1- A help for someone with more experience to say if the way i think for this is script is good...
its this good? should i change something?

2- There are some bugs on console:

No one logged floods this message:
Code:
[12:35:20.759] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[12:35:25.903] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[12:35:30.919] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[12:35:35.942] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[12:35:40.977] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[12:35:45.995] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[12:35:51.016] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player




Someone logins then floods this messages:
Code:
[12:36:13.014] [Error - GlobalEvent Interface]
[12:36:13.014] data/globalevents/scripts/lagprotect_server.lua:onThink
[12:36:13.014] Description:
[12:36:13.014] data/globalevents/scripts/lagprotect_server.lua:7: attempt to call global 'playerGetLastPing' (a nil value)
[12:36:13.014] stack traceback:
[12:36:13.014]     data/globalevents/scripts/lagprotect_server.lua:7: in function <data/globalevents/scripts/lagprotect_server.lua:1>
[12:36:13.014] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_server
[12:36:16.125] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player

[12:36:18.233] [Error - GlobalEvent Interface]
[12:36:18.233] data/globalevents/scripts/lagprotect_server.lua:onThink
[12:36:18.233] Description:
[12:36:18.233] data/globalevents/scripts/lagprotect_server.lua:7: attempt to call global 'playerGetLastPing' (a nil value)
[12:36:18.233] stack traceback:
[12:36:18.233]     data/globalevents/scripts/lagprotect_server.lua:7: in function <data/globalevents/scripts/lagprotect_server.lua:1>
[12:36:18.233] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_server
[12:36:21.572] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player

[12:36:23.435] [Error - GlobalEvent Interface]
[12:36:23.435] data/globalevents/scripts/lagprotect_server.lua:onThink
[12:36:23.435] Description:
[12:36:23.436] data/globalevents/scripts/lagprotect_server.lua:7: attempt to call global 'playerGetLastPing' (a nil value)
[12:36:23.436] stack traceback:
[12:36:23.436]     data/globalevents/scripts/lagprotect_server.lua:7: in function <data/globalevents/scripts/lagprotect_server.lua:1>
[12:36:23.436] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_server


What is happen?


Scripts:

Code:
<globalevent name="lagprotect_player" interval="5000" event="script" value="lagprotect_player.lua"/>
<globalevent name="lagprotect_server" interval="5200" event="script" value="lagprotect_server.lua"/>

lagprotect_player.lua
Code:
function imBeingAttacked(cid)
    for tid, _ in ipairs(getOnlinePlayers()) do -- Loop through all online players
        if(getCreatureTarget(tid) == cid) then
            return true
        end
    end
    return false
end

function myTargetIsPlayer(cid)
    local myTarget = getCreatureTarget(cid)
    if(isPlayer(myTarget)) then
        return true
    else
        return false
    end
end

function onThink(interval, lastExecution)
    local MAX_DELAY_SECS_RESPONSE = 2
    for tid, _ in ipairs(getOnlinePlayers()) do -- Loop through all online players
        if isPlayer(tid) then
            if getPlayerLastPing(tid) + MAX_DELAY_SECS_RESPONSE < os.time() then
                if imBeingAttacked(tid) == false and myTargetIsPlayer(tid) == false then
                      doPlayerSendTextMessage(tid,TALKTYPE_BROADCAST, "[LAG PROTECT] Your connection is lagged! Your exp,skills,items are protected.")
                      setPlayerStorageValue(tid, 7, 1)
                      addEvent(function(tid)
                        setPlayerStorageValue(tid, 7, -1)
                      end, 5000, tid)
                end
            end
        end
    end
end

lagprotect_server.lua
Code:
function onThink(interval, lastExecution)
    local MAX_DELAY_SECS_RESPONSE = 2
    local average_server_ping = 0
    local players_online = 0
    -- loop all players to get ping avarage and players online
    for _, cid in ipairs(getPlayersOnline()) do
        average_server_ping = getPlayerLastPing(cid) + average_server_ping
        players_online = players_online + 1
    end
    average_server_ping = (average_server_ping / players_online)
    -- check if server avarage ping is higher then limit
    if average_server_ping + MAX_DELAY_SECS_RESPONSE < os.time() then
        -- if it is higher, kick everyone
        doBroadcastMessage("[DDOS PROTECT] Server is under attack or lagged! Your exp,skills,items are protected.")
        for _, cid in ipairs(getPlayersOnline()) do
            if isPlayer(tid) then
              setPlayerStorageValue(cid, 7, 1)
              addEvent(function(cid)
                setPlayerStorageValue(cid, 7, -1)
              end, 5200, cid)
            end
        end
    end
    return TRUE
end


TALKACTION TO PRINT TEST \/

!ping prints
Code:
function onSay(cid, words, param, channel)
    print("doPlayerSendPing: ", doPlayerSendPing(cid))
    print("getPlayerLastPing: ", getPlayerLastPing(cid))
    print("getPlayerLastPong: ", getPlayerLastPong(cid))
    print("getOtsysTime: ", getOtsysTime(cid))
    return true
end

Code:
doPlayerSendPing:

getPlayerLastPing:
1586826102309
getPlayerLastPong:
1586826098349
getOtsysTime:
1586826102309

SOURCE BASE \/
source code edited base: Fir3element/3777 (https://github.com/Fir3element/3777)

SOURCES EDITS \/

luascript.cpp (https://pastebin.com/1RAxBrpx)

lines 2089-2098
Code:
    // ping 1
    //doPlayerSendPing(cid)
    lua_register(m_luaState, "doPlayerSendPing", LuaInterface::luaDoPlayerSendPing);
    //getPlayerLastPing(cid)
    lua_register(m_luaState, "getPlayerLastPing", LuaInterface::luaGetPlayerLastPing);
    //getPlayerLastPong(cid)
    lua_register(m_luaState, "getPlayerLastPong", LuaInterface::luaGetPlayerLastPong);
    //getOtsysTime(cid)
    lua_register(m_luaState, "getOtsysTime", LuaInterface::luaGetOtsysTime);
    // /ping 1

lines 7390-7450
Code:
// ping 2
// Adaptado by Yan Liima(Night for tibiaking.com)
int32_t LuaInterface::luaDoPlayerSendPing(lua_State* L)
{
    //doPlayerSendPing(cid)
    ScriptEnviroment* env = getEnv();
    Player* player = env->getPlayerByUID(popNumber(L));
    if(!player)
    {
        lua_pushboolean(L, false);
        return 1;
    }
    int64_t timeNow = OTSYS_TIME();
    player->lastPing = timeNow;
    if(player->client)
    {
            void sendPing();
            lua_pushboolean(L, true);
    }else{
          lua_pushboolean(L, false);     
          }
    lua_pushboolean(L, true);

    return 1;
}
int32_t LuaInterface::luaGetOtsysTime(lua_State* L)
{
    //getOtsysTime()
    lua_pushnumber(L, OTSYS_TIME());
    return 1;
}
int32_t LuaInterface::luaGetPlayerLastPing(lua_State* L)
{
    //getPlayerLastPing(cid)
    ScriptEnviroment* env = getEnv();
    Player* player = env->getPlayerByUID(popNumber(L));
    if(!player)
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
         lua_pushboolean(L, false);
        return 1;
    }
    int64_t timeNow = OTSYS_TIME();
    lua_pushnumber(L, player->lastPing);
    return 1;
}
int32_t LuaInterface::luaGetPlayerLastPong(lua_State* L)
{
    //getPlayerLastPong(cid)
    ScriptEnviroment* env = getEnv();
    Player* player = env->getPlayerByUID(popNumber(L));
    if(!player)
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
         lua_pushboolean(L, false);
        return 1;
    }
    lua_pushnumber(L, player->lastPong);
    return 1;
}
// /ping 2

luascript.h ([C++] luascript.h - Pastebin.com (https://pastebin.com/JtdnJS8a))
lines 347-352
Code:
        // Ping
        static int32_t luaDoPlayerSendPing(lua_State* L);
        static int32_t luaGetPlayerLastPing(lua_State* L);
        static int32_t luaGetPlayerLastPong(lua_State* L);
        static int32_t luaGetOtsysTime(lua_State* L);
        // /Ping
 
Last edited:
Solution
[12:35:20.759] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player

I'm not 100% sure but I think this message indicates that you are missing "return TRUE" in your global event.

If you add return TRUE at the end of the script, it says to the server, that the global event was executed successfully.

Alpha

Relentless
Premium User
Joined
Apr 3, 2011
Messages
637
Solutions
35
Reaction score
438
Location
South Korea
The playerGetLastPing function does not exist in the scope of the Globalevents script interface. Make sure you properly registered it in the engine.
 
OP
warriorfrog

warriorfrog

Active Member
Joined
Jul 29, 2015
Messages
334
Reaction score
35
The playerGetLastPing function does not exist in the scope of the Globalevents script interface. Make sure you properly registered it in the engine.

You were rigth lagprotect_server was wrong...

playerGetLastPing part should be getPlayerLastPing

i fix the script:
Code:
function onThink(interval, lastExecution)
    local MAX_DELAY_SECS_RESPONSE = 2
    local average_server_ping = 0
    local players_online = 0
    -- loop all players to get ping avarage and players online
    for _, cid in ipairs(getPlayersOnline()) do
        average_server_ping = getPlayerLastPing(cid) + average_server_ping
        players_online = players_online + 1
    end
    average_server_ping = (average_server_ping / players_online)
    -- check if server avarage ping is higher then limit
    if average_server_ping + MAX_DELAY_SECS_RESPONSE < os.time() then
        -- if it is higher, kick everyone
        doBroadcastMessage("[DDOS PROTECT] Server is under attack or lagged! Your exp,skills,items are protected.")
        for _, cid in ipairs(getPlayersOnline()) do
            if isPlayer(tid) then
              setPlayerStorageValue(cid, 7, 1)
              addEvent(function(cid)
                setPlayerStorageValue(cid, 7, -1)
              end, 5200, cid)
            end
        end
    end
    return TRUE
end

so stops to flood the second error...


but the first error still there, all the time:
Code:
[14:54:00.765] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:05.780] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:10.794] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:15.807] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:20.822] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:25.836] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:30.853] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:35.867] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:40.882] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:45.896] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:50.912] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player
[14:54:55.930] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player


why?

---

what about i did the script?
u think it is good?
Post automatically merged:

i've edit the main topic
 
Last edited:

slaw

Developer
Joined
Aug 27, 2007
Messages
3,271
Solutions
99
Reaction score
759
Location
Germany
GitHub
slawkens
[12:35:20.759] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player

I'm not 100% sure but I think this message indicates that you are missing "return TRUE" in your global event.

If you add return TRUE at the end of the script, it says to the server, that the global event was executed successfully.
 
Solution
OP
warriorfrog

warriorfrog

Active Member
Joined
Jul 29, 2015
Messages
334
Reaction score
35
[12:35:20.759] [Error - GlobalEvents::think] Couldn't execute event: lagprotect_player

I'm not 100% sure but I think this message indicates that you are missing "return TRUE" in your global event.

If you add return TRUE at the end of the script, it says to the server, that the global event was executed successfully.

It was it...
I've add a return TRUE in the end and stops the warnings:

---

Guys, everything is running without errors, but idk if it is working and if it is good...
What u guys think? @slaw @Alpha and everyone else with more experience...

Is it working well?
The script is good?
There is something i should change?

Code:
    <!-- PLAYERS LAGGED / DDOS PROTECT -->
    <!-- if player is lagged AND is not under atk AND is not atking -->
    <!-- SET STORAGE to do not lose anything in death_loss_lua.lua -->
    <globalevent name="lagprotect_player" interval="5000" event="script" value="lagprotect_player.lua"/>
    <!-- if players online avarage ping is high -->
    <!-- SET STORAGE to do not lose anything in death_loss_lua.lua -->
    <globalevent name="lagprotect_server" interval="5200" event="script" value="lagprotect_server.lua"/>

lagprotect_server.lua
Code:
function onThink(interval, lastExecution)
    local MAX_DELAY_SECS_RESPONSE = 2
    local average_server_ping = 0
    local players_online = 0
    -- loop all players to get ping avarage and players online
    for _, cid in ipairs(getPlayersOnline()) do
        average_server_ping = getPlayerLastPing(cid) + average_server_ping
        players_online = players_online + 1
    end
    average_server_ping = (average_server_ping / players_online)
    -- check if server avarage ping is higher then limit
    if average_server_ping + MAX_DELAY_SECS_RESPONSE < os.time() then
        -- if it is higher, kick everyone
        doBroadcastMessage("[DDOS PROTECT] Server is under attack or lagged! Your exp,skills,items are protected.")
        for _, cid in ipairs(getPlayersOnline()) do
            if isPlayer(tid) then
              setPlayerStorageValue(cid, 7, 1)
              addEvent(function(cid)
                setPlayerStorageValue(cid, 7, -1)
              end, 5200, cid)
            end
        end
    end
    return TRUE
end

lagprotect_player.lua
Code:
function imBeingAttacked(cid)
    for tid, _ in ipairs(getOnlinePlayers()) do -- Loop through all online players
        if(getCreatureTarget(tid) == cid) then
            return true
        end
    end
    return false
end

function myTargetIsPlayer(cid)
    local myTarget = getCreatureTarget(cid)
    if(isPlayer(myTarget)) then
        return true
    else
        return false
    end
end

function onThink(interval, lastExecution)
    local MAX_DELAY_SECS_RESPONSE = 2
  for tid, _ in ipairs(getOnlinePlayers()) do -- Loop through all online players
      if isPlayer(tid) then
          if getPlayerLastPing(tid) + MAX_DELAY_SECS_RESPONSE < os.time() then
              if imBeingAttacked(tid) == false and myTargetIsPlayer(tid) == false then
                    doPlayerSendTextMessage(tid,TALKTYPE_BROADCAST, "[LAG PROTECT] Your connection is lagged! Your exp,skills,items are protected.")
                    setPlayerStorageValue(tid, 7, 1)
                    addEvent(function(tid)
                      setPlayerStorageValue(tid, 7, -1)
                    end, 5000, tid)
              end
          end
      end
  end
  return TRUE
end
 
Top