From e816551eaba2847ef3fce3684ca19e00a88f45bc Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 2 Aug 2024 09:06:50 +0200 Subject: [PATCH] feat: ajout du holder de competence --- classes/dataholders/competenceholder.lua | 57 ++++++++++++++++++++++++ classes/dataholders/statholder.lua | 2 +- classes/datalist.lua | 5 ++- libs/commands.lua | 12 ++++- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 classes/dataholders/competenceholder.lua diff --git a/classes/dataholders/competenceholder.lua b/classes/dataholders/competenceholder.lua new file mode 100644 index 0000000..bb95ee7 --- /dev/null +++ b/classes/dataholders/competenceholder.lua @@ -0,0 +1,57 @@ +local CompetenceHolder = Object:extend() +local Competence = Object:extend() + +function Competence:new(name) + self.name = name; + self.base = 0; + self.lvl = 0; + self.bonus = 0; + self.add = 0; +end + +function Competence:reduce(level) + return { + name = self.name, + value = self.base + (self.lvl * level) + self.bonus + self.add + } +end + +function CompetenceHolder:new(key, datas) + self.key = key + self.datas = datas + self.list = {} +end + +function CompetenceHolder:applyCommand(command, args) + if (command == "reset") then + self.list = {} + return + end + local competence = self:getCompetence(args[1]) + if (command == "") then + competence.base = tonumber(args[2]) or 0 + elseif (command == "add") then + competence.add = tonumber(args[2]) or 0 + elseif (command == "bonus") then + competence.bonus = tonumber(args[2]) or 0 + elseif (command == "lvl") then + competence.lvl = tonumber(args[2]) or 0 + end +end + +function CompetenceHolder:getCompetence(name) + if self.list[name] == nil then + self.list[name] = Competence(name) + end + return self.list[name] +end + +function CompetenceHolder:reduce(level) + local list = {} + for key, value in pairs(self.list) do + table.insert(list, value:reduce(level)) + end + return list +end + +return CompetenceHolder \ No newline at end of file diff --git a/classes/dataholders/statholder.lua b/classes/dataholders/statholder.lua index 6c6e9d9..62ad3f0 100644 --- a/classes/dataholders/statholder.lua +++ b/classes/dataholders/statholder.lua @@ -11,7 +11,7 @@ function StatHolder:applyCommand(command, args) end function StatHolder:reduce(level) - return self.commands.base + ((self.commands.level or 0) * level) + (self.commands.add or 0) + (self.commands.bonus or 0) + return self.commands.base + ((self.commands.lvl or 0) * level) + (self.commands.add or 0) + (self.commands.bonus or 0) end return StatHolder \ No newline at end of file diff --git a/classes/datalist.lua b/classes/datalist.lua index bea511c..c372d58 100644 --- a/classes/datalist.lua +++ b/classes/datalist.lua @@ -4,6 +4,7 @@ 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) @@ -49,8 +50,10 @@ function RawData:getCommand() end function DataList.getHolder(key, value) - if (value.dataType == "comp" or value.dataType == "list") then + 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 diff --git a/libs/commands.lua b/libs/commands.lua index dea0408..90ec74d 100644 --- a/libs/commands.lua +++ b/libs/commands.lua @@ -20,6 +20,14 @@ local function addStatCommands(name, value) addCommands(name .. ".bonus", name, 0) end +local function addCompCommands(name, value) + addCommands(name, name) + addCommands(name .. ".reset", name) + addCommands(name .. ".lvl", name) + addCommands(name .. ".add", name) + addCommands(name .. ".bonus", name) +end + local function addListCommands(name, value) addCommands(name, name) addCommands(name .. ".replace", name) @@ -32,7 +40,7 @@ for key, value in pairs(struct) do elseif (value.dataType == "list") then addListCommands(key, value) elseif (value.dataType == "comp") then - addListCommands(key, value) + addCompCommands(key, value) else addCommands(key, key, value.default) end @@ -45,7 +53,7 @@ end function functions.clean(args, command) local baseCommand = commands[command] if (baseCommand == nil) then - error("Command " .. baseCommand .. " doesn't exists") + error("Command " .. command .. " doesn't exists") end local commandData = struct[baseCommand] if (commandData.args == nil or commandData.args == 1) then