feat: add data holding
This commit is contained in:
parent
09581b436d
commit
83f6cc6ef4
5 changed files with 117 additions and 12 deletions
39
classes/dataholders/listholder.lua
Normal file
39
classes/dataholders/listholder.lua
Normal file
|
@ -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
|
18
classes/dataholders/simpleholder.lua
Normal file
18
classes/dataholders/simpleholder.lua
Normal file
|
@ -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
|
|
@ -1,7 +1,10 @@
|
||||||
local Data = Object:extend()
|
local RawData = Object:extend()
|
||||||
local DataList = 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)
|
line = utils.removeComment(line)
|
||||||
if (#line == 0) then
|
if (#line == 0) then
|
||||||
return nil
|
return nil
|
||||||
|
@ -20,33 +23,76 @@ function Data.fromLine(line)
|
||||||
levelString = utils.trim(command[2]);
|
levelString = utils.trim(command[2]);
|
||||||
level = tonumber(levelString) or 0
|
level = tonumber(levelString) or 0
|
||||||
end
|
end
|
||||||
return Data(name, commands.clean(args, name), level)
|
return RawData(name, commands.clean(args, name), level)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Data:new(name, arguments, level)
|
function RawData:new(name, arguments, level)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
self.level = level
|
self.level = level
|
||||||
end
|
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()
|
function DataList:new()
|
||||||
self.list = {}
|
self.list = {}
|
||||||
|
self.holders = {}
|
||||||
self.reducedList = {}
|
self.reducedList = {}
|
||||||
for key, value in pairs(commands.getDefaults()) do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
function DataList:addLine(line)
|
function DataList:addLine(line)
|
||||||
table.insert(self.list, Data.fromLine(line))
|
table.insert(self.list, RawData.fromLine(line))
|
||||||
end
|
end
|
||||||
|
|
||||||
function DataList:reduce()
|
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
|
--TODO
|
||||||
end
|
end
|
||||||
|
|
||||||
function DataList:prepareJson()
|
function DataList:prepareJson()
|
||||||
return self.list
|
self:reduce()
|
||||||
|
return self.reducedList
|
||||||
end
|
end
|
||||||
|
|
||||||
return DataList
|
return DataList
|
|
@ -57,4 +57,6 @@ function functions.clean(args, command)
|
||||||
return args
|
return args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
functions.structs = struct
|
||||||
|
|
||||||
return functions
|
return functions
|
10
struct.lua
10
struct.lua
|
@ -1,6 +1,6 @@
|
||||||
return {
|
return {
|
||||||
name={contentType = "string"},
|
name={contentType = "string", preParse = true},
|
||||||
level={contentType = "number", default = 0},
|
level={contentType = "number", default = 0, preParse = true},
|
||||||
atk= {dataType= "stat", modulo= 5, default= 50, max=255, min=10},
|
atk= {dataType= "stat", modulo= 5, default= 50, max=255, min=10},
|
||||||
con= {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},
|
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},
|
armurephy= {dataType= "stat", modulo= 1, default= 0, max=9999999, min=0},
|
||||||
armurepsy= {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},
|
armurespe= {dataType= "stat", modulo= 1, default= 0, max=9999999, min=0},
|
||||||
armes= {datatype= "list", args=2},
|
armes= {dataType= "list", args=2},
|
||||||
competences= {datatype= "comp"}, -- on va le gérer différemment comme du code lol sinon c'est trop relou
|
competence= {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=1},
|
||||||
}
|
}
|
Loading…
Reference in a new issue