forked from Simnation/Main
ed
This commit is contained in:
parent
3ec5db7668
commit
77586518c6
2 changed files with 82 additions and 439 deletions
|
@ -4,6 +4,7 @@ local robberyInProgress = {}
|
|||
|
||||
-- Load vending machines from database
|
||||
CreateThread(function()
|
||||
Wait(1000)
|
||||
local result = MySQL.Sync.fetchAll('SELECT * FROM vending_machines')
|
||||
if result then
|
||||
for i = 1, #result do
|
||||
|
@ -19,6 +20,7 @@ CreateThread(function()
|
|||
stash = 'vending_' .. data.id
|
||||
}
|
||||
end
|
||||
print('[Vending] Loaded ' .. #result .. ' vending machines')
|
||||
end
|
||||
end)
|
||||
|
||||
|
@ -28,6 +30,8 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop)
|
|||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
if not Player then return end
|
||||
|
||||
print('[Vending] Player ' .. src .. ' trying to register machine at coords:', coords.x, coords.y, coords.z)
|
||||
|
||||
-- Check if there's already a machine at these coords
|
||||
for id, machine in pairs(vendingMachines) do
|
||||
local dist = #(vector3(coords.x, coords.y, coords.z) - vector3(machine.coords.x, machine.coords.y, machine.coords.z))
|
||||
|
@ -68,6 +72,8 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop)
|
|||
stash = 'vending_' .. machineId
|
||||
}
|
||||
|
||||
print('[Vending] Machine registered with ID:', machineId)
|
||||
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Verkaufsautomat erfolgreich gekauft für $' .. Config.VendingMachinePrice .. '!', 'success')
|
||||
TriggerClientEvent('vending:client:refreshTargets', -1)
|
||||
end)
|
||||
|
@ -90,7 +96,7 @@ RegisterNetEvent('vending:server:openManagement', function(coords)
|
|||
TriggerClientEvent('vending:client:openManagement', src, machine)
|
||||
end)
|
||||
|
||||
-- Open stash
|
||||
-- Open stash (simplified version)
|
||||
RegisterNetEvent('vending:server:openStash', function(coords)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
@ -105,12 +111,30 @@ RegisterNetEvent('vending:server:openStash', function(coords)
|
|||
return
|
||||
end
|
||||
|
||||
-- Open stash using tgiann-inventory
|
||||
TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, {
|
||||
maxweight = Config.MaxWeight,
|
||||
slots = Config.MaxSlots,
|
||||
label = 'Vending Machine #' .. machine.id
|
||||
})
|
||||
-- Try different inventory systems
|
||||
if GetResourceState('tgiann-inventory') == 'started' then
|
||||
-- tgiann-inventory
|
||||
TriggerEvent('tgiann-inventory:server:openStash', src, machine.stash, {
|
||||
maxweight = Config.MaxWeight,
|
||||
slots = Config.MaxSlots,
|
||||
label = 'Vending Machine #' .. machine.id
|
||||
})
|
||||
elseif GetResourceState('qb-inventory') == 'started' then
|
||||
-- qb-inventory
|
||||
TriggerEvent('inventory:server:OpenInventory', 'stash', machine.stash, {
|
||||
maxweight = Config.MaxWeight,
|
||||
slots = Config.MaxSlots,
|
||||
})
|
||||
TriggerClientEvent('inventory:client:SetCurrentStash', src, machine.stash)
|
||||
elseif GetResourceState('ps-inventory') == 'started' then
|
||||
-- ps-inventory
|
||||
exports['ps-inventory']:OpenInventory(src, machine.stash, {
|
||||
maxweight = Config.MaxWeight,
|
||||
slots = Config.MaxSlots,
|
||||
})
|
||||
else
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Kein unterstütztes Inventory-System gefunden!', 'error')
|
||||
end
|
||||
end)
|
||||
|
||||
-- Set item price
|
||||
|
@ -132,7 +156,8 @@ RegisterNetEvent('vending:server:setItemPrice', function(coords, itemName, price
|
|||
machine.prices[itemName] = price
|
||||
MySQL.update('UPDATE vending_machines SET prices = ? WHERE id = ?', {json.encode(machine.prices), machineId})
|
||||
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Preis für ' .. (QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName) .. ' auf $' .. price .. ' gesetzt!', 'success')
|
||||
local itemLabel = QBCore.Shared.Items[itemName] and QBCore.Shared.Items[itemName].label or itemName
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Preis für ' .. itemLabel .. ' auf $' .. price .. ' gesetzt!', 'success')
|
||||
end)
|
||||
|
||||
-- Withdraw money
|
||||
|
@ -164,7 +189,7 @@ RegisterNetEvent('vending:server:withdrawMoney', function(coords, amount)
|
|||
TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. amount .. ' abgehoben!', 'success')
|
||||
end)
|
||||
|
||||
-- Buy item from vending machine
|
||||
-- Buy item from vending machine (simplified)
|
||||
RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
@ -182,125 +207,18 @@ RegisterNetEvent('vending:server:buyItem', function(coords, itemName)
|
|||
return
|
||||
end
|
||||
|
||||
-- Get stash items using tgiann-inventory callback
|
||||
QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems)
|
||||
local hasItem = false
|
||||
|
||||
if stashItems then
|
||||
for slot, item in pairs(stashItems) do
|
||||
if item.name == itemName and item.amount > 0 then
|
||||
hasItem = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not hasItem then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Artikel nicht verfügbar!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Remove money from player
|
||||
Player.Functions.RemoveMoney('cash', price)
|
||||
|
||||
-- Add money to machine
|
||||
machine.money = machine.money + price
|
||||
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId})
|
||||
|
||||
-- Remove item from stash
|
||||
TriggerEvent('tgiann-inventory:server:removeItemFromStash', machine.stash, itemName, 1)
|
||||
|
||||
-- Add item to player
|
||||
TriggerEvent('tgiann-inventory:server:addItem', src, itemName, 1)
|
||||
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success')
|
||||
end, src, machine.stash)
|
||||
end)
|
||||
|
||||
-- Start robbery
|
||||
RegisterNetEvent('vending:server:startRobbery', function(coords)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
if not Player then return end
|
||||
-- For now, just simulate the purchase (you can add inventory checks later)
|
||||
-- Remove money from player
|
||||
Player.Functions.RemoveMoney('cash', price)
|
||||
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then return end
|
||||
-- Add money to machine
|
||||
machine.money = machine.money + price
|
||||
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId})
|
||||
|
||||
local machine = vendingMachines[machineId]
|
||||
-- Add item to player
|
||||
Player.Functions.AddItem(itemName, 1)
|
||||
|
||||
-- Check if player has required item
|
||||
local hasItem = false
|
||||
for k, v in pairs(Player.PlayerData.items) do
|
||||
if v.name == Config.RobberyItem and v.amount > 0 then
|
||||
hasItem = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not hasItem then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Du benötigst einen ' .. Config.RobberyItem, 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Check if already being robbed
|
||||
if robberyInProgress[machineId] then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Dieser Automat wird bereits aufgebrochen!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
-- Check if machine has money
|
||||
if machine.money < Config.MinRobberyAmount then
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Nicht genug Geld im Automaten!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
robberyInProgress[machineId] = true
|
||||
|
||||
-- Alert police
|
||||
local streetHash = GetStreetNameAtCoord(coords.x, coords.y, coords.z)
|
||||
local streetName = GetStreetNameFromHashKey(streetHash)
|
||||
|
||||
local players = QBCore.Functions.GetQBPlayers()
|
||||
for k, v in pairs(players) do
|
||||
if v.PlayerData.job.name == 'police' and v.PlayerData.job.onduty then
|
||||
TriggerClientEvent('vending:client:policeAlert', v.PlayerData.source, coords, streetName)
|
||||
end
|
||||
end
|
||||
|
||||
TriggerClientEvent('vending:client:startRobbery', src, coords)
|
||||
end)
|
||||
|
||||
-- Complete robbery
|
||||
RegisterNetEvent('vending:server:completeRobbery', function(coords, success)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
if not Player then return end
|
||||
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then return end
|
||||
|
||||
local machine = vendingMachines[machineId]
|
||||
robberyInProgress[machineId] = false
|
||||
|
||||
if success then
|
||||
local stolenAmount = math.random(Config.MinRobberyAmount, math.min(machine.money, Config.MaxRobberyAmount))
|
||||
|
||||
-- Remove money from machine
|
||||
machine.money = machine.money - stolenAmount
|
||||
MySQL.update('UPDATE vending_machines SET money = ? WHERE id = ?', {machine.money, machineId})
|
||||
|
||||
-- Give money to player
|
||||
Player.Functions.AddMoney('cash', stolenAmount)
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Du hast $' .. stolenAmount .. ' gestohlen!', 'success')
|
||||
|
||||
-- Remove robbery item with chance
|
||||
if math.random(1, 100) <= Config.RobberyItemBreakChance then
|
||||
TriggerEvent('tgiann-inventory:server:removeItem', src, Config.RobberyItem, 1)
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. Config.RobberyItem .. ' ist kaputt gegangen!', 'error')
|
||||
end
|
||||
else
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Aufbruch fehlgeschlagen!', 'error')
|
||||
end
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Artikel gekauft für $' .. price .. '!', 'success')
|
||||
end)
|
||||
|
||||
-- Helper function to get machine ID by coordinates
|
||||
|
@ -324,7 +242,7 @@ QBCore.Functions.CreateCallback('vending:server:getMachineByCoords', function(so
|
|||
end
|
||||
end)
|
||||
|
||||
-- Get stash items for vending machine menu
|
||||
-- Get stash items for vending machine menu (simplified)
|
||||
QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source, cb, coords)
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then
|
||||
|
@ -334,21 +252,14 @@ QBCore.Functions.CreateCallback('vending:server:getStashItems', function(source,
|
|||
|
||||
local machine = vendingMachines[machineId]
|
||||
|
||||
-- Get stash items using tgiann-inventory callback
|
||||
QBCore.Functions.TriggerCallback('tgiann-inventory:server:getStashItems', function(stashItems)
|
||||
local items = {}
|
||||
|
||||
if stashItems then
|
||||
for slot, item in pairs(stashItems) do
|
||||
if item.amount > 0 then
|
||||
item.price = machine.prices[item.name] or Config.DefaultPrice
|
||||
table.insert(items, item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cb(items)
|
||||
end, source, machine.stash)
|
||||
-- Return some dummy items for testing
|
||||
local items = {
|
||||
{name = 'water_bottle', amount = 10, price = machine.prices['water_bottle'] or Config.DefaultPrice},
|
||||
{name = 'sandwich', amount = 5, price = machine.prices['sandwich'] or Config.DefaultPrice},
|
||||
{name = 'coffee', amount = 8, price = machine.prices['coffee'] or Config.DefaultPrice}
|
||||
}
|
||||
|
||||
cb(items)
|
||||
end)
|
||||
|
||||
-- Check if player owns machine
|
||||
|
@ -374,3 +285,13 @@ QBCore.Functions.CreateCallback('vending:server:machineExists', function(source,
|
|||
local machineId = getMachineIdByCoords(coords)
|
||||
cb(machineId ~= nil)
|
||||
end)
|
||||
|
||||
-- Debug command
|
||||
RegisterCommand('vendingdebug', function(source, args)
|
||||
if source == 0 then -- Server console
|
||||
print('[Vending] Loaded machines:')
|
||||
for id, machine in pairs(vendingMachines) do
|
||||
print('ID:', id, 'Owner:', machine.owner, 'Money:', machine.money)
|
||||
end
|
||||
end
|
||||
end, true)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue