TESTA ESSAS DUAS VERSAO E VE SE RESOLVE
local function sendOpcodeJson(player, action, data)
local MAX_PACKET_SIZE = 8192 -- mais seguro que 65000
local buffer = json.encode({ action = action, data = data })
local chunks = {}
for i = 1, #buffer, MAX_PACKET_SIZE do
chunks[#chunks + 1] = buffer:sub(i, i + MAX_PACKET_SIZE - 1)
end
for index, chunk in ipairs(chunks) do
local msg = NetworkMessage()
msg:addByte(50)
msg:addByte(OPCODE)
local flag
if #chunks == 1 then
flag = "S" -- apenas um pacote
elseif index == 1 then
flag = "S" -- start
elseif index == #chunks then
flag = "E" -- end
else
flag = "P" -- partial
end
msg:addString(flag .. chunk)
msg:sendToPlayer(player)
end
end
--------------------------------------------------------------------------------------------------
local partialBuffer = ""
function getOpCode(protocol, opcode, json_data)
-- Se for string (em vez de table), processar fragmentação
if type(json_data) == "string" then
local flag = json_data:sub(1,1)
local content = json_data:sub(2)
if flag == "S" then
partialBuffer = content
return
elseif flag == "P" then
partialBuffer = partialBuffer .. content
return
elseif flag == "E" then
partialBuffer = partialBuffer .. content
json_data = json.decode(partialBuffer)
partialBuffer = ""
else
json_data = json.decode(json_data)
end
end
local action = json_data.action
local data = json_data.data
if not action or not data then
return false
end
if action == "sendInformation" then
showInformation(data)
elseif action == "lists" then
pokemons = data
elseif action == "updateList" then
local id = data.id
local pokename = capitalizeFirstLetter(pokemons[id].name)
for _, child in ipairs(pokedexWindow.panelSearch:getChildren()) do
if capitalizeFirstLetter(child:getId()) == pokename then
child.nome:setText(pokename)
child.nome:setColor("green")
child.pokemonImageSearch:setOutfit({ type = pokemons[id].outfit.type })
break
end
end
pokemons[id].found = true
end
end