bdd-creature/classes/datalist.lua

169 lines
4.7 KiB
Lua
Raw Permalink Normal View History

2024-08-02 08:34:54 +02:00
local RawData = Object:extend()
2024-08-01 23:14:47 +02:00
local DataList = Object:extend()
2024-08-02 08:34:54 +02:00
local ListHolder = require "classes.dataholders.listholder"
local SimpleHolder = require "classes.dataholders.simpleholder"
2024-08-02 08:39:28 +02:00
local StatHolder = require "classes.dataholders.statholder"
2024-08-02 09:06:50 +02:00
local CompetenceHolder = require "classes.dataholders.competenceholder"
local ArmesHolder = require "classes.dataholders.armesholder"
local ArmureHolder = require "classes.dataholders.armureholder"
2024-08-02 08:34:54 +02:00
2024-08-13 19:50:20 +02:00
local BoostHolder = require "classes.dataholders.boostholder"
2024-08-02 08:34:54 +02:00
function RawData.fromLine(line)
2024-08-01 23:14:47 +02:00
line = utils.removeComment(line)
if (#line == 0) then
return nil
end
local command = utils.split(line, "|")
local datas = utils.split(utils.trim(command[1]), ";")
local args = {}
name = utils.trim(datas[1])
for i, v in ipairs(datas) do
if (i > 1) then
2024-08-02 08:53:17 +02:00
local str = utils.trim(v)
if ((str ~= nil and str ~= "") or i < #datas) then
table.insert(args, str)
end
2024-08-01 23:14:47 +02:00
end
end
local level = 0
if (command[2] ~= nil) then
levelString = utils.trim(command[2]);
level = tonumber(levelString) or 0
end
2024-08-02 08:34:54 +02:00
return RawData(name, commands.clean(args, name), level)
2024-08-01 23:14:47 +02:00
end
2024-08-02 08:34:54 +02:00
function RawData:new(name, arguments, level)
2024-08-01 23:14:47 +02:00
self.name = name
self.arguments = arguments
self.level = level
end
2024-08-02 08:34:54 +02:00
function RawData:canBeUsed(level)
return level >= self.level
end
function RawData:getKey()
return utils.split(self.name, ".", true)[1]
end
function RawData:getCommand()
return utils.split(self.name, ".", true)[2] or ""
end
function DataList.getHolder(key, value)
2024-08-02 09:06:50 +02:00
if (value.dataType == "list") then
2024-08-02 08:34:54 +02:00
return ListHolder(key, value)
2024-08-02 09:06:50 +02:00
elseif (value.dataType == "comp") then
return CompetenceHolder(key, value)
2024-08-02 08:39:28 +02:00
elseif (value.dataType == "stat") then
return StatHolder(key, value)
elseif (value.dataType == "armure") then
return ArmureHolder(key, value)
elseif (value.dataType == "armes") then
return ArmesHolder(key, value)
2024-08-02 08:34:54 +02:00
end
return SimpleHolder(key, value)
end
2024-08-06 21:00:05 +02:00
function DataList:new(forceLevel)
2024-08-12 13:08:29 +02:00
self.forcedLevel = forceLevel
self.forcedName = nil
2024-08-01 23:14:47 +02:00
self.list = {}
2024-08-02 08:34:54 +02:00
self.holders = {}
2024-08-01 23:56:04 +02:00
self.reducedList = {}
2024-08-01 23:14:47 +02:00
for key, value in pairs(commands.getDefaults()) do
2024-08-02 08:34:54 +02:00
table.insert(self.list, RawData(key, value, 0))
end
for key, struct in pairs(commands.structs) do
self.holders[key] = DataList.getHolder(key, struct)
2024-08-01 23:14:47 +02:00
end
2024-08-13 19:50:20 +02:00
self.boosts = BoostHolder(self)
2024-08-01 23:14:47 +02:00
end
function DataList:addLine(line)
2024-08-02 08:34:54 +02:00
table.insert(self.list, RawData.fromLine(line))
2024-08-01 23:14:47 +02:00
end
2024-08-12 13:08:29 +02:00
function DataList:forceName(name)
self.forcedName = name
end
2024-08-13 19:50:20 +02:00
function DataList:addToHolder(key, command, arguments)
self.holders[key]:applyCommand(command, arguments)
end
2024-08-01 23:14:47 +02:00
function DataList:reduce()
2024-08-02 08:34:54 +02:00
local level = 0
local mode = "creature"
2024-08-02 08:34:54 +02:00
for _, rawdata in ipairs(self.list) do
if (rawdata.name == "level") then
2024-08-02 09:09:17 +02:00
level = rawdata.arguments
2024-08-02 08:34:54 +02:00
end
if (rawdata.name == "mode") then
mode = rawdata.arguments
end
2024-08-13 19:50:20 +02:00
if (rawdata.name == "boost") then
self.boosts:add(rawdata.arguments[1], rawdata.arguments[2])
end
2024-08-02 08:34:54 +02:00
end
2024-08-12 13:08:29 +02:00
if (self.forcedLevel ~= nil) then
level = self.forcedLevel
2024-08-06 21:00:05 +02:00
end
2024-08-02 08:34:54 +02:00
for _, rawdata in ipairs(self.list) do
2024-08-13 19:50:20 +02:00
if (rawdata:canBeUsed(level) and rawdata.name ~= "boost" and rawdata.name ~= "halfboost") then
self:addToHolder(rawdata:getKey(), rawdata:getCommand(), rawdata.arguments)
2024-08-02 08:34:54 +02:00
end
end
2024-08-13 19:50:20 +02:00
self.boosts:apply()
2024-08-02 08:34:54 +02:00
for key, holder in pairs(self.holders) do
self:addToReducedList(key, holder:reduce(level, mode))
2024-08-02 08:34:54 +02:00
end
2024-08-12 13:08:29 +02:00
if (self.forcedLevel ~= nil) then
self.reducedList["level"] = self.forcedLevel
end
if (self.forcedName ~= nil) then
self.reducedList["name"] = self.forcedName
2024-08-06 21:00:05 +02:00
end
2024-08-01 23:14:47 +02:00
end
2024-08-06 21:09:49 +02:00
function DataList:addToReducedList(key, data)
local to = commands.structs[key].to or key
local toSplited = utils.split(to, ".", true)
if (#toSplited == 1) then
self.reducedList[to] = data
else
local list = self.reducedList
for i, toPart in ipairs(toSplited) do
if (i == #toSplited) then
list[toPart] = data
else
if (list[toPart] == nil) then
list[toPart] = {}
end
list = list[toPart]
end
end
end
end
2024-08-02 11:43:34 +02:00
function DataList:prepareJson(simplercreatures, creatures, parent)
2024-08-02 08:34:54 +02:00
self:reduce()
2024-08-02 11:43:34 +02:00
self.reducedList.parent = parent
table.insert(simplercreatures, {nom = self.reducedList.name, level = self.reducedList.level, nomType = self.reducedList.nomType, categorie = self.reducedList.categorie})
table.insert(creatures, self.reducedList)
2024-08-01 23:14:47 +02:00
end
return DataList