2022-03-30 08:33:56 +00:00
|
|
|
---@class Engine : Object
|
2022-04-01 12:51:01 +00:00
|
|
|
---@field packages table<string, Package>
|
|
|
|
---@field skills table<string, Skill>
|
|
|
|
---@field related_skills table<string, Skill[]>
|
|
|
|
---@field global_trigger TriggerSkill[]
|
|
|
|
---@field generals table<string, General>
|
|
|
|
---@field lords string[]
|
|
|
|
---@field cards Card[]
|
|
|
|
---@field translations table<string, string>
|
2022-03-28 14:24:30 +00:00
|
|
|
local Engine = class("Engine")
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:initialize()
|
|
|
|
-- Engine should be singleton
|
|
|
|
if Fk ~= nil then
|
|
|
|
error("Engine has been initialized")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Fk = self
|
|
|
|
|
|
|
|
self.packages = {} -- name --> Package
|
|
|
|
self.skills = {} -- name --> Skill
|
2022-04-01 12:51:01 +00:00
|
|
|
self.related_skills = {} -- skillName --> relatedSkill[]
|
|
|
|
self.global_trigger = {}
|
2022-03-28 14:24:30 +00:00
|
|
|
self.generals = {} -- name --> General
|
|
|
|
self.lords = {} -- lordName[]
|
|
|
|
self.cards = {} -- Card[]
|
|
|
|
self.translations = {} -- srcText --> translated
|
|
|
|
|
|
|
|
self:loadPackages()
|
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param pack Package
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:loadPackage(pack)
|
|
|
|
assert(pack:isInstanceOf(Package))
|
|
|
|
if self.packages[pack.name] ~= nil then
|
|
|
|
error(string.format("Duplicate package %s detected", pack.name))
|
|
|
|
end
|
|
|
|
self.packages[pack.name] = pack
|
|
|
|
|
|
|
|
-- add cards, generals and skills to Engine
|
|
|
|
if pack.type == Package.CardPack then
|
|
|
|
self:addCards(pack.cards)
|
|
|
|
elseif pack.type == Package.GeneralPack then
|
|
|
|
self:addGenerals(pack.generals)
|
|
|
|
end
|
|
|
|
self:addSkills(pack:getSkills())
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:loadPackages()
|
2022-03-31 05:29:23 +00:00
|
|
|
for _, dir in ipairs(FileIO.ls("packages")) do
|
|
|
|
if FileIO.isDir("packages/" .. dir) then
|
|
|
|
self:loadPackage(require(string.format("packages.%s", dir)))
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param t table
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:loadTranslationTable(t)
|
|
|
|
assert(type(t) == "table")
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
self.translations[k] = v
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@param skill Skill
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addSkill(skill)
|
|
|
|
assert(skill.class:isSubclassOf(Skill))
|
|
|
|
if self.skills[skill.name] ~= nil then
|
|
|
|
error(string.format("Duplicate skill %s detected", skill.name))
|
|
|
|
end
|
|
|
|
self.skills[skill.name] = skill
|
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@param skills Skill[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addSkills(skills)
|
|
|
|
assert(type(skills) == "table")
|
|
|
|
for _, skill in ipairs(skills) do
|
|
|
|
self:addSkill(skill)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param general General
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addGeneral(general)
|
|
|
|
assert(general:isInstanceOf(General))
|
|
|
|
if self.generals[general.name] ~= nil then
|
|
|
|
error(string.format("Duplicate general %s detected", general.name))
|
|
|
|
end
|
|
|
|
self.generals[general.name] = general
|
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param generals General[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addGenerals(generals)
|
|
|
|
assert(type(generals) == "table")
|
|
|
|
for _, general in ipairs(generals) do
|
|
|
|
self:addGeneral(general)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
local cardId = 1
|
|
|
|
---@param card Card
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addCard(card)
|
|
|
|
assert(card.class:isSubclassOf(Card))
|
2022-03-31 05:29:23 +00:00
|
|
|
card.id = cardId
|
|
|
|
cardId = cardId + 1
|
2022-03-28 14:24:30 +00:00
|
|
|
table.insert(self.cards, card)
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param cards Card[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addCards(cards)
|
|
|
|
for _, card in ipairs(cards) do
|
|
|
|
self:addCard(card)
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@param num integer
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param generalPool General[]
|
|
|
|
---@param except string[]
|
|
|
|
---@param filter function
|
2022-04-02 13:39:44 +00:00
|
|
|
---@return General[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:getGeneralsRandomly(num, generalPool, except, filter)
|
2022-03-25 04:28:07 +00:00
|
|
|
if filter then
|
|
|
|
assert(type(filter) == "function")
|
|
|
|
end
|
|
|
|
|
|
|
|
generalPool = generalPool or self.generals
|
|
|
|
except = except or {}
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-03-25 04:28:07 +00:00
|
|
|
local availableGenerals = {}
|
2022-03-30 06:14:40 +00:00
|
|
|
for _, general in pairs(generalPool) do
|
|
|
|
if not table.contains(except, general.name) and not (filter and filter(general)) then
|
2022-03-25 04:28:07 +00:00
|
|
|
table.insert(availableGenerals, general)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #availableGenerals == 0 then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = {}
|
2022-03-30 06:14:40 +00:00
|
|
|
for i = 1, num do
|
2022-03-25 04:28:07 +00:00
|
|
|
local randomGeneral = math.random(1, #availableGenerals)
|
2022-03-30 06:14:40 +00:00
|
|
|
table.insert(result, availableGenerals[randomGeneral])
|
2022-03-25 04:28:07 +00:00
|
|
|
table.remove(availableGenerals, randomGeneral)
|
|
|
|
|
|
|
|
if #availableGenerals == 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param except General[]
|
|
|
|
---@return General[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:getAllGenerals(except)
|
2022-03-25 04:28:07 +00:00
|
|
|
local result = {}
|
|
|
|
for _, general in ipairs(self.generals) do
|
|
|
|
if not (except and table.contains(except, general)) then
|
|
|
|
table.insert(result, general)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
return Engine
|