bdd-creature/classes/datalist.lua
2024-08-06 21:00:05 +02:00

116 lines
No EOL
3.2 KiB
Lua

local RawData = Object:extend()
local DataList = Object:extend()
local ListHolder = require "classes.dataholders.listholder"
local SimpleHolder = require "classes.dataholders.simpleholder"
local StatHolder = require "classes.dataholders.statholder"
local CompetenceHolder = require "classes.dataholders.competenceholder"
function RawData.fromLine(line)
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
local str = utils.trim(v)
if ((str ~= nil and str ~= "") or i < #datas) then
table.insert(args, str)
end
end
end
local level = 0
if (command[2] ~= nil) then
levelString = utils.trim(command[2]);
level = tonumber(levelString) or 0
end
return RawData(name, commands.clean(args, name), level)
end
function RawData:new(name, arguments, level)
self.name = name
self.arguments = arguments
self.level = level
end
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)
if (value.dataType == "list") then
return ListHolder(key, value)
elseif (value.dataType == "comp") then
return CompetenceHolder(key, value)
elseif (value.dataType == "stat") then
return StatHolder(key, value)
end
return SimpleHolder(key, value)
end
function DataList:new(forceLevel)
self.forceLevel = forceLevel
self.list = {}
self.holders = {}
self.reducedList = {}
for key, value in pairs(commands.getDefaults()) do
table.insert(self.list, RawData(key, value, 0))
end
for key, struct in pairs(commands.structs) do
self.holders[key] = DataList.getHolder(key, struct)
end
end
function DataList:addLine(line)
table.insert(self.list, RawData.fromLine(line))
end
function DataList:reduce()
local level = 0
for _, rawdata in ipairs(self.list) do
if (rawdata.name == "level") then
level = rawdata.arguments
end
end
if (self.forceLevel ~= nil) then
level = self.forceLevel
end
for _, rawdata in ipairs(self.list) do
if (rawdata:canBeUsed(level)) then
self.holders[rawdata:getKey()]:applyCommand(rawdata:getCommand(), rawdata.arguments)
end
end
for key, holder in pairs(self.holders) do
self.reducedList[key] = holder:reduce(level)
end
if (self.forceLevel ~= nil) then
self.reducedList["level"] = self.forceLevel
end
end
function DataList:prepareJson(simplercreatures, creatures, parent)
self:reduce()
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)
end
return DataList