core: rebuild infect function

fix: a disease cannot be add in multiple time
This commit is contained in:
camille lechauve 2023-09-14 19:23:54 +02:00
parent 7bb58e5c09
commit 4f255b2f43
6 changed files with 69 additions and 23 deletions

View File

@ -1,6 +1,5 @@
settings = include("../sv_settings.lua") settings = include("../sv_settings.lua")
config = include("../sv_config.lua") config = include("../sv_config.lua")
infect = include("../sv_infect.lua")
util.AddNetworkString("get_diseases_name") util.AddNetworkString("get_diseases_name")
util.AddNetworkString("get_players_data") util.AddNetworkString("get_players_data")
@ -43,8 +42,19 @@ net.Receive("infect", function (len, ply)
local data = net.ReadTable() local data = net.ReadTable()
for _, infected in ipairs(data.infecteds) for _, infected in ipairs(data.infecteds)
do do
print("1") for _, disease_name in ipairs(data.diseases)
infect(infected, 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
end) end)

View File

@ -1,3 +1,4 @@
config = include("sv_config.lua")
Disease = {} Disease = {}
--- Disease class --- Disease class
@ -17,4 +18,45 @@ function Disease:new(name, symbtoms_by_stages, contamination_methods, transmissi
return instance return instance
end 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 return Disease

View File

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

View File

@ -1,3 +1,4 @@
include("menus/sv_infect_menu.lua") include("menus/sv_infect_menu.lua")
include("sv_disease.lua")
include("sv_saves.lua") include("sv_saves.lua")
include("sv_settings.lua") include("sv_settings.lua")

View File

@ -1,5 +1,6 @@
utils = include("sv_utils.lua") utils = include("sv_utils.lua")
config = include("sv_config.lua") config = include("sv_config.lua")
settings = include("sv_settings.lua")
utils.path_create(config.save_path .. "/ss") utils.path_create(config.save_path .. "/ss")

View File

@ -143,4 +143,16 @@ end
print("[OK] settings") 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 return settings