Skip to content

Commit

Permalink
(1.2.0.0) KVP Data storage implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cu-chi-zz committed Nov 3, 2021
1 parent c6c2f82 commit e9384e6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
60 changes: 55 additions & 5 deletions client/client.lua
Expand Up @@ -2,9 +2,44 @@ local wheelr, wheelg, wheelb = 0, 0, 0
local pauser, pauseg, pauseb = 0, 0, 0
local waypointr, waypointg, waypointb = 0, 0, 0
local showMenu = false
local kvpData = {
["pause"] = "",
["waypoint"] = "",
["wheel"] = ""
}

CreateThread(function()
TriggerServerEvent("cts:getThemeColors")
CreateThread(function()
if not Cfg.UseResourceKvp then
TriggerServerEvent("cts:getThemeColors")
kvpData = nil
else
for key, _ in pairs(kvpData) do
local supposedKey = "cts:"..key

if GetResourceKvpString(supposedKey) ~= nil then -- Check if the value is saved in the user cache files
kvpData[key] = GetResourceKvpString(supposedKey) -- Set the correct kvp key with in the correct field
print(("%s setted with the value of %s (%s)"):format(key, supposedKey, GetResourceKvpString(supposedKey)))

if kvpData[key] ~= "" then
colorData = splitString(kvpData[key], ",") -- Split with "," because the rgb value is stocked as a string value ("r,g,b")

if key == "pause" then
pauser, pauseg, pauseb = tonumber(colorData[1]), tonumber(colorData[2]), tonumber(colorData[3])
ReplaceHudColourWithRgba(117, pauser, pauseg, pauseb, 200)
elseif key == "waypoint" then
waypointr, waypointg, waypointb = tonumber(colorData[1]), tonumber(colorData[2]), tonumber(colorData[3])
ReplaceHudColourWithRgba(142, waypointr, waypointg, waypointb, 200)
elseif key == "wheel" then
wheelr, wheelg, wheelb = tonumber(colorData[1]), tonumber(colorData[2]), tonumber(colorData[3])
ReplaceHudColourWithRgba(116, wheelr, wheelg, wheelb, 200)
end
end
else
print(("%s is not saved in the files: %s"):format(key, tostring(GetResourceKvpString(supposedKey))))
SetResourceKvp(supposedKey, "")
end
end
end
end)

RegisterCommand(Cfg.Command, function()
Expand All @@ -24,17 +59,32 @@ RegisterNUICallback("action", function(data, cb)
wheelr, wheelg, wheelb = data.color[1], data.color[2], data.color[3]

ReplaceHudColourWithRgba(116, wheelr, wheelg, wheelb, 200)
TriggerServerEvent("cts:setThemeColors", wheelr..","..wheelg..","..wheelb, nil)

if not Cfg.UseResourceKvp then
TriggerServerEvent("cts:setThemeColors", wheelr..","..wheelg..","..wheelb, nil, nil)
else
SetResourceKvp("cts:wheel", wheelr..","..wheelg..","..wheelb)
end
elseif data.type == "pausecolor" then
pauser, pauseg, pauseb = data.color[1], data.color[2], data.color[3]

ReplaceHudColourWithRgba(117, pauser, pauseg, pauseb, 200)
TriggerServerEvent("cts:setThemeColors", nil, pauser..","..pauseg..","..pauseb, nil)

if not Cfg.UseResourceKvp then
TriggerServerEvent("cts:setThemeColors", nil, pauser..","..pauseg..","..pauseb, nil)
else
SetResourceKvp("cts:pause", pauser..","..pauseg..","..pauseb)
end
elseif data.type == "waypointcolor" then
waypointr, waypointg, waypointb = data.color[1], data.color[2], data.color[3]

ReplaceHudColourWithRgba(142, waypointr, waypointg, waypointb, 200)
TriggerServerEvent("cts:setThemeColors", nil, nil, waypointr..","..waypointg..","..waypointb)

if not Cfg.UseResourceKvp then
TriggerServerEvent("cts:setThemeColors", nil, nil, waypointr..","..waypointg..","..waypointb)
else
SetResourceKvp("cts:waypoint", waypointr..","..waypointg..","..waypointb)
end
end

cb("OK")
Expand Down
3 changes: 2 additions & 1 deletion config.lua
@@ -1,4 +1,5 @@
Cfg = {
Command = "theme", -- Command to open the menu
CheckVersion = true -- Check for a new version on startup
CheckVersion = true, -- Check for a new version on startup
UseResourceKvp = true -- This way when the player joins any server with this script, players can join any server that has this script, and their preferences will carry over
}
12 changes: 8 additions & 4 deletions server/version-check.lua
@@ -1,5 +1,9 @@
if Cfg.CheckVersion then
CreateThread( function()
CreateThread( function()
if GetCurrentResourceName() ~= "cuchi_themeSelector" then -- If the resource name is changed, then the resource kvp won't work
print("^7[^6"..GetCurrentResourceName().."^7] > ^1You shouldn't change the resource name")
end

if Cfg.CheckVersion then
updatePath = "/Cu-chi/cuchi_themeSelector"

PerformHttpRequest("https://raw.githubusercontent.com"..updatePath.."/master/version", function(err, responseText, headers)
Expand All @@ -11,5 +15,5 @@ if Cfg.CheckVersion then
print("^7[^6"..GetCurrentResourceName().."^7] > ^2Up to date!^7")
end
end, "GET")
end)
end
end
end)
2 changes: 1 addition & 1 deletion version
@@ -1 +1 @@
1.1.0.0
1.2.0.0

0 comments on commit e9384e6

Please sign in to comment.