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-04-02 07:55:01 +00:00
|
|
|
dofile "lua/client/client_util.lua"
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback = {}
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
function Client:initialize()
|
2022-03-31 05:29:23 +00:00
|
|
|
self.client = fk.ClientInstance
|
2022-03-02 12:56:37 +00:00
|
|
|
self.notifyUI = function(self, command, jsonData)
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.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-03-31 05:29:23 +00:00
|
|
|
local cb = fk.client_callback[command]
|
2022-01-24 02:23:08 +00:00
|
|
|
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-31 05:29:23 +00:00
|
|
|
fk.client_callback["Setup"] = function(jsonData)
|
2022-03-27 06:49:41 +00:00
|
|
|
-- jsonData: [ int id, string screenName, string avatar ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, name, avatar = data[1], data[2], data[3]
|
2022-03-31 05:29:23 +00:00
|
|
|
local self = fk.Self
|
2022-03-27 06:49:41 +00:00
|
|
|
self:setId(id)
|
|
|
|
self:setScreenName(name)
|
|
|
|
self:setAvatar(avatar)
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["AddPlayer"] = function(jsonData)
|
2022-03-27 06:49:41 +00:00
|
|
|
-- 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-31 05:29:23 +00:00
|
|
|
local player = fk.ClientInstance:addPlayer(id, name, avatar)
|
2022-03-28 14:24:30 +00:00
|
|
|
table.insert(ClientInstance.players, ClientPlayer:new(player))
|
|
|
|
ClientInstance:notifyUI("AddPlayer", jsonData)
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["RemovePlayer"] = function(jsonData)
|
2022-03-28 14:24:30 +00:00
|
|
|
-- jsonData: [ int id ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id = data[1]
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.ClientInstance:removePlayer(id)
|
2022-03-28 14:24:30 +00:00
|
|
|
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
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["ArrangeSeats"] = function(jsonData)
|
2022-03-28 14:24:30 +00:00
|
|
|
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()
|