2022-03-28 14:24:30 +00:00
|
|
|
Client = class('Client')
|
|
|
|
|
|
|
|
-- load client classes
|
2022-03-30 06:14:40 +00:00
|
|
|
ClientPlayer = require "client.clientplayer"
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
freekill.client_callback = {}
|
|
|
|
|
|
|
|
function Client:initialize()
|
|
|
|
self.client = freekill.ClientInstance
|
2022-03-02 12:56:37 +00:00
|
|
|
self.notifyUI = function(self, command, jsonData)
|
|
|
|
freekill.Backend:emitNotifyUI(command, jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
end
|
2022-03-02 12:56:37 +00:00
|
|
|
self.client.callback = function(_self, command, jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
local cb = freekill.client_callback[command]
|
|
|
|
if (type(cb) == "function") then
|
2022-03-02 12:56:37 +00:00
|
|
|
cb(jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
else
|
2022-03-02 12:56:37 +00:00
|
|
|
self:notifyUI(command, jsonData);
|
2022-01-24 02:23:08 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
|
|
|
|
self.players = {} -- ClientPlayer[]
|
2022-01-24 02:23:08 +00:00
|
|
|
end
|
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
freekill.client_callback["Setup"] = function(jsonData)
|
|
|
|
-- jsonData: [ int id, string screenName, string avatar ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, name, avatar = data[1], data[2], data[3]
|
|
|
|
local self = freekill.Self
|
|
|
|
self:setId(id)
|
|
|
|
self:setScreenName(name)
|
|
|
|
self:setAvatar(avatar)
|
|
|
|
end
|
|
|
|
|
|
|
|
freekill.client_callback["AddPlayer"] = function(jsonData)
|
|
|
|
-- jsonData: [ int id, string screenName, string avatar ]
|
|
|
|
-- when other player enter the room, we create clientplayer(C and lua) for them
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, name, avatar = data[1], data[2], data[3]
|
2022-03-28 14:24:30 +00:00
|
|
|
local player = freekill.ClientInstance:addPlayer(id, name, avatar)
|
|
|
|
table.insert(ClientInstance.players, ClientPlayer:new(player))
|
|
|
|
ClientInstance:notifyUI("AddPlayer", jsonData)
|
|
|
|
end
|
|
|
|
|
|
|
|
freekill.client_callback["RemovePlayer"] = function(jsonData)
|
|
|
|
-- jsonData: [ int id ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id = data[1]
|
|
|
|
freekill.ClientInstance:removePlayer(id)
|
|
|
|
for _, p in ipairs(ClientInstance.players) do
|
|
|
|
if p.player:getId() == id then
|
|
|
|
table.removeOne(ClientInstance.players, p)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ClientInstance:notifyUI("RemovePlayer", jsonData)
|
|
|
|
end
|
|
|
|
|
|
|
|
freekill.client_callback["ArrangeSeats"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local n = #ClientInstance.players
|
|
|
|
local players = {}
|
|
|
|
local function findPlayer(id)
|
|
|
|
for _, p in ipairs(ClientInstance.players) do
|
|
|
|
if p.player:getId() == id then return p end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, n do
|
|
|
|
table.insert(players, findPlayer(data[i]))
|
|
|
|
end
|
|
|
|
ClientInstance.players = players
|
|
|
|
|
|
|
|
ClientInstance:notifyUI("ArrangeSeats", jsonData)
|
2022-03-27 06:49:41 +00:00
|
|
|
end
|
|
|
|
|
2022-01-24 02:23:08 +00:00
|
|
|
-- Create ClientInstance (used by Lua)
|
|
|
|
ClientInstance = Client:new()
|