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

Implement item rewards #3

Merged
merged 1 commit into from
Feb 20, 2023
Merged
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
Implement item rewards
  • Loading branch information
Giana committed Feb 20, 2023
commit 398518a2e7bac897eebe37ebb752c22263e622a7
55 changes: 45 additions & 10 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Config.NpcRenderDistance = 60 -- Render distance for NPCs (if NPC enabled for
- 'itemName': Name of item
- sell_quantity: Amount to sell in increments of
- money_amount: Amount of money to reward per sell_quantity
- item_rewards:
- ['itemName'] (name of item) = itemAmount (amount of item to reward per sell_quantity)
- Example:
- ['sandwich'] = 2,
['joint'] = math.random(1, 2)
]]
Config.SellLocations = {
[1] = {
Expand Down Expand Up @@ -97,27 +102,45 @@ Config.SellLocations = {
sellable_items = {
['weed_white-widow'] = {
sell_quantity = 5,
money_amount = math.random(75, 120)
money_amount = math.random(75, 120),
item_rewards = {
['joint'] = math.random(1, 2)
}
},
['weed_skunk'] = {
sell_quantity = 5,
money_amount = math.random(75, 120)
money_amount = math.random(75, 120),
item_rewards = {
['joint'] = math.random(1, 2)
}
},
['weed_purple-haze'] = {
sell_quantity = 5,
money_amount = math.random(75, 120)
money_amount = math.random(75, 120),
item_rewards = {
['joint'] = math.random(1, 2)
}
},
['weed_og-kush'] = {
sell_quantity = 5,
money_amount = math.random(75, 120)
money_amount = math.random(75, 120),
item_rewards = {
['joint'] = math.random(1, 2)
}
},
['weed_amnesia'] = {
sell_quantity = 5,
money_amount = math.random(75, 120)
money_amount = math.random(75, 120),
item_rewards = {
['joint'] = math.random(1, 2),
}
},
['weed_brick'] = {
sell_quantity = 10,
money_amount = math.random(2000, 3000)
money_amount = math.random(2000, 3000),
item_rewards = {
['joint'] = math.random(1, 8),
}
}
}
},
Expand Down Expand Up @@ -162,7 +185,10 @@ Config.SellLocations = {
sellable_items = {
['meth'] = {
sell_quantity = 5,
money_amount = math.random(75, 120)
money_amount = math.random(75, 120),
item_rewards = {

}
}
}
},
Expand Down Expand Up @@ -207,7 +233,10 @@ Config.SellLocations = {
sellable_items = {
['crack_baggy'] = {
sell_quantity = 5,
money_amount = math.random(90, 170)
money_amount = math.random(90, 170),
item_rewards = {

}
}
}
},
Expand Down Expand Up @@ -252,11 +281,17 @@ Config.SellLocations = {
sellable_items = {
['cokebaggy'] = {
sell_quantity = 5,
money_amount = math.random(90, 185)
money_amount = math.random(90, 185),
item_rewards = {

}
},
['coke_brick'] = {
sell_quantity = 10,
money_amount = math.random(8500, 10000)
money_amount = math.random(8500, 10000),
item_rewards = {

}
}
}
}
Expand Down
43 changes: 36 additions & 7 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ function rollForNotifyingPolice(source, policeAlertChance)
end
end

function rewardItems(source, items, itemNotificationsEnabled)
local src = source
local player = QBCore.Functions.GetPlayer(src)
for k, v in pairs(items) do
player.Functions.AddItem(v.name, v.amount)
if itemNotificationsEnabled then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[v.name], 'add', v.amount)
end
end
end

-- Events --

RegisterNetEvent('g-drugselling:server:sellItems', function(sellLocationIndex, items, moneyType)
local src = source
local player = QBCore.Functions.GetPlayer(src)
local itemNotificationsEnabled = Config.SellLocations[sellLocationIndex].itemNotificationsEnabled
if CachedPolice[src] == nil then
DropPlayer(src, "Exploiting")
return
Expand All @@ -29,26 +41,33 @@ RegisterNetEvent('g-drugselling:server:sellItems', function(sellLocationIndex, i
for k, v in pairs(items) do
if player.Functions.RemoveItem(v.name, v.sellableQuantity) then
Citizen.Wait(900)
if not player.Functions.AddMoney(moneyType, v.price) then
if v.price > 0 and not player.Functions.AddMoney(moneyType, v.price) then
player.Functions.AddItem(v.name, v.sellableQuantity)
return
goto skipItem
end
if Config.SellLocations[sellLocationIndex].itemNotificationsEnabled then
if itemNotificationsEnabled then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[v.name], 'remove', v.sellableQuantity)
end
if v.rewardItems and #v.rewardItems > 0 then
rewardItems(src, v.rewardItems, itemNotificationsEnabled)
end
end
:: skipItem ::
end
rollForNotifyingPolice(src, Config.SellLocations[sellLocationIndex].policeAlertChance)
else
if player.Functions.RemoveItem(items[1].name, items[1].sellableQuantity) then
Citizen.Wait(800)
if not player.Functions.AddMoney(moneyType, items[1].price) then
Citizen.Wait(900)
if items[1].price > 0 and not player.Functions.AddMoney(moneyType, items[1].price) then
player.Functions.AddItem(items[1].name, items[1].sellableQuantity)
return
end
if Config.SellLocations[sellLocationIndex].itemNotificationsEnabled then
if itemNotificationsEnabled then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[items[1].name], 'remove', items[1].sellableQuantity)
end
if items[1].rewardItems and #items[1].rewardItems > 0 then
rewardItems(src, items[1].rewardItems, itemNotificationsEnabled)
end
end
rollForNotifyingPolice(src, Config.SellLocations[sellLocationIndex].policeAlertChance)
end
Expand All @@ -67,10 +86,20 @@ QBCore.Functions.CreateCallback('g-drugselling:server:getSellableItems', functio
local sellableQuantity = bundlesToSell * v.sell_quantity
local price = v.money_amount * bundlesToSell
local item = {}
local rewardItems = {}
item.name = k
item.sellableQuantity = sellableQuantity
item.price = price
table.insert(items, item)
for k2, v2 in pairs(v.item_rewards) do
local rewardItem = {}
rewardItem.name = k2
rewardItem.amount = v2 * bundlesToSell
table.insert(rewardItems, rewardItem)
end
item.rewardItems = rewardItems
if item.price > 0 or (item.price == 0 and (item.rewardItems and #item.rewardItems > 0)) then
table.insert(items, item)
end
end
end
cb(items)
Expand Down