From 8463b229281e3115d96c522f38f854b3fa4d951e Mon Sep 17 00:00:00 2001 From: camille lechauve Date: Mon, 4 Sep 2023 00:30:38 +0200 Subject: [PATCH] add: settings, parsing fix --- config.json.example | 18 ++- lua/autorun/server/sv_config.lua | 11 -- lua/autorun/server/sv_disease.lua | 14 +- lua/autorun/server/sv_settings.lua | 146 ++++++++++++++++++++ lua/autorun/server/sv_symbtom_list.lua | 9 ++ lua/autorun/server/sv_utils.lua | 1 - lua/autorun/server/symptoms/sv_cough.lua | 8 +- lua/autorun/server/symptoms/sv_symbtoms.lua | 3 - lua/autorun/server/symptoms/sv_vomit.lua | 8 +- lua/autorun/sh_config.lua | 2 +- 10 files changed, 180 insertions(+), 40 deletions(-) delete mode 100644 lua/autorun/server/sv_config.lua create mode 100644 lua/autorun/server/sv_settings.lua create mode 100644 lua/autorun/server/sv_symbtom_list.lua delete mode 100644 lua/autorun/server/symptoms/sv_symbtoms.lua diff --git a/config.json.example b/config.json.example index 17bb4e7..6d21a6c 100644 --- a/config.json.example +++ b/config.json.example @@ -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 ] } ] diff --git a/lua/autorun/server/sv_config.lua b/lua/autorun/server/sv_config.lua deleted file mode 100644 index bf4bc91..0000000 --- a/lua/autorun/server/sv_config.lua +++ /dev/null @@ -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 diff --git a/lua/autorun/server/sv_disease.lua b/lua/autorun/server/sv_disease.lua index 3e61bb4..06c5f95 100644 --- a/lua/autorun/server/sv_disease.lua +++ b/lua/autorun/server/sv_disease.lua @@ -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 diff --git a/lua/autorun/server/sv_settings.lua b/lua/autorun/server/sv_settings.lua new file mode 100644 index 0000000..751796d --- /dev/null +++ b/lua/autorun/server/sv_settings.lua @@ -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 \ No newline at end of file diff --git a/lua/autorun/server/sv_symbtom_list.lua b/lua/autorun/server/sv_symbtom_list.lua new file mode 100644 index 0000000..4df45b7 --- /dev/null +++ b/lua/autorun/server/sv_symbtom_list.lua @@ -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 \ No newline at end of file diff --git a/lua/autorun/server/sv_utils.lua b/lua/autorun/server/sv_utils.lua index 2542563..dfa4959 100644 --- a/lua/autorun/server/sv_utils.lua +++ b/lua/autorun/server/sv_utils.lua @@ -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) diff --git a/lua/autorun/server/symptoms/sv_cough.lua b/lua/autorun/server/symptoms/sv_cough.lua index 063379a..be075e8 100644 --- a/lua/autorun/server/symptoms/sv_cough.lua +++ b/lua/autorun/server/symptoms/sv_cough.lua @@ -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 \ No newline at end of file +end + +return cough \ No newline at end of file diff --git a/lua/autorun/server/symptoms/sv_symbtoms.lua b/lua/autorun/server/symptoms/sv_symbtoms.lua deleted file mode 100644 index 62bbbdc..0000000 --- a/lua/autorun/server/symptoms/sv_symbtoms.lua +++ /dev/null @@ -1,3 +0,0 @@ -symbtoms_list = {} - -return symbtoms_list \ No newline at end of file diff --git a/lua/autorun/server/symptoms/sv_vomit.lua b/lua/autorun/server/symptoms/sv_vomit.lua index 5c6c1d1..3880da3 100644 --- a/lua/autorun/server/symptoms/sv_vomit.lua +++ b/lua/autorun/server/symptoms/sv_vomit.lua @@ -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 \ No newline at end of file +end + +return vomit \ No newline at end of file diff --git a/lua/autorun/sh_config.lua b/lua/autorun/sh_config.lua index 3e0c621..a5d26df 100644 --- a/lua/autorun/sh_config.lua +++ b/lua/autorun/sh_config.lua @@ -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"