add: settings, parsing fix

This commit is contained in:
camille lechauve 2023-09-04 00:30:38 +02:00
parent 88b07a6194
commit 8463b22928
10 changed files with 180 additions and 40 deletions

View File

@ -7,29 +7,29 @@
{
"name": "burp",
"level": 1,
"delay": 150,
"delay": 150
},
{
"name": "fart",
"level": 1,
"delay": 250,
"delay": 250
}
],
[
{
"name": "burp",
"level": 1,
"delay": 50,
"delay": 50
},
{
"name": "fart",
"level": 1,
"delay": 150,
"delay": 150
},
{
"name": "vomit",
"level": 0.5,
"delay": 500,
"delay": 500
}
]
],
@ -43,9 +43,13 @@
"chance": 0.05
}
],
"stages_durations": [
"transmission_zone_stages": [
5,
10
],
"stages_duration": [
20000,
40000,
40000
]
}
]

View File

@ -1,11 +0,0 @@
utils = include("sv_utils.lua")
config = include("../sh_config.lua")
utils.path_create(config.config_path)
if not file.Exists(config.config_path, "DATA")
then
local default_config = {}
default_config.diseases = {}
file.Write(config.config_path, util.TableToJSON(default_config, true))
end

View File

@ -3,17 +3,17 @@ Disease = {}
--- Disease class
---@param side name string? the name of the disease (string)
---@param side symbtoms_by_stages list? a list of list of symbtom, a list of symbtoms by stage (Symbtom)
---@param side contaminations_methods list? a list of contamination method (string)
---@param side transmissions_zone_by_stage list? a list of transmissions zone (int), one number by stage
---@param side stages_durations list? a list of stage duration (int), one number by stage
function Disease:new(name, symbtoms_by_stages, contaminations_methods, transmissions_zone_by_stages, stages_durations)
---@param side contamination_methods list? a list of contamination method (string)
---@param side transmission_zone_stages list? a list of transmissions zone (int), one number by stage
---@param side stages_duration list? a list of stage duration (int), one number by stage
function Disease:new(name, symbtoms_by_stages, contamination_methods, transmission_zone_stages, stages_duration)
local instance = {}
setmetatable(instance, {__index = Disease})
instance.name = name
instance.symbtoms_by_stages = symbtoms_by_stages
instance.contaminations_methods = contaminations_methods
instance.transmissions_zone_by_stages = transmissions_zone_by_stages
instance.stages_durations = stages_durations
instance.contamination_methods = contamination_methods
instance.transmission_zone_stages = transmission_zone_stages
instance.stages_duration = stages_duration
return instance
end

View File

