From 83f6cc6ef4f87296372e11c7ce0941ea6f99190e Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 2 Aug 2024 08:34:54 +0200 Subject: [PATCH] feat: add data holding --- classes/dataholders/listholder.lua | 39 ++++++++++++++++++ classes/dataholders/simpleholder.lua | 18 +++++++++ classes/datalist.lua | 60 ++++++++++++++++++++++++---- libs/commands.lua | 2 + struct.lua | 10 ++--- 5 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 classes/dataholders/listholder.lua create mode 100644 classes/dataholders/simpleholder.lua diff --git a/classes/dataholders/listholder.lua b/classes/dataholders/listholder.lua new file mode 100644 index 0000000..409c1d4 --- /dev/null +++ b/classes/dataholders/listholder.lua @@ -0,0 +1,39 @@ +local ListHolder = Object:extend() + +function ListHolder:new(key, datas) + self.key = key + self.datas = datas + self.list = {} +end + +function ListHolder:applyCommand(command, args) + if (command == "") then + self:add(args) + elseif (command == "reset") then + self:reset() + elseif (command == "replace") then + self:replace(args) + end +end + +function ListHolder:reset() + self.list = {} +end + +function ListHolder:add(datas) + table.insert(self.list, datas) +end + +function ListHolder:replace(datas) + for index, args in ipairs(self.list) do + if (args[1] == datas[1]) then + self.list[index] = args + end + end +end + +function ListHolder:reduce(list, level) + return self.list +end + +return ListHolder \ No newline at end of file diff --git a/classes/dataholders/simpleholder.lua b/classes/dataholders/simpleholder.lua new file mode 100644 index 0000000..f6a0418 --- /dev/null +++ b/classes/dataholders/simpleholder.lua @@ -0,0 +1,18 @@ +local SimpleHolder = Object:extend() + +function SimpleHolder:new(key, datas) + self.key = key + self.datas = datas + self.value = nil +end + +function SimpleHolder:applyCommand(command, args) + print(self.key, command, args) + self.value = args +end + +function SimpleHolder:reduce(list, level) + return self.value +end + +return SimpleHolder \ No newline at end of file diff --git a/classes/datalist.lua b/classes/datalist.lua index e0924b8..e78d5d5 100644 --- a/classes/datalist.lua +++ b/classes/datalist.lua @@ -1,7 +1,10 @@ -local Data = Object:extend() +local RawData = Object:extend() local DataList = Object:extend() -function Data.fromLine(line) +local ListHolder = require "classes.dataholders.listholder" +local SimpleHolder = require "classes.dataholders.simpleholder" + +function RawData.fromLine(line) line = utils.removeComment(line) if (#line == 0) then return nil @@ -20,33 +23,76 @@ function Data.fromLine(line) levelString = utils.trim(command[2]); level = tonumber(levelString) or 0 end - return Data(name, commands.clean(args, name), level) + return RawData(name, commands.clean(args, name), level) end -function Data:new(name, arguments, level) +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 == "comp" or value.dataType == "list") then + print("list", key, value) + return ListHolder(key, value) + end + print("simple", key, value.dataType) + return SimpleHolder(key, value) +end + function DataList:new() self.list = {} + self.holders = {} self.reducedList = {} for key, value in pairs(commands.getDefaults()) do - table.insert(self.list, Data(key, value, 0)) + 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, Data.fromLine(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 + 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 + --TODO end function DataList:prepareJson() - return self.list + self:reduce() + return self.reducedList end return DataList \ No newline at end of file diff --git a/libs/commands.lua b/libs/commands.lua index d1917eb..ff46503 100644 --- a/libs/commands.lua +++ b/libs/commands.lua @@ -57,4 +57,6 @@ function functions.clean(args, command) return args end +functions.structs = struct + return functions \ No newline at end of file diff --git a/struct.lua b/struct.lua index 27a0976..99b7ef0 100644 --- a/struct.lua +++ b/struct.lua @@ -1,6 +1,6 @@ return { - name={contentType = "string"}, - level={contentType = "number", default = 0}, + name={contentType = "string", preParse = true}, + level={contentType = "number", default = 0, preParse = true}, atk= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, con= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, hab= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, @@ -17,7 +17,7 @@ return { armurephy= {dataType= "stat", modulo= 1, default= 0, max=9999999, min=0}, armurepsy= {dataType= "stat", modulo= 1, default= 0, max=9999999, min=0}, armurespe= {dataType= "stat", modulo= 1, default= 0, max=9999999, min=0}, - armes= {datatype= "list", args=2}, - competences= {datatype= "comp"}, -- on va le gérer différemment comme du code lol sinon c'est trop relou - armes= {datatype= "list", args=1}, + armes= {dataType= "list", args=2}, + competence= {dataType= "comp"}, -- on va le gérer différemment comme du code lol sinon c'est trop relou + armes= {dataType= "list", args=1}, } \ No newline at end of file