forked from Simnation/Main
110 lines
3.6 KiB
Lua
110 lines
3.6 KiB
Lua
local QBCore = exports['qb-core']:GetCoreObject()
|
|
|
|
local activeCompetitions = {}
|
|
|
|
QBCore.Functions.CreateCallback('qb-shootingrange:getHighscores', function(source, cb)
|
|
local scores = json.decode(LoadResourceFile(GetCurrentResourceName(), "highscores.json") or "[]")
|
|
cb(scores)
|
|
end)
|
|
|
|
RegisterNetEvent('qb-shootingrange:saveScore', function(name, score)
|
|
local scores = json.decode(LoadResourceFile(GetCurrentResourceName(), "highscores.json") or "[]")
|
|
|
|
local timestamp = os.date("%d.%m.%Y - %H:%M")
|
|
|
|
table.insert(scores, {
|
|
name = name,
|
|
score = score,
|
|
timestamp = timestamp
|
|
})
|
|
|
|
table.sort(scores, function(a, b)
|
|
return tonumber(a.score) > tonumber(b.score)
|
|
end)
|
|
|
|
while #scores > Config.MaxHighscores do
|
|
table.remove(scores, #scores)
|
|
end
|
|
|
|
SaveResourceFile(GetCurrentResourceName(), "highscores.json", json.encode(scores, {indent = true}), -1)
|
|
end)
|
|
|
|
RegisterNetEvent("qb-shootingrange:sendInvite")
|
|
AddEventHandler("qb-shootingrange:sendInvite", function(targetId)
|
|
local src = source
|
|
local inviterName = GetPlayerName(src)
|
|
|
|
if not activeCompetitions[src] then
|
|
activeCompetitions[src] = {
|
|
host = src,
|
|
players = {},
|
|
scores = {}
|
|
}
|
|
end
|
|
|
|
TriggerClientEvent("qb-shootingrange:receiveInvite", targetId, inviterName, src)
|
|
end)
|
|
|
|
RegisterNetEvent("qb-shootingrange:acceptInvite")
|
|
AddEventHandler("qb-shootingrange:acceptInvite", function(inviterId)
|
|
local src = source
|
|
local playerName = GetPlayerName(src)
|
|
|
|
if activeCompetitions[inviterId] then
|
|
activeCompetitions[inviterId].players[src] = true
|
|
activeCompetitions[inviterId].scores[playerName] = 0
|
|
|
|
TriggerClientEvent("qb-shootingrange:playerJoinedCompetition", inviterId, playerName)
|
|
end
|
|
end)
|
|
|
|
RegisterNetEvent("qb-shootingrange:declineInvite")
|
|
AddEventHandler("qb-shootingrange:declineInvite", function(inviterId)
|
|
local src = source
|
|
local playerName = GetPlayerName(src)
|
|
TriggerClientEvent("qb-shootingrange:inviteDeclined", inviterId, playerName)
|
|
end)
|
|
|
|
RegisterNetEvent("qb-shootingrange:startCompetition")
|
|
AddEventHandler("qb-shootingrange:startCompetition", function()
|
|
local src = source
|
|
|
|
if activeCompetitions[src] then
|
|
for playerId, _ in pairs(activeCompetitions[src].players) do
|
|
TriggerClientEvent("qb-shootingrange:competitionStarted", playerId)
|
|
end
|
|
TriggerClientEvent("qb-shootingrange:competitionStarted", src)
|
|
end
|
|
end)
|
|
|
|
RegisterNetEvent("qb-shootingrange:cancelCompetition")
|
|
AddEventHandler("qb-shootingrange:cancelCompetition", function()
|
|
local src = source
|
|
|
|
if activeCompetitions[src] then
|
|
for playerId, _ in pairs(activeCompetitions[src].players) do
|
|
TriggerClientEvent("qb-shootingrange:competitionCancelled", playerId)
|
|
end
|
|
activeCompetitions[src] = nil
|
|
end
|
|
end)
|
|
|
|
RegisterNetEvent("qb-shootingrange:updateCompetitionScore")
|
|
AddEventHandler("qb-shootingrange:updateCompetitionScore", function(score)
|
|
local src = source
|
|
local playerName = GetPlayerName(src)
|
|
|
|
for _, competition in pairs(activeCompetitions) do
|
|
if competition.players[src] or competition.host == src then
|
|
competition.scores[playerName] = score
|
|
|
|
for playerId, _ in pairs(competition.players) do
|
|
TriggerClientEvent("qb-shootingrange:updateCompetition", playerId, competition.scores)
|
|
end
|
|
TriggerClientEvent("qb-shootingrange:updateCompetition", competition.host, competition.scores)
|
|
break
|
|
end
|
|
end
|
|
end)
|
|
|
|
|