Skip to content

Commit

Permalink
fix(profiles): Profiles missing license data
Browse files Browse the repository at this point in the history
This is a dirty but functional fix to address missing licenses that breaks profiles (charges, history, unable to save, etc) when the license data is empty from the metadata.

It should always ensure at least something is in the licences metadata, so that if a profile breaks, it can be fixed by granting the user a new license without making database changes each time.

Fixes Project-Sloth#59
  • Loading branch information
jnccloud committed Jun 9, 2022
1 parent 646a457 commit 25e6599
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
21 changes: 18 additions & 3 deletions server/dbm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function GetPlayerDataById(id)
else
return MySQL.single.await('SELECT citizenid, charinfo, job, metadata FROM players WHERE citizenid = ? LIMIT 1', { id })
end

-- return exports.oxmysql:executeSync('SELECT citizenid, charinfo, job FROM players WHERE citizenid = ? LIMIT 1', { id })
end

Expand Down Expand Up @@ -127,6 +127,13 @@ function GetPlayerLicenses(identifier)
local metadata = json.decode(result)
if metadata["licences"] ~= nil and metadata["licences"] then
return metadata["licences"]
else
return {
['driver'] = false,
['business'] = false,
['weapon'] = false,
['pilot'] = false
}
end
end
end
Expand Down Expand Up @@ -157,11 +164,19 @@ function ManageLicenses(identifier, incomingLicenses)
local Player = QBCore.Functions.GetPlayerByCitizenId(identifier)
if Player ~= nil then
Player.Functions.SetMetaData("licences", incomingLicenses)

else
local result = MySQL.scalar.await('SELECT metadata FROM players WHERE citizenid = @identifier', {['@identifier'] = identifier})
result = json.decode(result)
for k, v in pairs(result.licences) do

result.licences = result.licences or {
['driver'] = true,
['business'] = false,
['weapon'] = false,
['pilot'] = false
}

