将执行回合和摸初始牌作为了“事件”
同时为事件添加了自定义清理函数(未经测试
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 GameEvent.Judge = 14
dofile "lua/server/events/judge.lua" dofile "lua/server/events/judge.lua"
GameEvent.DrawInitial = 15
GameEvent.Turn = 16
dofile "lua/server/events/gameflow.lua"
-- TODO: fix this -- TODO: fix this
GameEvent.BreakEvent = 999 GameEvent.BreakEvent = 999

View File

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

View File

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

View File

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