Ejemplo: comunicación del Server
Category: LuaScript | Subcategory: Added Scripts
¿Para qué sirve?
Responde a la solicitud de prueba del cliente y conserva los eventos existentes.
Ejemplo de uso
-- Guardar en Scripts/Guides/KGGuideServer.lua.
-- Cargar una vez con OpenExtension("Guides") al final de Scripts/GameServer.lua,
-- después de definir los callbacks originales.
-- Ejemplo de comunicación con el cliente.
-- Elegir un subcódigo libre e idéntico al ejemplo del cliente.
if KGGuideServer and KGGuideServer.installed then return end
KGGuideServer = { installed = true, subcode = 0x7E,
prefix = "KG_GUIDE_PING-", lastPing = {} }
local G = KGGuideServer
function G.OnEnter(aIndex)
local user = User.new(aIndex)
if user:getConnected() < 3 then return end
LogAdd("[Lua Guides] " .. user:getName() .. " | nivel " .. tostring(user:getLevel()))
end
function G.OnPacket(aIndex, subcode, packetName)
if subcode ~= G.subcode or type(packetName) ~= "string"
or packetName:sub(1, #G.prefix) ~= G.prefix then return false end
local user = User.new(aIndex)
if user:getConnected() < 3 or packetName ~= G.prefix .. user:getName() then
ClearPacket(packetName)
return true
end
local version = GetBytePacket(packetName, -1)
local nonce = GetWordPacket(packetName, -1)
ClearPacket(packetName)
if version ~= 1 or nonce < 1 then return true end
local now = os.time()
if G.lastPing[aIndex] == now then return true end
G.lastPing[aIndex] = now
CreatePacket(packetName, G.subcode)
SetBytePacket(packetName, 1)
SetWordPacket(packetName, nonce)
SendPacket(packetName, aIndex) -- servidor: requiere destinatario
ClearPacket(packetName)
return true
end
-- Conservar los callbacks originales y sus retornos.
local originalEnter = EnterCharacter
function EnterCharacter(aIndex)
local result
if type(originalEnter) == "function" then result = originalEnter(aIndex) end
G.OnEnter(aIndex)
return result
end
local originalProtocol = GameServerProtocol
function GameServerProtocol(aIndex, subcode, packetName)
if G.OnPacket(aIndex, subcode, packetName) then return end
if type(originalProtocol) == "function" then
return originalProtocol(aIndex, subcode, packetName)
end
end