From 4f255b2f43566624d4a777fd7a56d8979fa8f61d Mon Sep 17 00:00:00 2001 From: camille lechauve Date: Thu, 14 Sep 2023 19:23:54 +0200 Subject: [PATCH] core: rebuild infect function fix: a disease cannot be add in multiple time --- lua/autorun/server/menus/sv_infect_menu.lua | 16 ++++++-- lua/autorun/server/sv_disease.lua | 42 +++++++++++++++++++++ lua/autorun/server/sv_infect.lua | 20 ---------- lua/autorun/server/sv_init.lua | 1 + lua/autorun/server/sv_saves.lua | 1 + lua/autorun/server/sv_settings.lua | 12 ++++++ 6 files changed, 69 insertions(+), 23 deletions(-) delete mode 100644 lua/autorun/server/sv_infect.lua diff --git a/lua/autorun/server/menus/sv_infect_menu.lua b/lua/autorun/server/menus/sv_infect_menu.lua index ac4df4e..ecb08d5 100644 --- a/lua/autorun/server/menus/sv_infect_menu.lua +++ b/lua/autorun/server/menus/sv_infect_menu.lua @@ -1,6 +1,5 @@ settings = include("../sv_settings.lua") config = include("../sv_config.lua") -infect = include("../sv_infect.lua") util.AddNetworkString("get_diseases_name") util.AddNetworkString("get_players_data") @@ -43,8 +42,19 @@ net.Receive("infect", function (len, ply) local data = net.ReadTable() for _, infected in ipairs(data.infecteds) do - print("1") - infect(infected, data.diseases) + for _, disease_name in ipairs(data.diseases) + do + print(disease_name) + disease = settings.getDiseaseByName(disease_name) + if (disease == nil) + then + print(ply:GetName() .. " try to add undifined disease") + else + print() + disease:infect(infected, 1) + end + print("bozogang") + end end end) diff --git a/lua/autorun/server/sv_disease.lua b/lua/autorun/server/sv_disease.lua index 06c5f95..ab08f2f 100644 --- a/lua/autorun/server/sv_disease.lua +++ b/lua/autorun/server/sv_disease.lua @@ -1,3 +1,4 @@ +config = include("sv_config.lua") Disease = {} --- Disease class @@ -17,4 +18,45 @@ function Disease:new(name, symbtoms_by_stages, contamination_methods, transmissi return instance end +local function get_disease(diseases, to_find) + PrintTable(diseases) + for _, disease in ipairs(diseases) + do + print(disease.name .. " == " .. to_find) + if disease.name == to_find + then + return disease + end + end + return nil +end + +---@param side infected number? the steam id 64 of player infected +---@param side stage number? the the stage of the disease +function Disease:infect(infected, stage) + + if (stage == nil) + then + stage = 1 + end + + local file_path = config.save_path .. infected .. ".json" + + local disease = {} + disease.name = self.name + disease.stage = stage + disease.contamination_time = 0 + + local data = util.JSONToTable(file.Read(file_path)) + prev_disease = get_disease(data.diseases, self.name) + if prev_disease != nil + then + if (prev_disease.stage >= stage) then return end + prev_disease = disease + else + table.insert(data.diseases, disease) + end + file.Write(file_path, util.TableToJSON(data, true)) +end +print("bozo") return Disease \ No newline at end of file diff --git a/lua/autorun/server/sv_infect.lua b/lua/autorun/server/sv_infect.lua deleted file mode 100644 index edd85ed..0000000 --- a/lua/autorun/server/sv_infect.lua +++ /dev/null @@ -1,20 +0,0 @@ -config = include("sv_config.lua") - ----@param side infected number? the steam id 64 of player infected ----@param side diseases table? a table of diseases -function infect(infected, diseases) - local file_path = config.save_path .. infected .. ".json" - local diseases_stages = {} - for _, disease_name in ipairs(diseases) - do - local disease_stage = {} - disease_stage.disease = disease_name - disease_stage.stage = 1 - table.insert(diseases_stages, disease_stage) - end - local data = util.JSONToTable(file.Read(file_path)) - table.Add(data.diseases, diseases_stages) - file.Write(file_path, util.TableToJSON(data, true)) -end - -return infect \ No newline at end of file diff --git a/lua/autorun/server/sv_init.lua b/lua/autorun/server/sv_init.lua index 68b9983..6404365 100644 --- a/lua/autorun/server/sv_init.lua +++ b/lua/autorun/server/sv_init.lua @@ -1,3 +1,4 @@ include("menus/sv_infect_menu.lua") +include("sv_disease.lua") include("sv_saves.lua") include("sv_settings.lua") diff --git a/lua/autorun/server/sv_saves.lua b/lua/autorun/server/sv_saves.lua index b4a36bb..4f663d9 100644 --- a/lua/autorun/server/sv_saves.lua +++ b/lua/autorun/server/sv_saves.lua @@ -1,5 +1,6 @@ utils = include("sv_utils.lua") config = include("sv_config.lua") +settings = include("sv_settings.lua") utils.path_create(config.save_path .. "/ss") diff --git a/lua/autorun/server/sv_settings.lua b/lua/autorun/server/sv_settings.lua index 502c479..958079f 100644 --- a/lua/autorun/server/sv_settings.lua +++ b/lua/autorun/server/sv_settings.lua @@ -143,4 +143,16 @@ end print("[OK] settings") +---name string? the name of the disease +function settings.getDiseaseByName(name) + for _, disease in ipairs(settings.diseases) + do + if disease.name == name + then + return disease + end + end + return nil +end + return settings \ No newline at end of file