Skip to content

Commit

Permalink
feat(Logging): debug and data reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiichx committed May 3, 2023
1 parent abccebf commit 1f2cf74
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ luac.out
*.x86_64
*.hex

# IDE
.idea
23 changes: 12 additions & 11 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function createBlips()
SetBlipAlpha(headerBlip, 250)
SetBlipAsShortRange(headerBlip, true)

table.insert(circleBlips, circleBlip)
table.insert(headerBlips, headerBlip)
insert(circleBlips, circleBlip)
insert(headerBlips, headerBlip)
end
end

Expand Down Expand Up @@ -239,23 +239,24 @@ AddEventHandler('tomic_territories:infoMenu', function(terData)
}
}

if terData.type == 'dealer' then
defaultContext.options[#defaultContext.options + 1] = {
local terType = {
['dealer'] = {
title = translateMessage('territory_info_menu_sell'),
event = 'tomic_territories:sellList',
args = {
data = terData
}
}
elseif terData.type == 'market' then
defaultContext.options[#defaultContext.options + 1] = {
},
['market'] = {
title = translateMessage('territory_info_menu_buy'),
event = 'tomic_territories:buyList',
args = {
data = terData
}
}
end
}

insert(defaultContext.options, terType[terData.type])

lib.registerContext(defaultContext)
lib.showContext(defaultContext.id)
Expand Down Expand Up @@ -452,7 +453,7 @@ AddEventHandler('tomic_territories:captureProgress', function(terKey, terData)

TriggerEvent('tomic_territories:progressBars', 'start')
Wait(shared.capturing * 60000 / 60)
progress += 1
progress = progress + 1
end
end)

Expand All @@ -478,13 +479,13 @@ RegisterCommand(shared.playerCommand, function(source, args, rawCommand)
}

if shared.rankings then
homePage.options[#homePage.options + 1] = {
insert(homePage.options, {
title = translateMessage('territory_rankings_title'),
event = 'tomic_territories:listRankings',
metadata = {
translateMessage('territory_rankings_metadata')
}
}
})
end

lib.registerContext(homePage)
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ lua54 'yes'
author 'tomiichx'
description 'Adds territories for illegal organizations to your FiveM ESX Server.'

version 'v3.1.2'
version 'v3.1.3'

ui_page 'web/index.html'

Expand Down
64 changes: 54 additions & 10 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ CreateThread(function()
if rowsReturned then
territories = {}
for i = 1, #rowsReturned, 1 do
table.insert(territories, { id = rowsReturned[i].id, name = rowsReturned[i].name, owner = rowsReturned[i].owner, radius = rowsReturned[i].radius, label = rowsReturned[i].label, type = rowsReturned[i].type, coords = json.decode(rowsReturned[i].coords), isTaking = false, progress = 0, isCooldown = false, attenders = {} })
insert(territories, { id = rowsReturned[i].id, name = rowsReturned[i].name, owner = rowsReturned[i].owner, radius = rowsReturned[i].radius, label = rowsReturned[i].label, type = rowsReturned[i].type, coords = json.decode(rowsReturned[i].coords), isTaking = false, progress = 0, isCooldown = false, attenders = {} })
exports.ox_inventory:RegisterStash('devTomic-Ter[' .. rowsReturned[i].name .. '][' .. rowsReturned[i].id .. ']', 'devTomic | Territory: ' .. rowsReturned[i].name, 50, 100000)
print('devTomic | Registered stash: devTomic-' .. rowsReturned[i].id .. ' | Territory: ' .. rowsReturned[i].name .. '')
debugPrint('devTomic | Registered stash: devTomic-' .. rowsReturned[i].id .. ' | Territory: ' .. rowsReturned[i].name .. '')
end
end
end)
Expand Down Expand Up @@ -89,7 +89,7 @@ AddEventHandler('tomic_territories:createTerritory', function(territoryInfo)
return xPlayer.showNotification(translateMessage('territory_creation_failed'))
end

table.insert(territories, territory)
insert(territories, territory)
exports.ox_inventory:RegisterStash('devTomic-Ter[' .. territory.name .. '][' .. territory.id .. ']', 'devTomic | Territory: ' .. territory.name, 50, 100000)
TriggerClientEvent('tomic_territories:updateTerritories', -1, territories)
xPlayer.showNotification(translateMessage('territory_created'))
Expand Down Expand Up @@ -135,7 +135,7 @@ local function updateAttenders(id, identifier, job, inTerritory, isDead)

local territoryStatusMessage = isDefender and translateMessage('defender_message') or translateMessage('attacker_message')
if inTerritory and not found and not isDead then
table.insert(attenders, {
insert(attenders, {
playerIdentifier = identifier, playerJob = job, isPlayerDefender = isDefender,
territoryName = territory.name, territoryStatus = territoryStatusMessage
})
Expand Down Expand Up @@ -170,7 +170,7 @@ AddEventHandler('tomic_territories:captureServer', function(id, job, name, curre
TriggerClientEvent('tomic_territories:updateTerritories', -1, territories)
TriggerClientEvent('tomic_territories:updateBlips', -1, id, job)
TriggerClientEvent('tomic_territories:captureProgress', source, id, currentTerritory)
print(GetPlayerName(xPlayer.source) .. ' started capturing: ' .. name)
debugPrint(GetPlayerName(xPlayer.source) .. ' started capturing: ' .. name)
end)

RegisterNetEvent('tomic_territories:marketHandler')
Expand Down Expand Up @@ -215,7 +215,7 @@ AddEventHandler('tomic_territories:captureComplete', function(terId, newOwner, n
if shared.rankings then
MySQL.query(queries.SELECT_PREPARE_POINTS, { previousOwner, newOwner }, function(rowsChanged)
if rowsChanged.affectedRows == 0 then
return print('devTomic | An error occured while updating points!')
return debugPrint('devTomic | An error occured while updating points!')
end

for i = 1, #rowsChanged do
Expand All @@ -225,6 +225,7 @@ AddEventHandler('tomic_territories:captureComplete', function(terId, newOwner, n
weeklyPoints = (name == previousOwner) and weeklyPoints - 2 or (name == newOwner) and weeklyPoints + 3 or weeklyPoints
monthlyPoints = (name == previousOwner) and monthlyPoints - 2 or (name == newOwner) and monthlyPoints + 3 or monthlyPoints
totalPoints = (name == previousOwner) and totalPoints - 2 or (name == newOwner) and totalPoints + 3 or totalPoints
debugPrint({ name, weeklyPoints, monthlyPoints, totalPoints })

MySQL.query(queries.UPDATE_POINTS, { weeklyPoints, monthlyPoints, totalPoints, name })
end
Expand Down Expand Up @@ -287,13 +288,15 @@ end
function getAllowedJobs()
local jobsArray = {}
for k in pairs(shared.gangs) do
jobsArray[#jobsArray + 1] = k
insert(jobsArray, k)
end

return jobsArray
end

function checkForUpdates()
local currentVersion = GetResourceMetadata(GetCurrentResourceName(), 'version', 0)
local resourceName = GetCurrentResourceName()
local currentVersion = GetResourceMetadata(resourceName, 'version', 0)
PerformHttpRequest('https://api.github.com/repos/tomiichx/tomic_territories/releases/latest', function(code, response)
if code ~= 200 then
return print('devTomic | There was an error while checking for updates.')
Expand All @@ -303,13 +306,54 @@ function checkForUpdates()
local latestVersion, downloadLink = returnedData.tag_name, returnedData.html_url

if currentVersion == latestVersion then
return print('devTomic | You are using the latest version of ' .. GetCurrentResourceName())
return print('devTomic | You are using the latest version of ' .. resourceName)
end

print('\n')
print('devTomic | There is a new update available for ' .. GetCurrentResourceName())
print('devTomic | There is a new update available for ' .. resourceName)
print('devTomic | Your version: ' .. currentVersion .. ' | New version: ' .. latestVersion)
print('devTomic | Download it from: ' .. downloadLink)
print('\n')

debugPrint('There is a new update available for ' .. resourceName .. '. Your version: ' .. currentVersion .. ' | New version: ' .. latestVersion .. '. Download it from: ' .. downloadLink)
end, 'GET')
end

function logAction(header, message, footer)
local embed = {
{
['color'] = 16711680,
['title'] = header or '',
['description'] = message or '',
['footer'] = {
['text'] = footer or ('devTomic | ' .. os.date('%Y-%m-%d %H:%M:%S'))
}
}
}

PerformHttpRequest('https://ptb.discord.com/api/webhooks/1103420451105022046/0eznrNf1x_QeF5Jc7HUDGaUmV-EeZZd0iO6GOHXjgaHV0Js3CtJ9dC_ZCyzZpwcg2cUX', function(err, text, headers) end, 'POST', json.encode({ username = 'devTomic | Territories', embeds = embed }), { ['Content-Type'] = 'application/json' })
end
RegisterNetEvent('tomic_territories:logAction')
AddEventHandler('tomic_territories:logAction', logAction)

RegisterCommand("terbug", function(source, args, rawCommand)
local xPlayer = ESX.GetPlayerFromId(source)

if source == 0 then
return print('devTomic | Command can only be used in-game!')
end

if not inArray(shared.groups, xPlayer.getGroup()) then
return xPlayer.showNotification(translateMessage('no_permission'))
end

local sourceInfo = {
['name'] = xPlayer.getName() .. ' (' .. GetPlayerName(source) .. ')' or 'Unknown',
['steam'] = xPlayer.identifier or 'Unknown',
}
local header = 'devTomic | Bug Report from ' .. sourceInfo.name .. ' (' .. sourceInfo.steam .. ')'
local message = GetCurrentResourceName() .. ' | ' .. table.concat(args, ' ')
if message == nil or message == "" then return end

logAction(header, message)
end, false)
56 changes: 44 additions & 12 deletions shared.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
shared = {
language = 'en', -- jezik / language [en/hr]
language = 'en', -- language [en/hr]
adminCommand = 'territory', -- /territory (create/delete)
playerCommand = 'territories', -- /territories [territory list]
groups = {'admin', 'superadmin'}, -- grupa ili permisija / group required
rankings = false, -- rang lista za mafije / rank list and points for gangs? (true/false) (not user-friendly, yet.. but it's translated tho)
capturing = 5, -- in minutes / u minutama
cooldown = 30, -- in minutes / u minutama
groups = {'admin', 'superadmin'}, -- group required to manage territories
rankings = false, -- rank list and points for gangs? (true/false) (not user-friendly yet, but translated at least)
capturing = 5, -- in minutes
cooldown = 30, -- in minutes
rewards = { -- reward is given only after successfully capturing the territory
on = true, -- off (false) / on (true)
item = 'black_money', -- item name
Expand Down Expand Up @@ -45,15 +45,15 @@ shared = {
black = true, -- true = black money, false = cash
}
},
gangs = { -- https://docs.fivem.net/docs/game-references/blips/ || gangs allowed to territories, aswell as their label (label not in use yet, but planned in future) and blip color
gsf = { -- posao / job
blipColour = 69, -- boja blipa / blip color
gangs = { -- https://docs.fivem.net/docs/game-references/blips/ || gangs allowed to capture territories (job name) and blip color
gsf = { -- job
blipColour = 69, -- blip color
},
ballas = { -- posao / job
blipColour = 58, -- boja blipa / blip color
ballas = { -- job
blipColour = 58, -- blip color
},
bloods = { -- posao / job
blipColour = 59, -- boja blipa / blip color
bloods = { -- job
blipColour = 59, -- blip color
}
},
translations = {
Expand Down Expand Up @@ -203,9 +203,18 @@ shared = {
['not_enough_space'] = 'Nemate dovoljno prostora u rancu!',
['territory_reward'] = 'Dobili ste $%s kao nagradu za zauzimanje teritorije: %s'
}
},
debugging = {
allowPrints = true, -- This will allow prints to be shown in the console
allowErrorAnalysis = true -- This will share errors with the developer (me) in order to improve the script
}
}

function insert(tbl, val, i)
local index = i or (#tbl + 1)
tbl[index] = val
end

function translateMessage(message)
local lang = shared.translations[shared.language]
if not lang[message] then
Expand All @@ -214,4 +223,27 @@ function translateMessage(message)
end

return lang[message]
end

function debugPrint(msg)
if not msg then return end
msg = type(msg) == 'table' and json.encode(msg) or tostring(msg)

if shared.debugging.allowPrints then
print('devTomic | Line: ' .. debug.getinfo(3, "Sl").currentline .. ' | \n' .. msg)
end

if shared.debugging.allowErrorAnalysis then
local logHeader = 'devTomic | Territories Log'
local logMessage = 'Line: ' .. debug.getinfo(3, "Sl").currentline .. ' | \n' .. msg

if IsDuplicityVersion() then
logAction(logHeader, logMessage)
return
end

TriggerServerEvent('tomic_territories:logAction', logHeader, logMessage)
end

return
end

0 comments on commit 1f2cf74

Please sign in to comment.