2023-04-09 05:35:35 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-04-14 10:22:00 +00:00
|
|
|
---@class Client
|
2023-03-26 09:32:45 +00:00
|
|
|
---@field public client fk.Client
|
|
|
|
---@field public players ClientPlayer[]
|
|
|
|
---@field public alive_players ClientPlayer[]
|
|
|
|
---@field public observers ClientPlayer[]
|
|
|
|
---@field public current ClientPlayer
|
|
|
|
---@field public discard_pile integer[]
|
|
|
|
---@field public status_skills Skill[]
|
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
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback = {}
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
function Client:initialize()
|
2022-04-30 07:27:56 +00: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]
|
2023-04-10 07:55:06 +00:00
|
|
|
|
|
|
|
Fk.currentResponsePattern = nil
|
|
|
|
Fk.currentResponseReason = nil
|
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if (type(cb) == "function") then
|
|
|
|
cb(jsonData)
|
|
|
|
else
|
|
|
|
self:notifyUI(command, jsonData);
|
2022-01-24 02:23:08 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
self.players = {} -- ClientPlayer[]
|
2022-09-14 05:01:10 +00:00
|
|
|
self.alive_players = {}
|
2023-02-15 11:54:35 +00:00
|
|
|
self.observers = {}
|
2022-09-14 05:01:10 +00:00
|
|
|
self.discard_pile = {}
|
2023-01-04 06:21:29 +00:00
|
|
|
self.status_skills = {}
|
|
|
|
for class, skills in pairs(Fk.global_status_skill) do
|
|
|
|
self.status_skills[class] = {table.unpack(skills)}
|
|
|
|
end
|
2022-01-24 02:23:08 +00:00
|
|
|
end
|
|
|
|
|
2022-04-14 10:22:00 +00:00
|
|
|
---@param id integer
|
|
|
|
---@return ClientPlayer
|
2022-09-14 05:01:10 +00:00
|
|
|
function Client:getPlayerById(id)
|
2022-04-30 07:27:56 +00:00
|
|
|
for _, p in ipairs(self.players) do
|
2022-09-14 05:01:10 +00:00
|
|
|
if p.id == id then return p end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
|
|
|
return nil
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
|
|
|
|
2023-01-29 10:11:41 +00:00
|
|
|
---@param cardId integer | card
|
|
|
|
---@return CardArea
|
|
|
|
function Client:getCardArea(cardId)
|
|
|
|
if type(cardId) ~= "number" then
|
|
|
|
assert(cardId and cardId:isInstanceOf(Card))
|
|
|
|
cardId = cardId:getEffectiveId()
|
|
|
|
end
|
|
|
|
if table.contains(Self.player_cards[Player.Hand], cardId) then
|
|
|
|
return Card.PlayerHand
|
|
|
|
end
|
|
|
|
if table.contains(Self.player_cards[Player.Equip], cardId) then
|
|
|
|
return Card.PlayerEquip
|
|
|
|
end
|
2023-03-04 17:28:59 +00:00
|
|
|
for _, t in pairs(Self.special_cards) do
|
|
|
|
if table.contains(t, cardId) then
|
|
|
|
return Card.PlayerSpecial
|
|
|
|
end
|
|
|
|
end
|
2023-01-29 10:11:41 +00:00
|
|
|
error("Client:getCardArea can only judge cards in your hand or equip area")
|
|
|
|
end
|
|
|
|
|
2022-09-14 05:01:10 +00: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
|
2023-04-13 12:17:39 +00:00
|
|
|
if table.contains({ Player.Hand, Player.Equip, Player.Judge, Player.Special }, move.fromArea) then
|
|
|
|
from:removeCards(move.fromArea, move.ids, move.fromSpecialName)
|
|
|
|
end
|
2022-09-14 05:01:10 +00:00
|
|
|
end
|
|
|
|
elseif move.fromArea == Card.DiscardPile then
|
|
|
|
table.removeOne(self.discard_pile, move.ids[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
if move.to and move.toArea then
|
2023-03-07 02:21:56 +00:00
|
|
|
local ids = move.ids
|
|
|
|
if (move.to ~= Self.id and move.toArea == Card.PlayerHand) or table.contains(ids, -1) then
|
|
|
|
ids = table.map(ids, function() return -1 end)
|
|
|
|
end
|
|
|
|
self:getPlayerById(move.to):addCards(move.toArea, ids, move.specialName)
|
2022-09-14 05:01:10 +00:00
|
|
|
elseif move.toArea == Card.DiscardPile then
|
|
|
|
table.insert(self.discard_pile, move.ids[1])
|
|
|
|
end
|
2023-02-15 11:54:35 +00:00
|
|
|
|
2023-02-15 13:20:40 +00:00
|
|
|
if (move.ids[1] ~= -1) then
|
|
|
|
Fk:filterCard(move.ids[1], ClientInstance:getPlayerById(move.to))
|
|
|
|
end
|
2022-09-14 05:01:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
---@param msg LogMessage
|
|
|
|
function Client:appendLog(msg)
|
|
|
|
local data = msg
|
|
|
|
local function getPlayerStr(pid, color)
|
|
|
|
if not pid then
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
local ret = self:getPlayerById(pid)
|
|
|
|
ret = ret.general
|
|
|
|
ret = Fk:translate(ret)
|
|
|
|
ret = string.format('<font color="' .. color .. '"><b>%s</b></font>', ret)
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local from = getPlayerStr(data.from, "#0C8F0C")
|
|
|
|
|
|
|
|
local to = data.to or {}
|
|
|
|
local to_str = {}
|
|
|
|
for _, id in ipairs(to) do
|
|
|
|
table.insert(to_str, getPlayerStr(id, "#CC3131"))
|
|
|
|
end
|
|
|
|
to = table.concat(to_str, ", ")
|
|
|
|
|
|
|
|
local card = data.card or {}
|
|
|
|
local allUnknown = true
|
|
|
|
local unknownCount = 0
|
|
|
|
for _, id in ipairs(card) do
|
|
|
|
if id ~= -1 then
|
|
|
|
allUnknown = false
|
|
|
|
else
|
|
|
|
unknownCount = unknownCount + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if allUnknown then
|
|
|
|
card = ""
|
|
|
|
else
|
|
|
|
local card_str = {}
|
|
|
|
for _, id in ipairs(card) do
|
2023-02-15 11:54:35 +00:00
|
|
|
table.insert(card_str, Fk:getCardById(id, true):toLogString())
|
2022-12-18 04:52:52 +00:00
|
|
|
end
|
|
|
|
if unknownCount > 0 then
|
|
|
|
table.insert(card_str, Fk:translate("unknown_card")
|
|
|
|
.. unknownCount == 1 and "x" .. unknownCount or "")
|
|
|
|
end
|
|
|
|
card = table.concat(card_str, ", ")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function parseArg(arg)
|
|
|
|
arg = arg or ""
|
|
|
|
arg = Fk:translate(arg)
|
|
|
|
arg = string.format('<font color="#0598BC"><b>%s</b></font>', arg)
|
|
|
|
return arg
|
|
|
|
end
|
|
|
|
|
|
|
|
local arg = parseArg(data.arg)
|
|
|
|
local arg2 = parseArg(data.arg2)
|
|
|
|
local arg3 = parseArg(data.arg3)
|
|
|
|
|
|
|
|
local log = Fk:translate(data.type)
|
|
|
|
log = string.gsub(log, "%%from", from)
|
|
|
|
log = string.gsub(log, "%%to", to)
|
|
|
|
log = string.gsub(log, "%%card", card)
|
|
|
|
log = string.gsub(log, "%%arg2", arg2)
|
|
|
|
log = string.gsub(log, "%%arg3", arg3)
|
|
|
|
log = string.gsub(log, "%%arg", arg)
|
|
|
|
self:notifyUI("GameLog", log)
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["Setup"] = function(jsonData)
|
2022-04-30 07:27:56 +00: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 06:00:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["EnterRoom"] = function(jsonData)
|
2022-12-20 04:51:54 +00:00
|
|
|
Self = ClientPlayer:new(fk.Self)
|
2023-03-07 02:21:56 +00:00
|
|
|
ClientInstance = Client:new() -- clear old client data
|
2022-05-02 06:00:47 +00:00
|
|
|
ClientInstance.players = {Self}
|
2022-09-14 05:01:10 +00:00
|
|
|
ClientInstance.alive_players = {Self}
|
|
|
|
ClientInstance.discard_pile = {}
|
2023-03-14 06:12:13 +00:00
|
|
|
|
|
|
|
local data = json.decode(jsonData)[3]
|
|
|
|
Fk.disabled_packs = data.disabledPack
|
2022-05-02 06:00:47 +00:00
|
|
|
ClientInstance:notifyUI("EnterRoom", jsonData)
|
2022-03-27 06:49:41 +00:00
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["AddPlayer"] = function(jsonData)
|
2022-04-30 07:27:56 +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]
|
|
|
|
local player = fk.ClientInstance:addPlayer(id, name, avatar)
|
2022-09-14 05:01:10 +00:00
|
|
|
local p = ClientPlayer:new(player)
|
|
|
|
table.insert(ClientInstance.players, p)
|
|
|
|
table.insert(ClientInstance.alive_players, p)
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance:notifyUI("AddPlayer", jsonData)
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["RemovePlayer"] = function(jsonData)
|
2022-04-30 07:27:56 +00: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 05:01:10 +00:00
|
|
|
table.removeOne(ClientInstance.alive_players, p)
|
2022-04-30 07:27:56 +00:00
|
|
|
break
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-12-18 04:52:52 +00:00
|
|
|
if id ~= Self.id then
|
|
|
|
fk.ClientInstance:removePlayer(id)
|
|
|
|
ClientInstance:notifyUI("RemovePlayer", jsonData)
|
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2023-02-15 11:54:35 +00:00
|
|
|
fk.client_callback["AddObserver"] = function(jsonData)
|
|
|
|
-- jsonData: [ int id, string screenName, string avatar ]
|
|
|
|
-- when observer enter the room, we create lua clientplayer for them
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, name, avatar = data[1], data[2], data[3]
|
|
|
|
local player = {
|
|
|
|
getId = function() return id end,
|
|
|
|
getScreenName = function() return name end,
|
|
|
|
getAvatar = function() return avatar end,
|
|
|
|
}
|
|
|
|
local p = ClientPlayer:new(player)
|
|
|
|
table.insert(ClientInstance.observers, p)
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["RemoveObserver"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id = data[1]
|
|
|
|
for _, p in ipairs(ClientInstance.observers) do
|
|
|
|
if p.player:getId() == id then
|
|
|
|
table.removeOne(ClientInstance.observers, p)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.client_callback["ArrangeSeats"] = function(jsonData)
|
2022-04-30 07:27:56 +00:00
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local n = #ClientInstance.players
|
|
|
|
local players = {}
|
2022-03-28 14:24:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
for i = 1, n do
|
2022-09-14 05:01:10 +00:00
|
|
|
local p = ClientInstance:getPlayerById(data[i])
|
|
|
|
p.seat = i
|
|
|
|
table.insert(players, p)
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-12-20 04:51:54 +00:00
|
|
|
|
|
|
|
for i = 1, #players - 1 do
|
|
|
|
players[i].next = players[i + 1]
|
|
|
|
end
|
|
|
|
players[#players].next = players[1]
|
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance.players = players
|
2022-03-28 14:24:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance:notifyUI("ArrangeSeats", jsonData)
|
2022-03-27 06:49:41 +00:00
|
|
|
end
|
|
|
|
|
2022-04-14 10:22:00 +00:00
|
|
|
fk.client_callback["PropertyUpdate"] = function(jsonData)
|
2022-04-30 07:27:56 +00: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 05:01:10 +00:00
|
|
|
ClientInstance:getPlayerById(id)[name] = value
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance:notifyUI("PropertyUpdate", jsonData)
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
|
|
|
|
2022-09-14 05:01:10 +00: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 = {}
|
2023-03-07 02:21:56 +00:00
|
|
|
elseif target.id ~= Self.id then
|
|
|
|
-- FIXME: can not see other's handcard
|
|
|
|
hand = table.map(hand, function() return -1 end)
|
2022-09-14 05:01:10 +00:00
|
|
|
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
|
|
|
|
|
2023-03-01 13:41:16 +00:00
|
|
|
fk.client_callback["AskForCardsChosen"] = function(jsonData)
|
|
|
|
-- jsonData: [ int target_id, int min, int max, string flag, int reason ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, min, max, flag, reason = data[1], data[2], data[3], data[4], data[5]
|
|
|
|
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, min, max, reason}
|
|
|
|
ClientInstance:notifyUI("AskForCardsChosen", json.encode(ui_data))
|
|
|
|
end
|
|
|
|
|
2022-04-14 10:22:00 +00:00
|
|
|
--- separated moves to many moves(one card per move)
|
|
|
|
---@param moves CardsMoveStruct[]
|
|
|
|
local function separateMoves(moves)
|
2022-04-30 07:27:56 +00: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-12-18 04:52:52 +00:00
|
|
|
moveReason = move.moveReason,
|
2023-02-21 05:44:24 +00:00
|
|
|
specialName = move.specialName,
|
|
|
|
fromSpecialName = info.fromSpecialName,
|
2022-04-30 07:27:56 +00:00
|
|
|
})
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
|
|
|
return ret
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
|
|
|
|
2022-09-14 05:01:10 +00:00
|
|
|
--- merge separated moves that information is the same
|
2022-04-14 10:22:00 +00:00
|
|
|
local function mergeMoves(moves)
|
2022-04-30 07:27:56 +00:00
|
|
|
local ret = {}
|
|
|
|
local temp = {}
|
|
|
|
for _, move in ipairs(moves) do
|
2023-02-26 08:51:29 +00:00
|
|
|
local info = string.format("%q,%q,%q,%q,%s,%s",
|
2023-02-21 05:44:24 +00:00
|
|
|
move.from, move.to, move.fromArea, move.toArea,
|
|
|
|
move.specialName, move.fromSpecialName)
|
2023-02-26 08:51:29 +00:00
|
|
|
if temp[info] == nil then
|
2022-09-14 05:01:10 +00:00
|
|
|
temp[info] = {
|
2022-04-30 07:27:56 +00:00
|
|
|
ids = {},
|
|
|
|
from = move.from,
|
|
|
|
to = move.to,
|
|
|
|
fromArea = move.fromArea,
|
2022-12-18 04:52:52 +00:00
|
|
|
toArea = move.toArea,
|
|
|
|
moveReason = move.moveReason,
|
2023-02-21 05:44:24 +00:00
|
|
|
specialName = move.specialName,
|
|
|
|
fromSpecialName = move.fromSpecialName,
|
2022-04-30 07:27:56 +00:00
|
|
|
}
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
2022-09-14 05:01:10 +00:00
|
|
|
table.insert(temp[info].ids, move.ids[1])
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
|
|
|
for _, v in pairs(temp) do
|
|
|
|
table.insert(ret, v)
|
|
|
|
end
|
|
|
|
return ret
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
local function sendMoveCardLog(move)
|
2023-03-18 07:35:44 +00:00
|
|
|
if #move.ids == 0 then return end
|
|
|
|
local hidden = table.contains(move.ids, -1)
|
|
|
|
local msgtype
|
|
|
|
|
|
|
|
if move.from and move.toArea == Card.DrawPile then
|
|
|
|
msgtype = hidden and "$PutCard" or "$PutKnownCard"
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = msgtype,
|
|
|
|
from = move.from,
|
|
|
|
card = move.ids,
|
|
|
|
arg = #move.ids,
|
|
|
|
}
|
|
|
|
elseif move.toArea == Card.PlayerSpecial then
|
|
|
|
msgtype = hidden and "$RemoveCardFromGame" or "$AddToPile"
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = msgtype,
|
|
|
|
arg = move.specialName,
|
|
|
|
arg2 = #move.ids,
|
|
|
|
card = move.ids,
|
|
|
|
}
|
|
|
|
elseif move.fromArea == Card.PlayerSpecial and move.to then
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = "$GetCardsFromPile",
|
|
|
|
from = move.to,
|
|
|
|
arg = move.fromSpecialName,
|
|
|
|
arg2 = #move.ids,
|
|
|
|
card = move.ids,
|
|
|
|
}
|
|
|
|
elseif move.fromArea == Card.DrawPile and move.toArea == Card.PlayerHand then
|
2022-12-18 04:52:52 +00:00
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = "$DrawCards",
|
|
|
|
from = move.to,
|
|
|
|
card = move.ids,
|
|
|
|
arg = #move.ids,
|
|
|
|
}
|
2023-03-18 07:35:44 +00:00
|
|
|
elseif (move.fromArea == Card.Processing or move.fromArea == Card.PlayerJudge)
|
|
|
|
and move.toArea == Card.PlayerHand then
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = "$GotCardBack",
|
|
|
|
from = move.to,
|
|
|
|
card = move.ids,
|
|
|
|
arg = #move.ids,
|
|
|
|
}
|
|
|
|
elseif move.fromArea == Card.DiscardPile and move.toArea == Card.PlayerHand then
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = "$RecycleCard",
|
|
|
|
from = move.to,
|
|
|
|
card = move.ids,
|
|
|
|
arg = #move.ids,
|
|
|
|
}
|
|
|
|
elseif move.from and move.fromArea ~= Card.PlayerJudge and
|
|
|
|
move.toArea ~= Card.PlayerJudge and move.to and move.from ~= move.to then
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = "$MoveCards",
|
|
|
|
from = move.from,
|
|
|
|
to = { move.to },
|
|
|
|
arg = #move.ids,
|
|
|
|
card = move.ids,
|
|
|
|
}
|
|
|
|
elseif move.from and move.to and move.toArea == Card.PlayerJudge then
|
|
|
|
if move.fromArea == Card.PlayerJudge and move.from ~= move.to then
|
|
|
|
msgtype = "$LightningMove"
|
|
|
|
elseif move.fromArea ~= Card.PlayerJudge then
|
|
|
|
msgtype = "$PasteCard"
|
|
|
|
end
|
|
|
|
if msgtype then
|
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = msgtype,
|
|
|
|
from = move.from,
|
|
|
|
to = { move.to },
|
|
|
|
card = move.ids,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO ...
|
|
|
|
if move.moveReason == fk.ReasonDiscard then
|
2022-12-18 04:52:52 +00:00
|
|
|
ClientInstance:appendLog{
|
|
|
|
type = "$DiscardCards",
|
|
|
|
from = move.from,
|
|
|
|
card = move.ids,
|
|
|
|
arg = #move.ids,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-14 10:22:00 +00:00
|
|
|
fk.client_callback["MoveCards"] = function(jsonData)
|
2022-04-30 07:27:56 +00:00
|
|
|
-- jsonData: CardsMoveStruct[]
|
|
|
|
local raw_moves = json.decode(jsonData)
|
|
|
|
local separated = separateMoves(raw_moves)
|
2022-09-14 05:01:10 +00:00
|
|
|
ClientInstance:moveCards(separated)
|
2022-04-30 07:27:56 +00:00
|
|
|
local merged = mergeMoves(separated)
|
2022-12-18 04:52:52 +00:00
|
|
|
for _, move in ipairs(merged) do
|
|
|
|
sendMoveCardLog(move)
|
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance:notifyUI("MoveCards", json.encode(merged))
|
2022-04-14 10:22:00 +00:00
|
|
|
end
|
|
|
|
|
2023-03-20 12:15:24 +00:00
|
|
|
fk.client_callback["ShowCard"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local from = data.from
|
|
|
|
local cards = data.cards
|
|
|
|
ClientInstance:notifyUI("MoveCards", json.encode{
|
|
|
|
{
|
|
|
|
ids = cards,
|
|
|
|
fromArea = Card.DrawPile,
|
|
|
|
toArea = Card.Processing,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-09-14 05:01:10 +00: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
|
|
|
|
|
2023-04-04 17:05:06 +00:00
|
|
|
-- 说是限定技,其实也适用于觉醒技、转换技、使命技
|
|
|
|
---@param skill Skill
|
|
|
|
---@param times integer
|
|
|
|
local function updateLimitSkill(pid, skill, times)
|
|
|
|
if not skill.visible then return end
|
|
|
|
if skill.frequency == Skill.Limited or skill.frequency == Skill.Wake then
|
|
|
|
ClientInstance:notifyUI("UpdateLimitSkill", json.encode{ pid, skill.name, times })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-14 05:01:10 +00:00
|
|
|
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
|
2023-04-04 17:05:06 +00:00
|
|
|
|
|
|
|
updateLimitSkill(id, skill, target:usedSkillTimes(skill_name, Player.HistoryGame))
|
2022-09-14 05:01:10 +00:00
|
|
|
end
|
|
|
|
|
2022-09-15 03:17:13 +00: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
|
2023-04-10 07:55:06 +00:00
|
|
|
|
|
|
|
Fk.currentResponseReason = extra_data.skillName
|
2022-09-15 03:17:13 +00:00
|
|
|
ClientInstance:notifyUI("AskForUseActiveSkill", jsonData)
|
|
|
|
end
|
|
|
|
|
2023-04-10 07:55:06 +00:00
|
|
|
fk.client_callback["AskForUseCard"] = function(jsonData)
|
|
|
|
Fk.currentResponsePattern = json.decode(jsonData)[2]
|
|
|
|
ClientInstance:notifyUI("AskForUseCard", jsonData)
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["AskForResponseCard"] = function(jsonData)
|
|
|
|
Fk.currentResponsePattern = json.decode(jsonData)[2]
|
|
|
|
ClientInstance:notifyUI("AskForResponseCard", jsonData)
|
|
|
|
end
|
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
fk.client_callback["SetPlayerMark"] = function(jsonData)
|
|
|
|
-- jsonData: [ int id, string mark, int value ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local player, mark, value = data[1], data[2], data[3]
|
|
|
|
ClientInstance:getPlayerById(player):setMark(mark, value)
|
|
|
|
|
2023-02-15 11:54:35 +00:00
|
|
|
if string.sub(mark, 1, 1) == "@" then
|
|
|
|
ClientInstance:notifyUI("SetPlayerMark", jsonData)
|
|
|
|
end
|
2022-12-18 04:52:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["Chat"] = function(jsonData)
|
2023-04-05 07:13:58 +00:00
|
|
|
-- jsonData: { int type, int sender, string msg }
|
2022-12-18 04:52:52 +00:00
|
|
|
local data = json.decode(jsonData)
|
2023-04-05 07:13:58 +00:00
|
|
|
if data.type == 1 then
|
|
|
|
data.general = ""
|
|
|
|
data.time = os.date("%H:%M:%S")
|
|
|
|
ClientInstance:notifyUI("Chat", json.encode(data))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local p = ClientInstance:getPlayerById(data.sender)
|
2023-02-15 11:54:35 +00:00
|
|
|
if not p then
|
|
|
|
for _, pl in ipairs(ClientInstance.observers) do
|
2023-04-05 07:13:58 +00:00
|
|
|
if pl.id == data.sender then
|
2023-02-15 11:54:35 +00:00
|
|
|
p = pl; break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not p then return end
|
|
|
|
data.general = ""
|
|
|
|
else
|
|
|
|
data.general = p.general
|
|
|
|
end
|
2022-12-18 04:52:52 +00:00
|
|
|
data.userName = p.player:getScreenName()
|
|
|
|
data.time = os.date("%H:%M:%S")
|
|
|
|
ClientInstance:notifyUI("Chat", json.encode(data))
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["GameLog"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
ClientInstance:appendLog(data)
|
|
|
|
end
|
|
|
|
|
2022-12-20 04:51:54 +00:00
|
|
|
fk.client_callback["LogEvent"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
if data.type == "Death" then
|
|
|
|
table.removeOne(
|
|
|
|
ClientInstance.alive_players,
|
|
|
|
ClientInstance:getPlayerById(data.to)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
ClientInstance:notifyUI("LogEvent", jsonData)
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["AddCardUseHistory"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
Self:addCardUseHistory(data[1], data[2])
|
|
|
|
end
|
|
|
|
|
2023-01-29 10:11:41 +00:00
|
|
|
fk.client_callback["SetCardUseHistory"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
Self:setCardUseHistory(data[1], data[2], data[3])
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["AddSkillUseHistory"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
2023-04-04 17:05:06 +00:00
|
|
|
local playerid, skill_name, time = data[1], data[2], data[3]
|
|
|
|
local player = ClientInstance:getPlayerById(playerid)
|
|
|
|
player:addSkillUseHistory(skill_name, time)
|
|
|
|
if not Fk.skills[skill_name] then return end
|
|
|
|
updateLimitSkill(playerid, Fk.skills[skill_name], player:usedSkillTimes(skill_name, Player.HistoryGame))
|
2023-01-29 10:11:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["SetSkillUseHistory"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
2023-04-04 17:05:06 +00:00
|
|
|
local id, skill_name, time, scope = data[1], data[2], data[3], data[4]
|
|
|
|
local player = ClientInstance:getPlayerById(id)
|
|
|
|
player:setSkillUseHistory(skill_name, time, scope)
|
|
|
|
if not Fk.skills[skill_name] then return end
|
|
|
|
updateLimitSkill(id, Fk.skills[skill_name], player:usedSkillTimes(skill_name, Player.HistoryGame))
|
2023-01-29 10:11:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["AddVirtualEquip"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local cname = data.name
|
|
|
|
local player = ClientInstance:getPlayerById(data.player)
|
|
|
|
local subcards = data.subcards
|
|
|
|
local c = Fk:cloneCard(cname)
|
|
|
|
c:addSubcards(subcards)
|
|
|
|
player:addVirtualEquip(c)
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.client_callback["RemoveVirtualEquip"] = function(jsonData)
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local player = ClientInstance:getPlayerById(data.player)
|
|
|
|
player:removeVirtualEquip(data.id)
|
2022-12-20 04:51:54 +00:00
|
|
|
end
|
|
|
|
|
2023-04-05 07:13:58 +00:00
|
|
|
fk.client_callback["Heartbeat"] = function()
|
|
|
|
ClientInstance.client:notifyServer("Heartbeat", "")
|
|
|
|
end
|
|
|
|
|
2022-01-24 02:23:08 +00:00
|
|
|
-- Create ClientInstance (used by Lua)
|
|
|
|
ClientInstance = Client:new()
|
2022-04-30 07:27:56 +00:00
|
|
|
dofile "lua/client/client_util.lua"
|