Update online courses to be compatible with new database (#176)

This commit is contained in:
Jonny_Bro (Nikita) 2024-03-27 20:07:35 +05:00 committed by GitHub
parent 98435b18eb
commit 7cf77f4029
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,11 @@ local QueuedArgs = NULL
local QueuedFunction = NULL local QueuedFunction = NULL
local currentMap = game.GetMap() local currentMap = game.GetMap()
concommand.Add("Beatrun_Cancel", function()
QueuedArgs = NULL
QueuedFunction = NULL
end)
concommand.Add("Beatrun_Confirm", function() concommand.Add("Beatrun_Confirm", function()
if QueuedArgs and QueuedFunction then if QueuedArgs and QueuedFunction then
QueuedFunction(QueuedArgs) QueuedFunction(QueuedArgs)
@ -19,11 +24,6 @@ concommand.Add("Beatrun_Confirm", function()
end end
end) end)
concommand.Add("Beatrun_Cancel", function()
QueuedArgs = NULL
QueuedFunction = NULL
end)
local function GetCurrentMapWorkshopID() local function GetCurrentMapWorkshopID()
for _, addon in pairs(engine.GetAddons()) do for _, addon in pairs(engine.GetAddons()) do
if not addon or not addon.title or not addon.wsid or not addon.mounted or not addon.downloaded then continue end if not addon or not addon.title or not addon.wsid or not addon.mounted or not addon.downloaded then continue end
@ -36,27 +36,71 @@ local function GetCurrentMapWorkshopID()
return 0 return 0
end end
function GetCourse(sharecode)
local url = domain:GetString() .. "/api/download"
http.Fetch(url, function(body, length, headers, code)
local response = util.JSONToTable(body)
if response.res == 200 then
print("Success! | Length: " .. length .. "\nLoading course...")
local dir = "beatrun/courses/" .. currentMap .. "/"
file.CreateDir(dir)
local coursedata = util.Compress(response.file)
file.Write(dir .. sharecode .. ".txt", coursedata)
LoadCourseRaw(coursedata)
return true
else
print("Error! | Response: " .. response.message)
return false
end
end, function(message)
print("An error occurred: " .. message)
return false
end, {
authorization = apikey:GetString(),
code = sharecode,
map = string.Replace(currentMap, " ", "-")
})
end
concommand.Add("Beatrun_LoadCode", function(ply, cmd, args, argstr)
GetCourse(args[1])
end)
function UploadCourse() function UploadCourse()
if Course_Name == "" or Course_ID == "" then return print(language.GetPhrase("beatrun.coursesdatabase.cantuploadfreeplay")) end if Course_Name == "" or Course_ID == "" then return print(language.GetPhrase("beatrun.coursesdatabase.cantuploadfreeplay")) end
local url = domain:GetString() .. "/upload.php" local url = domain:GetString() .. "/api/upload"
local data = file.Open("beatrun/courses/" .. currentMap .. "/" .. Course_ID .. ".txt", "rb", "DATA") local data = file.Open("beatrun/courses/" .. currentMap .. "/" .. Course_ID .. ".txt", "rb", "DATA")
local filedata = util.Decompress(data:Read(data:Size())) local filedata = util.Decompress(data:Read(data:Size())) or data:Read(data:Size())
http.Post(url, { http.Post(url, NULL, function(body, length, headers, code)
key = apikey:GetString(), local response = util.JSONToTable(body)
map = string.Replace(currentMap, " ", "-"),
course_data = util.Base64Encode(filedata, true), if response.res == 200 then
mapid = GetCurrentMapWorkshopID() print("Success! | Code: " .. response.code)
}, function(body, length, headers, code) -- onSuccess function
if code == 200 then return true
print("Response: " .. body)
else else
print("Error (" .. code .. "): " .. body) print("An error occurred: " .. message)
return false
end end
end, function(message) -- onFailure function end, function(message)
print("Unexpected error: " .. message) print("Unexpected error: " .. message)
end) end, {
authorization = apikey:GetString(),
course = util.Base64Encode(filedata, true),
map = string.Replace(currentMap, " ", "-"),
mapid = GetCurrentMapWorkshopID()
})
end end
concommand.Add("Beatrun_UploadCourse", function() concommand.Add("Beatrun_UploadCourse", function()
@ -66,67 +110,33 @@ concommand.Add("Beatrun_UploadCourse", function()
print(language.GetPhrase("beatrun.coursesdatabase.upload2")) print(language.GetPhrase("beatrun.coursesdatabase.upload2"))
end) end)
function GetCourse(sharecode) function UpdateCourse(course_code)
local url = domain:GetString() .. "/getcourse.php" if Course_Name == "" or Course_ID == "" then return print(language.GetPhrase("beatrun.coursesdatabase.cantuploadfreeplay")) end
.. "?sharecode=" .. sharecode
.. "&map=" .. string.Replace(currentMap, " ", "-")
.. "&key=" .. apikey:GetString()
http.Fetch(url, function(body, length, headers, code) local url = domain:GetString() .. "/api/update"
if code == 200 then local data = file.Open("beatrun/courses/" .. currentMap .. "/" .. Course_ID .. ".txt", "rb", "DATA")
print("Success! | Response: " .. code .. " | Length: " .. length .. "\nLoading course...") local filedata = util.Decompress(data:Read(data:Size())) or data:Read(data:Size())
PrintTable(headers) http.Post(url, NULL, function(body, length, headers, code)
local response = util.JSONToTable(body)
local dir = "beatrun/courses/" .. currentMap .. "/" if response.res == 200 then
file.CreateDir(dir) print("Success! | Code: " .. response.code)
local coursedata = util.Compress(body)
if not file.Exists(dir .. sharecode .. ".txt", "DATA") then
file.Write(dir .. sharecode .. ".txt", coursedata)
end
LoadCourseRaw(coursedata)
return true return true
else else
print("Error! | Response: " .. code) print("An error occurred: " .. message)
print(body)
return false return false
end end
end, function(message) end, function(message)
print("An error occurred: ", message)
return false
end)
end
concommand.Add("Beatrun_LoadCode", function(ply, cmd, args, argstr)
GetCourse(args[1])
end)
function UpdateCourse(course_code)
if Course_Name == "" or Course_ID == "" then return print(language.GetPhrase("beatrun.coursesdatabase.cantuploadfreeplay")) end
local url = domain:GetString() .. "/updatecourse.php"
local data = file.Open("beatrun/courses/" .. currentMap .. "/" .. Course_ID .. ".txt", "rb", "DATA")
local filedata = util.Decompress(data:Read(data:Size()))
http.Post(url, {
key = apikey:GetString(),
map = string.Replace(currentMap, " ", "-"),
course_data = util.Base64Encode(filedata, true),
code = course_code
}, function(body, length, headers, code) -- onSuccess function
if code == 200 then
print("Response: " .. body)
else
print("Error (" .. code .. "): " .. body)
end
end, function(message) -- onFailure function
print("Unexpected error: " .. message) print("Unexpected error: " .. message)
end) end, {
authorization = apikey:GetString(),
code = course_code,
course = util.Base64Encode(filedata, true),
map = string.Replace(currentMap, " ", "-")
})
end end
concommand.Add("Beatrun_UpdateCode", function(ply, cmd, args, argstr) concommand.Add("Beatrun_UpdateCode", function(ply, cmd, args, argstr)