Skip to content

Commit

Permalink
hotfix: leaderboard conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeelot committed Nov 6, 2022
1 parent f1bc65a commit 9db3d95
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
30 changes: 24 additions & 6 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1303,23 +1303,41 @@ RegisterNetEvent("cw-racingapp:Client:TrackRecordList", function(data)
print('RecordData', dump(RecordData), i)
end

local class = RecordData.Class
local holder = RecordData.Holder
local vehicle = RecordData.Vehicle
local time = RecordData.Time

if not class then
class = 'NO DATA AVAILABLE'
end
if not holder then
holder = 'NO DATA AVAILABLE'
end
if not vehicle then
vehicle = 'NO DATA AVAILABLE'
end
if not time then
time = 'NO DATA AVAILABLE'
end

local text = ''
local first = '🥇 '
local second = '🥈 '
local third = '🥉 '
local header = ''
if i == 1 then
header = first..RecordData.Holder
header = first..holder
elseif i == 2 then
header = second..RecordData.Holder
header = second..holder
elseif i == 3 then
header = third..RecordData.Holder
header = third..holder
else
header = i..'. '..RecordData.Holder
header = i..'. '..holder
end
text = SecondsToClock(RecordData.Time).. ' | '..RecordData.Vehicle
text = SecondsToClock(time).. ' | '..vehicle
if data.class == 'all' then
text = text.. ' | '..RecordData.Class
text = text.. ' | '..class
end

menu[#menu + 1] = {
Expand Down
27 changes: 22 additions & 5 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ MySQL.ready(function ()
if v.records ~= nil then
Records = json.decode(v.records)
if #Records == 0 then
if Config.Debug then
print('Only one record')
MySQL.Async.execute('UPDATE race_tracks SET records = ? WHERE raceid = ?',
{json.encode({Records}), v.raceid})
end
Records = { Records }
end
end
Expand Down Expand Up @@ -72,12 +77,15 @@ local function filterLeaderboardsByClass(Leaderboard, class)
end

local function racerHasPreviousRecordInClass(Records, RacerName, CarClass)
for i, Record in pairs(Records) do
if Record.Holder == RacerName and Record.Class == CarClass then
return Record, i
if Records then
for i, Record in pairs(Records) do
if Record.Holder == RacerName and Record.Class == CarClass then
return Record, i
end
end
else
return false
end
return false
end

local function getLatestRecordsById(RaceId)
Expand Down Expand Up @@ -119,17 +127,20 @@ local function newRecord(src, RacerName, PlayerTime, RaceData, CarClass, Vehicle
end
else
FilteredLeaderboard = filterLeaderboardsByClass(records, CarClass)
local PersonalBest, index = racerHasPreviousRecordInClass(records, RacerName, CarClass)
end
if Config.Debug then
print('Time', PlayerTime, RacerName, CarClass, VehicleModel)
print('All times for this class', dump(FilteredLeaderboard))
end

local PersonalBest, index = racerHasPreviousRecordInClass(records, RacerName, CarClass)
if Config.Debug then
print('racer has previous record: ', dump(PersonalBest), index)
end
if PersonalBest and PersonalBest.Time > PlayerTime then
if Config.Debug then
print('new pb', PlayerTime, RacerName, CarClass, VehicleModel)
end
TriggerClientEvent('QBCore:Notify', src, string.format(Lang:t("success.new_pb"), RaceData.RaceName, SecondsToClock(PlayerTime)), 'success')
local playerPlacement = {
Time = PlayerTime,
Expand All @@ -139,18 +150,24 @@ local function newRecord(src, RacerName, PlayerTime, RaceData, CarClass, Vehicle
}
records[index] = playerPlacement
records = sortRecordsByTime(records)
print('records being sent to db', dump(records))
MySQL.Async.execute('UPDATE race_tracks SET records = ? WHERE raceid = ?',
{json.encode(records), RaceData.RaceId})
return true

elseif not PersonalBest then
TriggerClientEvent('QBCore:Notify', src, string.format(Lang:t("success.time_added"), RaceData.RaceName, SecondsToClock(PlayerTime)), 'success')
print('had no pb')
if Config.Debug then
print('new pb', PlayerTime, RacerName, CarClass, VehicleModel)
end
local playerPlacement = {
Time = PlayerTime,
Holder = RacerName,
Class = CarClass,
Vehicle = VehicleModel
}
print('records', dump(records))
table.insert(records, playerPlacement)
records = sortRecordsByTime(records)
if Config.Debug then
Expand Down

0 comments on commit 9db3d95

Please sign in to comment.