Skip to content

Commit

Permalink
wip: started to replace dynamic config with non config data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed Nov 8, 2023
1 parent e438690 commit cc8a863
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
7 changes: 4 additions & 3 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ local function takeCoral(coral)
}
}) then
Config.CoralLocations[currentDivingLocation.area].corals[coral].PickedUp = true
TriggerServerEvent('qb-diving:server:TakeCoral', currentDivingLocation.area, coral, true)
TriggerServerEvent('qb-diving:server:TakeCoral', currentDivingLocation.area, coral)
end

ClearPedTasks(cache.ped)
Expand Down Expand Up @@ -248,8 +248,9 @@ RegisterNetEvent('qb-diving:client:NewLocations', function()
setDivingLocation(area)
end)

RegisterNetEvent('qb-diving:client:UpdateCoral', function(area, coral, bool)
Config.CoralLocations[area].corals[coral].PickedUp = bool
RegisterNetEvent('qbx_diving:client:coralTaken', function(area, coralIndex)
--- TODO: remove zone at area, and coralIndex
Config.CoralLocations[area].corals[coralIndex].PickedUp = true
end)

RegisterNetEvent('qb-diving:client:CallCops', function(coords, msg)
Expand Down
33 changes: 18 additions & 15 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local currentDivingArea = math.random(1, #Config.CoralLocations)

--- Maybe table<integer, boolean>
---@type integer[]
local pickedUpCoralIndexes = {}

local function getItemPrice(amount, price)
for i = 1, #Config.PriceModifiers do
local modifier = Config.PriceModifiers[i]
Expand Down Expand Up @@ -63,30 +67,29 @@ RegisterNetEvent('qb-diving:server:SellCoral', function()
end
end)

local function getNewLocation()
local newLocation
repeat
newLocation = math.random(1, #Config.CoralLocations)
until newLocation ~= currentDivingArea
return newLocation
end

--- TODO: do not modify Config
RegisterNetEvent('qb-diving:server:TakeCoral', function(area, coral, bool)
RegisterNetEvent('qb-diving:server:TakeCoral', function(area, coralIndex)
local src = source
local coralType = Config.CoralTypes[math.random(1, #Config.CoralTypes)]
local amount = math.random(1, coralType.maxAmount)

exports.ox_inventory:AddItem(src, coralType.item, amount)
if Config.CoralLocations[area].maxHarvestAmount - 1 == 0 then
for _, v in pairs(Config.CoralLocations[currentDivingArea].corals) do
v.PickedUp = false
end
Config.CoralLocations[currentDivingArea].maxHarvestAmount = #Config.CoralLocations[currentDivingArea].corals
local newLocation = math.random(1, #Config.CoralLocations)
while newLocation == currentDivingArea do
newLocation = math.random(1, #Config.CoralLocations)
end
currentDivingArea = newLocation
pickedUpCoralIndexes[#pickedUpCoralIndexes+1] = coralIndex
if #pickedUpCoralIndexes == Config.CoralLocations[area].maxHarvestAmount then
pickedUpCoralIndexes = {}
currentDivingArea = getNewLocation()
TriggerClientEvent('qb-diving:client:NewLocations', -1)
else
Config.CoralLocations[area].corals[coral].PickedUp = bool
Config.CoralLocations[area].maxHarvestAmount -= 1
end

TriggerClientEvent('qb-diving:client:UpdateCoral', -1, area, coral, bool)
TriggerClientEvent('qbx_diving:client:coralTaken', -1, area, coralIndex)
end)

RegisterNetEvent('qb-diving:server:removeItemAfterFill', function()
Expand Down

0 comments on commit cc8a863

Please sign in to comment.