2023-04-09 13:35:35 +08:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-03-30 16:33:56 +08:00
|
|
|
---@class ServerPlayer : Player
|
2023-03-26 17:32:45 +08:00
|
|
|
---@field public serverplayer fk.ServerPlayer
|
|
|
|
---@field public room Room
|
|
|
|
---@field public next ServerPlayer
|
|
|
|
---@field public request_data string
|
|
|
|
---@field public client_reply string
|
|
|
|
---@field public default_reply string
|
|
|
|
---@field public reply_ready boolean
|
|
|
|
---@field public reply_cancel boolean
|
|
|
|
---@field public phases Phase[]
|
|
|
|
---@field public skipped_phases Phase[]
|
|
|
|
---@field public phase_state table[]
|
|
|
|
---@field public phase_index integer
|
|
|
|
---@field public role_shown boolean
|
|
|
|
---@field public ai AI
|
|
|
|
---@field public ai_data any
|
2022-03-27 14:49:41 +08:00
|
|
|
local ServerPlayer = Player:subclass("ServerPlayer")
|
2022-03-25 12:28:07 +08:00
|
|
|
|
2022-03-27 14:49:41 +08:00
|
|
|
function ServerPlayer:initialize(_self)
|
2022-04-30 15:27:56 +08:00
|
|
|
Player.initialize(self)
|
2023-04-27 14:15:08 +08:00
|
|
|
self.serverplayer = _self -- 控制者
|
|
|
|
self._splayer = _self -- 真正在玩的玩家
|
|
|
|
self._observers = { _self } -- "旁观"中的玩家,然而不包括真正的旁观者
|
2022-04-30 15:27:56 +08:00
|
|
|
self.id = _self:getId()
|
|
|
|
self.room = nil
|
|
|
|
|
|
|
|
-- Below are for doBroadcastRequest
|
|
|
|
self.request_data = ""
|
|
|
|
self.client_reply = ""
|
|
|
|
self.default_reply = ""
|
|
|
|
self.reply_ready = false
|
2022-12-18 21:19:35 +08:00
|
|
|
self.reply_cancel = false
|
2022-04-30 15:27:56 +08:00
|
|
|
self.phases = {}
|
2022-12-20 18:40:17 +08:00
|
|
|
self.skipped_phases = {}
|
2023-02-26 15:01:14 +08:00
|
|
|
self.ai = RandomAI:new(self)
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param command string
|
|
|
|
---@param jsonData string
|
2022-03-28 22:24:30 +08:00
|
|
|
function ServerPlayer:doNotify(command, jsonData)
|
2023-04-27 14:15:08 +08:00
|
|
|
for _, p in ipairs(self._observers) do
|
|
|
|
p:doNotify(command, jsonData)
|
|
|
|
end
|
|
|
|
|
2023-02-15 19:54:35 +08:00
|
|
|
local room = self.room
|
|
|
|
for _, t in ipairs(room.observers) do
|
|
|
|
local id, p = table.unpack(t)
|
2023-07-12 20:35:05 +08:00
|
|
|
if id == self.id and room.room:hasObserver(p) then
|
2023-02-15 19:54:35 +08:00
|
|
|
p:doNotify(command, jsonData)
|
|
|
|
end
|
|
|
|
end
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
--- Send a request to client, and allow client to reply within *timeout* seconds.
|
|
|
|
---
|
|
|
|
--- *timeout* must not be negative. If nil, room.timeout is used.
|
|
|
|
---@param command string
|
|
|
|
---@param jsonData string
|
2023-07-14 22:17:54 +08:00
|
|
|
---@param timeout integer|nil
|
2022-03-28 22:24:30 +08:00
|
|
|
function ServerPlayer:doRequest(command, jsonData, timeout)
|
2022-04-30 15:27:56 +08:00
|
|
|
self.client_reply = ""
|
|
|
|
self.reply_ready = false
|
2022-12-18 21:19:35 +08:00
|
|
|
self.reply_cancel = false
|
2023-04-27 14:15:08 +08:00
|
|
|
|
|
|
|
if self.serverplayer:busy() then
|
|
|
|
self.room.request_queue[self.serverplayer] = self.room.request_queue[self.serverplayer] or {}
|
|
|
|
table.insert(self.room.request_queue[self.serverplayer], { self.id, command, jsonData, timeout })
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
self.room.request_self[self.serverplayer:getId()] = self.id
|
|
|
|
|
|
|
|
if not table.contains(self._observers, self.serverplayer) then
|
|
|
|
self.serverplayer:doNotify("StartChangeSelf", tostring(self.id))
|
|
|
|
end
|
|
|
|
|
|
|
|
timeout = timeout or self.room.timeout
|
|
|
|
self.serverplayer:setBusy(true)
|
2023-02-26 15:01:14 +08:00
|
|
|
self.ai_data = {
|
|
|
|
command = command,
|
|
|
|
jsonData = jsonData,
|
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
self.serverplayer:doRequest(command, jsonData, timeout)
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
2023-01-17 22:34:15 +08:00
|
|
|
local function _waitForReply(player, timeout)
|
|
|
|
local result
|
2023-02-21 13:44:24 +08:00
|
|
|
local start = os.getms()
|
2023-06-16 10:56:33 +08:00
|
|
|
local state = player.serverplayer:getState()
|
|
|
|
player.request_timeout = timeout
|
|
|
|
player.request_start = start
|
|
|
|
if state ~= fk.Player_Online then
|
2023-07-02 20:39:42 +08:00
|
|
|
if player.room.hasSurrendered then
|
|
|
|
return "__cancel"
|
|
|
|
end
|
|
|
|
|
2023-06-16 10:56:33 +08:00
|
|
|
if state ~= fk.Player_Robot then
|
|
|
|
player.room:checkNoHuman()
|
2023-06-04 19:31:44 +08:00
|
|
|
player.room:delay(500)
|
|
|
|
return "__cancel"
|
|
|
|
end
|
2023-02-26 15:01:14 +08:00
|
|
|
-- Let AI make reply. First handle request
|
2023-06-16 10:56:33 +08:00
|
|
|
-- coroutine.yield("__handleRequest", 0)
|
2023-02-26 15:01:14 +08:00
|
|
|
|
2023-06-16 10:56:33 +08:00
|
|
|
player.room:checkNoHuman()
|
2023-02-26 15:01:14 +08:00
|
|
|
player.ai:readRequestData()
|
|
|
|
local reply = player.ai:makeReply()
|
|
|
|
return reply
|
|
|
|
end
|
2023-01-17 22:34:15 +08:00
|
|
|
while true do
|
2023-06-16 10:56:33 +08:00
|
|
|
player.serverplayer:setThinking(true)
|
2023-01-17 22:34:15 +08:00
|
|
|
result = player.serverplayer:waitForReply(0)
|
|
|
|
if result ~= "__notready" then
|
2023-06-16 10:56:33 +08:00
|
|
|
player.serverplayer:setThinking(false)
|
2023-01-17 22:34:15 +08:00
|
|
|
return result
|
|
|
|
end
|
2023-02-26 15:01:14 +08:00
|
|
|
local rest = timeout * 1000 - (os.getms() - start) / 1000
|
|
|
|
if timeout and rest <= 0 then
|
2023-06-16 10:56:33 +08:00
|
|
|
player.serverplayer:setThinking(false)
|
2023-01-17 22:34:15 +08:00
|
|
|
return ""
|
|
|
|
end
|
2023-07-02 20:39:42 +08:00
|
|
|
|
|
|
|
if player.room.hasSurrendered then
|
|
|
|
player.serverplayer:setThinking(false)
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2023-03-01 01:43:44 +08:00
|
|
|
coroutine.yield("__handleRequest", rest)
|
2023-01-17 22:34:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
--- Wait for at most *timeout* seconds for reply from client.
|
|
|
|
---
|
|
|
|
--- If *timeout* is negative or **nil**, the function will wait forever until get reply.
|
2022-04-02 21:39:44 +08:00
|
|
|
---@param timeout integer @ seconds to wait
|
|
|
|
---@return string @ JSON data
|
2022-03-30 14:14:40 +08:00
|
|
|
function ServerPlayer:waitForReply(timeout)
|
2023-01-17 22:34:15 +08:00
|
|
|
local result = _waitForReply(self, timeout)
|
2023-04-27 14:15:08 +08:00
|
|
|
local sid = self.serverplayer:getId()
|
|
|
|
local id = self.id
|
|
|
|
if self.room.request_self[sid] ~= id then
|
|
|
|
result = ""
|
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.request_data = ""
|
|
|
|
self.client_reply = result
|
2022-12-18 21:19:35 +08:00
|
|
|
if result == "__cancel" then
|
|
|
|
result = ""
|
|
|
|
self.reply_cancel = true
|
2023-04-27 14:15:08 +08:00
|
|
|
self.serverplayer:setBusy(false)
|
2023-06-19 00:20:50 +08:00
|
|
|
self.serverplayer:setThinking(false)
|
2023-04-27 14:15:08 +08:00
|
|
|
end
|
|
|
|
if result ~= "" then
|
|
|
|
self.reply_ready = true
|
|
|
|
self.serverplayer:setBusy(false)
|
2023-06-19 00:20:50 +08:00
|
|
|
self.serverplayer:setThinking(false)
|
2022-12-18 21:19:35 +08:00
|
|
|
end
|
2023-04-27 14:15:08 +08:00
|
|
|
|
|
|
|
local queue = self.room.request_queue[self.serverplayer]
|
|
|
|
if queue and #queue > 0 and not self.serverplayer:busy() then
|
|
|
|
local i, c, j, t = table.unpack(table.remove(queue, 1))
|
|
|
|
self.room:getPlayerById(i):doRequest(c, j, t)
|
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return result
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2023-01-17 22:34:15 +08:00
|
|
|
---@param player ServerPlayer
|
|
|
|
function ServerPlayer:marshal(player)
|
|
|
|
local room = self.room
|
2023-02-16 00:54:39 +08:00
|
|
|
if not room.game_started then
|
|
|
|
-- If game does not starts, that mean we are entering room that
|
|
|
|
-- all players are choosing their generals.
|
|
|
|
-- Note that when we are in this function, the main thread must be
|
|
|
|
-- calling delay() or waiting for reply.
|
|
|
|
if self.role_shown then
|
|
|
|
room:notifyProperty(player, self, "role")
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-01-17 22:34:15 +08:00
|
|
|
room:notifyProperty(player, self, "maxHp")
|
|
|
|
room:notifyProperty(player, self, "hp")
|
2023-04-13 20:17:39 +08:00
|
|
|
room:notifyProperty(player, self, "shield")
|
2023-01-29 18:11:41 +08:00
|
|
|
room:notifyProperty(player, self, "gender")
|
2023-04-12 22:28:19 +08:00
|
|
|
room:notifyProperty(player, self, "kingdom")
|
2023-01-17 22:34:15 +08:00
|
|
|
|
|
|
|
if self.dead then
|
|
|
|
room:notifyProperty(player, self, "dead")
|
|
|
|
room:notifyProperty(player, self, "role")
|
|
|
|
else
|
|
|
|
room:notifyProperty(player, self, "seat")
|
|
|
|
room:notifyProperty(player, self, "phase")
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self.faceup then
|
|
|
|
room:notifyProperty(player, self, "faceup")
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.chained then
|
|
|
|
room:notifyProperty(player, self, "chained")
|
|
|
|
end
|
|
|
|
|
|
|
|
local card_moves = {}
|
|
|
|
if #self.player_cards[Player.Hand] ~= 0 then
|
|
|
|
local info = {}
|
|
|
|
for _, i in ipairs(self.player_cards[Player.Hand]) do
|
|
|
|
table.insert(info, { cardId = i, fromArea = Card.DrawPile })
|
|
|
|
end
|
|
|
|
local move = {
|
|
|
|
moveInfo = info,
|
|
|
|
to = self.id,
|
|
|
|
toArea = Card.PlayerHand
|
|
|
|
}
|
|
|
|
table.insert(card_moves, move)
|
|
|
|
end
|
|
|
|
if #self.player_cards[Player.Equip] ~= 0 then
|
|
|
|
local info = {}
|
|
|
|
for _, i in ipairs(self.player_cards[Player.Equip]) do
|
|
|
|
table.insert(info, { cardId = i, fromArea = Card.DrawPile })
|
|
|
|
end
|
|
|
|
local move = {
|
|
|
|
moveInfo = info,
|
|
|
|
to = self.id,
|
|
|
|
toArea = Card.PlayerEquip
|
|
|
|
}
|
|
|
|
table.insert(card_moves, move)
|
|
|
|
end
|
|
|
|
if #self.player_cards[Player.Judge] ~= 0 then
|
|
|
|
local info = {}
|
|
|
|
for _, i in ipairs(self.player_cards[Player.Judge]) do
|
|
|
|
table.insert(info, { cardId = i, fromArea = Card.DrawPile })
|
|
|
|
end
|
|
|
|
local move = {
|
|
|
|
moveInfo = info,
|
|
|
|
to = self.id,
|
|
|
|
toArea = Card.PlayerJudge
|
|
|
|
}
|
|
|
|
table.insert(card_moves, move)
|
|
|
|
end
|
2023-06-16 13:26:02 +08:00
|
|
|
|
|
|
|
for k, v in pairs(self.special_cards) do
|
|
|
|
local info = {}
|
|
|
|
for _, i in ipairs(v) do
|
|
|
|
table.insert(info, { cardId = i, fromArea = Card.DrawPile })
|
|
|
|
end
|
|
|
|
local move = {
|
|
|
|
moveInfo = info,
|
|
|
|
to = self.id,
|
|
|
|
toArea = Card.PlayerSpecial,
|
|
|
|
specialName = k,
|
|
|
|
specialVisible = self == player,
|
|
|
|
}
|
|
|
|
table.insert(card_moves, move)
|
|
|
|
end
|
|
|
|
|
2023-01-17 22:34:15 +08:00
|
|
|
if #card_moves > 0 then
|
|
|
|
room:notifyMoveCards({ player }, card_moves)
|
|
|
|
end
|
|
|
|
|
2023-05-26 20:53:26 +08:00
|
|
|
for k, v in pairs(self.mark) do
|
|
|
|
player:doNotify("SetPlayerMark", json.encode{self.id, k, v})
|
|
|
|
end
|
2023-01-17 22:34:15 +08:00
|
|
|
|
|
|
|
for _, s in ipairs(self.player_skills) do
|
|
|
|
player:doNotify("AddSkill", json.encode{self.id, s.name})
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in pairs(self.cardUsedHistory) do
|
2023-02-21 13:44:24 +08:00
|
|
|
if v[1] > 0 then
|
|
|
|
player:doNotify("AddCardUseHistory", json.encode{k, v[1]})
|
|
|
|
end
|
2023-01-17 22:34:15 +08:00
|
|
|
end
|
|
|
|
|
2023-06-04 19:31:44 +08:00
|
|
|
for k, v in pairs(self.skillUsedHistory) do
|
2023-06-16 13:26:02 +08:00
|
|
|
if v[4] > 0 then
|
|
|
|
player:doNotify("SetSkillUseHistory", json.encode{self.id, k, v[1], 1})
|
|
|
|
player:doNotify("SetSkillUseHistory", json.encode{self.id, k, v[2], 2})
|
|
|
|
player:doNotify("SetSkillUseHistory", json.encode{self.id, k, v[3], 3})
|
|
|
|
player:doNotify("SetSkillUseHistory", json.encode{self.id, k, v[4], 4})
|
2023-06-04 19:31:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-17 22:34:15 +08:00
|
|
|
if self.role_shown then
|
|
|
|
room:notifyProperty(player, self, "role")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:reconnect()
|
|
|
|
local room = self.room
|
2023-06-16 10:56:33 +08:00
|
|
|
self.serverplayer:setState(fk.Player_Online)
|
2023-01-17 22:34:15 +08:00
|
|
|
|
|
|
|
self:doNotify("Setup", json.encode{
|
|
|
|
self.id,
|
2023-07-01 04:12:19 +08:00
|
|
|
self._splayer:getScreenName(),
|
|
|
|
self._splayer:getAvatar(),
|
2023-01-17 22:34:15 +08:00
|
|
|
})
|
|
|
|
self:doNotify("EnterLobby", "")
|
|
|
|
self:doNotify("EnterRoom", json.encode{
|
2023-04-20 00:19:48 +08:00
|
|
|
#room.players, room.timeout, room.settings,
|
2023-01-17 22:34:15 +08:00
|
|
|
})
|
2023-05-19 16:23:24 +08:00
|
|
|
self:doNotify("StartGame", "")
|
2023-01-17 22:34:15 +08:00
|
|
|
room:notifyProperty(self, self, "role")
|
|
|
|
|
|
|
|
-- send player data
|
2023-07-04 18:20:52 +08:00
|
|
|
for _, p in ipairs(room:getOtherPlayers(self, false, true)) do
|
2023-01-17 22:34:15 +08:00
|
|
|
self:doNotify("AddPlayer", json.encode{
|
|
|
|
p.id,
|
2023-07-01 04:12:19 +08:00
|
|
|
p._splayer:getScreenName(),
|
|
|
|
p._splayer:getAvatar(),
|
2023-01-17 22:34:15 +08:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_circle = {}
|
|
|
|
for i = 1, #room.players do
|
|
|
|
table.insert(player_circle, room.players[i].id)
|
|
|
|
end
|
|
|
|
self:doNotify("ArrangeSeats", json.encode(player_circle))
|
|
|
|
|
|
|
|
for _, p in ipairs(room.players) do
|
|
|
|
room:notifyProperty(self, p, "general")
|
2023-04-20 00:19:48 +08:00
|
|
|
room:notifyProperty(self, p, "deputyGeneral")
|
2023-01-17 22:34:15 +08:00
|
|
|
p:marshal(self)
|
|
|
|
end
|
|
|
|
|
2023-04-20 00:19:48 +08:00
|
|
|
self:doNotify("UpdateDrawPile", #room.draw_pile)
|
2023-04-23 21:10:07 +08:00
|
|
|
self:doNotify("UpdateRoundNum", room:getTag("RoundCount") or 0)
|
2023-01-17 22:34:15 +08:00
|
|
|
|
|
|
|
room:broadcastProperty(self, "state")
|
|
|
|
end
|
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
function ServerPlayer:isAlive()
|
2022-04-30 15:27:56 +08:00
|
|
|
return self.dead == false
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|
|
|
|
|
2022-04-02 21:39:44 +08:00
|
|
|
function ServerPlayer:turnOver()
|
2023-05-28 18:45:54 +08:00
|
|
|
if self.room.logic:trigger(fk.BeforeTurnOver, self) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.faceup = not self.faceup
|
|
|
|
self.room:broadcastProperty(self, "faceup")
|
2022-04-02 21:39:44 +08:00
|
|
|
|
2022-12-18 12:52:52 +08:00
|
|
|
self.room:sendLog{
|
|
|
|
type = "#TurnOver",
|
|
|
|
from = self.id,
|
|
|
|
arg = self.faceup and "face_up" or "face_down",
|
|
|
|
}
|
2023-05-28 18:45:54 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.room.logic:trigger(fk.TurnedOver, self)
|
2022-04-02 21:39:44 +08:00
|
|
|
end
|
|
|
|
|
2023-03-20 20:15:24 +08:00
|
|
|
function ServerPlayer:showCards(cards)
|
|
|
|
cards = Card:getIdList(cards)
|
2023-06-24 15:05:58 +08:00
|
|
|
for _, id in ipairs(cards) do
|
|
|
|
Fk:filterCard(id, self)
|
|
|
|
end
|
|
|
|
|
2023-03-20 20:15:24 +08:00
|
|
|
local room = self.room
|
|
|
|
room:sendLog{
|
|
|
|
type = "#ShowCard",
|
|
|
|
from = self.id,
|
|
|
|
card = cards,
|
|
|
|
}
|
|
|
|
room:doBroadcastNotify("ShowCard", json.encode{
|
|
|
|
from = self.id,
|
|
|
|
cards = cards,
|
|
|
|
})
|
2023-06-10 02:18:51 +08:00
|
|
|
room:sendFootnote(cards, {
|
|
|
|
type = "##ShowCard",
|
|
|
|
from = self.id,
|
|
|
|
})
|
2023-05-20 16:00:03 +08:00
|
|
|
|
|
|
|
room.logic:trigger(fk.CardShown, self, { cardIds = cards })
|
2023-03-20 20:15:24 +08:00
|
|
|
end
|
|
|
|
|
2023-06-09 01:10:16 +08:00
|
|
|
local phase_name_table = {
|
|
|
|
[Player.Judge] = "phase_judge",
|
|
|
|
[Player.Draw] = "phase_draw",
|
|
|
|
[Player.Play] = "phase_play",
|
|
|
|
[Player.Discard] = "phase_discard",
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:04:55 +08:00
|
|
|
---@param from_phase Phase
|
|
|
|
---@param to_phase Phase
|
|
|
|
function ServerPlayer:changePhase(from_phase, to_phase)
|
2022-04-30 15:27:56 +08:00
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
|
|
|
self.phase = Player.PhaseNone
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local phase_change = {
|
|
|
|
from = from_phase,
|
|
|
|
to = to_phase
|
|
|
|
}
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local skip = logic:trigger(fk.EventPhaseChanging, self, phase_change)
|
|
|
|
if skip and to_phase ~= Player.NotActive then
|
|
|
|
self.phase = from_phase
|
|
|
|
return true
|
|
|
|
end
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.phase = to_phase
|
2023-07-16 15:32:16 +08:00
|
|
|
room:broadcastProperty(self, "phase")
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2023-02-26 16:51:29 +08:00
|
|
|
if #self.phases > 0 then
|
2022-04-30 15:27:56 +08:00
|
|
|
table.remove(self.phases, 1)
|
|
|
|
end
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2023-04-22 15:52:26 +08:00
|
|
|
GameEvent(GameEvent.Phase, self):exec()
|
2022-04-30 15:27:56 +08:00
|
|
|
|
|
|
|
return false
|
2022-04-04 19:04:55 +08:00
|
|
|
end
|
|
|
|
|
2023-06-09 01:10:16 +08:00
|
|
|
function ServerPlayer:gainAnExtraPhase(phase, delay)
|
2023-04-22 15:52:26 +08:00
|
|
|
local room = self.room
|
2023-06-09 01:10:16 +08:00
|
|
|
delay = (delay == nil) and true or delay
|
|
|
|
if delay then
|
|
|
|
local logic = room.logic
|
|
|
|
local turn = logic:getCurrentEvent():findParent(GameEvent.Phase, true)
|
|
|
|
if turn then
|
|
|
|
turn:addExitFunc(function() self:gainAnExtraPhase(phase, false) end)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-22 15:52:26 +08:00
|
|
|
local current = self.phase
|
|
|
|
self.phase = phase
|
2023-07-16 15:32:16 +08:00
|
|
|
room:broadcastProperty(self, "phase")
|
2023-04-22 15:52:26 +08:00
|
|
|
|
2023-06-09 01:10:16 +08:00
|
|
|
room:sendLog{
|
|
|
|
type = "#GainAnExtraPhase",
|
|
|
|
from = self.id,
|
|
|
|
arg = phase_name_table[phase],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-22 15:52:26 +08:00
|
|
|
GameEvent(GameEvent.Phase, self):exec()
|
|
|
|
|
|
|
|
self.phase = current
|
2023-07-16 15:32:16 +08:00
|
|
|
room:broadcastProperty(self, "phase")
|
2023-04-22 15:52:26 +08:00
|
|
|
end
|
|
|
|
|
2023-07-14 22:17:54 +08:00
|
|
|
---@param phase_table Phase[]|nil
|
2022-04-04 19:04:55 +08:00
|
|
|
function ServerPlayer:play(phase_table)
|
2022-04-30 15:27:56 +08:00
|
|
|
phase_table = phase_table or {}
|
|
|
|
if #phase_table > 0 then
|
|
|
|
if not table.contains(phase_table, Player.NotActive) then
|
|
|
|
table.insert(phase_table, Player.NotActive)
|
2022-04-04 19:04:55 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
else
|
|
|
|
phase_table = {
|
|
|
|
Player.RoundStart, Player.Start,
|
|
|
|
Player.Judge, Player.Draw, Player.Play, Player.Discard,
|
|
|
|
Player.Finish, Player.NotActive,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
self.phases = phase_table
|
|
|
|
self.phase_state = {}
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local phases = self.phases
|
|
|
|
local phase_state = self.phase_state
|
|
|
|
local room = self.room
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for i = 1, #phases do
|
|
|
|
phase_state[i] = {
|
|
|
|
phase = phases[i],
|
2022-12-20 18:40:17 +08:00
|
|
|
skipped = self.skipped_phases[phases[i]] or false
|
2022-04-30 15:27:56 +08:00
|
|
|
}
|
|
|
|
end
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for i = 1, #phases do
|
|
|
|
if self.dead then
|
|
|
|
self:changePhase(self.phase, Player.NotActive)
|
|
|
|
break
|
2022-04-04 19:04:55 +08:00
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.phase_index = i
|
|
|
|
local phase_change = {
|
|
|
|
from = self.phase,
|
|
|
|
to = phases[i]
|
|
|
|
}
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local logic = self.room.logic
|
|
|
|
self.phase = Player.PhaseNone
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-12-20 18:40:17 +08:00
|
|
|
local skip = phase_state[i].skipped
|
|
|
|
if not skip then
|
|
|
|
skip = logic:trigger(fk.EventPhaseChanging, self, phase_change)
|
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
phases[i] = phase_change.to
|
|
|
|
phase_state[i].phase = phases[i]
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.phase = phases[i]
|
2023-07-16 15:32:16 +08:00
|
|
|
room:broadcastProperty(self, "phase")
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local cancel_skip = true
|
2022-12-20 18:40:17 +08:00
|
|
|
if phases[i] ~= Player.NotActive and (skip) then
|
2022-04-30 15:27:56 +08:00
|
|
|
cancel_skip = logic:trigger(fk.EventPhaseSkipping, self)
|
|
|
|
end
|
2022-04-04 19:04:55 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if (not skip) or (cancel_skip) then
|
2023-04-22 15:52:26 +08:00
|
|
|
GameEvent(GameEvent.Phase, self):exec()
|
2022-12-20 18:40:17 +08:00
|
|
|
else
|
|
|
|
room:sendLog{
|
|
|
|
type = "#PhaseSkipped",
|
|
|
|
from = self.id,
|
|
|
|
arg = phase_name_table[self.phase],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param phase Phase
|
|
|
|
function ServerPlayer:skip(phase)
|
|
|
|
if not table.contains({
|
|
|
|
Player.Judge,
|
|
|
|
Player.Draw,
|
|
|
|
Player.Play,
|
|
|
|
Player.Discard
|
|
|
|
}, phase) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.skipped_phases[phase] = true
|
|
|
|
for _, t in ipairs(self.phase_state) do
|
|
|
|
if t.phase == phase then
|
|
|
|
t.skipped = true
|
2022-04-04 19:04:55 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-04 19:04:55 +08:00
|
|
|
end
|
|
|
|
|
2023-06-09 01:10:16 +08:00
|
|
|
function ServerPlayer:gainAnExtraTurn(delay)
|
2023-03-13 20:51:12 +08:00
|
|
|
local room = self.room
|
2023-06-09 01:10:16 +08:00
|
|
|
delay = (delay == nil) and true or delay
|
|
|
|
if delay then
|
|
|
|
local logic = room.logic
|
|
|
|
local turn = logic:getCurrentEvent():findParent(GameEvent.Turn, true)
|
|
|
|
if turn then
|
|
|
|
turn:addExitFunc(function() self:gainAnExtraTurn(false) end)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-13 20:51:12 +08:00
|
|
|
room:sendLog{
|
|
|
|
type = "#GainAnExtraTurn",
|
|
|
|
from = self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
local current = room.current
|
|
|
|
room.current = self
|
|
|
|
GameEvent(GameEvent.Turn):exec()
|
|
|
|
room.current = current
|
|
|
|
end
|
|
|
|
|
2022-12-20 12:51:54 +08:00
|
|
|
function ServerPlayer:drawCards(num, skillName, fromPlace)
|
|
|
|
return self.room:drawCards(self, num, skillName, fromPlace)
|
|
|
|
end
|
|
|
|
|
2023-02-21 13:44:24 +08:00
|
|
|
---@param pile_name string
|
|
|
|
---@param card integer|Card
|
|
|
|
---@param visible boolean
|
2023-07-14 23:12:46 +08:00
|
|
|
---@param skillName string|nil
|
2023-02-21 13:44:24 +08:00
|
|
|
function ServerPlayer:addToPile(pile_name, card, visible, skillName)
|
|
|
|
local room = self.room
|
|
|
|
room:moveCardTo(card, Card.PlayerSpecial, self, fk.ReasonJustMove, skillName, pile_name, visible)
|
|
|
|
end
|
|
|
|
|
2022-12-20 12:51:54 +08:00
|
|
|
function ServerPlayer:bury()
|
2023-03-18 23:37:21 +08:00
|
|
|
self:setCardUseHistory("")
|
|
|
|
self:setSkillUseHistory("")
|
2022-12-20 12:51:54 +08:00
|
|
|
self:throwAllCards()
|
2023-03-18 23:37:21 +08:00
|
|
|
self:throwAllMarks()
|
|
|
|
self:clearPiles()
|
2023-07-11 23:16:46 +08:00
|
|
|
self:reset()
|
2022-12-20 12:51:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:throwAllCards(flag)
|
|
|
|
local room = self.room
|
|
|
|
flag = flag or "hej"
|
|
|
|
if string.find(flag, "h") then
|
|
|
|
room:throwCard(self.player_cards[Player.Hand], "", self)
|
|
|
|
end
|
|
|
|
|
|
|
|
if string.find(flag, "e") then
|
|
|
|
room:throwCard(self.player_cards[Player.Equip], "", self)
|
|
|
|
end
|
|
|
|
|
|
|
|
if string.find(flag, "j") then
|
|
|
|
room:throwCard(self.player_cards[Player.Judge], "", self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-18 23:37:21 +08:00
|
|
|
function ServerPlayer:throwAllMarks()
|
|
|
|
for name, _ in pairs(self.mark) do
|
|
|
|
self.room:setPlayerMark(self, name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:clearPiles()
|
|
|
|
for _, ids in pairs(self.special_cards) do
|
|
|
|
self.room:throwCard(ids, "", self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-29 18:11:41 +08:00
|
|
|
function ServerPlayer:addVirtualEquip(card)
|
|
|
|
Player.addVirtualEquip(self, card)
|
|
|
|
self.room:doBroadcastNotify("AddVirtualEquip", json.encode{
|
|
|
|
player = self.id,
|
|
|
|
name = card.name,
|
|
|
|
subcards = card.subcards,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:removeVirtualEquip(cid)
|
|
|
|
local ret = Player.removeVirtualEquip(self, cid)
|
|
|
|
self.room:doBroadcastNotify("RemoveVirtualEquip", json.encode{
|
|
|
|
player = self.id,
|
|
|
|
id = cid,
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2022-12-20 12:51:54 +08:00
|
|
|
function ServerPlayer:addCardUseHistory(cardName, num)
|
|
|
|
Player.addCardUseHistory(self, cardName, num)
|
|
|
|
self:doNotify("AddCardUseHistory", json.encode{cardName, num})
|
|
|
|
end
|
|
|
|
|
2023-01-29 18:11:41 +08:00
|
|
|
function ServerPlayer:setCardUseHistory(cardName, num, scope)
|
|
|
|
Player.setCardUseHistory(self, cardName, num, scope)
|
|
|
|
self:doNotify("SetCardUseHistory", json.encode{cardName, num, scope})
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:addSkillUseHistory(cardName, num)
|
|
|
|
Player.addSkillUseHistory(self, cardName, num)
|
2023-04-05 01:05:06 +08:00
|
|
|
self.room:doBroadcastNotify("AddSkillUseHistory", json.encode{self.id, cardName, num})
|
2023-01-29 18:11:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:setSkillUseHistory(cardName, num, scope)
|
|
|
|
Player.setSkillUseHistory(self, cardName, num, scope)
|
2023-04-05 01:05:06 +08:00
|
|
|
self.room:doBroadcastNotify("SetSkillUseHistory", json.encode{self.id, cardName, num, scope})
|
2022-12-20 12:51:54 +08:00
|
|
|
end
|
|
|
|
|
2023-03-09 12:19:16 +08:00
|
|
|
---@param chained boolean
|
|
|
|
function ServerPlayer:setChainState(chained)
|
2023-05-28 18:45:54 +08:00
|
|
|
if self.room.logic:trigger(fk.BeforeChainStateChange, self) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-03-09 12:19:16 +08:00
|
|
|
self.chained = chained
|
|
|
|
self.room:broadcastProperty(self, "chained")
|
|
|
|
self.room:sendLog{
|
|
|
|
type = "#ChainStateChange",
|
|
|
|
from = self.id,
|
2023-07-11 23:16:46 +08:00
|
|
|
arg = self.chained and "chained" or "un-chained"
|
2023-03-09 12:19:16 +08:00
|
|
|
}
|
2023-05-28 18:45:54 +08:00
|
|
|
|
|
|
|
self.room.logic:trigger(fk.ChainStateChanged, self)
|
2023-03-09 12:19:16 +08:00
|
|
|
end
|
|
|
|
|
2023-07-11 23:16:46 +08:00
|
|
|
function ServerPlayer:reset()
|
|
|
|
self.room:sendLog{
|
|
|
|
type = "#ResetGeneral",
|
|
|
|
from = self.id,
|
|
|
|
}
|
|
|
|
self:setChainState(false)
|
|
|
|
if not self.faceup then self:turnOver() end
|
|
|
|
end
|
|
|
|
|
2023-07-14 23:12:46 +08:00
|
|
|
--@param from ServerPlayer
|
2023-03-14 20:48:08 +08:00
|
|
|
---@param tos ServerPlayer[]
|
|
|
|
---@param skillName string
|
2023-07-14 23:12:46 +08:00
|
|
|
---@param initialCard Card|nil
|
2023-03-14 20:48:08 +08:00
|
|
|
---@return PindianStruct
|
|
|
|
function ServerPlayer:pindian(tos, skillName, initialCard)
|
2023-04-30 18:55:59 +08:00
|
|
|
local pindianData = { from = self, tos = tos, reason = skillName, fromCard = initialCard, results = {} }
|
2023-03-14 20:48:08 +08:00
|
|
|
self.room:pindian(pindianData)
|
|
|
|
return pindianData
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:10:07 +08:00
|
|
|
-- Hegemony func
|
|
|
|
|
|
|
|
function ServerPlayer:prelightSkill(skill, isPrelight)
|
|
|
|
if isPrelight then
|
|
|
|
self:addSkill(skill)
|
|
|
|
else
|
|
|
|
self:loseSkill(skill)
|
|
|
|
end
|
|
|
|
self:doNotify("PrelightSkill", json.encode{ skill, isPrelight })
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:revealGeneral(isDeputy)
|
|
|
|
local room = self.room
|
|
|
|
local generalName
|
|
|
|
if isDeputy then
|
|
|
|
if self.deputyGeneral ~= "anjiang" then return end
|
|
|
|
generalName = self:getMark("__heg_deputy")
|
|
|
|
else
|
|
|
|
if self.general ~= "anjiang" then return end
|
|
|
|
generalName = self:getMark("__heg_general")
|
|
|
|
end
|
|
|
|
|
|
|
|
local general = Fk.generals[generalName]
|
|
|
|
for _, s in ipairs(general:getSkillNameList()) do
|
|
|
|
local skill = Fk.skills[s]
|
|
|
|
if skill:isInstanceOf(TriggerSkill) or table.find(skill.related_skills,
|
|
|
|
function(s) return s:isInstanceOf(TriggerSkill) end) then
|
|
|
|
self:doNotify("LoseSkill", json.encode{ self.id, s, true })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local oldKingdom = self.kingdom
|
|
|
|
room:changeHero(self, generalName, false, isDeputy)
|
|
|
|
local kingdom = general.kingdom
|
|
|
|
if oldKingdom == "unknown" and #table.filter(room:getOtherPlayers(self),
|
|
|
|
function(p)
|
|
|
|
return p.kingdom == kingdom
|
2023-04-27 14:19:36 +08:00
|
|
|
end) >= #room.players // 2 then
|
2023-04-23 21:10:07 +08:00
|
|
|
|
|
|
|
self.kingdom = "wild"
|
|
|
|
room:broadcastProperty(self, "kingdom")
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.gender ~= General.Male and self.gender ~= General.Female then
|
|
|
|
self.gender = general.gender
|
|
|
|
end
|
|
|
|
|
|
|
|
room:sendLog{
|
|
|
|
type = "#RevealGeneral",
|
|
|
|
from = self.id,
|
|
|
|
arg = isDeputy and "deputyGeneral" or "mainGeneral",
|
|
|
|
arg2 = generalName,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- TODO: 阴阳鱼摸牌
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:revealBySkillName(skill_name)
|
|
|
|
local main = self.general == "anjiang"
|
|
|
|
local deputy = self.deputyGeneral == "anjiang"
|
|
|
|
|
|
|
|
if main then
|
|
|
|
if table.contains(Fk.generals[self:getMark("__heg_general")]
|
|
|
|
:getSkillNameList(), skill_name) then
|
|
|
|
self:revealGeneral(false)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if deputy then
|
|
|
|
if table.contains(Fk.generals[self:getMark("__heg_deputy")]
|
|
|
|
:getSkillNameList(), skill_name) then
|
|
|
|
self:revealGeneral(true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-27 14:15:08 +08:00
|
|
|
-- 神貂蝉
|
|
|
|
|
2023-07-14 23:12:46 +08:00
|
|
|
---@param p ServerPlayer
|
2023-04-27 14:15:08 +08:00
|
|
|
function ServerPlayer:control(p)
|
|
|
|
if self == p then
|
|
|
|
self.room:setPlayerMark(p, "@ControledBy", 0)
|
|
|
|
else
|
|
|
|
self.room:setPlayerMark(p, "@ControledBy", "seat#" .. self.seat)
|
|
|
|
end
|
|
|
|
p.serverplayer = self._splayer
|
|
|
|
end
|
|
|
|
|
2022-03-27 14:49:41 +08:00
|
|
|
return ServerPlayer
|