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:
Someone logins then floods this messages:
What is happen?
Scripts:
lagprotect_player.lua
lagprotect_server.lua
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: