2023-04-09 05:35:35 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@class GameLogic: Object
|
2023-03-26 09:32:45 +00:00
|
|
|
---@field public room Room
|
|
|
|
---@field public skill_table table<Event, TriggerSkill[]>
|
|
|
|
---@field public refresh_skill_table table<Event, TriggerSkill[]>
|
|
|
|
---@field public skills string[]
|
|
|
|
---@field public event_stack Stack
|
|
|
|
---@field public game_event_stack Stack
|
|
|
|
---@field public role_table string[][]
|
2022-03-28 14:24:30 +00:00
|
|
|
local GameLogic = class("GameLogic")
|
|
|
|
|
|
|
|
function GameLogic:initialize(room)
|
2022-04-30 07:27:56 +00:00
|
|
|
self.room = room
|
|
|
|
self.skill_table = {} -- TriggerEvent --> TriggerSkill[]
|
|
|
|
self.refresh_skill_table = {}
|
|
|
|
self.skills = {} -- skillName[]
|
|
|
|
self.event_stack = Stack:new()
|
2023-02-28 17:43:44 +00:00
|
|
|
self.game_event_stack = Stack:new()
|
2022-04-30 07:27:56 +00:00
|
|
|
|
|
|
|
self.role_table = {
|
|
|
|
{ "lord" },
|
|
|
|
{ "lord", "rebel" },
|
|
|
|
{ "lord", "rebel", "renegade" },
|
|
|
|
{ "lord", "loyalist", "rebel", "renegade" },
|
|
|
|
{ "lord", "loyalist", "rebel", "rebel", "renegade" },
|
|
|
|
{ "lord", "loyalist", "rebel", "rebel", "rebel", "renegade" },
|
|
|
|
{ "lord", "loyalist", "loyalist", "rebel", "rebel", "rebel", "renegade" },
|
|
|
|
{ "lord", "loyalist", "loyalist", "rebel", "rebel", "rebel", "rebel", "renegade" },
|
|
|
|
}
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function GameLogic:run()
|
2022-04-30 07:27:56 +00:00
|
|
|
-- default logic
|
|
|
|
table.shuffle(self.room.players)
|
|
|
|
self:assignRoles()
|
|
|
|
self.room:adjustSeats()
|
|
|
|
|
|
|
|
self:chooseGenerals()
|
|
|
|
self:prepareForStart()
|
2023-02-15 16:54:39 +00:00
|
|
|
self.room.game_started = true
|
2022-04-30 07:27:56 +00:00
|
|
|
self:action()
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2023-03-07 06:55:28 +00:00
|
|
|
local function execGameEvent(type, ...)
|
|
|
|
local event = GameEvent:new(type, ...)
|
|
|
|
local _, ret = event:exec()
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function GameLogic:assignRoles()
|
2022-04-30 07:27:56 +00:00
|
|
|
local room = self.room
|
|
|
|
local n = #room.players
|
|
|
|
local roles = self.role_table[n]
|
|
|
|
table.shuffle(roles)
|
|
|
|
|
|
|
|
for i = 1, n do
|
|
|
|
local p = room.players[i]
|
|
|
|
p.role = roles[i]
|
|
|
|
if p.role == "lord" then
|
2023-01-17 14:34:15 +00:00
|
|
|
p.role_shown = true
|
2022-04-30 07:27:56 +00:00
|
|
|
room:broadcastProperty(p, "role")
|
|
|
|
else
|
|
|
|
room:notifyProperty(p, p, "role")
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function GameLogic:chooseGenerals()
|
2022-04-30 07:27:56 +00:00
|
|
|
local room = self.room
|
2023-03-20 06:53:56 +00:00
|
|
|
local generalNum = room.settings.generalNum
|
2022-04-30 07:27:56 +00:00
|
|
|
local lord = room:getLord()
|
|
|
|
local lord_general = nil
|
|
|
|
if lord ~= nil then
|
|
|
|
room.current = lord
|
2023-03-20 06:53:56 +00:00
|
|
|
local generals = Fk:getGeneralsRandomly(generalNum)
|
2022-04-30 07:27:56 +00:00
|
|
|
for i = 1, #generals do
|
|
|
|
generals[i] = generals[i].name
|
2022-03-30 06:14:40 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
lord_general = room:askForGeneral(lord, generals)
|
2023-04-04 18:21:59 +00:00
|
|
|
room:setPlayerGeneral(lord, lord_general, true)
|
2022-04-30 07:27:56 +00:00
|
|
|
room:broadcastProperty(lord, "general")
|
|
|
|
end
|
|
|
|
|
2022-12-20 04:51:54 +00:00
|
|
|
local nonlord = room:getOtherPlayers(lord, true)
|
2023-03-20 06:53:56 +00:00
|
|
|
local generals = Fk:getGeneralsRandomly(#nonlord * generalNum, nil, {lord_general})
|
2022-04-30 07:27:56 +00:00
|
|
|
table.shuffle(generals)
|
|
|
|
for _, p in ipairs(nonlord) do
|
2023-03-20 06:53:56 +00:00
|
|
|
local arg = {}
|
|
|
|
for i = 1, generalNum do
|
|
|
|
table.insert(arg, table.remove(generals, 1).name)
|
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
p.request_data = json.encode(arg)
|
|
|
|
p.default_reply = arg[1]
|
|
|
|
end
|
|
|
|
|
2023-03-14 12:48:08 +00:00
|
|
|
room:notifyMoveFocus(nonlord, "AskForGeneral")
|
2022-04-30 07:27:56 +00:00
|
|
|
room:doBroadcastRequest("AskForGeneral", nonlord)
|
|
|
|
for _, p in ipairs(nonlord) do
|
|
|
|
if p.general == "" and p.reply_ready then
|
|
|
|
local general = json.decode(p.client_reply)[1]
|
2023-04-04 18:21:59 +00:00
|
|
|
room:setPlayerGeneral(p, general, true)
|
2022-04-30 07:27:56 +00:00
|
|
|
else
|
2023-04-04 18:21:59 +00:00
|
|
|
room:setPlayerGeneral(p, p.default_reply, true)
|
2022-03-30 06:14:40 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
p.default_reply = ""
|
|
|
|
end
|
2022-03-30 06:14:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function GameLogic:prepareForStart()
|
2022-04-30 07:27:56 +00:00
|
|
|
local room = self.room
|
|
|
|
local players = room.players
|
|
|
|
room.alive_players = {table.unpack(players)}
|
|
|
|
for i = 1, #players - 1 do
|
|
|
|
players[i].next = players[i + 1]
|
|
|
|
end
|
|
|
|
players[#players].next = players[1]
|
|
|
|
|
|
|
|
for _, p in ipairs(players) do
|
|
|
|
assert(p.general ~= "")
|
|
|
|
local general = Fk.generals[p.general]
|
|
|
|
p.maxHp = general.maxHp
|
|
|
|
p.hp = general.hp
|
2023-04-13 12:17:39 +00:00
|
|
|
p.shield = general.shield
|
2022-04-30 07:27:56 +00:00
|
|
|
-- TODO: setup AI here
|
|
|
|
|
|
|
|
if p.role ~= "lord" then
|
|
|
|
room:broadcastProperty(p, "general")
|
|
|
|
elseif #players >= 5 then
|
|
|
|
p.maxHp = p.maxHp + 1
|
|
|
|
p.hp = p.hp + 1
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
room:broadcastProperty(p, "maxHp")
|
|
|
|
room:broadcastProperty(p, "hp")
|
2023-04-13 12:17:39 +00:00
|
|
|
room:broadcastProperty(p, "shield")
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local allCardIds = Fk:getAllCardIds()
|
|
|
|
table.shuffle(allCardIds)
|
|
|
|
room.draw_pile = allCardIds
|
|
|
|
for _, id in ipairs(room.draw_pile) do
|
2022-09-14 05:01:10 +00:00
|
|
|
self.room:setCardArea(id, Card.DrawPile, nil)
|
|
|
|
end
|
|
|
|
|
2023-04-15 04:06:24 +00:00
|
|
|
local addRoleModSkills = function(player, skillName)
|
|
|
|
local skill = Fk.skills[skillName]
|
|
|
|
if skill.lordSkill and (player.role ~= "lord" or #room.players < 5) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
room:handleAddLoseSkills(player, skillName, nil, false)
|
|
|
|
end
|
2022-09-14 05:01:10 +00:00
|
|
|
for _, p in ipairs(room.alive_players) do
|
2023-01-21 16:49:11 +00:00
|
|
|
local skills = Fk.generals[p.general].skills
|
|
|
|
for _, s in ipairs(skills) do
|
2023-04-15 04:06:24 +00:00
|
|
|
addRoleModSkills(p, s.name)
|
2023-01-21 16:49:11 +00:00
|
|
|
end
|
2023-02-15 11:54:35 +00:00
|
|
|
for _, sname in ipairs(Fk.generals[p.general].other_skills) do
|
2023-04-15 04:06:24 +00:00
|
|
|
addRoleModSkills(p, sname)
|
2023-02-15 11:54:35 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self:addTriggerSkill(GameRule)
|
|
|
|
for _, trig in ipairs(Fk.global_trigger) do
|
|
|
|
self:addTriggerSkill(trig)
|
|
|
|
end
|
2022-12-18 04:52:52 +00:00
|
|
|
|
|
|
|
self.room:sendLog{ type = "$GameStart" }
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 06:14:40 +00:00
|
|
|
function GameLogic:action()
|
2022-04-30 07:27:56 +00:00
|
|
|
self:trigger(fk.GameStart)
|
|
|
|
local room = self.room
|
|
|
|
|
2023-03-07 06:55:28 +00:00
|
|
|
execGameEvent(GameEvent.DrawInitial)
|
2022-04-30 07:27:56 +00:00
|
|
|
|
|
|
|
while true do
|
2023-04-08 12:45:55 +00:00
|
|
|
execGameEvent(GameEvent.Round)
|
2022-04-30 07:27:56 +00:00
|
|
|
if room.game_finished then break end
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param skill TriggerSkill
|
|
|
|
function GameLogic:addTriggerSkill(skill)
|
2022-04-30 07:27:56 +00:00
|
|
|
if skill == nil or table.contains(self.skills, skill.name) then
|
|
|
|
return
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
table.insert(self.skills, skill.name)
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
for _, event in ipairs(skill.refresh_events) do
|
|
|
|
if self.refresh_skill_table[event] == nil then
|
|
|
|
self.refresh_skill_table[event] = {}
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
table.insert(self.refresh_skill_table[event], skill)
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
for _, event in ipairs(skill.events) do
|
|
|
|
if self.skill_table[event] == nil then
|
|
|
|
self.skill_table[event] = {}
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
table.insert(self.skill_table[event], skill)
|
|
|
|
end
|
|
|
|
|
|
|
|
if skill.visible then
|
|
|
|
if (Fk.related_skills[skill.name] == nil) then return end
|
|
|
|
for _, s in ipairs(Fk.related_skills[skill.name]) do
|
|
|
|
if (s.class == TriggerSkill) then
|
|
|
|
self:addTriggerSkill(s)
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param event Event
|
|
|
|
---@param target ServerPlayer
|
|
|
|
---@param data any
|
|
|
|
function GameLogic:trigger(event, target, data)
|
2022-04-30 07:27:56 +00:00
|
|
|
local room = self.room
|
|
|
|
local broken = false
|
|
|
|
local skills = self.skill_table[event] or {}
|
|
|
|
local skills_to_refresh = self.refresh_skill_table[event] or {}
|
2023-02-15 11:54:35 +00:00
|
|
|
local _target = target or room.current -- for iteration
|
|
|
|
local player = _target
|
2022-04-30 07:27:56 +00:00
|
|
|
|
|
|
|
self.event_stack:push({event, target, data})
|
|
|
|
|
|
|
|
repeat do
|
|
|
|
-- refresh skills. This should not be broken
|
|
|
|
for _, skill in ipairs(skills_to_refresh) do
|
|
|
|
if skill:canRefresh(event, target, player, data) then
|
|
|
|
skill:refresh(event, target, player, data)
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
player = player.next
|
2023-02-15 11:54:35 +00:00
|
|
|
end until player == _target
|
2022-04-30 07:27:56 +00:00
|
|
|
|
|
|
|
---@param a TriggerSkill
|
|
|
|
---@param b TriggerSkill
|
|
|
|
local compare_func = function (a, b)
|
|
|
|
return a.priority_table[event] > b.priority_table[event]
|
|
|
|
end
|
|
|
|
table.sort(skills, compare_func)
|
|
|
|
|
|
|
|
repeat do
|
|
|
|
local triggerable_skills = {} ---@type table<number, TriggerSkill[]>
|
|
|
|
local priority_table = {} ---@type number[]
|
|
|
|
for _, skill in ipairs(skills) do
|
|
|
|
if skill:triggerable(event, target, player, data) then
|
|
|
|
local priority = skill.priority_table[event]
|
|
|
|
if triggerable_skills[priority] == nil then
|
|
|
|
triggerable_skills[priority] = {}
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
table.insert(triggerable_skills[priority], skill)
|
|
|
|
if not table.contains(priority_table, priority) then
|
|
|
|
table.insert(priority_table, priority)
|
2022-04-01 12:51:01 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
for _, priority in ipairs(priority_table) do
|
|
|
|
local triggerables = triggerable_skills[priority]
|
|
|
|
local skill_names = {} ---@type string[]
|
|
|
|
for _, skill in ipairs(triggerables) do
|
|
|
|
table.insert(skill_names, skill.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
while #skill_names > 0 do
|
2023-01-29 10:11:41 +00:00
|
|
|
local skill_name = room:askForChoice(player, skill_names, "trigger", "#choose-trigger")
|
2022-04-30 07:27:56 +00:00
|
|
|
local skill = triggerables[table.indexOf(skill_names, skill_name)]
|
|
|
|
broken = skill:trigger(event, target, player, data)
|
2023-04-04 08:25:37 +00:00
|
|
|
broken = broken or (event == fk.AskForPeaches and room:getPlayerById(data.who).hp > 0)
|
2022-04-01 12:51:01 +00:00
|
|
|
if broken then break end
|
2022-04-30 07:27:56 +00:00
|
|
|
table.removeOne(skill_names, skill_name)
|
|
|
|
table.removeOne(triggerables, skill)
|
|
|
|
end
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if broken then break end
|
2022-03-28 14:24:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
player = player.next
|
2023-02-15 11:54:35 +00:00
|
|
|
end until player == _target
|
2022-04-30 07:27:56 +00:00
|
|
|
|
|
|
|
self.event_stack:pop()
|
|
|
|
return broken
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2023-04-09 03:44:19 +00:00
|
|
|
---@return GameEvent
|
2023-03-04 17:28:59 +00:00
|
|
|
function GameLogic:getCurrentEvent()
|
|
|
|
return self.game_event_stack.t[self.game_event_stack.p]
|
|
|
|
end
|
|
|
|
|
2023-04-09 03:44:19 +00:00
|
|
|
function GameLogic:dumpEventStack(detailed)
|
|
|
|
local top = self:getCurrentEvent()
|
|
|
|
local i = self.game_event_stack.p
|
|
|
|
local inspect = p
|
|
|
|
if not top then return end
|
|
|
|
|
|
|
|
print("===== Start of event stack dump =====")
|
|
|
|
if not detailed then print("") end
|
|
|
|
|
|
|
|
repeat
|
|
|
|
local printable_data
|
|
|
|
if type(top.data) ~= "table" then
|
|
|
|
printable_data = top.data
|
|
|
|
else
|
|
|
|
printable_data = table.cloneWithoutClass(top.data)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not detailed then
|
|
|
|
print("Stack level #" .. i .. ": " .. GameEvent:translate(top.event))
|
|
|
|
else
|
|
|
|
print("\nStack level #" .. i .. ":")
|
|
|
|
inspect{
|
|
|
|
eventId = GameEvent:translate(top.event),
|
|
|
|
data = printable_data or "nil",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
top = top.parent
|
|
|
|
i = i - 1
|
|
|
|
until not top
|
|
|
|
|
|
|
|
print("\n===== End of event stack dump =====")
|
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
function GameLogic:breakEvent(ret)
|
2023-03-04 17:28:59 +00:00
|
|
|
coroutine.yield("__breakEvent", ret)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
return GameLogic
|