feat: initialise fichiers et commandes

This commit is contained in:
Kazhnuz 2024-08-01 23:14:47 +02:00
parent 03f4145270
commit 4f5e789dc3
7 changed files with 278 additions and 2 deletions

14
classes/beastfile.lua Normal file
View file

@ -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

51
classes/datalist.lua Normal file
View file

@ -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

View file

@ -1,4 +1,5 @@
local FolderLoader = Object:extend() local FolderLoader = Object:extend()
local BeastFile = require "classes.beastfile"
function FolderLoader.getAllDatas(value) function FolderLoader.getAllDatas(value)
local folderLoader = FolderLoader(value) local folderLoader = FolderLoader(value)
@ -12,13 +13,27 @@ function FolderLoader:new(value)
self.data.nom = value.nom self.data.nom = value.nom
self.data.list = {} self.data.list = {}
self.files = {}
for _, filename in ipairs(utils.scandir(self.folder)) do 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
end end
function FolderLoader:getDatas() function FolderLoader:getDatas()
self:prepareJson()
return self.data return self.data
end end

45
libs/commands.lua Normal file
View file

@ -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

View file

@ -1,2 +1,3 @@
utils = require "libs.utils" utils = require "libs.utils"
Object = require "libs.classic" Object = require "libs.classic"
commands = require "libs.commands"

127
libs/split.lua Normal file
View file

@ -0,0 +1,127 @@
------------------------------------------------------------------
--
-- Author: Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
-- Copyright (C) 2016 Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
-- 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
})

23
struct.lua Normal file
View file

@ -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},
}