Skip to content

Commit

Permalink
Feat(all): Garage Keys (Garage Sharing) p1
Browse files Browse the repository at this point in the history
part 1 of Garage Sharing (keys)

/garagekeys
args: give, manage
/garagekeys manage or give

This is WIP, use it only on testing enviroment.
  • Loading branch information
renzuzu committed Oct 26, 2021
1 parent a38b2bf commit 11609e9
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 5 deletions.
46 changes: 46 additions & 0 deletions client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3755,6 +3755,52 @@ RegisterCommand('impound', function(source, args, rawCommand)
end
end, false)

local garagekeysdata = nil

RegisterNUICallback("receive_garagekeys", function(data, cb)
SetNuiFocus(false,false)
garagekeysdata = data
end)

RegisterCommand('garagekeys', function(source, args, rawCommand)
ESX.TriggerServerCallback("renzu_garage:getgaragekeys",function(sharedkeys,players)
if Config.GarageKeys and PlayerData.job ~= nil then
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
local vehicle = GetNearestVehicleinPool(coords, 5)
local mykeys = {}
table.insert(mykeys,{identifier = 'own', name = 'My Owned key'})
if sharedkeys then
for k,v in pairs(sharedkeys) do
table.insert(mykeys,v)
end
end
SendNUIMessage(
{
data = {garages = garagecoord, mykeys = mykeys, action = args[1], players = players},
type = "garagekeys"
}
)
SetNuiFocus(true, true)
while garagekeysdata == nil do Wait(100) end
if garagekeysdata.action == 'give' then
TriggerServerEvent('renzu_garage:updategaragekeys',garagekeysdata.action,garagekeysdata.data)
TriggerEvent('renzu_notify:Notify', 'success','Garage', 'Garage Key has been share')
end
if garagekeysdata.action == 'del' then
TriggerServerEvent('renzu_garage:updategaragekeys',garagekeysdata.action,garagekeysdata.data.mygaragekeys)
LocalPlayer.state:set('garagekey', false, true)
TriggerEvent('renzu_notify:Notify', 'success','Garage', 'Garage Key has been Deleted')
end
if garagekeysdata.action == 'use' then
LocalPlayer.state:set('garagekey', garagekeysdata.data.mygaragekeys ~= 'own' and garagekeysdata.data.mygaragekeys or false, true)
TriggerEvent('renzu_notify:Notify', 'success','Garage', 'Garage Key has been changed')
end
garagekeysdata = nil
end
end)
end, false)

