• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

Lua Function getOtsysTime() + getPlayerPing(cid) + doPlayerSendPing(cid)

Mock

Mock the bear (MTB)
Joined
Jul 29, 2008
Messages
619
Reaction score
106
Location
Brazil
I did this function by things that already exist on open tibia.

for tfs 03.6

If you dont know the tibia client already got a ping system so i am using it.
And getOtsystime is like os.time() + os.clock(), you got year, month, day, hour, minut, second, miliseconds :D

All functions by me ^^

Just open luascripts.cpp and add it:

Near:
Code:
//doSetMonsterOutfit(cid, name, time)
lua_register(m_luaState, "doSetMonsterOutfit", LuaScriptInterface::luaSetMonsterOutfit);
Add:
Code:
//doPlayerSendPing(cid)
	lua_register(m_luaState, "doPlayerSendPing", LuaScriptInterface::luadoPlayerSendPing);
	//getPlayerLastPing(cid)
	lua_register(m_luaState, "getPlayerLastPing", LuaScriptInterface::luagetPlayerLastPing);
	//getPlayerLastPong(cid)
	lua_register(m_luaState, "getPlayerLastPong", LuaScriptInterface::luagetPlayerLastPong);
	//getOtsysTime(cid)
	lua_register(m_luaState, "getOtsysTime", LuaScriptInterface::luagetOtsysTime);
Almost in the end add:
Code:
int32_t LuaScriptInterface::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)
    {
 			player->client->sendPing();
			lua_pushboolean(L, true);
    }else{
          lua_pushboolean(L, false);         
          }
	lua_pushboolean(L, true);

	return 1;
}
int32_t LuaScriptInterface::luagetOtsysTime(lua_State* L)
{
    //getOtsysTime()
	lua_pushnumber(L, OTSYS_TIME());
	return 1;
}
int32_t LuaScriptInterface::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 LuaScriptInterface::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;
}

In luascript.h near some another thing like this add it:
Code:
	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);


In 050-functions.lua add:
Lua:
function getPlayerPing(cid) -- getPlayerPing By mock
	local c = getPlayerLastPong(cid)
	local l = getPlayerLastPing(cid)
	if not c or not l then
		return 'Disconected'
	end
	local ping = math.floor((c-l)/10)
	if ping < 0 then
		if ping*-1 > 2000 then
			return 'Disconected'
		end
		return 'Wating response'
	end
	return ping
end

So you can get the player ping in ms :p

And if you want a safe ping result you can add this lib:

http://otland.net/f163/lua-c-ping-lib-ping-getping-cid-storage-121183
 
Last edited:
Error:

IA6sEx6jKa.png
 
mEaPRUFfW.png

Line 2096:
[cpp] lua_register(m_luaState, "getOtsysTime", LuaInterface::luagetOtsysTime;[/cpp]
 
Mock, i'm using TFS 0.3.6 (;

I'll try the protocol... (cpp or h?)
 
Mock could you convert it to 0.4?
protocolgame.h: In static member function 'static int32_t LuaInterface::luadoPlayerSendPing(lua_State*)':
protocolgame.h:185: error: 'void ProtocolGame::sendPing()' is private
luascript.cpp:5035: error: within this context
cc1plus: warnings being treated as errors
luascript.cpp: In static member function 'static int32_t LuaInterface::luagetPlayerLastPing(lua_State*)':
luascript.cpp:5063: error: unused variable 'timeNow'
make[1]: *** [luascript.o] Error 1
 
Hmm, thanks.
It's working for now, I think.

Good job, again, Mock.
 
friend class ProtocolGame at liascript.h?
;p


Mock
doPlayerSetPing it has possibility of reduce ping of player to 0?
Like Proxy Programs. to reduce excessives lags?

find on protocolgame.h

[cpp]
sendPing()[/cpp]

and change it for

[cpp]
public:
sendPing()
private:[/cpp]
 
Back
Top Bottom