Skip to content

Commit

Permalink
rewrite: adapt to new ui
Browse files Browse the repository at this point in the history
changes event names and callbacks to match new ui and remove old callbacks. still untested
  • Loading branch information
solareon committed Jun 26, 2024
1 parent 62f6b43 commit 7565e3d
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 200 deletions.
164 changes: 66 additions & 98 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
---@diagnostic disable: lowercase-global

local identifier = 'slrn_groups'
local groupStage -- Stores group stage
local finishedNotify -- store answer to leave group

-- Bridge
if GetResourceState('qbx_core') == 'started' then
function getPlayerData()
return QBX.PlayerData
return QBX.PlayerData.citizenid
end
else
local QBCore = exports['qb-core']:GetCoreObject()
Expand All @@ -29,8 +27,8 @@ CreateThread(function()
description = 'Group app to do stuff together',
developer = 'solareon',
defaultApp = true,
ui = 'slrn_groups/ui/index.html',
icon = 'https://cfx-nui-slrn_groups/ui/assets/icon.png'
ui = 'slrn_groups/web/dist/assets/index.html',
icon = 'https://cfx-nui-slrn_groups/web/dist/icon.png'
})
if not added then
print('Could not add app:', errorMessage)
Expand All @@ -46,7 +44,6 @@ CreateThread(function()
end)
end)

local inJob = false
local GroupBlips = {}

local function FindBlipByName(name)
Expand All @@ -67,26 +64,6 @@ RegisterNetEvent('groups:removeBlip', function(name)
end
end)

local function PhoneNotification(title, description, accept, deny)
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'PhoneNotification',
PhoneNotify = {
title = title,
text = description,
accept = accept,
deny = deny,
},
})
finishedNotify = nil
local timeout = 500
while not finishedNotify and timeout > 0 do
Wait(100)
timeout -= 1
end
if not finishedNotify then return false end
return finishedNotify == 'success' or false
end

RegisterNetEvent('groups:createBlip', function(name, data)
if not data then return print('Invalid Data was passed to the create blip event') end

Expand Down Expand Up @@ -130,112 +107,103 @@ RegisterNetEvent('groups:createBlip', function(name, data)
GroupBlips[#GroupBlips + 1] = { name = name, blip = blip }
end)

RegisterNUICallback('GetGroupsApp', function(_, cb)
lib.callback('slrn_groups:server:getAllGroups', false, function(getGroups)
cb(getGroups)
end)
RegisterNuiCallback('getPlayerData', function(_, cb)
local citizenId = getPlayerData()
local playerData = {
source = cache.serverId,
citizenId = citizenId
}
cb(playerData)
end)

RegisterNetEvent('slrn_groups:client:RefreshGroupsApp', function(Groups, finish)
if finish then inJob = false end
if inJob then return end
RegisterNuiCallback('getGroupData', function(_, cb)
cb({})
local groups, inGroup, groupStatus, groupStages = lib.callback.await('slrn_groups:server:getAllGroups')
if groups then
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'setGroups',
data = groups,
})
end
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'refreshApp',
data = Groups,
action = 'setInGroup',
data = inGroup and true or false
})
end)

RegisterNetEvent('slrn_groups:client:AddGroupStage', function(status, stage)
inJob = true
groupStage = stage
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'addGroupStage',
status = stage
action = 'setCurrentGroup',
data = inGroup or nil
})
end)

RegisterNUICallback('jobcenter_CreateJobGroup', function(data, cb)
TriggerServerEvent('slrn_groups:server:jobcenter_CreateJobGroup', data)
cb('ok')
end)

RegisterNUICallback('AnsweredNotify', function(data, cb)
finishedNotify = data.type
cb('ok')
end)

RegisterNUICallback('onStartup', function(data, cb)
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'LoadPhoneData',
PlayerData = getPlayerData(),
action = 'setGroupJobSteps',
data = groupStages or {}
})
if not inJob then
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'LoadJobCenterApp',
})
else
if groupStatus == 'IN_PROGRESS' then
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'addGroupStage',
status = groupStage
action = 'startJob',
data = {}
})
end
cb('ok')
end)

