2022-03-30 16:33:56 +08:00
|
|
|
---@class Room : Object
|
2022-03-31 13:29:23 +08:00
|
|
|
---@field room fk.Room
|
2022-04-01 20:51:01 +08:00
|
|
|
---@field players ServerPlayer[]
|
|
|
|
---@field alive_players ServerPlayer[]
|
|
|
|
---@field current ServerPlayer
|
2022-03-31 13:29:23 +08:00
|
|
|
---@field game_finished boolean
|
2022-04-01 20:51:01 +08:00
|
|
|
---@field timeout integer
|
2022-04-02 21:39:44 +08:00
|
|
|
---@field tag table<string, any>
|
2022-04-08 18:39:58 +08:00
|
|
|
---@field draw_pile integer[]
|
|
|
|
---@field discard_pile integer[]
|
|
|
|
---@field processing_area integer[]
|
|
|
|
---@field void integer[]
|
|
|
|
---@field card_place table<integer, CardArea>
|
2022-03-27 14:49:41 +08:00
|
|
|
local Room = class("Room")
|
2022-03-25 12:28:07 +08:00
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
-- load classes used by the game
|
|
|
|
GameLogic = require "server.gamelogic"
|
|
|
|
ServerPlayer = require "server.serverplayer"
|
|
|
|
|
|
|
|
fk.room_callback = {}
|
|
|
|
|
|
|
|
---@param _room fk.Room
|
2022-03-27 14:49:41 +08:00
|
|
|
function Room:initialize(_room)
|
2022-04-30 15:27:56 +08:00
|
|
|
self.room = _room
|
|
|
|
self.room.callback = function(_self, command, jsonData)
|
|
|
|
local cb = fk.room_callback[command]
|
|
|
|
if (type(cb) == "function") then
|
|
|
|
cb(jsonData)
|
|
|
|
else
|
|
|
|
print("Lobby error: Unknown command " .. command);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.room.startGame = function(_self)
|
|
|
|
self:run()
|
|
|
|
end
|
|
|
|
|
|
|
|
self.players = {}
|
|
|
|
self.alive_players = {}
|
|
|
|
self.current = nil
|
|
|
|
self.game_finished = false
|
|
|
|
self.timeout = _room:getTimeout()
|
|
|
|
self.tag = {}
|
|
|
|
self.draw_pile = {}
|
|
|
|
self.discard_pile = {}
|
|
|
|
self.processing_area = {}
|
|
|
|
self.void = {}
|
|
|
|
self.card_place = {}
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- When this function returns, the Room(C++) thread stopped.
|
|
|
|
function Room:run()
|
2022-04-30 15:27:56 +08:00
|
|
|
for _, p in fk.qlist(self.room:getPlayers()) do
|
|
|
|
local player = ServerPlayer:new(p)
|
|
|
|
player.state = p:getStateString()
|
|
|
|
player.room = self
|
|
|
|
table.insert(self.players, player)
|
|
|
|
end
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.logic = GameLogic:new(self)
|
|
|
|
self.logic:run()
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
2022-03-25 12:28:07 +08:00
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param property string
|
2022-03-28 22:24:30 +08:00
|
|
|
function Room:broadcastProperty(player, property)
|
2022-04-30 15:27:56 +08:00
|
|
|
for _, p in ipairs(self.players) do
|
|
|
|
self:notifyProperty(p, player, property)
|
|
|
|
end
|
2022-03-25 12:28:07 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param p ServerPlayer
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param property string
|
2022-03-28 22:24:30 +08:00
|
|
|
function Room:notifyProperty(p, player, property)
|
2022-04-30 15:27:56 +08:00
|
|
|
p:doNotify("PropertyUpdate", json.encode{
|
|
|
|
player:getId(),
|
|
|
|
property,
|
|
|
|
player[property],
|
|
|
|
})
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param command string
|
|
|
|
---@param jsonData string
|
2022-04-02 21:39:44 +08:00
|
|
|
---@param players ServerPlayer[] @ default all players
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:doBroadcastNotify(command, jsonData, players)
|
2022-04-30 15:27:56 +08:00
|
|
|
players = players or self.players
|
|
|
|
local tolist = fk.SPlayerList()
|
|
|
|
for _, p in ipairs(players) do
|
|
|
|
tolist:append(p.serverplayer)
|
|
|
|
end
|
|
|
|
self.room:doBroadcastNotify(tolist, command, jsonData)
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param command string
|
|
|
|
---@param jsonData string
|
2022-04-02 21:39:44 +08:00
|
|
|
---@param wait boolean @ default true
|
2022-03-31 13:29:23 +08:00
|
|
|
---@return string | nil
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:doRequest(player, command, jsonData, wait)
|
2022-04-30 15:27:56 +08:00
|
|
|
if wait == nil then wait = true end
|
|
|
|
player:doRequest(command, jsonData, self.timeout)
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if wait then
|
|
|
|
return player:waitForReply(self.timeout)
|
|
|
|
end
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param command string
|
|
|
|
---@param players ServerPlayer[]
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:doBroadcastRequest(command, players)
|
2022-04-30 15:27:56 +08:00
|
|
|
players = players or self.players
|
|
|
|
self:notifyMoveFocus(players, command)
|
|
|
|
for _, p in ipairs(players) do
|
|
|
|
self:doRequest(p, command, p.request_data, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
local remainTime = self.timeout
|
|
|
|
local currentTime = os.time()
|
|
|
|
local elapsed = 0
|
|
|
|
for _, p in ipairs(players) do
|
|
|
|
elapsed = os.time() - currentTime
|
|
|
|
remainTime = remainTime - elapsed
|
|
|
|
p:waitForReply(remainTime)
|
|
|
|
end
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param players ServerPlayer | ServerPlayer[]
|
|
|
|
---@param command string
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:notifyMoveFocus(players, command)
|
2022-04-30 15:27:56 +08:00
|
|
|
if (players.class) then
|
|
|
|
players = {players}
|
|
|
|
end
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local ids = {}
|
|
|
|
for _, p in ipairs(players) do
|
|
|
|
table.insert(ids, p:getId())
|
|
|
|
end
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self:doBroadcastNotify("MoveFocus", json.encode{
|
|
|
|
ids,
|
|
|
|
command
|
|
|
|
})
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function Room:adjustSeats()
|
2022-04-30 15:27:56 +08:00
|
|
|
local players = {}
|
|
|
|
local p = 0
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for i = 1, #self.players do
|
|
|
|
if self.players[i].role == "lord" then
|
|
|
|
p = i
|
|
|
|
break
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
for j = p, #self.players do
|
|
|
|
table.insert(players, self.players[j])
|
|
|
|
end
|
|
|
|
for j = 1, p - 1 do
|
|
|
|
table.insert(players, self.players[j])
|
|
|
|
end
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.players = players
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local player_circle = {}
|
|
|
|
for i = 1, #self.players do
|
|
|
|
self.players[i].seat = i
|
|
|
|
table.insert(player_circle, self.players[i]:getId())
|
|
|
|
end
|
2022-03-28 22:24:30 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self:doBroadcastNotify("ArrangeSeats", json.encode(player_circle))
|
2022-03-28 22:24:30 +08:00
|
|
|
end
|
|
|
|
|
2022-04-08 18:39:58 +08:00
|
|
|
function Room:shuffleDrawPile()
|
2022-04-30 15:27:56 +08:00
|
|
|
if #self.draw_pile + #self.discard_pile == 0 then
|
|
|
|
return
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
table.insertTable(self.draw_pile, self.discard_pile)
|
|
|
|
for _, id in ipairs(self.discard_pile) do
|
|
|
|
self:setCardArea(id, Card.DrawPile)
|
|
|
|
end
|
|
|
|
self.discard_pile = {}
|
|
|
|
table.shuffle(self.draw_pile)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param num integer
|
|
|
|
---@param from string
|
|
|
|
---@return integer[]
|
|
|
|
function Room:getNCards(num, from)
|
2022-04-30 15:27:56 +08:00
|
|
|
from = from or "top"
|
|
|
|
assert(from == "top" or from == "bottom")
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local cardIds = {}
|
|
|
|
while num > 0 do
|
|
|
|
if #self.draw_pile < 1 then
|
|
|
|
self:shuffleDrawPile()
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local index = from == "top" and 1 or #self.draw_pile
|
|
|
|
table.insert(cardIds, self.draw_pile[index])
|
|
|
|
table.remove(self.draw_pile, index)
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
num = num - 1
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return cardIds
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param cardId integer
|
|
|
|
---@param cardArea CardArea
|
|
|
|
function Room:setCardArea(cardId, cardArea)
|
2022-04-30 15:27:56 +08:00
|
|
|
self.card_place[cardId] = cardArea
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param cardId integer
|
|
|
|
---@return CardArea
|
|
|
|
function Room:getCardArea(cardId)
|
2022-04-30 15:27:56 +08:00
|
|
|
return self.card_place[cardId] or Card.Unknown
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
2022-04-14 18:22:00 +08:00
|
|
|
---@param players ServerPlayer[]
|
|
|
|
---@param card_moves CardsMoveStruct[]
|
|
|
|
---@param forceVisible boolean
|
|
|
|
function Room:notifyMoveCards(players, card_moves, forceVisible)
|
2022-04-30 15:27:56 +08:00
|
|
|
if players == nil or players == {} then players = self.players end
|
|
|
|
for _, p in ipairs(players) do
|
|
|
|
local arg = table.clone(card_moves)
|
|
|
|
for _, move in ipairs(arg) do
|
|
|
|
-- local to = self:getPlayerById(move.to)
|
|
|
|
|
|
|
|
-- forceVisible make the move visible
|
|
|
|
-- FIXME: move.moveInfo is an array, fix this
|
|
|
|
move.moveVisible = (forceVisible)
|
|
|
|
-- if move is relevant to player, it should be open
|
|
|
|
or ((move.from == p:getId()) or (move.to == p:getId() and move.toArea ~= Card.PlayerSpecial))
|
|
|
|
-- cards move from/to equip/judge/discard/processing should be open
|
|
|
|
or move.moveInfo.fromArea == Card.PlayerEquip
|
|
|
|
or move.toArea == Card.PlayerEquip
|
|
|
|
or move.moveInfo.fromArea == Card.PlayerJudge
|
|
|
|
or move.toArea == Card.PlayerJudge
|
|
|
|
or move.moveInfo.fromArea == Card.DiscardPile
|
|
|
|
or move.toArea == Card.DiscardPile
|
|
|
|
or move.moveInfo.fromArea == Card.Processing
|
|
|
|
or move.toArea == Card.Processing
|
|
|
|
-- TODO: PlayerSpecial
|
|
|
|
|
|
|
|
if not move.moveVisible then
|
|
|
|
for _, info in ipairs(move.moveInfo) do
|
|
|
|
info.cardId = -1
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
p:doNotify("MoveCards", json.encode(arg))
|
|
|
|
end
|
2022-04-14 18:22:00 +08:00
|
|
|
end
|
|
|
|
|
2022-04-08 18:39:58 +08:00
|
|
|
---@vararg CardsMoveInfo
|
|
|
|
---@return boolean
|
|
|
|
function Room:moveCards(...)
|
2022-04-30 15:27:56 +08:00
|
|
|
---@type CardsMoveStruct[]
|
|
|
|
local cardsMoveStructs = {}
|
|
|
|
local infoCheck = function(info)
|
|
|
|
assert(table.contains({ Card.PlayerHand, Card.PlayerEquip, Card.PlayerJudge, Card.PlayerSpecial, Card.Processing, Card.DrawPile, Card.DiscardPile, Card.Void }, info.toArea))
|
|
|
|
assert(info.toArea ~= Card.PlayerSpecial or type(info.specialName) == "string")
|
|
|
|
assert(type(info.moveReason) == "number")
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, cardsMoveInfo in ipairs({...}) do
|
|
|
|
if #cardsMoveInfo.ids > 0 then
|
|
|
|
infoCheck(cardsMoveInfo)
|
|
|
|
|
|
|
|
---@type MoveInfo[]
|
|
|
|
local infos = {}
|
|
|
|
for _, id in ipairs(cardsMoveInfo.ids) do
|
|
|
|
table.insert(infos, { cardId = id, fromArea = self:getCardArea(id) })
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type CardsMoveStruct
|
|
|
|
local cardsMoveStruct = {
|
|
|
|
moveInfo = infos,
|
|
|
|
from = cardsMoveInfo.from,
|
|
|
|
to = cardsMoveInfo.to,
|
|
|
|
toArea = cardsMoveInfo.toArea,
|
|
|
|
moveReason = cardsMoveInfo.moveReason,
|
|
|
|
proposer = cardsMoveInfo.proposer,
|
|
|
|
skillName = cardsMoveInfo.skillName,
|
|
|
|
moveVisible = cardsMoveInfo.moveVisible,
|
|
|
|
specialName = cardsMoveInfo.specialName,
|
|
|
|
specialVisible = cardsMoveInfo.specialVisible,
|
|
|
|
}
|
|
|
|
|
|
|
|
table.insert(cardsMoveStructs, cardsMoveStruct)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #cardsMoveStructs < 1 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.logic:trigger(fk.BeforeCardsMove, nil, cardsMoveStructs) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
self:notifyMoveCards(self.players, cardsMoveStructs)
|
|
|
|
|
|
|
|
for _, data in ipairs(cardsMoveStructs) do
|
|
|
|
if #data.moveInfo > 0 then
|
|
|
|
infoCheck(data)
|
|
|
|
|
|
|
|
---@param info MoveInfo
|
|
|
|
for _, info in ipairs(data.moveInfo) do
|
|
|
|
local realFromArea = self:getCardArea(info.cardId)
|
|
|
|
local playerAreas = { Player.Hand, Player.Equip, Player.Judge, Player.Special }
|
|
|
|
|
|
|
|
if table.contains(playerAreas, realFromArea) and data.from then
|
|
|
|
self:getPlayerById(data.from):removeCards(realFromArea, { info.cardId }, data.specialName)
|
|
|
|
elseif realFromArea ~= Card.Unknown then
|
|
|
|
local fromAreaIds = {}
|
|
|
|
if realFromArea == Card.Processing then
|
|
|
|
fromAreaIds = self.processing_area
|
|
|
|
elseif realFromArea == Card.DrawPile then
|
|
|
|
fromAreaIds = self.draw_pile
|
|
|
|
elseif realFromArea == Card.DiscardPile then
|
|
|
|
fromAreaIds = self.discard_pile
|
|
|
|
elseif realFromArea == Card.Void then
|
|
|
|
fromAreaIds = self.void
|
|
|
|
end
|
|
|
|
|
|
|
|
table.removeOne(fromAreaIds, info.cardId)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if table.contains(playerAreas, data.toArea) and data.to then
|
|
|
|
self:getPlayerById(data.to):addCards(data.toArea, { info.cardId }, data.specialName)
|
|
|
|
else
|
|
|
|
local toAreaIds = {}
|
|
|
|
if data.toArea == Card.Processing then
|
|
|
|
toAreaIds = self.processing_area
|
|
|
|
elseif data.toArea == Card.DrawPile then
|
|
|
|
toAreaIds = self.draw_pile
|
|
|
|
elseif data.toArea == Card.DiscardPile then
|
|
|
|
toAreaIds = self.discard_pile
|
|
|
|
elseif data.toArea == Card.Void then
|
|
|
|
toAreaIds = self.void
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(toAreaIds, toAreaIds == Card.DrawPile and 1 or #toAreaIds + 1, info.cardId)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
self:setCardArea(info.cardId, data.toArea)
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.logic:trigger(fk.AfterCardsMove, nil, cardsMoveStructs)
|
|
|
|
return true
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param num integer
|
|
|
|
---@param skillName string
|
|
|
|
---@param fromPlace "top"|"bottom"
|
|
|
|
---@return integer[]
|
|
|
|
function Room:drawCards(player, num, skillName, fromPlace)
|
2022-04-30 15:27:56 +08:00
|
|
|
local topCards = self:getNCards(num, fromPlace)
|
|
|
|
self:moveCards({
|
|
|
|
ids = topCards,
|
|
|
|
to = player:getId(),
|
|
|
|
toArea = Card.PlayerHand,
|
|
|
|
moveReason = fk.ReasonDraw,
|
|
|
|
proposer = player:getId(),
|
|
|
|
skillName = skillName,
|
|
|
|
})
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return { table.unpack(topCards) }
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param minNum integer
|
|
|
|
---@param maxNum integer
|
|
|
|
---@param includeEquip boolean
|
|
|
|
---@param skillName string
|
|
|
|
function Room:askForDiscard(player, minNum, maxNum, includeEquip, skillName)
|
2022-04-30 15:27:56 +08:00
|
|
|
if minNum < 1 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local hands = player:getCardIds(Player.Hand)
|
|
|
|
local toDiscard = {}
|
|
|
|
for i = 1, minNum do
|
|
|
|
local randomId = hands[math.random(1, #hands)]
|
|
|
|
table.insert(toDiscard, randomId)
|
|
|
|
table.removeOne(hands, randomId)
|
|
|
|
end
|
|
|
|
|
|
|
|
self:moveCards({
|
|
|
|
ids = toDiscard,
|
|
|
|
from = player:getId(),
|
|
|
|
toArea = Card.DiscardPile,
|
|
|
|
moveReason = fk.ReasonDiscard,
|
|
|
|
proposer = player:getId(),
|
|
|
|
skillName = skillName
|
|
|
|
})
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param id integer
|
|
|
|
---@return ServerPlayer
|
|
|
|
function Room:getPlayerById(id)
|
2022-04-30 15:27:56 +08:00
|
|
|
assert(type(id) == "number")
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for _, p in ipairs(self.players) do
|
|
|
|
if p:getId() == id then
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
error("cannot find player by " .. id)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param playerIds integer[]
|
|
|
|
function Room:sortPlayersByAction(playerIds)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function Room:deadPlayerFilter(playerIds)
|
|
|
|
local newPlayerIds = {}
|
|
|
|
for _, playerId in ipairs(playerIds) do
|
|
|
|
if self:getPlayerById(playerId):isAlive() then
|
|
|
|
table.insert(newPlayerIds, playerId)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return newPlayerIds
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param sortBySeat boolean
|
|
|
|
---@return ServerPlayer[]
|
|
|
|
function Room:getAlivePlayers(sortBySeat)
|
2022-04-30 15:27:56 +08:00
|
|
|
sortBySeat = sortBySeat or true
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local alivePlayers = {}
|
|
|
|
for _, player in ipairs(self.players) do
|
|
|
|
if player:isAlive() then
|
|
|
|
table.insert(alivePlayers, player)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return alivePlayers
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param sortBySeat boolean
|
|
|
|
---@return ServerPlayer[]
|
|
|
|
function Room:getOtherPlayers(player, sortBySeat)
|
2022-04-30 15:27:56 +08:00
|
|
|
local alivePlayers = self:getAlivePlayers(sortBySeat)
|
|
|
|
for _, p in ipairs(alivePlayers) do
|
|
|
|
if p:getId() == player:getId() then
|
|
|
|
table.removeOne(alivePlayers, player)
|
|
|
|
break
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return alivePlayers
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@return ServerPlayer | null
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:getLord()
|
2022-04-30 15:27:56 +08:00
|
|
|
local lord = self.players[1]
|
|
|
|
if lord.role == "lord" then return lord end
|
|
|
|
for _, p in ipairs(self.players) do
|
|
|
|
if p.role == "lord" then return p end
|
|
|
|
end
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return nil
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param expect ServerPlayer
|
|
|
|
---@return ServerPlayer[]
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:getOtherPlayers(expect)
|
2022-04-30 15:27:56 +08:00
|
|
|
local ret = {table.unpack(self.players)}
|
|
|
|
table.removeOne(ret, expect)
|
|
|
|
return ret
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param generals string[]
|
|
|
|
---@return string
|
2022-03-30 14:14:40 +08:00
|
|
|
function Room:askForGeneral(player, generals)
|
2022-04-30 15:27:56 +08:00
|
|
|
local command = "AskForGeneral"
|
|
|
|
self:notifyMoveFocus(player, command)
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if #generals == 1 then return generals[1] end
|
|
|
|
local defaultChoice = generals[1]
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if (player.state == "online") then
|
|
|
|
local result = self:doRequest(player, command, json.encode(generals))
|
|
|
|
if result == "" then
|
|
|
|
return defaultChoice
|
|
|
|
else
|
|
|
|
-- TODO: result is a JSON array
|
|
|
|
-- update here when choose multiple generals
|
|
|
|
return json.decode(result)[1]
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-03-30 14:14:40 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return defaultChoice
|
2022-03-30 14:14:40 +08:00
|
|
|
end
|
|
|
|
|
2022-03-27 14:49:41 +08:00
|
|
|
function Room:gameOver()
|
2022-04-30 15:27:56 +08:00
|
|
|
self.game_finished = true
|
|
|
|
-- dosomething
|
|
|
|
self.room:gameOver()
|
2022-03-25 12:28:07 +08:00
|
|
|
end
|
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param choices string[]
|
2022-04-02 15:11:13 +08:00
|
|
|
---@param skill_name string
|
|
|
|
function Room:askForChoice(player, choices, skill_name, data)
|
2022-04-30 15:27:56 +08:00
|
|
|
if #choices == 1 then return choices[1] end
|
|
|
|
local command = "AskForChoice"
|
|
|
|
self:notifyMoveFocus(player, skill_name)
|
|
|
|
local result = self:doRequest(player, command, json.encode{
|
|
|
|
choices, skill_name
|
|
|
|
})
|
|
|
|
if result == "" then result = choices[1] end
|
|
|
|
return result
|
2022-04-02 15:11:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param skill_name string
|
|
|
|
---@return boolean
|
|
|
|
function Room:askForSkillInvoke(player, skill_name, data)
|
2022-04-30 15:27:56 +08:00
|
|
|
local command = "AskForSkillInvoke"
|
|
|
|
self:notifyMoveFocus(player, skill_name)
|
|
|
|
local invoked = false
|
|
|
|
local result = self:doRequest(player, command, skill_name)
|
|
|
|
if result ~= "" then invoked = true end
|
|
|
|
return invoked
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|
|
|
|
|
2022-04-08 18:39:58 +08:00
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param num integer
|
|
|
|
---@param reason "loseHp"|"damage"|"recover"|null
|
|
|
|
---@param skillName string
|
|
|
|
---@param damageStruct DamageStruct|null
|
|
|
|
---@return boolean
|
|
|
|
function Room:changeHp(player, num, reason, skillName, damageStruct)
|
2022-04-30 15:27:56 +08:00
|
|
|
if num == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
assert(reason == nil or table.contains({ "loseHp", "damage", "recover" }, reason))
|
|
|
|
|
|
|
|
---@type HpChangedData
|
|
|
|
local data = {
|
|
|
|
num = num,
|
|
|
|
reason = reason,
|
|
|
|
skillName = skillName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.logic:trigger(fk.BeforeHpChanged, player, data) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(not (data.reason == "recover" and data.num < 0))
|
|
|
|
player.hp = math.min(player.hp + data.num, player.maxHp)
|
|
|
|
|
|
|
|
self.logic:trigger(fk.HpChanged, player, data)
|
|
|
|
|
|
|
|
if player.hp < 1 then
|
|
|
|
---@type DyingStruct
|
|
|
|
local dyingStruct = {
|
|
|
|
who = player:getId(),
|
|
|
|
damage = damageStruct,
|
2022-04-08 18:39:58 +08:00
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
self:enterDying(dyingStruct)
|
|
|
|
elseif player.dying then
|
|
|
|
player.dying = false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
return true
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param num integer
|
|
|
|
---@param skillName string
|
|
|
|
---@return boolean
|
|
|
|
function Room:loseHp(player, num, skillName)
|
2022-04-30 15:27:56 +08:00
|
|
|
if num == nil then
|
|
|
|
num = 1
|
|
|
|
elseif num < 1 then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
---@type HpLostData
|
|
|
|
local data = {
|
|
|
|
num = num,
|
|
|
|
skillName = skillName,
|
|
|
|
}
|
|
|
|
if self.logic:trigger(fk.PreHpLost, player, data) or data.num < 1 then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if not self:changeHp(player, -num, "loseHp", skillName) then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.logic:trigger(fk.HpLost, player, data)
|
|
|
|
return true
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param player ServerPlayer
|
|
|
|
---@param num integer
|
|
|
|
---@return boolean
|
|
|
|
function Room:changeMaxHp(player, num)
|
2022-04-30 15:27:56 +08:00
|
|
|
if num == 0 then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
player.maxHp = math.max(player.maxHp + num, 0)
|
|
|
|
local diff = player.hp - player.maxHp
|
|
|
|
if diff > 0 then
|
|
|
|
if not self:changeHp(player, -diff) then
|
|
|
|
player.hp = player.hp - diff
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if player.maxHp == 0 then
|
|
|
|
self:killPlayer({ who = player:getId() })
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.logic:trigger(fk.MaxHpChanged, player, { num = num })
|
|
|
|
return true
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param damageStruct DamageStruct
|
|
|
|
---@return boolean
|
|
|
|
function Room:damage(damageStruct)
|
2022-04-30 15:27:56 +08:00
|
|
|
if damageStruct.damage < 1 then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
assert(type(damageStruct.to) == "number")
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local stages = {
|
|
|
|
[fk.PreDamage] = damageStruct.from,
|
|
|
|
[fk.DamageCaused] = damageStruct.from,
|
|
|
|
[fk.DamageInflicted] = damageStruct.to,
|
|
|
|
}
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for event, playerId in ipairs(stages) do
|
|
|
|
local player = playerId and self:getPlayerById(playerId) or nil
|
|
|
|
if self.logic:trigger(event, player, damageStruct) or damageStruct.damage < 1 then
|
|
|
|
return false
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
assert(type(damageStruct.to) == "number")
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
assert(self:getPlayerById(damageStruct.to))
|
|
|
|
local victim = self:getPlayerById(damageStruct.to)
|
|
|
|
if not victim:isAlive() then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if not self:changeHp(victim, -damageStruct.damage, "damage", damageStruct.skillName, damageStruct) then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
stages = {
|
|
|
|
[fk.Damage] = damageStruct.from,
|
|
|
|
[fk.Damaged] = damageStruct.to,
|
|
|
|
[fk.DamageFinished] = damageStruct.from,
|
|
|
|
}
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for event, playerId in ipairs(stages) do
|
|
|
|
local player = playerId and self:getPlayerById(playerId) or nil
|
|
|
|
self.logic:trigger(event, player, damageStruct)
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param recoverStruct RecoverStruct
|
|
|
|
---@return boolean
|
|
|
|
function Room:recover(recoverStruct)
|
2022-04-30 15:27:56 +08:00
|
|
|
if recoverStruct.num < 1 then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
local who = self:getPlayerById(recoverStruct.who)
|
|
|
|
if self.logic:trigger(fk.PreHpRecover, who, recoverStruct) or recoverStruct.num < 1 then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if not self:changeHp(who, recoverStruct.num, "recover", recoverStruct.skillName) then
|
|
|
|
return false
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
self.logic:trigger(fk.HpRecover, who, recoverStruct)
|
|
|
|
return true
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param dyingStruct DyingStruct
|
|
|
|
function Room:enterDying(dyingStruct)
|
2022-04-30 15:27:56 +08:00
|
|
|
local dyingPlayer = self:getPlayerById(dyingStruct.who)
|
|
|
|
dyingPlayer.dying = true
|
|
|
|
self.logic:trigger(fk.EnterDying, dyingPlayer, dyingStruct)
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if dyingPlayer.hp < 1 then
|
|
|
|
local alivePlayers = self:getAlivePlayers()
|
|
|
|
for _, player in ipairs(alivePlayers) do
|
|
|
|
self.logic:trigger(fk.Dying, player, dyingStruct)
|
|
|
|
|
|
|
|
if player.hp > 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-04-08 18:39:58 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if dyingPlayer.hp < 1 then
|
|
|
|
---@type DeathStruct
|
|
|
|
local deathData = {
|
|
|
|
who = dyingPlayer:getId(),
|
|
|
|
damage = dyingStruct.damage,
|
|
|
|
}
|
|
|
|
self:killPlayer(deathData)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
self.logic:trigger(fk.AfterDying, dyingPlayer, dyingStruct)
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param deathStruct DeathStruct
|
|
|
|
function Room:killPlayer(deathStruct)
|
2022-04-30 15:27:56 +08:00
|
|
|
print(self:getPlayerById(deathStruct.who).general .. " is dead")
|
|
|
|
self:gameOver()
|
2022-04-08 18:39:58 +08:00
|
|
|
end
|
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
---@param room Room
|
|
|
|
---@param cardUseEvent CardUseStruct
|
|
|
|
---@param aimEventCollaborators table<string, AimStruct[]>
|
|
|
|
---@return boolean
|
|
|
|
local onAim = function(room, cardUseEvent, aimEventCollaborators)
|
|
|
|
local eventStages = { fk.TargetSpecifying, fk.TargetConfirming, fk.TargetSpecified, fk.TargetConfirmed }
|
|
|
|
for _, stage in ipairs(eventStages) do
|
|
|
|
if not cardUseEvent.tos then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
room:sortPlayersByAction(cardUseEvent.tos)
|
|
|
|
local aimGroup = AimGroup:initAimGroup(TargetGroup:getRealTargets(cardUseEvent.tos))
|
|
|
|
|
|
|
|
local collaboratorsIndex = {}
|
|
|
|
local firstTarget = true
|
|
|
|
repeat
|
|
|
|
local toId = AimGroup:getUndoneOrDoneTargets(aimGroup)[1]
|
|
|
|
---@type AimStruct
|
|
|
|
local aimStruct
|
|
|
|
local initialEvent = false
|
|
|
|
collaboratorsIndex[toId] = collaboratorsIndex[toId] or 0
|
|
|
|
|
|
|
|
if not aimEventCollaborators[toId] or collaboratorsIndex[toId] >= #aimEventCollaborators[toId] then
|
|
|
|
aimStruct = {
|
|
|
|
from = cardUseEvent.from,
|
|
|
|
cardId = cardUseEvent.cardId,
|
|
|
|
to = toId,
|
|
|
|
targetGroup = cardUseEvent.tos,
|
|
|
|
nullifiedTargets = cardUseEvent.nullifiedTargets or {},
|
|
|
|
tos = aimGroup,
|
|
|
|
firstTarget = firstTarget,
|
|
|
|
additionalDamage = cardUseEvent.addtionalDamage
|
|
|
|
}
|
|
|
|
|
|
|
|
collaboratorsIndex[toId] = 1
|
|
|
|
initialEvent = true
|
|
|
|
else
|
|
|
|
aimStruct = aimEventCollaborators[toId][collaboratorsIndex[toId]]
|
|
|
|
aimStruct.from = cardUseEvent.from
|
|
|
|
aimStruct.cardId = cardUseEvent.cardId
|
|
|
|
aimStruct.tos = aimGroup
|
|
|
|
aimStruct.targetGroup = cardUseEvent.tos
|
|
|
|
aimStruct.nullifiedTargets = cardUseEvent.nullifiedTargets or {}
|
|
|
|
aimStruct.firstTarget = firstTarget
|
|
|
|
end
|
|
|
|
|
|
|
|
firstTarget = false
|
|
|
|
|
|
|
|
if room.logic:trigger(stage, (stage == fk.TargetSpecifying or stage == fk.TargetSpecified) and room:getPlayerById(aimStruct.from) or room:getPlayerById(aimStruct.to), aimStruct) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
AimGroup:removeDeadTargets(room, aimStruct)
|
|
|
|
|
|
|
|
local aimEventTargetGroup = aimStruct.targetGroup
|
|
|
|
if aimEventTargetGroup then
|
|
|
|
room:sortPlayersByAction(aimEventTargetGroup)
|
|
|
|
end
|
|
|
|
|
|
|
|
cardUseEvent.from = aimStruct.from
|
|
|
|
cardUseEvent.tos = aimEventTargetGroup
|
|
|
|
cardUseEvent.nullifiedTargets = aimStruct.nullifiedTargets
|
|
|
|
|
|
|
|
if #AimGroup:getAllTargets(aimStruct.tos) == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local cancelledTargets = AimGroup:getCancelledTargets(aimStruct.tos)
|
|
|
|
if #cancelledTargets > 0 then
|
|
|
|
for _, target in ipairs(cancelledTargets) do
|
|
|
|
aimEventCollaborators[target] = {}
|
|
|
|
collaboratorsIndex[target] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
aimStruct.tos[AimGroup.Cancelled] = {}
|
|
|
|
|
|
|
|
aimEventCollaborators[toId] = aimEventCollaborators[toId] or {}
|
|
|
|
if not room:getPlayerById(toId):isAlive() then
|
|
|
|
if initialEvent then
|
|
|
|
table.insert(aimEventCollaborators[toId], aimStruct)
|
|
|
|
else
|
|
|
|
aimEventCollaborators[toId][collaboratorsIndex[toId]] = aimStruct
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
AimGroup:setTargetDone(aimStruct.tos, toId)
|
|
|
|
aimGroup = aimStruct.tos
|
|
|
|
until #AimGroup:getUndoneOrDoneTargets(aimGroup) == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param cardUseEvent CardUseStruct
|
|
|
|
---@return boolean
|
|
|
|
function Room:useCard(cardUseEvent)
|
|
|
|
self:moveCards({
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
from = cardUseEvent.customFrom or cardUseEvent.from,
|
|
|
|
toArea = Card.Processing,
|
|
|
|
moveReason = fk.ReasonUse,
|
|
|
|
})
|
|
|
|
|
|
|
|
if Fk:getCardById(cardUseEvent.cardId).skill then
|
|
|
|
Fk:getCardById(cardUseEvent.cardId).skill:onUse(self, cardUseEvent)
|
|
|
|
end
|
|
|
|
if self.logic:trigger(fk.PreCardUse, self:getPlayerById(cardUseEvent.from), cardUseEvent) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if not cardUseEvent.extraUse then
|
|
|
|
self:getPlayerById(cardUseEvent.from):addCardUseHistory(Fk:getCardById(cardUseEvent.cardId).trueName, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
if cardUseEvent.responseToEvent then
|
|
|
|
cardUseEvent.responseToEvent.cardIdsResponded = cardUseEvent.responseToEvent.cardIdsResponded or {}
|
|
|
|
table.insert(cardUseEvent.responseToEvent.cardIdsResponded, cardUseEvent.cardId)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, event in ipairs({ fk.AfterCardUseDeclared, fk.AfterCardTargetDeclared, fk.BeforeCardUseEffect, fk.CardUsing }) do
|
|
|
|
-- TODO: need to complete the cards for response
|
|
|
|
|
|
|
|
self.logic:trigger(event, self:getPlayerById(cardUseEvent.from), cardUseEvent)
|
|
|
|
if event == fk.CardUsing then
|
|
|
|
---@type table<string, AimStruct>
|
|
|
|
local aimEventCollaborators = {}
|
|
|
|
if cardUseEvent.tos and not onAim(self, cardUseEvent, aimEventCollaborators) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if Fk:getCardById(cardUseEvent.cardId).type == Card.TypeEquip then
|
|
|
|
if self:getCardArea(cardUseEvent.cardId) ~= Card.Processing then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if self:getPlayerById(TargetGroup:getRealTargets(cardUseEvent.tos)[1]).dead then
|
|
|
|
self.moveCards({
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
toArea = Card.DiscardPile,
|
|
|
|
moveReason = fk.ReasonPutIntoDiscardPile,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
local target = TargetGroup:getRealTargets(cardUseEvent.tos)[1]
|
|
|
|
local existingEquipId = self:getPlayerById(target):getEquipment(Fk:getCardById(cardUseEvent.cardId).sub_type)
|
|
|
|
if existingEquipId then
|
|
|
|
self:moveCards(
|
|
|
|
{
|
|
|
|
ids = { existingEquipId },
|
|
|
|
from = target,
|
|
|
|
toArea = Card.DiscardPile,
|
|
|
|
moveReason = fk.ReasonPutIntoDiscardPile,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
to = target,
|
|
|
|
toArea = Card.PlayerEquip,
|
|
|
|
moveReason = fk.ReasonUse,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else
|
|
|
|
self:moveCards({
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
to = target,
|
|
|
|
toArea = Card.PlayerEquip,
|
|
|
|
moveReason = fk.ReasonUse,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
break
|
|
|
|
elseif Fk:getCardById(cardUseEvent.cardId).sub_type == Card.SubtypeDelayedTrick then
|
|
|
|
if self:getCardArea(cardUseEvent.cardId) ~= Card.Processing then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
local target = TargetGroup:getRealTargets(cardUseEvent.tos)[1]
|
|
|
|
if not self:getPlayerById(target).dead then
|
|
|
|
local findSameCard = false
|
|
|
|
for _, cardId in ipairs(self:getPlayerById(target):getCardIds(Player.Equip)) do
|
|
|
|
if Fk:getCardById(cardId).trueName == Fk:getCardById(cardUseEvent.cardId) then
|
|
|
|
findSameCard = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not findSameCard then
|
|
|
|
self:moveCards({
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
to = target,
|
|
|
|
toArea = Card.PlayerJudge,
|
|
|
|
moveReason = fk.ReasonUse,
|
|
|
|
})
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:moveCards({
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
toArea = Card.DiscardPile,
|
|
|
|
moveReason = fk.ReasonPutIntoDiscardPile,
|
|
|
|
})
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if Fk:getCardById(cardUseEvent.cardId).skill then
|
|
|
|
Fk:getCardById(cardUseEvent.cardId).skill:onEffect(self, cardUseEvent)
|
|
|
|
end
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
self.logic:trigger(fk.CardUseFinished, self:getPlayerById(cardUseEvent.from), cardUseEvent)
|
|
|
|
if self:getCardArea(cardUseEvent.cardId) == Card.Processing then
|
|
|
|
self:moveCards({
|
|
|
|
ids = { cardUseEvent.cardId },
|
|
|
|
toArea = Card.DiscardPile,
|
|
|
|
moveReason = fk.ReasonPutIntoDiscardPile,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fk.room_callback["QuitRoom"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local player = fk.ServerInstance:findPlayer(tonumber(data[1]))
|
|
|
|
local room = player:getRoom()
|
|
|
|
if not room:isLobby() then
|
|
|
|
room:removePlayer(player)
|
|
|
|
end
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|
|
|
|
|
2022-04-03 06:41:55 +08:00
|
|
|
fk.room_callback["AddRobot"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int uid ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local player = fk.ServerInstance:findPlayer(tonumber(data[1]))
|
|
|
|
local room = player:getRoom()
|
|
|
|
|
|
|
|
if not room:isLobby() then
|
|
|
|
room:addRobot(player)
|
|
|
|
end
|
2022-04-03 06:41:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
fk.room_callback["PlayerRunned"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int runner_id, int robot_id ]
|
|
|
|
-- note: this function is not called by Router.
|
|
|
|
-- note: when this function is called, the room must be started
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local runner = data[1]
|
|
|
|
local robot = data[2]
|
|
|
|
for _, p in ipairs(RoomInstance.players) do
|
|
|
|
if p:getId() == runner then
|
|
|
|
p.serverplayer = RoomInstance.room:findPlayer(robot)
|
|
|
|
p.id = p.serverplayer:getId()
|
2022-04-03 06:41:55 +08:00
|
|
|
end
|
2022-04-30 15:27:56 +08:00
|
|
|
end
|
2022-04-03 06:41:55 +08:00
|
|
|
end
|
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
fk.room_callback["PlayerStateChanged"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int uid, string stateString ]
|
|
|
|
-- note: this function is not called by Router.
|
|
|
|
-- note: when this function is called, the room must be started
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id = data[1]
|
|
|
|
local stateString = data[2]
|
|
|
|
RoomInstance:getPlayerById(id).state = stateString
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|
|
|
|
|
2022-04-03 21:43:35 +08:00
|
|
|
fk.room_callback["RoomDeleted"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
debug.sethook(function ()
|
|
|
|
error("Room is deleted when running")
|
|
|
|
end, "l")
|
2022-04-03 21:43:35 +08:00
|
|
|
end
|
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
fk.room_callback["DoLuaScript"] = function(jsonData)
|
2022-04-30 15:27:56 +08:00
|
|
|
-- jsonData: [ int uid, string luaScript ]
|
|
|
|
-- warning: only use this in debugging mode.
|
|
|
|
if not DebugMode then return end
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
assert(load(data[2]))()
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function CreateRoom(_room)
|
2022-04-30 15:27:56 +08:00
|
|
|
RoomInstance = Room:new(_room)
|
2022-04-01 20:51:01 +08:00
|
|
|
end
|