将执行回合和摸初始牌作为了“事件”
同时为事件添加了自定义清理函数(未经测试
This commit is contained in:
notify 2023-03-07 14:55:28 +08:00 committed by GitHub
parent 861d87849a
commit 5488469ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 7 deletions

View File

@ -0,0 +1,11 @@
GameEvent.functions[GameEvent.DrawInitial] = function(self)
local room = self.room
for _, p in ipairs(room.alive_players) do
room.logic:trigger(fk.DrawInitialCards, p, { num = 4 })
end
end
GameEvent.functions[GameEvent.Turn] = function(self)
local room = self.room
room.logic:trigger(fk.TurnStart, room.current)
end

View File

@ -26,6 +26,10 @@ dofile "lua/server/events/skill.lua"
GameEvent.Judge = 14
dofile "lua/server/events/judge.lua"
GameEvent.DrawInitial = 15
GameEvent.Turn = 16
dofile "lua/server/events/gameflow.lua"
-- TODO: fix this
GameEvent.BreakEvent = 999

View File

@ -49,6 +49,7 @@ GameEvent.cleaners[GameEvent.Judge] = function(self)
if self:getCardArea(data.card.id) == Card.Processing then
self:moveCardTo(data.card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile)
end
if not self.interrupted then return end
-- prohibit access to judge.card
setmetatable(data, {

View File

@ -2,6 +2,10 @@
---@field room Room
---@field event integer
---@field data any
---@field main_func fun(self: GameEvent)
---@field clear_func fun(self: GameEvent)
---@field extra_clear_funcs any[]
---@field interrupted boolean
local GameEvent = class("GameEvent")
GameEvent.functions = {}
@ -19,6 +23,15 @@ function GameEvent:initialize(event, ...)
self.data = { ... }
self.main_func = wrapCoFunc(GameEvent.functions[event], self) or dummyFunc
self.clear_func = wrapCoFunc(GameEvent.cleaners[event], self) or dummyFunc
self.extra_clear_funcs = {}
self.interrupted = false
end
function GameEvent:clear()
for _, f in ipairs(self.extra_clear_funcs) do
if type(f) == "function" then f(self) end
end
self:clear_func()
end
function GameEvent:exec()
@ -38,7 +51,8 @@ function GameEvent:exec()
fk.qCritical(yield_result)
print(debug.traceback(co))
end
self.clear_func()
self.interrupted = true
self:clear()
ret = true
break
end
@ -50,7 +64,8 @@ function GameEvent:exec()
elseif type(yield_result) == "table" and yield_result.class
and yield_result:isInstanceOf(GameEvent) then
-- yield to corresponding GameEvent, first pop self from stack
self.clear_func()
self.interrupted = true
self:clear()
logic.game_event_stack:pop(self)
-- then, call yield
@ -61,7 +76,8 @@ function GameEvent:exec()
local cancelEvent = GameEvent:new(GameEvent.BreakEvent, self)
local notcanceled = cancelEvent:exec()
if not notcanceled then
self.clear_func()
self.interrupted = true
self:clear()
ret = true
extra_ret = extra_yield_result
break
@ -69,6 +85,7 @@ function GameEvent:exec()
else
-- normally exit, simply break the loop
self:clear()
extra_ret = yield_result
break
end

View File

@ -40,6 +40,13 @@ function GameLogic:run()
self:action()
end
local function execGameEvent(type, ...)
local event = GameEvent:new(type, ...)
local _, ret = event:exec()
return ret
end
function GameLogic:assignRoles()
local room = self.room
local n = #room.players
@ -162,12 +169,10 @@ function GameLogic:action()
self:trigger(fk.GameStart)
local room = self.room
for _, p in ipairs(room.alive_players) do
self:trigger(fk.DrawInitialCards, p, { num = 4 })
end
execGameEvent(GameEvent.DrawInitial)
while true do
self:trigger(fk.TurnStart, room.current)
execGameEvent(GameEvent.Turn)
if room.game_finished then break end
room.current = room.current:getNextAlive()
end