RegisterNUICallback('jobcenter_JoinTheGroup', function(data, cb)
local message = lib.callback.await('slrn_groups:server:jobcenter_JoinTheGroup')
RegisterNuiCallback('createGroup', function(data, cb)
TriggerServerEvent('slrn_groups:server:createGroup', data)
cb({})
end)

RegisterNuiCallback('joinGroup', function(data, cb)
local message = lib.callback.await('slrn_groups:server:joinGroup', data)

exports['lb-phone']:SendNotification({
app = identifier,
content = message,
})

cb('ok')
cb({})
end)

RegisterNetEvent('slrn_groups:client:CustomNotification', function(header, msg)
RegisterNuiCallback('leaveGroup', function(_, cb)
local message = lib.callback.await('slrn_groups:server:leaveGroup')

exports['lb-phone']:SendNotification({
app = identifier,
title = header,
content = msg,
content = message,
})
cb({})
end)

RegisterNUICallback('jobcenter_leave_grouped', function(_, cb)

local success = PhoneNotification('Job Center', 'Are you sure you want to leave the group?', 'Accept', 'Deny')

if success then
local message = lib.callback.await('slrn_groups:server:jobcenter_leave_grouped')

exports['lb-phone']:SendNotification({
app = identifier,
content = message,
})
end

cb('ok')
end)

RegisterNUICallback('jobcenter_DeleteGroup', function(data, cb)
local message = lib.callback.await('slrn_groups:server:jobcenter_DeleteGroup')
RegisterNuiCallback('deleteGroup', function(_, cb)
local message = lib.callback.await('slrn_groups:server:deleteGroup')

exports['lb-phone']:SendNotification({
app = identifier,
content = message,
})

cb('ok')
cb({})
end)

RegisterNUICallback('jobcenter_CheckPlayerNames', function(data, cb)
local groupNames = lib.callback.await('slrn_groups:server:jobcenter_CheckPlayerNames')

RegisterNUICallback('getMemberList', function(_, cb)
local groupNames = lib.callback.await('slrn_groups:server:getGroupMembers')
cb(groupNames)
end)

RegisterCommand('testpass', function()
RegisterNetEvent('slrn_groups:client:refreshGroups', function(groupData)
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'setGroups',
data = groupData,
})
end)

RegisterNetEvent('slrn_groups:client:updateGroupStage', function(_, stage)
groupStage = stage
exports['lb-phone']:SendCustomAppMessage(identifier, {
action = 'testPassword'
action = 'setGroupJobSteps',
data = stage
})
end, false)
end)

RegisterNetEvent('slrn_groups:client:CustomNotification', function(header, msg)
exports['lb-phone']:SendNotification({
app = identifier,
title = header,
content = msg,
})
end)
23 changes: 13 additions & 10 deletions server/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ function api.DestroyGroup(groupID)

table.remove(groups, groupID)

TriggerEvent('slrn_groups:server:GroupDeleted', groupID, groups:getGroupMembers())
TriggerClientEvent('slrn_groups:client:RefreshGroupsApp', -1, groups)
TriggerEvent('slrn_groups:server:GroupDeleted', groupID, group:getGroupMembers())
lib.TriggerClientEvent('slrn_groups:client:refreshGroups', -1, api.GetAllGroups())
end
utils.exportHandler('DestroyGroup', api.DestroyGroup)

Expand All @@ -177,7 +177,7 @@ function api.AddMember(groupID, source)

group:addMember(source)

TriggerClientEvent('slrn_groups:client:RefreshGroupsApp', -1, groups)
lib.TriggerClientEvent('slrn_groups:client:refreshGroups', -1, api.GetAllGroups())
end
utils.exportHandler('AddMember', api.AddMember)

Expand Down Expand Up @@ -232,7 +232,7 @@ function api.RemovePlayerFromGroup(source, groupID)
if member.Player == source then
table.remove(group.members, i)

TriggerClientEvent('slrn_groups:client:RefreshGroupsApp', -1, groups)
lib.TriggerClientEvent('slrn_groups:client:refreshGroups', -1, api.GetAllGroups())

-- There are no more members in the group, destroy it
if memberCount == 1 then
Expand Down Expand Up @@ -260,7 +260,7 @@ function api.setJobStatus(groupID, status, stages)
group.status = status
group.stage = stages

lib.triggerClientEvent('slrn_groups:client:AddGroupStage', group:getGroupMembers(), status, stages)
lib.triggerClientEvent('slrn_groups:client:updateGroupStage', group:getGroupMembers(), status, stages)
end
utils.exportHandler('setJobStatus', api.setJobStatus)

Expand Down Expand Up @@ -329,7 +329,7 @@ function api.GetAllGroups()
local data = {}

for i = 1, #groups do
data[i] = groups[i].getClientData()
data[i] = groups[i]:getClientData()
end

return data
Expand All @@ -346,15 +346,18 @@ function api.GetGroupMembersNames(groupId)
return lib.print.error('GetGroupMembersNames was sent an invalid groupID :'..groupId)
end

local names = {}
local members = {}
local amount = 0

for i = 1, #group.members do
amount += 1
names[amount] = group.members[i].name
local member = {}
member.name = group.members[i].name
member.Player = group.members[i].Player
members[amount] = member
end

return names
return members
end
utils.exportHandler('GetGroupMembersNames', api.GetGroupMembersNames)

Expand Down Expand Up @@ -395,7 +398,7 @@ function api.CreateGroup(src, name, password)
groups[id] = group

-- Send non-sensitive data to all clients (id, name, memberCount)
TriggerClientEvent('slrn_groups:client:RefreshGroupsApp', -1, group.getClientData())
lib.TriggerClientEvent('slrn_groups:client:refreshGroups', -1, api.GetAllGroups())

return id
end
Expand Down
4 changes: 2 additions & 2 deletions server/groups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function groups:refreshGroupStages()
if self.members[i] then
local source = self.members[i].Player

TriggerClientEvent('slrn_groups:client:AddGroupStage', source, self.status, self.stage)
TriggerClientEvent('slrn_groups:client:RefreshGroupsApp', source, self:getClientData(), true)
TriggerClientEvent('slrn_groups:client:updateGroupStage', source, self.status, self.stage)
TriggerClientEvent('slrn_groups:client:refreshGroups', source, self:getClientData(), true)
end
end
end
Expand Down
Loading

0 comments on commit 7565e3d

Please sign in to comment.