RegisterCommand('transfer', function(source, args, rawCommand)
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
Expand Down
2 changes: 1 addition & 1 deletion conf/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config.floatingtext = true -- use native floating text and marker to interact wi
Config.UsePopUI = false -- Create a Thread for checking playercoords and Use POPUI to Trigger Event, set this to false if using rayzone. Popui is originaly built in to RayZone -- DOWNLOAD https://github.com/renzuzu/renzu_popui
Config.Quickpick = true -- if false system will create a garage shell and spawn every vehicle you preview
Config.UniqueCarperGarage = false -- if false, show all vehicles to all garage location! else if true, Vehicles Saved in Garage A cannot be take out from Garage B for example.

Config.GarageKeys = true -- Enable Player to Give Public Garage Keys to enable vehicle sharing
-- BLIPS --
Config.BlipNamesStatic = true -- if true no more garage a garage b blip names from MAP , only says Garage
--GENERAL SETTING
Expand Down
18 changes: 18 additions & 0 deletions garage.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ CREATE TABLE IF NOT EXISTS `parking_meter` (
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

CREATE TABLE IF NOT EXISTS `garagekeys` (
`identifier` VARCHAR(64) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`keys` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`identifier`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

CREATE TABLE IF NOT EXISTS `vehiclekeys` (
`plate` VARCHAR(64) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`keys` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`plate`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
9 changes: 9 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ <h4 style="margin: 5px;margin-bottom: 10px;">Vehicle Mods</h4>
</div>
<!-- impound form -->

<div class="smart-wrap" id="garagekeys" style="display: none;">
<div class="smart-forms smart-container wrap-2">
<div class="form-header header-primary"><h4><i class="fa fa-pencil-square"></i>Garage Keys</h4></div><!-- end .form-header section -->
<div id="formid">


</div>
</div><!-- end .smart-forms section -->
</div><!-- end .smart-wrap section -->

<div class="smart-wrap" id="impoundform" style="display: none;">
<div class="smart-forms smart-container wrap-2">
Expand Down
125 changes: 125 additions & 0 deletions html/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,131 @@ window.addEventListener('message', function(event) {
}
document.getElementById("impoundform").style.display = 'block';
}
if (event.data.type == "garagekeys") {
var garages = event.data.data.garages
var mykeys = event.data.data.mykeys
var action = event.data.data.action
var playersnearby = event.data.data.players
$("#garages").html('')
$("#mygaragekeys").html('')
$("#formid").html('')
var managekey = `<form method="post" id="use" name="new_post" action="" class="wpcf7-form" enctype="mu ltipart/form-data">
<div class="form-body">
<div class="spacer-b30">
<div class="tagline"><span>Garage Data</span></div><!-- .tagline -->
</div>
<div class="section" style="display:none;">
<label class="field select">
<select id="garages" name="garages">
<option value="">Select Garage Location...</option>
</select>
<i class="arrow double"></i>
</label>
</div><!-- end section -->
<div class="section">
<label class="field select">
<select id="mygaragekeys" name="mygaragekeys">
<option value="">Select Keys</option>
</select>
<i class="arrow double"></i>
</label>
</div><!-- end section -->
</div><!-- end .form-body section -->
<div class="form-footer">
<button type="submit" class="button btn-primary" id="use_key"> Use </button>
<button type="submit" class="button btn-primary" style="background:red;" id="del_key"> Delete </button>
<button type="reset" class="button" id="cancel_keys"> Cancel </button>
</div><!-- end .form-footer section -->
</form>`
var givekey = `<form method="post" id="new_post" name="new_post" action="" class="wpcf7-form" enctype="mu ltipart/form-data">
<div class="form-body">
<div class="spacer-b30">
<div class="tagline"><span>Nearby Citizen</span></div><!-- .tagline -->
</div>
<div class="section">
<label class="field select">
<select id="playerslist" name="playerslist">
<option value="">Select Citizen</option>
</select>
<i class="arrow double"></i>
</label>
</div><!-- end section -->
<div class="section">
<label class="field select">
<select id="garages" name="garages">
<option value="">Select Garage</option>
</select>
<i class="arrow double"></i>
</label>
</div><!-- end section -->
</div><!-- end .form-body section -->
<div class="form-footer">
<button type="submit" class="button btn" style = "color:white; background-color: rgb(47, 109, 255);" id="give_key"> Give </button>
<button type="reset" class="button" id="cancel_keys"> Cancel </button>
</div><!-- end .form-footer section -->
</form>`
if (action == 'manage') {
$("#formid").append(managekey)
for (const i in garages) {
$("#garages").append(`<option value="`+garages[i].garage+`">Garage `+garages[i].garage+`</option>`)
}
for (const i in mykeys) {
$("#mygaragekeys").append(`<option value="`+mykeys[i].identifier+`">`+mykeys[i].name+` Key</option>`)
}
document.getElementById("use_key").addEventListener("click", function(event){
var givedata = {}
for (const i in $( "#use" ).serializeArray()) {
var data = $( "#use" ).serializeArray()
givedata[data[i].name] = data[i].value
}
document.getElementById("garagekeys").style.display = 'none';
$.post("https://renzu_garage/receive_garagekeys", JSON.stringify({ action: 'use', data: givedata}));
});
document.getElementById("del_key").addEventListener("click", function(event){
var givedata = {}
for (const i in $( "#use" ).serializeArray()) {
var data = $( "#use" ).serializeArray()
givedata[data[i].name] = data[i].value
}
document.getElementById("garagekeys").style.display = 'none';
$.post("https://renzu_garage/receive_garagekeys", JSON.stringify({ action: 'del', data: givedata}));
});

document.getElementById("cancel_keys").addEventListener("click", function(event) {
document.getElementById("garagekeys").style.display = 'none';
$.post("https://renzu_garage/receive_garagekeys", JSON.stringify({ garagekeysdata: 'cancel' }));
});
} else {
$("#formid").append(givekey)
for (const i in playersnearby) {
console.log(playersnearby[i],i,playersnearby[i].identifier)
$("#playerslist").append(`<option value="`+playersnearby[i].identifier+`">`+playersnearby[i].name+` - [`+playersnearby[i].source+`]</option>`)
}
for (const i in garages) {
$("#garages").append(`<option value="`+garages[i].garage+`">Garage `+garages[i].garage+` Key</option>`)
}
document.getElementById("give_key").addEventListener("click", function(event){
var datagive = {}
for (const i in $( "form" ).serializeArray()) {
var data = $( "form" ).serializeArray()
console.log($( "form" ).serializeArray(),data[i].name)
datagive[data[i].name] = data[i].value
console.log()
}
document.getElementById("garagekeys").style.display = 'none';
$.post("https://renzu_garage/receive_garagekeys", JSON.stringify({ action: 'give', data: datagive}));
});
document.getElementById("cancel_keys").addEventListener("click", function(event) {
document.getElementById("garagekeys").style.display = 'none';
$.post("https://renzu_garage/receive_garagekeys", JSON.stringify({ garagekeysdata: 'cancel' }));
});
}
document.getElementById("garagekeys").style.display = 'block';
}
if (event.data.type == "cats") {
var cats = event.data.cats
for (const i in cats) {
Expand Down
87 changes: 83 additions & 4 deletions server/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ RegisterServerEvent('renzu_garage:GetVehiclesTable')
AddEventHandler('renzu_garage:GetVehiclesTable', function()
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local identifier = xPlayer.identifier
local ply = Player(src).state
local identifier = ply.garagekey or xPlayer.identifier
--local Owned_Vehicle = MySQL.Sync.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner', {['@owner'] = xPlayer.identifier})
local Owned_Vehicle = MysqlGarage(Config.Mysql,'fetchAll','SELECT * FROM owned_vehicles WHERE owner = @owner', {['@owner'] = xPlayer.identifier})
local Owned_Vehicle = MysqlGarage(Config.Mysql,'fetchAll','SELECT * FROM owned_vehicles WHERE owner = @owner', {['@owner'] = identifier})
TriggerClientEvent("renzu_garage:receive_vehicles", src , Owned_Vehicle or {},vehicles or {})
end)

Expand Down Expand Up @@ -704,6 +705,82 @@ ESX.RegisterServerCallback('renzu_garage:isvehicleingarage', function (source, c
end
end)

ESX.RegisterServerCallback('renzu_garage:getgaragekeys', function (source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
local result = MysqlGarage(Config.Mysql,'fetchAll','SELECT * FROM garagekeys WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
})
local xPlayers = ESX.GetPlayers()
local players = {}
for i=1, #xPlayers, 1 do
local x = ESX.GetPlayerFromId(xPlayers[i])
if x.identifier ~= xPlayer.identifier then
table.insert(players,x)
end
end
cb(json.decode(result[1] ~= nil and result[1].keys) or false,players)
end)

RegisterNetEvent('renzu_garage:updategaragekeys')
AddEventHandler('renzu_garage:updategaragekeys', function(action,data)
if action == 'give' then
local xPlayer = ESX.GetPlayerFromIdentifier(data.playerslist)
local sender = ESX.GetPlayerFromId(source)
local result = MysqlGarage(Config.Mysql,'fetchAll','SELECT * FROM garagekeys WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
})

if result[1] then
local keys = json.decode(result[1].keys) or {}
local existkey = false
for k,v in ipairs(keys) do
if v.identifier == sender.identifier then
existkey = true
local newgarage = true
for k,garage in pairs(v.garages or {}) do
if garage == data.garages then
newgarage = false
end
end
if newgarage then
table.insert(v.garages, data.garages)
end
end
end
if not existkey then
table.insert(keys, {identifier = sender.identifier, name = sender.name, garages = {data.garages}})
end
MysqlGarage(Config.Mysql,'execute','UPDATE garagekeys SET `keys` = @keys WHERE identifier = @identifier', {
['@keys'] = json.encode(keys),
['@identifier'] = xPlayer.identifier,
})
else
result = {}
table.insert(result, {identifier = sender.identifier, name = sender.name, garages = {data.garages}})
MysqlGarage(Config.Mysql,'execute','INSERT INTO garagekeys (identifier, `keys`) VALUES (@identifier, @keys)', {
['@identifier'] = xPlayer.identifier,
['@keys'] = json.encode(result),
})
end
TriggerClientEvent('renzu_notify:Notify',xPlayer.source 'success','Garage', 'You receive a Garage Key from '..sender.name)
elseif action == 'del' then
local xPlayer = ESX.GetPlayerFromId(source)
local result = MysqlGarage(Config.Mysql,'fetchAll','SELECT * FROM garagekeys WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
})
local keys = json.decode(result[1] ~= nil and result[1].keys or '[]') or {}
for k,v in pairs(keys) do
if v.identifier == data then
keys[k] = nil
end
end
MysqlGarage(Config.Mysql,'execute','UPDATE garagekeys SET `keys` = @keys WHERE identifier = @identifier', {
['@keys'] = json.encode(keys),
['@identifier'] = xPlayer.identifier,
})
end
end)

RegisterServerEvent('renzu_garage:GetParkedVehicles')
AddEventHandler('renzu_garage:GetParkedVehicles', function()
TriggerClientEvent('renzu_garage:update_parked',source,parkedvehicles, false, parkmeter)
Expand Down Expand Up @@ -802,9 +879,11 @@ AddEventHandler('renzu_garage:changestate', function(plate,state,garage_id,model
local state = tonumber(state)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local ply = Player(source).state
local identifier = ply.garagekey or xPlayer.identifier
if xPlayer then
local result = MysqlGarage(Config.Mysql,'fetchAll','SELECT * FROM owned_vehicles WHERE owner = @owner and TRIM(UPPER(plate)) = @plate LIMIT 1', {
['@owner'] = xPlayer.identifier,
['@owner'] = identifier,
['@plate'] = plate
})
if #result > 0 and not string.find(garage_id, "impound") then
Expand All @@ -826,7 +905,7 @@ AddEventHandler('renzu_garage:changestate', function(plate,state,garage_id,model
['vehicle'] = json.encode(props),
['@garage_id'] = garage_id,
['@plate'] = plate:upper(),
['@owner'] = xPlayer.identifier,
['@owner'] = identifier,
['@stored'] = state,
['@isparked'] = 0
})
Expand Down

0 comments on commit 11609e9

Please sign in to comment.