• 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 when he is lagged and server is lagged

warriorfrog

Active Member
Joined
Jul 29, 2015
Messages
334
Reaction score
35
I did this 2 scripts (with help from some people here on forum ofc) that protect players when they are lagged, and protect players when server is lagged

In my mind it is working, but idk if it is working right and idk if i should do something in other way...

So i need someone with exp to tell me if i should do something in other way and if something looks break...

The scripts:
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_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

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


Script base (sources changes):

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
 
Back
Top