for k, _ in pairs(incomingLicenses) do
result.licences[k] = incomingLicenses[k]
end
MySQL.query.await('UPDATE `players` SET `metadata` = @metadata WHERE citizenid = @citizenid', {['@metadata'] = json.encode(result), ['@citizenid'] = identifier})
Expand Down
9 changes: 8 additions & 1 deletion server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ QBCore.Functions.CreateCallback('mdt:server:GetProfileData', function(source, cb
if type(target.charinfo) == 'string' then target.charinfo = json.decode(target.charinfo) end
if type(target.metadata) == 'string' then target.metadata = json.decode(target.metadata) end

local licencesdata = target.metadata['licences'] or {
['driver'] = false,
['business'] = false,
['weapon'] = false,
['pilot'] = false
}

local job, grade = UnpackJob(target.job)

local person = {
Expand All @@ -214,7 +221,7 @@ QBCore.Functions.CreateCallback('mdt:server:GetProfileData', function(source, cb
job = job.label,
grade = grade.name,
pp = ProfPic(target.charinfo.gender),
licences = target.metadata['licences'],
licences = licencesdata,,
dob = target.charinfo.birthdate,
mdtinfo = '',
fingerprint = '',
Expand Down
40 changes: 25 additions & 15 deletions ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,15 @@ $(document).ready(() => {
// convert key value pair object of licenses to array
let licenses = Object.entries(result.licences);

if (licenses.length == 0 || licenses.length == undefined) {
var licenseTypes = ['business', 'pilot', 'weapon', 'driver'];
licenses = Object.entries(licenseTypes.reduce((licenseType, licenseValue) => (licenseType[licenseValue] = false, licenseType), {}));
}

if (licenses.length > 0 && (PoliceJobs[playerJob] !== undefined || DojJobs[playerJob] !== undefined)) {
licencesHTML = '';
for (const [lic, hasLic] of licenses) {

let tagColour = hasLic == true ? "green-tag" : "red-tag";
licencesHTML += `<span class="license-tag ${tagColour} ${lic}" data-type="${lic}">${titleCase(lic)}</span>`;
}
Expand Down Expand Up @@ -307,7 +312,7 @@ $(document).ready(() => {
<div class="bulletin-item-title">${title}</div>
<div class="bulletin-item-info">${info}</div>
<div class="bulletin-bottom-info">
<div class="bulletin-date">${MyName} - ${timeAgo(
Number(time.getTime())
)}</div>
Expand Down Expand Up @@ -411,7 +416,7 @@ $(document).ready(() => {
let tags = new Array();
let gallery = new Array();
let licenses = {};

$(".tags-holder")
.find("div")
.each(function () {
Expand All @@ -438,16 +443,16 @@ $(document).ready(() => {
let description = $(".manage-profile-info").val();
let fingerprint = $(".manage-profile-fingerprint").val();
let id = $(".manage-profile-citizenid-input").val();

$(".licenses-holder")
.find("span")
.each(function(){
let type = $(this).data("type")
if ($(this).attr('class').includes('green-tag')){
licenses[type] = true
licenses[type] = true;
}
else{
licenses[type] = false
licenses[type] = false;
}
})

Expand Down Expand Up @@ -2574,7 +2579,7 @@ $(document).ready(() => {
return true;
}
$(".dmv-items").empty();

let vehicleHTML = "";

result.forEach((value) => {
Expand Down Expand Up @@ -2668,7 +2673,7 @@ $(document).ready(() => {
} else {
imageurl = newImageurl;
}

let code5 = false;
let code5tag = $(".vehicle-tags").find(".code5-tag");
if (code5tag.hasClass("green-tag")) {
Expand Down Expand Up @@ -3839,7 +3844,7 @@ $(document).ready(() => {
</div>
</div>`);
});

let policeCount = 0;
let emsCount = 0;
let dojCount = 0;
Expand Down Expand Up @@ -4077,15 +4082,15 @@ $(document).ready(() => {
if (value["doorCount"]) {
DispatchItem += `<div class="call-bottom-info"><span class="fas fa-door-open"></span>${value.doorCount}</div>`;
}

if (value["speed"]) {
DispatchItem += `<div class="call-bottom-info"><span class="fas fa-arrow-right"></span>${value.speed}</div>`;
}

if (value["weapon"]) {
DispatchItem += `<div class="call-bottom-info"><span class="fas fa-bullseye"></span>${value.weapon}</div>`;
}

if (value["heading"]) {
DispatchItem += `<div class="call-bottom-info"><span class="fas fa-share"></span>${value.heading}</div>`;
}
Expand Down Expand Up @@ -4386,7 +4391,7 @@ $(document).ready(() => {
);
$(".manage-incidents-title").css("width", "95%");
}

let associateddata = eventData.convictions;
$.each(associateddata, function (index, value) {
$(".associated-incidents-tags-holder").prepend(
Expand Down Expand Up @@ -4470,7 +4475,7 @@ $(document).ready(() => {
$(".fine-recommended-amount")
.filter("[data-id='" + value.cid + "']")
.val(value.recfine);

$(".sentence-recommended-amount")
.filter("[data-id='" + value.cid + "']")
.val(value.recsentence);
Expand Down Expand Up @@ -4991,6 +4996,11 @@ function searchProfilesResults(result) {
let licences = "";
let licArr = Object.entries(value.licences);

if (licArr.length == 0 || licArr.length == undefined) {
var licenseTypes = ['business', 'pilot', 'weapon', 'driver'];
licArr = Object.entries(licenseTypes.reduce((licenseType, licenseValue) => (licenseType[licenseValue] = false, licenseType), {}));
}

if (licArr.length > 0 && (PoliceJobs[playerJob] !== undefined || DojJobs[playerJob] !== undefined)) {
for (const [lic, hasLic] of licArr) {
let tagColour =
Expand All @@ -5011,7 +5021,7 @@ function searchProfilesResults(result) {
) {
convictions = "orange-tag";
}

if (value.pp == '') {
value.pp = 'img/not-found.jpg'
}
Expand Down

0 comments on commit 25e6599

Please sign in to comment.