@ -0,0 +1,146 @@
utils = include("sv_utils.lua")
config = include("../sh_config.lua")
symbtoms_list = include("sv_symbtom_list.lua")
Symbtom = include("sv_symbtom.lua")
Disease = include("sv_disease.lua")
local settings = {}
utils.path_create(config.settings_path)
if not file.Exists(config.settings_path, "DATA")
then
local default_config = {}
default_config.diseases = {}
file.Write(config.settings_path, util.TableToJSON(default_config, true))
end
local data = file.Read(config.settings_path)
data = util.JSONToTable(data)
if data.diseases == nil
then
print(config.settings_path .. "file error")
return nil
end
settings.diseases = {}
for i, disease in ipairs(data.diseases)
do
local name = ""
local symbtoms_by_stages = {}
local contamination_methods = {}
local transmission_zone_stages = {}
local stages_duration = {}
if (disease.name == nil)
then
print("[SKIP] missing name in the " .. i .. " disease")
return nil
end
name = disease.name
if (disease.symbtoms_by_stages == nil)
then
print("[SKIP] missing symbtoms_by_stages in " .. name .." disease")
continue
end
table.insert(symbtoms_by_stages, {})
for y, stage in ipairs(disease.symbtoms_by_stages)
do
symbtoms_by_stages[y] = {}
for j, symbtom in ipairs(stage)
do
if symbtom.name == nil
then
print("[ERROR] missing name in the " .. j .. " symbtom of the " .. y .. " stage of the " .. name .. " disease")
return nil
end
if symbtoms_list[symbtom.name] == nil
then
print("[ERROR] " .. symbtom.name .. " unknown symbtom in the " .. j .. " symbtom of the " .. y .. " stage of the " .. name .. " disease")
return nil
end
if symbtom.level == nil
then
print("[ERROR] missing level in the " .. j .. " symbtom (" .. symbtom.name ..") of the " .. y .. " stage of the " .. name .. " disease")
return nil
end
if type(symbtom.level) != "number" or 0 > symbtom.level or symbtom.level > 1
then
print("[ERROR] invalid level in the " .. j .. " symbtom (" .. symbtom.name ..") of the " .. y .. " stage of the " .. name .. " disease")
return nil
end
if symbtom.delay == nil
then
print("[ERROR] missing delay in the " .. j .. " symbtom (" .. symbtom.name ..") of the " .. y .. " stage of the " .. name .. " disease")
return nil
end
if type(symbtom.delay) != "number"
then
print("[ERROR] invalid delay in the " .. j .. " symbtom (" .. symbtom.name ..") of the " .. y .. " stage of the " .. name .. " disease")
return nil
end
table.insert(symbtoms_by_stages[y], Symbtom:new(symbtom.name, symbtoms_list[symbtom.name], symbtom.level, symbtom.delay))
end
end
if disease.contaminations_method == nil
then
print("[SKIP] missing contamination_method in the " .. name .. " disease")
continue
end
for k, contamination_method in ipairs(disease.contaminations_method)
do
if contamination_method.name == nil
then
print("[SKIP] missing name in the " .. k .. " contamination_method of the " .. name .. " disease")
break
end
if contamination_method.chance == nil
then
print("[SKIP] missing chance in the " .. contamination_method.name .. " contaminations_method of the " .. name .. " disease")
break
end
end
contamination_methods = disease.contamination_methods
if disease.transmission_zone_stages == nil
then
print("[SKIP] missing transmission_zone_stages in the " .. name .. " disease")
continue
end
for k, transmission_zone_stage in ipairs(disease.transmission_zone_stages)
do
if type(transmission_zone_stage) != "number"
then
print("[SKIP] non number " .. k .. " transmission_zone_stages in the " .. name)
break
end
end
transmission_zone_stages = disease.transmission_zone_stages
if disease.stages_duration == nil
then
print("[SKIP] missing stages_duration in the " .. name .. " disease")
continue
end
for k, stage_duration in ipairs(disease.stages_duration)
do
if type(stage_duration) != "number"
then
print("[SKIP] non number " .. k .. " stages_duration in the " .. name)
break
end
end
stages_duration = disease.stages_duration
table.insert(settings.diseases, Disease:new(name, symbtoms_by_stages, contamination_methods, transmission_zone_stages, stages_durations))
end
print("[OK] settings")
return settings

View File

@ -0,0 +1,9 @@
cough = include("symptoms/sv_cough.lua")
vomit = include("symptoms/sv_vomit.lua")
symbtom_list = {}
symbtom_list["cough"] = cough
symbtom_list["vomit"] = vomit
return symbtom_list

View File

@ -15,7 +15,6 @@ function utils.path_create(path)
local tmp = "";
for i, dir in ipairs(dirs) do
tmp = tmp .. dir
print(tmp)
if not file.Exists(tmp, "DATA")
then
file.CreateDir(tmp)

View File

@ -1,11 +1,9 @@
symbtoms_list = include("sv_symbtoms.lua")
table.insert(symbtoms_list, "cough" = cough)
--- Send a packet to trigger cough animation to the player
---@param side UID64 string? UID64 of the player
---@param side level float? A float between 0 and 1
---@return void
function cough(UID64, level)
end
end
return cough

View File

@ -1,3 +0,0 @@
symbtoms_list = {}
return symbtoms_list

View File

@ -1,11 +1,9 @@
symbtoms_list = include("sv_symbtoms.lua")
table.insert(symbtoms_list, "vomit" = vomit)
--- Send a packet to trigger cough animation to the player
---@param side UID64 string? UID64 of the player
---@param side level float? A float between 0 and 1
---@return void
function vomit(UID64, level)
end
end
return vomit

View File

@ -1,7 +1,7 @@
config = {}
-- the config file path location
config.config_path = "disease/config.json"
config.settings_path = "disease/settings.json"
config.save_path = "disease/saves"