2022-04-14 18:22:00 +08:00
|
|
|
---@class Client
|
|
|
|
---@field client fk.Client
|
|
|
|
---@field players ClientPlayer[]
|
2022-09-14 13:01:10 +08:00
|
|
|
---@field alive_players ClientPlayer[]
|
|
|
|
---@field current ClientPlayer
|
|
|
|
---@field discard_pile integer[]
|
2022-03-28 22:24:30 +08:00
|
|
|
Client = class('Client')
|
|
|
|
|
|
|
|
-- load client classes
|
2022-03-30 14:14:40 +08:00
|
|
|
ClientPlayer = require "client.clientplayer"
|
2022-01-24 10:23:08 +08:00
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
fk.client_callback = {}
|
2022-01-24 10:23:08 +08:00
|
|
|
|
|
|
|
function Client:initialize()
|
2022-04-30 15:27:56 +08:00
|
|
|
self.client = fk.ClientInstance
|
|
|
|
self.notifyUI = function(self, command, jsonData)
|
|
|
|
fk.Backend:emitNotifyUI(command, jsonData)
|
|
|
|
end
|
|
|
|
self.client.callback = function(_self, command, jsonData)
|
|
|
|
local cb = fk.client_callback[command]
|
|
|
|
if (type(cb) == "function") then
|
|
|
|
cb(jsonData)
|
|
|
|
else
|
|
|
|
self:notifyUI(command, jsonData);
|
2022-01-24 10:23:08 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.players = {} -- ClientPlayer[]
|
2022-09-14 13:01:10 +08:00
|
|
|
self.alive_players = {}
|
|
|
|
self.discard_pile = {}
|
2022-01-24 10:23:08 +08:00
|
|
|
end
|
|
|
|
|
2022-04-14 18:22:00 +08:00
|
|
|
---@param id integer
|
|
|
|
---@return ClientPlayer
|
2022-09-14 13:01:10 +08:00
|
|
|
function Client:getPlayerById(id)
|
2022-04-30 15:27:56 +08:00
|
|
|
for _, p in ipairs(self.players) do
|
2022-09-14 13:01:10 +08:00
|
|
|
if p.id == id then return p end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
return nil
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
|
|
|
|
2022-09-14 13:01:10 +08:00
|
|
|
function Client:moveCards(moves)
|
|
|
|
for _, move in ipairs(moves) do
|
|
|
|
if move.from and move.fromArea then
|
|
|
|
local from = self:getPlayerById(move.from)
|
|
|
|
if from.id ~= Self.id and move.fromArea == Card.PlayerHand then
|
|
|
|
for i = 1, #move.ids do
|
|
|
|
table.remove(from.player_cards[Player.Hand])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
from:removeCards(move.fromArea, move.ids)
|
|
|
|
end
|
|
|
|
elseif move.fromArea == Card.DiscardPile then
|
|
|
|
table.removeOne(self.discard_pile, move.ids[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
if move.to and move.toArea then
|
|
|
|
self:getPlayerById(move.to):addCards(move.toArea, move.ids)
|
|
|
|
elseif move.toArea == Card.DiscardPile then
|
|
|
|
table.insert(self.discard_pile, move.ids[1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
fk.client_callback["Setup"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int id, string screenName, string avatar ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, name, avatar = data[1], data[2], data[3]
|
|
|
|
local self = fk.Self
|
|
|
|
self:setId(id)
|
|
|
|
self:setScreenName(name)
|
|
|
|
self:setAvatar(avatar)
|
|
|
|
Self = ClientPlayer:new(fk.Self)
|
2022-05-02 14:00:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["EnterRoom"] = function(jsonData)
|
|
|
|
ClientInstance.players = {Self}
|
2022-09-14 13:01:10 +08:00
|
|
|
ClientInstance.alive_players = {Self}
|
|
|
|
ClientInstance.discard_pile = {}
|
2022-05-02 14:00:47 +08:00
|
|
|
ClientInstance:notifyUI("EnterRoom", jsonData)
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
fk.client_callback["AddPlayer"] = function(jsonData)
|
2022-04-30 15:27:56 +08: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]
|
|
|
|
local player = fk.ClientInstance:addPlayer(id, name, avatar)
|
2022-09-14 13:01:10 +08:00
|
|
|
local p = ClientPlayer:new(player)
|
|
|
|
table.insert(ClientInstance.players, p)
|
|
|
|
table.insert(ClientInstance.alive_players, p)
|
2022-04-30 15:27:56 +08:00
|
|
|
ClientInstance:notifyUI("AddPlayer", jsonData)
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
fk.client_callback["RemovePlayer"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int id ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id = data[1]
|
|
|
|
for _, p in ipairs(ClientInstance.players) do
|
|
|
|
if p.player:getId() == id then
|
|
|
|
table.removeOne(ClientInstance.players, p)
|
2022-09-14 13:01:10 +08:00
|
|
|
table.removeOne(ClientInstance.alive_players, p)
|
2022-04-30 15:27:56 +08:00
|
|
|
break
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
fk.ClientInstance:removePlayer(id)
|
|
|
|
ClientInstance:notifyUI("RemovePlayer", jsonData)
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
fk.client_callback["ArrangeSeats"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local n = #ClientInstance.players
|
|
|
|
local players = {}
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for i = 1, n do
|
2022-09-14 13:01:10 +08:00
|
|
|
local p = ClientInstance:getPlayerById(data[i])
|
|
|
|
p.seat = i
|
|
|
|
table.insert(players, p)
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
ClientInstance.players = players
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
ClientInstance:notifyUI("ArrangeSeats", jsonData)
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
|
|
|
|
2022-04-14 18:22:00 +08:00
|
|
|
fk.client_callback["PropertyUpdate"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int id, string property_name, value ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, name, value = data[1], data[2], data[3]
|
2022-09-14 13:01:10 +08:00
|
|
|
ClientInstance:getPlayerById(id)[name] = value
|
2022-04-30 15:27:56 +08:00
|
|
|
ClientInstance:notifyUI("PropertyUpdate", jsonData)
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
|
|
|
|
2022-09-14 13:01:10 +08:00
|
|
|
fk.client_callback["AskForCardChosen"] = function(jsonData)
|
|
|
|
-- jsonData: [ int target_id, string flag, int reason ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, flag, reason = data[1], data[2], data[3]
|
|
|
|
local target = ClientInstance:getPlayerById(id)
|
|
|
|
local hand = target.player_cards[Player.Hand]
|
|
|
|
local equip = target.player_cards[Player.Equip]
|
|
|
|
local judge = target.player_cards[Player.Judge]
|
|
|
|
if not string.find(flag, "h") then
|
|
|
|
hand = {}
|
|
|
|
end
|
|
|
|
if not string.find(flag, "e") then
|
|
|
|
equip = {}
|
|
|
|
end
|
|
|
|
if not string.find(flag, "j") then
|
|
|
|
judge = {}
|
|
|
|
end
|
|
|
|
local ui_data = {hand, equip, judge, reason}
|
|
|
|
ClientInstance:notifyUI("AskForCardChosen", json.encode(ui_data))
|
|
|
|
end
|
|
|
|
|
2022-04-14 18:22:00 +08:00
|
|
|
--- separated moves to many moves(one card per move)
|
|
|
|
---@param moves CardsMoveStruct[]
|
|
|
|
local function separateMoves(moves)
|
2022-04-30 15:27:56 +08:00
|
|
|
local ret = {} ---@type CardsMoveInfo[]
|
|
|
|
for _, move in ipairs(moves) do
|
|
|
|
for _, info in ipairs(move.moveInfo) do
|
|
|
|
table.insert(ret, {
|
|
|
|
ids = {info.cardId},
|
|
|
|
from = move.from,
|
|
|
|
to = move.to,
|
|
|
|
toArea = move.toArea,
|
|
|
|
fromArea = info.fromArea,
|
|
|
|
})
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
return ret
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
|
|
|
|
2022-09-14 13:01:10 +08:00
|
|
|
--- merge separated moves that information is the same
|
2022-04-14 18:22:00 +08:00
|
|
|
local function mergeMoves(moves)
|
2022-04-30 15:27:56 +08:00
|
|
|
local ret = {}
|
|
|
|
local temp = {}
|
|
|
|
for _, move in ipairs(moves) do
|
2022-09-14 13:01:10 +08:00
|
|
|
local info = string.format("%q,%q,%q,%q",
|
|
|
|
move.from, move.to, move.fromArea, move.toArea)
|
|
|
|
if temp[info] == nil then
|
|
|
|
temp[info] = {
|
2022-04-30 15:27:56 +08:00
|
|
|
ids = {},
|
|
|
|
from = move.from,
|
|
|
|
to = move.to,
|
|
|
|
fromArea = move.fromArea,
|
|
|
|
toArea = move.toArea
|
|
|
|
}
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
2022-09-14 13:01:10 +08:00
|
|
|
table.insert(temp[info].ids, move.ids[1])
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
for _, v in pairs(temp) do
|
|
|
|
table.insert(ret, v)
|
|
|
|
end
|
|
|
|
return ret
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["MoveCards"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: CardsMoveStruct[]
|
|
|
|
local raw_moves = json.decode(jsonData)
|
|
|
|
local separated = separateMoves(raw_moves)
|
2022-09-14 13:01:10 +08:00
|
|
|
ClientInstance:moveCards(separated)
|
2022-04-30 15:27:56 +08:00
|
|
|
local merged = mergeMoves(separated)
|
|
|
|
ClientInstance:notifyUI("MoveCards", json.encode(merged))
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
|
|
|
|
2022-09-14 13:01:10 +08:00
|
|
|
fk.client_callback["LoseSkill"] = function(jsonData)
|
|
|
|
-- jsonData: [ int player_id, string skill_name ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, skill_name = data[1], data[2]
|
|
|
|
local target = ClientInstance:getPlayerById(id)
|
|
|
|
local skill = Fk.skills[skill_name]
|
|
|
|
target:loseSkill(skill)
|
|
|
|
if skill.visible then
|
|
|
|
ClientInstance:notifyUI("LoseSkill", jsonData)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["AddSkill"] = function(jsonData)
|
|
|
|
-- jsonData: [ int player_id, string skill_name ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, skill_name = data[1], data[2]
|
|
|
|
local target = ClientInstance:getPlayerById(id)
|
|
|
|
local skill = Fk.skills[skill_name]
|
|
|
|
target:addSkill(skill)
|
|
|
|
if skill.visible then
|
|
|
|
ClientInstance:notifyUI("AddSkill", jsonData)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-15 11:17:13 +08:00
|
|
|
fk.client_callback["AskForUseActiveSkill"] = function(jsonData)
|
|
|
|
-- jsonData: [ string skill_name, string prompt, bool cancelable. json extra_data ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local skill = Fk.skills[data[1]]
|
|
|
|
local extra_data = json.decode(data[4])
|
|
|
|
for k, v in pairs(extra_data) do
|
|
|
|
skill[k] = v
|
|
|
|
end
|
|
|
|
ClientInstance:notifyUI("AskForUseActiveSkill", jsonData)
|
|
|
|
end
|
|
|
|
|
2022-01-24 10:23:08 +08:00
|
|
|
-- Create ClientInstance (used by Lua)
|
|
|
|
ClientInstance = Client:new()
|
2022-04-30 15:27:56 +08:00
|
|
|
dofile "lua/client/client_util.lua"
|