2023-04-09 05:35:35 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-05-18 23:45:21 +00:00
|
|
|
local function drawInit(room, player, n)
|
|
|
|
-- TODO: need a new function to call the UI
|
|
|
|
local cardIds = room:getNCards(n)
|
|
|
|
player:addCards(Player.Hand, cardIds)
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
Fk:filterCard(id, player)
|
|
|
|
end
|
|
|
|
local move_to_notify = {} ---@type CardsMoveStruct
|
|
|
|
move_to_notify.toArea = Card.PlayerHand
|
|
|
|
move_to_notify.to = player.id
|
|
|
|
move_to_notify.moveInfo = {}
|
|
|
|
move_to_notify.moveReason = fk.ReasonDraw
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
table.insert(move_to_notify.moveInfo,
|
|
|
|
{ cardId = id, fromArea = Card.DrawPile })
|
|
|
|
end
|
|
|
|
room:notifyMoveCards(nil, {move_to_notify})
|
|
|
|
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
room:setCardArea(id, Card.PlayerHand, player.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function discardInit(room, player)
|
|
|
|
local cardIds = player:getCardIds(Player.Hand)
|
|
|
|
player:removeCards(Player.Hand, cardIds)
|
|
|
|
table.insertTable(room.draw_pile, cardIds)
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
Fk:filterCard(id, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
local move_to_notify = {} ---@type CardsMoveStruct
|
|
|
|
move_to_notify.from = player.id
|
|
|
|
move_to_notify.toArea = Card.DrawPile
|
|
|
|
move_to_notify.moveInfo = {}
|
|
|
|
move_to_notify.moveReason = fk.ReasonJustMove
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
table.insert(move_to_notify.moveInfo,
|
|
|
|
{ cardId = id, fromArea = Card.PlayerHand })
|
|
|
|
end
|
|
|
|
room:notifyMoveCards(nil, {move_to_notify})
|
|
|
|
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
room:setCardArea(id, Card.DrawPile, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-07 06:55:28 +00:00
|
|
|
GameEvent.functions[GameEvent.DrawInitial] = function(self)
|
|
|
|
local room = self.room
|
2023-05-18 23:45:21 +00:00
|
|
|
|
|
|
|
local luck_data = {
|
|
|
|
drawInit = drawInit,
|
|
|
|
discardInit = discardInit,
|
|
|
|
playerList = table.map(room.alive_players, Util.IdMapper),
|
|
|
|
}
|
|
|
|
|
2023-04-22 07:52:26 +00:00
|
|
|
for _, player in ipairs(room.alive_players) do
|
|
|
|
local draw_data = { num = 4 }
|
|
|
|
room.logic:trigger(fk.DrawInitialCards, player, draw_data)
|
2023-05-18 23:45:21 +00:00
|
|
|
luck_data[player.id] = draw_data
|
|
|
|
luck_data[player.id].luckTime = room.settings.luckTime
|
|
|
|
if player.id < 0 then -- Robot
|
|
|
|
luck_data[player.id].luckTime = 0
|
|
|
|
end
|
2023-04-22 07:52:26 +00:00
|
|
|
if draw_data.num > 0 then
|
2023-05-18 23:45:21 +00:00
|
|
|
drawInit(room, player, draw_data.num)
|
|
|
|
end
|
|
|
|
end
|
2023-04-22 07:52:26 +00:00
|
|
|
|
2023-05-18 23:45:21 +00:00
|
|
|
if room.settings.luckTime <= 0 then
|
|
|
|
for _, player in ipairs(room.alive_players) do
|
|
|
|
local draw_data = luck_data[player.id]
|
|
|
|
draw_data.luckTime = nil
|
2023-07-07 17:05:54 +00:00
|
|
|
room.logic:trigger(fk.AfterDrawInitialCards, player, draw_data)
|
2023-04-22 07:52:26 +00:00
|
|
|
end
|
2023-05-18 23:45:21 +00:00
|
|
|
return
|
2023-03-07 06:55:28 +00:00
|
|
|
end
|
2023-05-18 23:45:21 +00:00
|
|
|
|
|
|
|
room:setTag("LuckCardData", luck_data)
|
|
|
|
room:notifyMoveFocus(room.alive_players, "AskForLuckCard")
|
|
|
|
room:doBroadcastNotify("AskForLuckCard", room.settings.luckTime or 4)
|
|
|
|
|
2023-05-20 08:00:03 +00:00
|
|
|
local remainTime = room.timeout + 1
|
2023-05-18 23:45:21 +00:00
|
|
|
local currentTime = os.time()
|
|
|
|
local elapsed = 0
|
|
|
|
|
2023-06-16 02:56:33 +00:00
|
|
|
for _, id in ipairs(luck_data.playerList) do
|
|
|
|
local pl = room:getPlayerById(id)
|
|
|
|
if luck_data[id].luckTime > 0 then
|
|
|
|
pl.serverplayer:setThinking(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-18 23:45:21 +00:00
|
|
|
while true do
|
|
|
|
elapsed = os.time() - currentTime
|
|
|
|
if remainTime - elapsed <= 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2023-06-04 11:31:44 +00:00
|
|
|
-- local ldata = room:getTag("LuckCardData")
|
|
|
|
local ldata = luck_data
|
|
|
|
|
|
|
|
if table.every(ldata.playerList, function(id)
|
|
|
|
return ldata[id].luckTime == 0
|
2023-05-18 23:45:21 +00:00
|
|
|
end) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2023-06-04 11:31:44 +00:00
|
|
|
for _, id in ipairs(ldata.playerList) do
|
2023-06-16 02:56:33 +00:00
|
|
|
local pl = room:getPlayerById(id)
|
|
|
|
if pl._splayer:getState() ~= fk.Player_Online then
|
2023-06-04 11:31:44 +00:00
|
|
|
ldata[id].luckTime = 0
|
2023-06-16 02:56:33 +00:00
|
|
|
pl.serverplayer:setThinking(false)
|
2023-06-04 11:31:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- room:setTag("LuckCardData", ldata)
|
|
|
|
|
2023-06-16 02:56:33 +00:00
|
|
|
room:checkNoHuman()
|
2023-05-19 08:23:24 +00:00
|
|
|
|
2023-05-18 23:45:21 +00:00
|
|
|
coroutine.yield("__handleRequest", (remainTime - elapsed) * 1000)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, player in ipairs(room.alive_players) do
|
|
|
|
local draw_data = luck_data[player.id]
|
|
|
|
draw_data.luckTime = nil
|
2023-07-07 17:05:54 +00:00
|
|
|
room.logic:trigger(fk.AfterDrawInitialCards, player, draw_data)
|
2023-05-18 23:45:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
room:removeTag("LuckCardData")
|
2023-03-07 06:55:28 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 12:45:55 +00:00
|
|
|
GameEvent.functions[GameEvent.Round] = function(self)
|
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
|
|
|
local p
|
|
|
|
|
2023-05-20 08:00:03 +00:00
|
|
|
local isFirstRound = room:getTag("FirstRound")
|
|
|
|
if isFirstRound then
|
2023-04-22 07:52:26 +00:00
|
|
|
room:setTag("FirstRound", false)
|
|
|
|
end
|
|
|
|
room:setTag("RoundCount", room:getTag("RoundCount") + 1)
|
|
|
|
room:doBroadcastNotify("UpdateRoundNum", room:getTag("RoundCount"))
|
|
|
|
|
2023-05-20 08:00:03 +00:00
|
|
|
if isFirstRound then
|
|
|
|
logic:trigger(fk.GameStart, room.current)
|
|
|
|
end
|
|
|
|
|
2023-04-08 12:45:55 +00:00
|
|
|
logic:trigger(fk.RoundStart, room.current)
|
|
|
|
|
|
|
|
repeat
|
|
|
|
p = room.current
|
|
|
|
GameEvent(GameEvent.Turn):exec()
|
|
|
|
if room.game_finished then break end
|
|
|
|
room.current = room.current:getNextAlive()
|
2023-08-01 18:19:51 +00:00
|
|
|
until p.seat >= p:getNextAlive().seat
|
2023-04-08 12:45:55 +00:00
|
|
|
|
|
|
|
logic:trigger(fk.RoundEnd, p)
|
|
|
|
end
|
|
|
|
|
2023-04-22 07:52:26 +00:00
|
|
|
GameEvent.cleaners[GameEvent.Round] = function(self)
|
|
|
|
local room = self.room
|
|
|
|
|
|
|
|
for _, p in ipairs(room.players) do
|
|
|
|
p:setCardUseHistory("", 0, Player.HistoryRound)
|
|
|
|
p:setSkillUseHistory("", 0, Player.HistoryRound)
|
|
|
|
for name, _ in pairs(p.mark) do
|
|
|
|
if name:endsWith("-round") then
|
|
|
|
room:setPlayerMark(p, name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-07-11 15:16:46 +00:00
|
|
|
|
|
|
|
for cid, cmark in pairs(room.card_marks) do
|
|
|
|
for name, _ in pairs(cmark) do
|
|
|
|
if name:endsWith("-round") then
|
|
|
|
room:setCardMark(Fk:getCardById(cid), name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-04-22 07:52:26 +00:00
|
|
|
end
|
|
|
|
|
2023-03-07 06:55:28 +00:00
|
|
|
GameEvent.functions[GameEvent.Turn] = function(self)
|
|
|
|
local room = self.room
|
|
|
|
room.logic:trigger(fk.TurnStart, room.current)
|
2023-04-22 06:10:06 +00:00
|
|
|
|
2023-04-22 07:52:26 +00:00
|
|
|
room:sendLog{ type = "$AppendSeparator" }
|
|
|
|
|
2023-04-22 06:10:06 +00:00
|
|
|
local player = room.current
|
|
|
|
if not player.faceup then
|
|
|
|
player:turnOver()
|
|
|
|
elseif not player.dead then
|
|
|
|
player:play()
|
|
|
|
end
|
2023-04-22 07:52:26 +00:00
|
|
|
|
|
|
|
room.logic:trigger(fk.TurnEnd, room.current)
|
|
|
|
end
|
|
|
|
|
|
|
|
GameEvent.cleaners[GameEvent.Turn] = function(self)
|
|
|
|
local room = self.room
|
|
|
|
|
|
|
|
for _, p in ipairs(room.players) do
|
|
|
|
p:setCardUseHistory("", 0, Player.HistoryTurn)
|
|
|
|
p:setSkillUseHistory("", 0, Player.HistoryTurn)
|
|
|
|
for name, _ in pairs(p.mark) do
|
|
|
|
if name:endsWith("-turn") then
|
|
|
|
room:setPlayerMark(p, name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-11 15:16:46 +00:00
|
|
|
for cid, cmark in pairs(room.card_marks) do
|
|
|
|
for name, _ in pairs(cmark) do
|
|
|
|
if name:endsWith("-turn") then
|
|
|
|
room:setCardMark(Fk:getCardById(cid), name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-23 13:10:07 +00:00
|
|
|
local current = room.current
|
|
|
|
local logic = room.logic
|
2023-04-22 07:52:26 +00:00
|
|
|
if self.interrupted then
|
2023-04-23 13:10:07 +00:00
|
|
|
current.phase = Player.Finish
|
|
|
|
logic:trigger(fk.EventPhaseStart, current, nil, true)
|
|
|
|
logic:trigger(fk.EventPhaseEnd, current, nil, true)
|
2023-04-22 07:52:26 +00:00
|
|
|
|
2023-04-23 13:10:07 +00:00
|
|
|
current.phase = Player.NotActive
|
2023-07-16 07:32:16 +00:00
|
|
|
room:broadcastProperty(current, "phase")
|
2023-04-23 13:10:07 +00:00
|
|
|
logic:trigger(fk.EventPhaseChanging, current,
|
2023-04-22 07:52:26 +00:00
|
|
|
{ from = Player.Finish, to = Player.NotActive }, true)
|
2023-04-23 13:10:07 +00:00
|
|
|
logic:trigger(fk.EventPhaseStart, current, nil, true)
|
2023-04-22 07:52:26 +00:00
|
|
|
|
2023-04-23 13:10:07 +00:00
|
|
|
current.skipped_phases = {}
|
2023-04-22 07:52:26 +00:00
|
|
|
|
2023-04-23 13:10:07 +00:00
|
|
|
logic:trigger(fk.TurnEnd, current, nil, true)
|
2023-04-22 07:52:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
GameEvent.functions[GameEvent.Phase] = function(self)
|
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
|
|
|
|
|
|
|
local player = self.data[1]
|
|
|
|
if not logic:trigger(fk.EventPhaseStart, player) then
|
|
|
|
if player.phase ~= Player.NotActive then
|
|
|
|
logic:trigger(fk.EventPhaseProceeding, player)
|
|
|
|
|
|
|
|
switch(player.phase, {
|
|
|
|
[Player.PhaseNone] = function()
|
|
|
|
error("You should never proceed PhaseNone")
|
|
|
|
end,
|
|
|
|
[Player.RoundStart] = function()
|
|
|
|
|
|
|
|
end,
|
|
|
|
[Player.Start] = function()
|
|
|
|
|
|
|
|
end,
|
|
|
|
[Player.Judge] = function()
|
|
|
|
local cards = player:getCardIds(Player.Judge)
|
|
|
|
for i = #cards, 1, -1 do
|
|
|
|
local card
|
|
|
|
card = player:removeVirtualEquip(cards[i])
|
|
|
|
if not card then
|
|
|
|
card = Fk:getCardById(cards[i])
|
|
|
|
end
|
|
|
|
room:moveCardTo(card, Card.Processing, nil, fk.ReasonPut, self.name)
|
|
|
|
|
|
|
|
---@type CardEffectEvent
|
|
|
|
local effect_data = {
|
|
|
|
card = card,
|
|
|
|
to = player.id,
|
|
|
|
tos = { {player.id} },
|
|
|
|
}
|
|
|
|
room:doCardEffect(effect_data)
|
2023-06-19 13:56:06 +00:00
|
|
|
if effect_data.isCancellOut and card.skill then
|
2023-04-22 07:52:26 +00:00
|
|
|
card.skill:onNullified(room, effect_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[Player.Draw] = function()
|
|
|
|
local data = {
|
|
|
|
n = 2
|
|
|
|
}
|
|
|
|
room.logic:trigger(fk.DrawNCards, player, data)
|
|
|
|
room:drawCards(player, data.n, self.name)
|
|
|
|
room.logic:trigger(fk.AfterDrawNCards, player, data)
|
|
|
|
end,
|
|
|
|
[Player.Play] = function()
|
|
|
|
while not player.dead do
|
|
|
|
room:notifyMoveFocus(player, "PlayCard")
|
|
|
|
local result = room:doRequest(player, "PlayCard", player.id)
|
|
|
|
if result == "" then break end
|
|
|
|
|
|
|
|
local use = room:handleUseCardReply(player, result)
|
|
|
|
if use then
|
|
|
|
room:useCard(use)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[Player.Discard] = function()
|
2023-06-04 11:39:20 +00:00
|
|
|
local discardNum = #table.filter(
|
|
|
|
player:getCardIds(Player.Hand), function(id)
|
|
|
|
local card = Fk:getCardById(id)
|
2023-06-11 08:22:11 +00:00
|
|
|
return table.every(room.status_skills[MaxCardsSkill] or Util.DummyTable, function(skill)
|
2023-06-04 11:39:20 +00:00
|
|
|
return not skill:excludeFrom(player, card)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
) - player:getMaxCards()
|
2023-04-22 07:52:26 +00:00
|
|
|
if discardNum > 0 then
|
2023-06-11 08:22:11 +00:00
|
|
|
room:askForDiscard(player, discardNum, discardNum, false, "game_rule", false)
|
2023-04-22 07:52:26 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
[Player.Finish] = function()
|
|
|
|
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-23 13:10:07 +00:00
|
|
|
if player.phase ~= Player.NotActive then
|
|
|
|
logic:trigger(fk.EventPhaseEnd, player)
|
2023-04-22 07:52:26 +00:00
|
|
|
else
|
2023-04-23 13:10:07 +00:00
|
|
|
player.skipped_phases = {}
|
2023-04-22 07:52:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
GameEvent.cleaners[GameEvent.Phase] = function(self)
|
|
|
|
local room = self.room
|
|
|
|
local player = self.data[1]
|
|
|
|
|
|
|
|
for _, p in ipairs(room.players) do
|
|
|
|
p:setCardUseHistory("", 0, Player.HistoryPhase)
|
|
|
|
p:setSkillUseHistory("", 0, Player.HistoryPhase)
|
|
|
|
for name, _ in pairs(p.mark) do
|
|
|
|
if name:endsWith("-phase") then
|
|
|
|
room:setPlayerMark(p, name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-11 15:16:46 +00:00
|
|
|
for cid, cmark in pairs(room.card_marks) do
|
|
|
|
for name, _ in pairs(cmark) do
|
|
|
|
if name:endsWith("-phase") then
|
|
|
|
room:setCardMark(Fk:getCardById(cid), name, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-22 07:52:26 +00:00
|
|
|
if self.interrupted then
|
|
|
|
room.logic:trigger(fk.EventPhaseEnd, player, nil, true)
|
|
|
|
end
|
2023-03-07 06:55:28 +00:00
|
|
|
end
|