Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ox_core): added support for groups #466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions client/framework/ox/main.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
if not Framework.Ox() then return end

-- [ Local Variables ] --

local Ox = require '@ox_core.lib.init'
local player = Ox.GetPlayer()

RegisterNetEvent("ox:setActiveCharacter", function(character)
if character.isNew then
return InitializeCharacter(Framework.GetGender(true))
end

InitAppearance()
end)
-- [ Local Functions ] --

---@todo groups
-- RegisterNetEvent("ox:setGroup", function(group, grade)
-- end)
local function onSetActiveCharacter(character)
if not character.isNew then InitAppearance() return end
InitializeCharacter(Framework.GetGender(true))
end

function Framework.GetPlayerGender()
return player.get('gender') == 'female' and 'Female' or 'Male'
local function getPlayerGroupGradeByGroupType(groupType)
local playerData = {charId = player.get('charId'), groups = player.getGroups(), groupType = groupType}
return lib.callback.await('illenium-appearance:server:ox:getGroupGradeByType', false, playerData)
end

-- [ Net Events ] --

RegisterNetEvent('ox:setActiveCharacter', onSetActiveCharacter)

-- [ Lib Client Callbacks ] --

lib.callback.register('illenium-appearance:client:ox:resetBlips', ResetBlips)

-- [ Global Functions ] --

function Framework.GetPlayerGender() return player.get('gender') == 'female' and 'Female' or 'Male' end

function Framework.UpdatePlayerData() end

function Framework.HasTracker() return false end

function Framework.CheckPlayerMeta()
return LocalPlayer.state.isDead or IsPedCuffed(cache.ped)
end
function Framework.CheckPlayerMeta() return LocalPlayer.state.isDead or IsPedCuffed(cache.ped) end

function Framework.IsPlayerAllowed(charId)
return charId == player.charId
end
function Framework.IsPlayerAllowed(charId) return charId == player.charId end

function Framework.GetRankInputValues() end
function Framework.GetRankInputValues(groupType) return lib.callback.await('illenium-appearance:server:ox:getGroupGrades', false, groupType) end

function Framework.GetJobGrade() end
function Framework.GetJobGrade() return getPlayerGroupGradeByGroupType('job') end

function Framework.GetGangGrade() end
function Framework.GetGangGrade() return getPlayerGroupGradeByGroupType('gang') end

function Framework.CachePed() end

function Framework.RestorePlayerArmour() end
function Framework.RestorePlayerArmour() end
94 changes: 80 additions & 14 deletions server/framework/ox/main.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,95 @@
if not Framework.Ox() then return end

-- [ Local Variables ] --

local Ox = require '@ox_core.lib.init'

function Framework.GetPlayerID(playerId)
return Ox.GetPlayer(playerId).charId
-- [ Local Functions ] --

---@param grades table
---@return table
local function formatGroupGrades(grades)
local tempGrades = {}
for i = 1, #grades do tempGrades[#tempGrades + 1] = {label = grades[i].label, value = i} end
if not #tempGrades >= 1 then return {} end
return tempGrades
end

function Framework.HasMoney(playerId, item, amount)
return exports.ox_inventory:GetItemCount(playerId, item) >= amount
---@param groupType string
---@return table
local function getGroupGrades(_, groupType)
local group = Ox.GetGroupsByType(groupType)
if not type(group) == 'table' then return {} end
return formatGroupGrades(group.grades)
end

function Framework.RemoveMoney(playerId, type, amount)
return exports.ox_inventory:RemoveItem(playerId, type, amount)
---@class PlayerData
---@field charId number
---@field groups table
---@field groupType string

---@param playerData PlayerData
---@return number
local function getGroupGradeByType(_, playerData)
for i = 1, #playerData.groups do
local group = playerData.groups[i]
if group.type == playerData.groupType then
local row = MySQL.single.await('SELECT `grade`, `isActive` FROM `character_groups` WHERE `name` = ? AND `charId` = ?', {group.name, playerData.charId})
if row and row.isActive then return row.grade end
end
end
return 0
end

function Framework.GetJob()
return ---@todo
---@param name string
---@param grade number
---@return table
local function fixGroupTable(name, grade)
return {name = name, grade = grade}
end

function Framework.GetGang()
return ---@todo
---@param playerId number
---@param groupType string
local function getGroupByType(playerId, groupType)
local player = Ox.GetPlayer(playerId)
local groups = player.getGroups()
for i = 1, #groups do
local group = groups[i]
if group.type == groupType then
local row = MySQL.single.await('SELECT `grade`, `isActive` FROM `character_groups` WHERE `name` = ? AND `charId` = ?', {group.name, player.charId})
if row and row.isActive then return fixGroupTable(group.name, row.grade) end
end
end
end

local function resetBlips(playerId) return lib.callback.await('illenium-appearance:client:ox:resetBlips', playerId) end

local function getPlayerId(playerId) return Ox.GetPlayer(playerId).charId end

-- [ Server Events ] --

AddEventHandler('ox:setActiveGroup', resetBlips)

AddEventHandler('ox:setGroup', resetBlips)

-- [ Lib Server Callbacks ] --

lib.callback.register('illenium-appearance:server:ox:getGroupGrades', getGroupGrades)

lib.callback.register('illenium-appearance:server:ox:getGroupGradeByType', getGroupGradeByType)

-- [ Global Functions ] --

function Framework.GetPlayerID(playerId) return getPlayerId(playerId) end

function Framework.HasMoney(playerId, item, amount) return exports.ox_inventory:GetItemCount(playerId, item) >= amount end

function Framework.RemoveMoney(playerId, type, amount) return exports.ox_inventory:RemoveItem(playerId, type, amount) end

function Framework.GetJob(playerId) return getGroupByType(playerId, 'job') end

function Framework.GetGang(playerId) return getGroupByType(playerId, 'gang') end

function Framework.SaveAppearance(appearance, charId)
Database.PlayerSkins.UpdateActiveField(charId, 0)
Database.PlayerSkins.DeleteByModel(charId, appearance.model)
Expand All @@ -30,7 +98,5 @@ end

function Framework.GetAppearance(charId, model)
local result = Database.PlayerSkins.GetByCitizenID(charId, model)
if result then
return json.decode(result)
end
end
if result then return json.decode(result) end
end