From 4f5e789dc34ca90f83f17c026209f3936f41de83 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 1 Aug 2024 23:14:47 +0200 Subject: [PATCH] feat: initialise fichiers et commandes --- classes/beastfile.lua | 14 +++++ classes/datalist.lua | 51 ++++++++++++++++ classes/folderloader.lua | 17 +++++- libs/commands.lua | 45 ++++++++++++++ libs/init.lua | 3 +- libs/split.lua | 127 +++++++++++++++++++++++++++++++++++++++ struct.lua | 23 +++++++ 7 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 classes/beastfile.lua create mode 100644 classes/datalist.lua create mode 100644 libs/commands.lua create mode 100644 libs/split.lua create mode 100644 struct.lua diff --git a/classes/beastfile.lua b/classes/beastfile.lua new file mode 100644 index 0000000..b64663b --- /dev/null +++ b/classes/beastfile.lua @@ -0,0 +1,14 @@ +local BeastFile = Object:extend() +local DataList = require "classes.datalist" + +function BeastFile:new(folder, name) + self.filepath = folder .. "/" .. name + print("Loading " .. self.filepath) + self.datas = DataList() +end + +function BeastFile:prepareJson() + return self.datas:prepareJson() +end + +return BeastFile \ No newline at end of file diff --git a/classes/datalist.lua b/classes/datalist.lua new file mode 100644 index 0000000..dab8d74 --- /dev/null +++ b/classes/datalist.lua @@ -0,0 +1,51 @@ +local Data = Object:extend() +local DataList = Object:extend() + +function Data.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 + table.insert(utils.trim(v)) + end + end + local level = 0 + if (command[2] ~= nil) then + levelString = utils.trim(command[2]); + level = tonumber(levelString) or 0 + end + return Data(name, args, level) +end + +function Data:new(name, arguments, level) + self.name = name + self.arguments = arguments + self.level = level +end + +function DataList:new() + self.list = {} + for key, value in pairs(commands.getDefaults()) do + table.insert(self.list, Data(key, value, 0)) + end +end + +function DataList:addLine(line) + table.insert(self.list, Data.line(line)) +end + +function DataList:reduce() + --TODO +end + +function DataList:prepareJson() + return self.list +end + +return DataList \ No newline at end of file diff --git a/classes/folderloader.lua b/classes/folderloader.lua index 93cf3c7..5ac953f 100644 --- a/classes/folderloader.lua +++ b/classes/folderloader.lua @@ -1,4 +1,5 @@ local FolderLoader = Object:extend() +local BeastFile = require "classes.beastfile" function FolderLoader.getAllDatas(value) local folderLoader = FolderLoader(value) @@ -12,13 +13,27 @@ function FolderLoader:new(value) self.data.nom = value.nom self.data.list = {} + self.files = {} + for _, filename in ipairs(utils.scandir(self.folder)) do - print(filename) + local file = utils.split(filename, ".", true) + if (file[2] == "beast") then + table.insert(self.files, BeastFile(self.folder, filename)) + else + print("[WARNING] Unknown extension " .. file[2] .. " for " .. filename) + end + end +end + +function FolderLoader:prepareJson() + for _, file in ipairs(self.files) do + table.insert(self.data.list, file:prepareJson()) end end function FolderLoader:getDatas() + self:prepareJson() return self.data end diff --git a/libs/commands.lua b/libs/commands.lua new file mode 100644 index 0000000..294bb78 --- /dev/null +++ b/libs/commands.lua @@ -0,0 +1,45 @@ +local struct = require "struct" +local commands = {} +local defaults = {} + +local functions = {} + +print("Compilation des commandes") + +local function addCommands(command, parent, default) + commands[command] = parent + if (default ~= nil) then + defaults[command] = default + end +end + +local function addStatCommands(name, value) + addCommands(name .. ".base", name, value.default) + addCommands(name .. ".lvl", name, 0) + addCommands(name .. ".add", name, 0) + addCommands(name .. ".bonus", name, 0) +end + +local function addListCommands(name, value) + addCommands(name, name) + addCommands(name .. ".replace", name) + addCommands(name .. ".reset", name) +end + +for key, value in pairs(struct) do + if (value.dataType == "stat") then + addStatCommands(key, value) + elseif (value.dataType == "list") then + addListCommands(key, value) + elseif (value.dataType == "comp") then + addListCommands(key, value) + else + addCommands(key, key) + end +end + +function functions.getDefaults() + return defaults +end + +return functions \ No newline at end of file diff --git a/libs/init.lua b/libs/init.lua index 7ab700a..a32612e 100644 --- a/libs/init.lua +++ b/libs/init.lua @@ -1,2 +1,3 @@ utils = require "libs.utils" -Object = require "libs.classic" \ No newline at end of file +Object = require "libs.classic" +commands = require "libs.commands" \ No newline at end of file diff --git a/libs/split.lua b/libs/split.lua new file mode 100644 index 0000000..63552b8 --- /dev/null +++ b/libs/split.lua @@ -0,0 +1,127 @@ +------------------------------------------------------------------ +-- +-- Author: Alexey Melnichuk +-- +-- Copyright (C) 2016 Alexey Melnichuk +-- +-- Licensed according to the included 'LICENSE' document +-- +-- This file is part of lua-split library. +-- +------------------------------------------------------------------ + +--- +-- @usage +-- split = require "split" +-- lines = split(str, '\r?\n') +-- key, val = split.first(str, '=', true) +-- a,b,c,d = split.unpack(str, ':', true) + +local unpack = unpack or table.unpack + +local function is_match_empty(pat, plain) + return not not string.find('', pat, nil, plain) +end + +local function split(str, sep, plain) + local b, res = 0, {} + sep = sep or '%s+' + + assert(type(sep) == 'string') + assert(type(str) == 'string') + + if #sep == 0 then + for i = 1, #str do + res[#res + 1] = string.sub(str, i, i) + end + return res + end + + assert(not is_match_empty(sep, plain), 'delimiter can not match empty string') + + while b <= #str do + local e, e2 = string.find(str, sep, b, plain) + if e then + res[#res + 1] = string.sub(str, b, e-1) + b = e2 + 1 + if b > #str then res[#res + 1] = "" end + else + res[#res + 1] = string.sub(str, b) + break + end + end + return res +end + +local function split_iter(str, sep, plain) + sep = sep or '%s+' + + assert(type(sep) == 'string') + assert(type(str) == 'string') + + if #sep == 0 then + local i = 0 + return function() + i = i + 1 + if i > #str then return end + return (string.sub(str, i, i)) + end + end + + assert(not is_match_empty(sep, plain), 'delimiter can not match empty string') + + local b, eol = 0 + return function() + if b > #str then + if eol then + eol = nil + return "" + end + return + end + + local e, e2 = string.find(str, sep, b, plain) + if e then + local s = string.sub(str, b, e-1) + b = e2 + 1 + if b > #str then eol = true end + return s + end + + local s = string.sub(str, b) + b = #str + 1 + return s + end +end + +local function usplit(...) return unpack(split(...)) end + +local function split_first(str, sep, plain) + sep = sep or '%s+' + + assert(type(sep) == 'string') + assert(type(str) == 'string') + + if #sep == 0 then + return string.sub(str, 1, 1), string.sub(str, 2) + end + + assert(not is_match_empty(sep, plain), 'delimiter can not match empty string') + + local e, e2 = string.find(str, sep, nil, plain) + if e then + return string.sub(str, 1, e - 1), string.sub(str, e2 + 1) + end + return str +end + +return setmetatable({ + split = split; + unpack = usplit; + first = split_first; + each = split_iter; +},{ + __call = function(_, ...) + return split(...) + end +}) \ No newline at end of file diff --git a/struct.lua b/struct.lua new file mode 100644 index 0000000..2316878 --- /dev/null +++ b/struct.lua @@ -0,0 +1,23 @@ +return { + name={contentType = "string"}, + level={contentType = "number"}, + 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}, + int= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + sag= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + vol= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + cha= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + dis= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + rel= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + per= {dataType= "stat", modulo= 5, default= 50, max=255, min=10}, + pv= {dataType= "stat", modulo= 1, default= 12, max=9999999, min=1}, + pe= {dataType= "stat", modulo= 1, default= 12, max=9999999, min=1}, + eclat= {dataType= "stat", modulo= 1, default= 10, max=200, min=1}, + 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}, +} \ No newline at end of file