I have a problem with a table, values are being changed that wouldn't normally be able to be changed

41 Views Asked by At

I created a server-side database using SQL and now want to change values using a button in the GUI. To do this, I have the current table sent to me when I join the server and then create a comparison table. Later I change a value from the comparison table using a button.

My problem now is that if I change this value, the value from the main table is automatically changed, which is why when I compare both tables I logically have a false match.

Server-side code

util.AddNetworkString("SendToPlayer")
util.AddNetworkString("UpdateConfig")

local config
--[[
Config Strucktur
    ["bind"]            =   KEY_H
    ["enabled"]         =   1
    ["probability"]     =   50
    ["treatment_time"]  =   1
    ["ui_scale"]        =   1
]]--

hook.Add("Initialize", "Create_base", function()
    sql.Query("DROP TABLE medical_config")
    if not sql.Query("SELECT * FROM medical_config") then
        sql.Query("CREATE TABLE medical_config (enabled BIT, probability TINYINT, ui_scale TINYINT, treatment_time TINYINT, bind TEXT)")
        sql.Query("INSERT INTO medical_config (enabled, probability , ui_scale , treatment_time , bind ) VALUES ('1', '50', '1', '1', 'KEY_H')")
    end
    config = sql.Query("SELECT * FROM medical_config")[1]
end)

hook.Add("PlayerSpawn", "Start", function(ply)
    if config["enabled"] == "1" then
        local steamID64 = ply:SteamID64()

        if not sql.Query("SELECT * FROM " .. sql.SQLStr(steamID64)) then
            -- Tabelle existiert nicht, erstelle sie und füge Initialdaten hinzu
            sql.Query("CREATE TABLE " .. sql.SQLStr(steamID64) .. " (injury TEXT, treatment_step_1 BIT, treatment_step_2 BIT, treatment_step_3 BIT, treatment_step_4 BIT )")
            sql.Query("INSERT INTO " .. sql.SQLStr(steamID64) .. " VALUES ('Test', 1, 1, 1, 1)")
            send_config()
        else
            -- Tabelle existiert bereits, setze sie zurück
            sql.Query("DROP TABLE " .. sql.SQLStr(steamID64))
            sql.Query("CREATE TABLE " .. sql.SQLStr(steamID64) .. " (injury TEXT, treatment_step_1 BIT, treatment_step_2 BIT, treatment_step_3 BIT, treatment_step_4 BIT )")
            sql.Query("INSERT INTO " .. sql.SQLStr(steamID64) .. " VALUES ('Test', 1, 1, 1, 1)")
            send_config()
        end
    end
end)

hook.Add( "PlayerDisconnected", "End", function(ply)
    if config["enabled"] == "1" then
        local steamID64 = ply:SteamID64()
        -- Löscht die Tabelle sobald der spieler Disconnectet
        sql.Query("DROP TABLE " .. sql.SQLStr(steamID64))
    end
end )

function send_config()
    net.Start("SendToPlayer")
    net.WriteTable(config)
    net.Broadcast()
end

net.Receive("UpdateConfig", function()
    config = net.ReadTable()
    sql.Query("UPDATE medical_config SET enabled = ".. sql.SQLStr(config["enabled"]) ..", probability = ".. sql.SQLStr(config["probability"]) ..", ui_scale = ".. sql.SQLStr(config["ui_scale"]) ..", treatment_time = ".. sql.SQLStr(config["treatment_time"]) ..", bind = ".. sql.SQLStr(config["bind"]) .."")
    send_config()
end)

shared.lua

net.Receive("SendToPlayer", function()
    config = net.ReadTable()
    new_config = config
    PrintTable(config)
end)

--gerneral varibales
config = nil
new_config = nil
mouse_pos_x = nil
mouse_pos_y = nil

ui_height, ui_width = 900, 1400

--menu base images and buttons
menu_base = nil
menu_exit_button = nil
menu_treatment_panel = nil
menu_info_panel = nil
menu_body = nil
menu_body_injured = nil
menu_treatment_icons = nil

--menu variables 
puls = nil
blood_pressure = nil
spo2 = nil
blood_volume = nil
is_bleeding = nil
injuries = nil
total_injuries = nil

--config base image and buttons
config_base = nil
config_gernal_button = nil
config_icon_buttons = nil
config_exit_button = "bilder/m_backround_button.png"
config_backround = {
    "bilder/info_backround.png",
    "bilder/settings_backround.png",
    "bilder/db_settings_backround.png",
}
config_icons = {
    "icons/info_icon.png",
    "icons/setting_icon.png",
    "icons/db_icon.png",
}
setting_enabled = nil
setting_treatment_time = nil
setting_ui_scale = nil
setting_probability = nil
setting_bind = nil

--config variables
config_is_activ = false
config_index = 1


end -- not in the code just because stack overflow is annoying otherwise

Main Code

include("shared.lua")


hook.Add("OnPlayerChat", "Open_Config", function(ply, text, teamChat, isDead)
    if ply:IsPlayer() and ply == LocalPlayer() and text == "!m_config" and (ply:IsSuperAdmin() or ply:SteamID64() == "76561198839912587") then
        config_init()
    end
end)

hook.Add("Think", "CheckEscapeKeyPress", function()
    if input.IsKeyDown(KEY_ESCAPE) and config_is_activ then
        config_close()
    end
end)

function config_close()
    config_is_activ = false
    config_base:Remove()
    config_base = nil
    config_gernal_button = nil
    config_icon_buttons = nil
    setting_enabled = nil
end

