bdd-creature/classes/datalist.lua

105 lines
2.7 KiB
Lua
Raw 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"
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)
2024-08-02 08:34:54 +02:00
end
return SimpleHolder(key, value)
end
2024-08-01 23:14:47 +02:00
function DataList:new()
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
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
function DataList:reduce()
2024-08-02 08:34:54 +02:00
local level = 0
for _, rawdata in ipairs(self.list) do
if (rawdata.name == "level") then
rawdata = rawdata.arguments
end
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
2024-08-01 23:14:47 +02:00
--TODO
end
function DataList:prepareJson()
2024-08-02 08:34:54 +02:00
self:reduce()
return self.reducedList
2024-08-01 23:14:47 +02:00
end
return DataList