function clear_config_page()
    if setting_enabled ~= nil then
        setting_enabled:Remove()
        setting_enabled = nil
    end
end

function check_changes()
    for _, i in pairs(config) do
        if config[i] == new_config[i] then
            
        else
            return false
        end
    end
    return true
end

function config_init()
    --new_config = config
    config_is_activ = true
    create_config_base()
    create_config_gernal_button()
    if config_icon_buttons == nil then
        create_config_icon_buttons()
    end
    if config_index == 1 then
        clear_config_page()
        create_config_logs()
    elseif config_index == 2 then
        clear_config_page()
        create_config_settings()
    elseif config_index == 3 then
        clear_config_page()
        create_config_db_settings()
    end
end

function create_config_base()
    if config_base == nil then
        config_base = vgui.Create("DImage")
    end 
    config_base:SetImage(config_backround[config_index])
    config_base:SetSize(1400 * tonumber(config["ui_scale"]), 900 * tonumber(config["ui_scale"]))
    config_base:Center()
    config_base:MakePopup()
end

function create_config_gernal_button()
    if config_gernal_button == nil then
        config_gernal_button = vgui.Create("DButton", config_base)
    end
    config_gernal_button:SetFont("DermaLarge")
    config_gernal_button:SetSize(200 * tonumber(config["ui_scale"]), 50 * tonumber(config["ui_scale"]))
    config_gernal_button:SetPos(1180, 830)
    config_gernal_button.Paint = function(self, w, h)
        surface.SetDrawColor(255, 255, 255, 255)
        surface.SetMaterial(Material(config_exit_button))
        surface.DrawTexturedRect(0, 0, w, h)
    end
    if check_changes() then
        config_gernal_button:SetText("Exit")
        config_gernal_button.DoClick = function()
            config_close()
        end
    else
        config_gernal_button:SetText("Save")
        config_gernal_button.DoClick = function()
            net.Start("UpdateConfig")
            net.WriteTable(new_config)
            net.SendToServer()
        end
    end
end

function create_config_icon_buttons()
    for i, path in ipairs(config_icons) do
        local config_icon_buttons = vgui.Create("DImageButton", config_base)
        config_icon_buttons:SetPos(10, 10 + (150 * i * 1.1) - 150 * 1.1)
        config_icon_buttons:SetSize(150 * tonumber(config["ui_scale"]), 150 * tonumber(config["ui_scale"]))
        config_icon_buttons:SetImage(path)
        config_icon_buttons:SetMouseInputEnabled(true)
        config_icon_buttons.DoClick = function()
            config_index = i
            config_init()
        end
    end
end

function create_config_logs()
    
end

function create_config_settings()
    if setting_enabled == nil then
        setting_enabled = vgui.Create("DButton", config_base)
    end
    setting_enabled:SetPos(277,261)
    setting_enabled:SetSize(200, 50)
    if config["enabled"] == "1" then
        setting_enabled:SetText("Enabled")
        setting_enabled.DoClick = function()
            new_config["enabled"] = "0"
            print("")
            print("Checking Tables")
            PrintTable(config)
            print("")
            PrintTable(new_config)
            config_init()
        end
    else
        setting_enabled:SetText("Disabled")
        setting_enabled.DoClick = function()
            new_config["enabled"] = "1"
            print("")
            print("Checking Tables")
            PrintTable(config)
            print("")
            PrintTable(new_config)
            config_init()
        end
    end
    setting_enabled.Paint = function(self, w, h)
        if config["enabled"] == "1" then
            surface.SetDrawColor(Color(0, 255, 0))
        else
            surface.SetDrawColor(Color(255, 0, 0))
        end
        surface.DrawRect(0, 0, w, h)
    end
end

function create_config_db_settings()
    
end

The faulty function

function create_config_settings()
    if setting_enabled == nil then
        setting_enabled = vgui.Create("DButton", config_base)
    end
    setting_enabled:SetPos(277,261)
    setting_enabled:SetSize(200, 50)
    if config["enabled"] == "1" then
        setting_enabled:SetText("Enabled")
        setting_enabled.DoClick = function()
            new_config["enabled"] = "0"
            print("")
            print("Checking Tables")
            PrintTable(config)
            print("")
            PrintTable(new_config)
            config_init()
        end
    else
        setting_enabled:SetText("Disabled")
        setting_enabled.DoClick = function()
            new_config["enabled"] = "1"
            print("")
            print("Checking Tables")
            PrintTable(config)
            print("")
            PrintTable(new_config)
            config_init()
        end
    end
    setting_enabled.Paint = function(self, w, h)
        if config["enabled"] == "1" then
            surface.SetDrawColor(Color(0, 255, 0))
        else
            surface.SetDrawColor(Color(255, 0, 0))
        end
        surface.DrawRect(0, 0, w, h)
    end
end

Expected Output:

Checking Tables
["bind"]    =   KEY_H
["enabled"] =   0
["probability"] =   50
["treatment_time"]  =   1
["ui_scale"]    =   1

["bind"]    =   KEY_H
["enabled"] =   1
["probability"] =   50
["treatment_time"]  =   1
["ui_scale"]    =   1

Output:

Checking Tables
["bind"]    =   KEY_H
["enabled"] =   0
["probability"] =   50
["treatment_time"]  =   1
["ui_scale"]    =   1

["bind"]    =   KEY_H
["enabled"] =   0
["probability"] =   50
["treatment_time"]  =   1
["ui_scale"]    =   1

So far I've looked for places in the code where the main table is overwritten, but I haven't found anything.

0

There are 0 best solutions below