diff --git a/client.lua b/client.lua index aea533c..35850ee 100644 --- a/client.lua +++ b/client.lua @@ -1,10 +1,16 @@ - -local ESX, PlayerData, territoriesClient, Blips, iBlips, progress, isTaking = nil, {}, {}, {}, {}, 0, false +local ESX, PlayerData, territories, headerBlips, circleBlips, progress = nil, {}, {}, {}, {}, 0 CreateThread(function() - while ESX == nil do TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) Wait(250) end - while ESX.GetPlayerData().job == nil do Wait(250) end + ESX = exports['es_extended']:getSharedObject() PlayerData = ESX.GetPlayerData() + + ESX.TriggerServerCallback('tomic_territories:getTerritories', function(cb) + territories = cb + end) + + Wait(1000) -- Wait for the territories to load (in case you have a lot of them, this would be clever to keep here)... + + createBlips() end) RegisterNetEvent('esx:playerLoaded') @@ -17,41 +23,81 @@ AddEventHandler('esx:setJob', function(job) PlayerData.job = job end) -CreateThread(function() - ESX.TriggerServerCallback('tomic_territories:getTerritories', function(territories) - territoriesClient = territories - end) - Wait(1000) - createBlips() -end) +local function checkInput(input, typeRequired, checkEmpty) + checkEmpty = checkEmpty or false + if input == nil then return false end + if typeRequired == nil then return false end -RegisterNetEvent('tomic_territories:updateTerritories') -AddEventHandler('tomic_territories:updateTerritories', function(territories) - territoriesClient = territories - for i, blipovi in pairs(iBlips) do - RemoveBlip(blipovi) + if typeRequired == 'any' then return true end + if typeRequired == 'number' then input = tonumber(input) end + if typeRequired == 'string' then input = tostring(input) end + + if type(input) == 'table' then + for k, v in pairs(input) do + if type(v) ~= typeRequired or (checkEmpty and (v == '' or v == nil)) then return false end + end + else + if type(input) ~= typeRequired or (checkEmpty and (input == '' or input == nil)) then return false end end - iBlips = {} - for i, blipovix in pairs(Blips) do - RemoveBlip(blipovix) + + return true +end + +function createBlips() + for i = 1, #territories do + local territory = territories[i] + local circleBlip = AddBlipForCoord(territory.coords.x, territory.coords.y, territory.coords.z) + SetBlipSprite(circleBlip, 373) + SetBlipDisplay(circleBlip, 8) + SetBlipScale(circleBlip, 4.0) + SetBlipAlpha(circleBlip, 100) + SetBlipAsShortRange(circleBlip, true) + for k, v in pairs(shared.gangs) do + if territory.owner == k then + SetBlipColour(circleBlip, v.blipColour) + break + end + end + table.insert(circleBlips, circleBlip) + + -- ////////////////////////////////////////// -- + + local headerBlip = AddBlipForCoord(territory.coords.x, territory.coords.y, territory.coords.z + 15) + SetBlipSprite(headerBlip, 310) + SetBlipDisplay(headerBlip, 4) + BeginTextCommandSetBlipName('STRING') + AddTextComponentString(string.format('Territory: %s | Owner: %s', territory.name, territory.owner ~= 'noone' and territory.label or 'Free Territory!')) + EndTextCommandSetBlipName(headerBlip) + SetBlipScale(headerBlip, 0.75) + SetBlipColour(headerBlip, 0) + SetBlipAlpha(headerBlip, 250) + SetBlipAsShortRange(headerBlip, true) + table.insert(headerBlips, headerBlip) end - Blips = {} - Wait(1000) +end + +RegisterNetEvent('tomic_territories:updateTerritories') +AddEventHandler('tomic_territories:updateTerritories', function(cb) + territories = cb + for _, blip in pairs(circleBlips) do RemoveBlip(blip) end + for _, blip in pairs(headerBlips) do RemoveBlip(blip) end + circleBlips, headerBlips = {}, {} + Wait(1000) -- Wait for blips to disappear... createBlips() end) -RegisterNetEvent('tomic_territories:blipblink') -AddEventHandler('tomic_territories:blipblink', function(id, job, label) +RegisterNetEvent('tomic_territories:updateBlips') +AddEventHandler('tomic_territories:updateBlips', function(id, job, label) while true do Wait(1000) - for i, v in pairs(territoriesClient) do + for i, v in pairs(territories) do if v.id == id then if v.isTaking then for k, p in pairs(shared.gangs) do if v.owner == k then - SetBlipColour(iBlips[i], p.blipboja) + SetBlipColour(circleBlips[i], p.blipColour) Wait(1000) - SetBlipColour(iBlips[i], shared.gangs[job].blipboja) + SetBlipColour(circleBlips[i], shared.gangs[job].blipColour) end end else @@ -64,8 +110,6 @@ end) RegisterNetEvent('tomic_territories:createTerritory') AddEventHandler('tomic_territories:createTerritory', function() - local player = PlayerPedId() - local coords = GetEntityCoords(player) local input = lib.inputDialog('Create a new territory', { { type = 'input', label = 'Territory Name' }, { type = 'input', label = 'Radius' }, @@ -73,543 +117,279 @@ AddEventHandler('tomic_territories:createTerritory', function() { value = 'market', label = 'Market (Buying)' }, { value = 'dealer', label = 'Market (Selling)' }, { value = 'default', label = 'Default (Stash Only)' }, - }}, + } }, }) - if input then - if input[1] ~= nil and tonumber(input[2]) ~= nil and input[3] ~= nil then - local name = input[1] - local radius = input[2] - local type = input[3] - local territoryInfo = { - id = #territoriesClient +1, - name = name, - type = type, - owner = 'noone', - label = 'NoOne', - radius = radius, - isTaking = false, - progress = 0, - cooldown = false, - coords = coords, - } - TriggerServerEvent('tomic_territories:createTerritory', territoryInfo) - else - ESX.ShowNotification('You must fill all fields correctly!') - end - else - ESX.ShowNotification('You cancelled the creation of a new territory.') + + if not input then + return ESX.ShowNotification('Something went wrong!') + end + + if not checkInput({ input[1], input[3] }, 'any', true) or not checkInput(input[2], 'number') then + return ESX.ShowNotification('You must fill all fields correctly!') end + + TriggerServerEvent('tomic_territories:createTerritory', { + id = #territories + 1, + name = input[1], + type = input[3], + owner = 'noone', + label = 'NoOne', + radius = input[2], + isTaking = false, + progress = 0, + isCooldown = false, + coords = GetEntityCoords(PlayerPedId()), + }) end) RegisterNetEvent('tomic_territories:deleteTerritory') AddEventHandler('tomic_territories:deleteTerritory', function() - local player = PlayerPedId() - local coords = GetEntityCoords(player) local input = lib.inputDialog('Delete a territory', { { type = 'input', label = 'Territory name' }, }) - if input then - if input[1] ~= nil then - local name = input[1] - TriggerServerEvent('tomic_territories:deleteTerritory', name) - else - ESX.ShowNotification('You must fill all fields correctly!') - end - else - ESX.ShowNotification('You cancelled the creation of a new territory.') + + if not input then + return ESX.ShowNotification('Something went wrong!') end -end) -createBlips = function() - for i = 1, #territoriesClient, 1 do - local info = territoriesClient[i] - vlasnik = info.owner - label = info.label - pocetnoslovo = string.upper(string.sub(vlasnik, 1, 1)) - ostatak = string.sub(vlasnik, 2) - local blipovi = AddBlipForCoord(info.coords.x, info.coords.y, info.coords.z) -- T1 - SetBlipSprite(blipovi, 373) - SetBlipDisplay(blipovi, 8) - SetBlipScale(blipovi, 4.0) - for k, v in pairs(shared.gangs) do - if info.owner == k then - SetBlipColour(blipovi, v.blipboja) - end - end - SetBlipAlpha(blipovi, 100) - SetBlipAsShortRange(blipovi, true) - table.insert(iBlips, blipovi) - -- Kostur Blipovi // Skull Blips - local blipovix = AddBlipForCoord(info.coords.x, info.coords.y, info.coords.z + 15) -- T1 - SetBlipSprite(blipovix, 310) - SetBlipDisplay(blipovix, 4) - BeginTextCommandSetBlipName('STRING') - if info.owner ~= 'noone' then - AddTextComponentString('Territory: '..info.name..' | Owner: '..info.label..'') - else - AddTextComponentString('Territory: '..info.name..' | Free Territory!') - end - EndTextCommandSetBlipName(blipovix) - SetBlipScale(blipovix, 0.75) - SetBlipColour(blipovix, 0) - SetBlipAlpha(blipovix, 250) - SetBlipAsShortRange(blipovix, true) - table.insert(Blips, blipovix) + if not checkInput(input[1], 'any', true) then + return ESX.ShowNotification('You must fill all fields correctly!') end -end --- e.g | PomocniText('Press ~INPUT_CONTEXT~ to do something!') --- Good way to replace the default ESX Show Help Notification... -PomocniText = function(tekst) - SetTextComponentFormat('STRING') - AddTextComponentString(tekst) - DisplayHelpTextFromStringLabel(0, 0, 1, -1) -end + TriggerServerEvent('tomic_territories:deleteTerritory', input[1]) +end) CreateThread(function() - Wait(1000) + Wait(1000) -- Wait for everything to load before displaying the markers... local showUI while true do Wait(0) - local playerPed = PlayerPedId() - local spavaj = true - local playerCoords = GetEntityCoords(playerPed) - for k, v in pairs(territoriesClient) do + local sleepThread = true + local playerCoords = GetEntityCoords(PlayerPedId()) + for k, v in pairs(territories) do local coords = vec3(v.coords.x, v.coords.y, v.coords.z) - local coordsx = vec3(v.coords.x, v.coords.y, v.coords.z-17.0) local distance = #(playerCoords - coords) - if PlayerData.job and shared.gangs[PlayerData.job.name] then - if distance < tonumber(v.radius) then - spavaj = false - DrawMarker(2, coords, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.15, 0.15, 0.15, 200, 0, 50, 230, true, true, 2, true, false, false, false) - DrawMarker(1, coords, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.25, 0.25, 0.02, 255, 255, 255, 255, false, true, 1, true, false, false, false) - territorydata = { - id = v.id, - job = PlayerData.job.name, - label = PlayerData.job.label, - name = v.name, - currentOwner = v.owner, - crds = vec3(v.coords.x, v.coords.y, v.coords.z), - taking = v.isTaking, - cooldown = v.cooldown, - type = v.type, - spawnano = v.spawnano, - radi = v.radi - } - if distance < 1.5 then - if IsControlJustPressed(0, 38) then - TriggerEvent('tomic_territories:infoMeni', territorydata) - end - if showUI ~= 0 then - lib.showTextUI('[E] - Info | '..territorydata.name..'') - elseif showUI ~= 0 and distance > 2.0 then - showUI = nil - lib.hideTextUI() - end - elseif distance > 2.0 then + if PlayerData.job and shared.gangs[PlayerData.job.name] and distance <= tonumber(v.radius) then + sleepThread = false + DrawMarker(2, coords, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.15, 0.15, 0.15, 200, 0, 50, 230, true, true, 2, true, false, false, false) + DrawMarker(1, coords, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.25, 0.25, 0.02, 255, 255, 255, 255, false, true, 1, true, false, false, false) + if distance < 1.5 then + if IsControlJustPressed(0, 38) then + TriggerEvent('tomic_territories:infoMenu', { + id = v.id, + job = PlayerData.job.name, + label = PlayerData.job.label, + name = v.name, + currentOwner = v.owner, + terCoords = vec3(v.coords.x, v.coords.y, v.coords.z), + taking = v.isTaking, + cooldown = v.isCooldown, + type = v.type + }) + end + if showUI ~= 0 then + lib.showTextUI('[E] - Info | ' .. v.name .. '') + elseif showUI ~= 0 and distance > 2.0 then showUI = nil lib.hideTextUI() end + elseif distance > 2.0 then + showUI = nil + lib.hideTextUI() end end end - if spavaj then Wait(1337) end + if sleepThread then Wait(2000) end end end) -RegisterNetEvent('tomic_territories:infoMeni') -AddEventHandler('tomic_territories:infoMeni', function(territorydata) - if territorydata.type == 'dealer' then - lib.registerContext({ - id = 'infoMeniDealer', - title = 'Territory: '..territorydata.name..' | 🎲', - options = { - { - title = 'Capture the territory | 🚩', - event = 'tomic_territories:pokreni', - metadata = { - 'Press the button to start capturing territory '..territorydata.name..'!', - }, - args = { - x = territorydata - } - }, - { - title = 'Territory stash | πŸ“¦', - event = 'tomic_territories:openStash', - metadata = { - 'Open the stash from territory '..territorydata.name..'.', - }, - args = { - x = territorydata - } +RegisterNetEvent('tomic_territories:infoMenu') +AddEventHandler('tomic_territories:infoMenu', function(terData) + local defaultContext = { + id = 'infoMenu' .. terData.id, + title = 'Territory: ' .. terData.name .. ' | 🎲', + options = { + { + title = 'Capture the territory | 🚩', + event = 'tomic_territories:captureClient', + metadata = { + 'Press the button to start capturing territory ' .. terData.name .. '!', }, - { - title = 'Sell Shop | 🌿', - event = 'tomic_territories:listaItema', - args = { - data = territorydata - } + args = { + currentTerritory = terData + } + }, + { + title = 'Territory stash | πŸ“¦', + event = 'tomic_territories:openStash', + metadata = { + 'Open the stash from territory ' .. terData.name .. '.', }, + args = { + currentTerritory = terData + } } - }) - lib.showContext('infoMeniDealer') - elseif territorydata.type == 'market' then - lib.registerContext({ - id = 'infoMeniMarket', - title = 'Territory: '..territorydata.name..' | 🎲', - options = { - { - title = 'Capture the territory | 🚩', - event = 'tomic_territories:pokreni', - metadata = { - 'Press the button to start capturing territory '..territorydata.name..'!', - }, - args = { - x = territorydata - } - }, - { - title = 'Territory stash | πŸ“¦', - event = 'tomic_territories:openStash', - metadata = { - 'Open the stash from territory '..territorydata.name..'.', - }, - args = { - x = territorydata - } - }, - { - title = 'Buy Shop | πŸ›’', - event = 'tomic_territories:BuyList', - args = { - data = territorydata - } - }, + } + } + + if terData.type == 'dealer' then + defaultContext.options[#defaultContext.options + 1] = { + title = 'Sell Shop | 🌿', + event = 'tomic_territories:sellList', + args = { + data = terData } - }) - lib.showContext('infoMeniMarket') - -- Work in progress! - -- elseif territorydata.type == 'drugs' then - -- lib.registerContext({ - -- id = 'infoMeniDroga', - -- title = 'Territory: '..territorydata.name..' | 🎲', - -- options = { - -- { - -- title = 'Capture the territory | 🚩', - -- event = 'tomic_territories:pokreni', - -- metadata = { - -- 'Press the button to start capturing territory '..territorydata.name..'!', - -- }, - -- args = { - -- x = territorydata - -- } - -- }, - -- { - -- title = 'Territory stash | πŸ“¦', - -- event = 'tomic_territories:openStash', - -- metadata = { - -- 'Open the stash from territory '..territorydata.name..'.', - -- }, - -- args = { - -- x = territorydata - -- } - -- }, - -- { - -- title = 'Burrito Meth | πŸ›’', - -- event = 'tomic_territories:burritoMeth', - -- args = { - -- data = territorydata - -- } - -- }, - -- } - -- }) - -- lib.showContext('infoMeniDroga') - -- Work in progress! - elseif territorydata.type == 'default' then - lib.registerContext({ - id = 'infoMeni', - title = 'Territory: '..territorydata.name..' | 🎲', - options = { - { - title = 'Capture the territory | 🚩', - event = 'tomic_territories:pokreni', - metadata = { - 'Press the button to start capturing territory '..territorydata.name..'!', - }, - args = { - x = territorydata - } - }, - { - title = 'Territory stash | πŸ“¦', - event = 'tomic_territories:openStash', - metadata = { - 'Open the stash from territory '..territorydata.name..'.', - }, - args = { - x = territorydata - } - }, + } + elseif terData.type == 'market' then + defaultContext.options[#defaultContext.options + 1] = { + title = 'Buy Shop | πŸ›’', + event = 'tomic_territories:buyList', + args = { + data = terData } - }) - lib.showContext('infoMeni') + } end + + lib.registerContext(defaultContext) + lib.showContext(defaultContext.id) end) --- Work in progress! -RegisterNetEvent('tomic_territories:burritoMeth') -AddEventHandler('tomic_territories:burritoMeth', function(data) - infoX = data - if data.data.spawnano == false then - local spawnpoint = ESX.Game.IsSpawnPointClear(data.data.crds, 5.0) - local closestPlayer, closestPlayerDistance = ESX.Game.GetClosestPlayer() - local vozilo - if spawnpoint then - if closestPlayer == -1 or closestPlayerDistance > 3.0 then - ESX.ShowNotification('devTomic | There is no one close. You need one more person to start the mission!') - else - ESX.Game.SpawnVehicle('burrito', data.data.crds-4.0, 50.0, function(vehicle) - vozilo = vehicle - ESX.Game.SetVehicleProperties(vozilo, { - plate = 'METH', - dirtLevel = 0.0, - fuelLevel = 100.0, - color1 = 0, - color2 = 2, - modArmor = 4 - }) - end) - local radi = data.data.radi - local showUIx - while not radi do - Wait(0) - local closestVeh, distance = ESX.Game.GetClosestVehicle() - if distance < 7 then - if IsControlJustPressed(0, 38) then - TaskWarpPedIntoVehicle(PlayerPedId(), vozilo, 1) - TriggerServerEvent('tomic_territories:MethServer', infoX, vozilo) - break - end - if showUIx ~= 0 then - lib.showTextUI('[E] - To Enter!') - elseif showUIx ~= 0 and IsPedInAnyVehicle(PlayerPedId(), false) then - showUIx = nil - lib.hideTextUI() - end - elseif IsPedInAnyVehicle(PlayerPedId(), false) then - showUIx = nil - lib.hideTextUI() - end - end - end - else - ESX.ShowNotification('devTomic | Spawnpoint is not clear!') - end - else - ESX.ShowNotification('devTomic | Mission has already been started!') +RegisterNetEvent('tomic_territories:letCount') +AddEventHandler('tomic_territories:letCount', function(data) + local input = lib.inputDialog(data.selected.name, { 'Amount' }) + local count = input[1] + + if not input then + return ESX.ShowNotification('Something went wrong!') end -end) --- Work in progress! -RegisterNetEvent('tomic_territories:MethClient') -AddEventHandler('tomic_territories:MethClient', function(voziloInfo) - -- local gas = 0 - -- while IsEntityInAir(voziloInfo) and gas < 60 do - -- Wait(1000) - -- gas = gas + 1 - -- print(gas) - -- end - - exports.rprogress:Custom({ - Async = false, - canCancel = false, - cancelKey = 178, - x = 0.5, - y = 0.9, - From = 0, - To = 100, - Duration = shared.capturing * 60000 + 500, - Radius = 40, - Stroke = 3, - Cap = 'round', - Padding = 0, - MaxAngle = 360, - Rotation = 0, - Width = 300, - Height = 40, - ShowTimer = false, - ShowProgress = true, - Easing = 'easeLinear', - Label = 'Cooking meth...', - LabelPosition = 'left', - Color = 'rgba(255, 255, 255, 1.0)', - BGColor = 'rgba(0, 0, 0, 0.4)', - ZoneColor = 'rgba(51, 105, 30, 1)', - DisableControls = { - Mouse = true, - Player = true, - Vehicle = true - }, - onStart = function() - - end, - onComplete = function(cancelled) - while true do - Wait(2000) - local uvozilu = IsPedInAnyVehicle(PlayerPedId(), true) - if cancelled then - exports.rprogress:Stop() - else - exports.rprogress:Stop() - end - end - end - }) -end) + if not checkInput(count, 'number', true) then + return ESX.ShowNotification('You must fill all fields correctly!') + end -RegisterNetEvent('tomic_territories:listaItema') -AddEventHandler('tomic_territories:listaItema', function(data) - local itemList = {} - if PlayerData.job.name == data.data.currentOwner then - for k, v in pairs(shared.itemsToSell) do - local currentItemInfo = shared.itemsToSell - local item = k - local label = v.label - local price = v.worth - local black = v.black - itemList[item] = { - title = v.label, - description = 'πŸ’Έ | Worth: $'..v.worth, - event = 'tomic_territories:letCount', - args = { - selected = { - name = label, - key = item, - worth = price, - moneytype = black - }, - datax = data - } - } - end - lib.registerContext({ - id = 'itemListaX', - title = 'devTomic | Sellable items', - options = itemList, - menu = 'infoMeniDealer' - }) - lib.showContext('itemListaX') - else - ESX.ShowNotification('devTomic | You do not own this territory!') + if count < 1 then + return ESX.ShowNotification('devTomic | Amount cannot be lower than 1!') + end + + local itemObject = { + itemKey = data.selected.key, + itemName = data.selected.name, + itemCount = count, + itemWorth = data.selected.worth, + itemCurrency = data.selected.currency + } + + if data.selected.type == 'buy' then + TriggerServerEvent('tomic_territories:buyMarket', itemObject) + elseif data.selected.type == 'sell' then + TriggerServerEvent('tomic_territories:sellDealer', itemObject) end end) -RegisterNetEvent('tomic_territories:letCount') -AddEventHandler('tomic_territories:letCount', function(selected, datax) - local input = lib.inputDialog(selected.selected.name, {'Amount'}) - if input then - local count = tonumber(input[1]) - if count < 1 then - return ESX.ShowNotification('devTomic | Amount cannot be lower than 1!') - else - allInfo = { - i = selected.selected.key, - ime = selected.selected.name, - xCount = count, - xWorth = selected.selected.worth, - xType = selected.selected.moneytype +RegisterNetEvent('tomic_territories:sellList') +AddEventHandler('tomic_territories:sellList', function(args) + if PlayerData.job.name ~= args.data.currentOwner then + return ESX.ShowNotification('devTomic | You do not own this territory!') + end + + local itemList = {} + for k, v in pairs(shared.itemsToSell) do + local item, label, price, black = k, v.label, v.worth, v.black + itemList[item] = { + title = v.label, + description = 'πŸ’Έ | Worth: $' .. v.worth, + event = 'tomic_territories:letCount', + args = { + selected = { + name = label, + key = item, + worth = price, + currency = black, + type = 'sell' + }, } - TriggerServerEvent('tomic_territories:sellDealer', allInfo) - end + } end + + lib.registerContext({ + id = 'territorySellList', + title = 'devTomic | Sellable items', + options = itemList, + menu = 'TerritoryDealer' + }) + lib.showContext('territorySellList') end) -RegisterNetEvent('tomic_territories:BuyList') -AddEventHandler('tomic_territories:BuyList', function(data) +RegisterNetEvent('tomic_territories:buyList') +AddEventHandler('tomic_territories:buyList', function(terData) local buyList = {} - if PlayerData.job.name == data.data.currentOwner then - for k, v in pairs(shared.itemsToBuy) do - local item = k - local label = v.label - local price = v.worth - local black = v.black - buyList[item] = { - title = v.label, - description = 'πŸ’Έ | Price: $'..v.worth, - event = 'tomic_territories:buyCount', - args = { - selected = { - name = label, - key = item, - worth = price, - moneytype = black - }, - datax = data - } - } - end - lib.registerContext({ - id = 'buyList', - title = 'devTomic | Buyable items', - options = buyList, - menu = 'infoMeniMarket' - }) - lib.showContext('buyList') - else - ESX.ShowNotification('devTomic | You do not own this territory!') + terData = terData.data + if PlayerData.job.name ~= terData.currentOwner then + return ESX.ShowNotification('devTomic | You do not own this territory!') end -end) -RegisterNetEvent('tomic_territories:buyCount') -AddEventHandler('tomic_territories:buyCount', function(selected, datax) - local input = lib.inputDialog(selected.selected.name, {'Amount'}) - if input then - local count = tonumber(input[1]) - if count < 1 then - return ESX.ShowNotification('devTomic | Amount cannot be lower than 1!') - else - allInfo = { - i = selected.selected.key, - ime = selected.selected.name, - xCount = count, - xWorth = selected.selected.worth, - xType = selected.selected.moneytype + for k, v in pairs(shared.itemsToBuy) do + local item = k + local label = v.label + local price = v.worth + local black = v.black + buyList[item] = { + title = v.label, + description = 'πŸ’Έ | Price: $' .. v.worth, + event = 'tomic_territories:letCount', + args = { + selected = { + name = label, + key = item, + worth = price, + currency = black, + type = 'buy' + }, } - TriggerServerEvent('tomic_territories:buyMarket', allInfo) - end + } end + + lib.registerContext({ + id = 'territoryBuyList', + title = 'devTomic | Buyable items', + options = buyList, + menu = 'TerritoryMarket' + }) + lib.showContext('territoryBuyList') end) -RegisterNetEvent('tomic_territories:pokreni') -AddEventHandler('tomic_territories:pokreni', function(x) - if territorydata.job ~= territorydata.currentOwner then - if territorydata.taking == false then - if territorydata.cooldown == false then - TriggerServerEvent('tomic_territories:capturestart', territorydata.id, territorydata.job, territorydata.label, territorydata.name, territorydata.currentOwner) - else - ESX.ShowNotification('devTomic | This territory was recently captured, or capture was attempted!') - end - else - ESX.ShowNotification('devTomic | Someone is already taking the territory!') - end - else - ESX.ShowNotification('devTomic | This territory already belongs to you!') +RegisterNetEvent('tomic_territories:captureClient') +AddEventHandler('tomic_territories:captureClient', function(terData) + terData = terData.currentTerritory + if terData.job == terData.currentOwner then + return ESX.ShowNotification('devTomic | This territory already belongs to you!') + end + + if terData.taking == true then + return ESX.ShowNotification('devTomic | Someone is already taking the territory!') end + + if terData.cooldown == true then + return ESX.ShowNotification('devTomic | This territory was recently captured, or capture was attempted!') + end + + TriggerServerEvent('tomic_territories:captureServer', terData.id, terData.job, terData.label, terData.name, terData.currentOwner) end) RegisterNetEvent('tomic_territories:openStash') -AddEventHandler('tomic_territories:openStash', function(x) - local igrac = PlayerPedId() - local kordinateigraca = GetEntityCoords(igrac) - local distanca = #(territorydata.crds - kordinateigraca) - if distanca < 5.0 then - if PlayerData.job and PlayerData.job.name == territorydata.currentOwner then - exports.ox_inventory:openInventory('stash', {id = 'devTomic-Ter['..territorydata.name..']['..territorydata.id..']'}) - else - ESX.ShowNotification('devTomic | You do not own this territory!') - end +AddEventHandler('tomic_territories:openStash', function(terData) + terData = terData.currentTerritory + local distance = #(terData.terCoords - GetEntityCoords(PlayerPedId())) + + if PlayerData.job and PlayerData.job.name ~= terData.currentOwner then + return ESX.ShowNotification('devTomic | You do not own this territory!') + end + + if distance > 3.0 then + return ESX.ShowNotification('devTomic | You are too far away from the territory!') end + + exports.ox_inventory:openInventory('stash', { id = 'devTomic-Ter[' .. terData.name .. '][' .. terData.id .. ']' }) end) RegisterNetEvent('tomic_territories:progressBars') @@ -645,46 +425,38 @@ AddEventHandler('tomic_territories:progressBars', function(type) Player = false, Vehicle = false }, - onStart = function() - end, onComplete = function(cancelled) exports.rprogress:Stop() end - } - ) + }) elseif type == 'stop' then exports.rprogress:Stop() end end) -RegisterNetEvent('tomic_territories:captureprogress') -AddEventHandler('tomic_territories:captureprogress', function(datakey, data) +RegisterNetEvent('tomic_territories:captureProgress') +AddEventHandler('tomic_territories:captureProgress', function(terKey, terData) + local lastTerritory = terKey while true do Wait(0) local playerPed = PlayerPedId() local playerCoords = GetEntityCoords(playerPed) - local coords = vec3(data.coords.x, data.coords.y, data.coords.z) + local coords = vec3(terData.coords.x, terData.coords.y, terData.coords.z) local distance = #(playerCoords - coords) local isDead = IsPedDeadOrDying(playerPed, true) - lastTerritory = datakey - prekidanje = data.radius + 1.0 - if distance < prekidanje then + + if distance < terData.radius then if not isDead then - if data.isTaking == true then + if terData.isTaking then if progress < 60 then TriggerEvent('tomic_territories:progressBars', 'start') Wait(shared.capturing * 60000 / 60) progress = progress + 1 else TriggerEvent('tomic_territories:progressBars', 'stop') - job = PlayerData.job.name - label = PlayerData.job.label - prvivlasnik = data.owner - TriggerServerEvent('tomic_territories:capturecomplete', data.id, job, label, prvivlasnik) + TriggerServerEvent('tomic_territories:captureComplete', terData.id, PlayerData.job.name, PlayerData.job.label, terData.owner) progress = 0 - TriggerEvent('tomic_territories:progressBars', 'stop') - ESX.ShowNotification('devTomic | You have successfully captured '..data.name..'!') - TriggerEvent('tomic_territories:progressBars', 'stop') + ESX.ShowNotification('devTomic | You have successfully captured ' .. terData.name .. '!') break end else @@ -694,7 +466,7 @@ AddEventHandler('tomic_territories:captureprogress', function(datakey, data) TriggerEvent('tomic_territories:progressBars', 'stop') ESX.ShowNotification('devTomic | You died, the capturing progress has stopped!') progress = 0 - TriggerServerEvent('tomic_territories:captureend', lastTerritory) + TriggerServerEvent('tomic_territories:endCapturing', lastTerritory) lastTerritory = nil break end @@ -702,128 +474,115 @@ AddEventHandler('tomic_territories:captureprogress', function(datakey, data) TriggerEvent('tomic_territories:progressBars', 'stop') ESX.ShowNotification('devTomic | You left the territory, capturing progress has stopped!') progress = 0 - TriggerServerEvent('tomic_territories:captureend', lastTerritory) + TriggerServerEvent('tomic_territories:endCapturing', lastTerritory) lastTerritory = nil break end end end) -RegisterCommand('territories', function() - if shared.rankings then - lib.registerContext({ - id = 'prvastrana', - title = 'Teritorije Meni | 🎲', - options = { - { - title = 'Territory List | 🚩', - event = 'tomic_territories:drugastrana', - metadata = { - 'List of territories.', - }, - }, - { - title = 'Rank List | πŸ†', - event = 'tomic_territories:trecastrana', - metadata = { - 'Show the list of every gang and their All-Time points...', - }, - }, - { - title = 'Info | ❓', - metadata = { - '| Made by TomiΔ‡ βœ…', - }, - }, +RegisterCommand('territories', function(source, args, rawCommand) + local homePage = { + id = 'homePage', + title = 'Territories | 🎲', + options = { + { + title = 'Territory List | 🚩', + event = 'tomic_territories:listTerritories', + metadata = { + 'List of territories.' + } + }, + { + title = 'Info | ❓', + metadata = { + '| Made by TomiΔ‡ βœ…' + } } - }) - else - lib.registerContext({ - id = 'prvastrana', - title = 'Teritorije Meni | 🎲', - options = { - { - title = 'Territory List | 🚩', - event = 'tomic_territories:drugastrana', - metadata = { - 'List of territories.', - }, - }, - { - title = 'Info | ❓', - metadata = { - '| Made by TomiΔ‡ βœ…', - }, - }, + } + } + + if shared.rankings then + homePage.options[#homePage.options + 1] = { + title = 'Rank List | πŸ†', + event = 'tomic_territories:listRankings', + metadata = { + 'Show the list of every gang and their All-Time points...' } - }) + } end - lib.showContext('prvastrana') -end) -RegisterNetEvent('tomic_territories:drugastrana') -AddEventHandler('tomic_territories:drugastrana', function(args) - local libTer = {} - if territoriesClient ~= nil then - for i = 1, #territoriesClient, 1 do - local info = territoriesClient[i] - local ter = vec3(info.coords.x, info.coords.y, info.coords.z) - local pr = nil - local cd = nil - local vl = info.owner - local ps = string.upper(string.sub(vl, 1, 1)) - local os = string.sub(vl, 2) - if info.isTaking == true then pr = 'Yes' else pr = 'No' end - if info.cooldown == true then cd = 'Yes' else cd = 'No' end - libTer[i] = { - title = 'πŸ’€ | Territory: '..info.name, - description = '🚩 | Owner: '..info.label, + lib.registerContext(homePage) + lib.showContext(homePage.id) +end, false) + +RegisterNetEvent('tomic_territories:listTerritories') +AddEventHandler('tomic_territories:listTerritories', function() + local terCollection = {} + local territoryStatuses = { + ['isCooldown'] = nil, + ['isTaking'] = nil + } + + if territories ~= nil then + for i = 1, #territories, 1 do + local info = territories[i] + if info.isTaking == true then territoryStatuses.isTaking = 'Yes' else territoryStatuses.isTaking = 'No' end + if info.isCooldown == true then territoryStatuses.isCooldown = 'Yes' else territoryStatuses.isCooldown = 'No' end + terCollection[i] = { + title = 'πŸ’€ | Territory: ' .. info.name, + description = '🚩 | Owner: ' .. info.label, metadata = { - 'Capturing: '..pr, - 'Cooldown: '..cd, + 'Capturing: ' .. territoryStatuses.isTaking, + 'Cooldown: ' .. territoryStatuses.isCooldown, }, } end + lib.registerContext({ - id = 'drugastrana', + id = 'listTerritories', title = 'devTomic | Territory List', - menu = 'prvastrana', - options = libTer, + menu = 'homePage', + options = terCollection, }) - lib.showContext('drugastrana') + lib.showContext('listTerritories') end end) -RegisterNetEvent('tomic_territories:trecastrana') -AddEventHandler('tomic_territories:trecastrana', function(args) - ESX.TriggerServerCallback('tomic_territories:povucipoene', function(lista) - local trTabela = {} - if territoriesClient ~= nil then - for i = 1, #lista, 1 do - table.sort(lista, function(a, b) return a.poeni > b.poeni end) - trTabela[i] = { - title = 'πŸ’€ | Gang: '..lista[i].label, - description = 'πŸ† | Position: '..i, - metadata = { - '⭐ | All-Time Points: '..lista[i].poeni, - '⭐ | Monthly Points: '..lista[i].mespoeni, - '⭐ | Weekly Points: '..lista[i].nedpoeni +if shared.rankings then + RegisterNetEvent('tomic_territories:listRankings') + AddEventHandler('tomic_territories:listRankings', function() + ESX.TriggerServerCallback('tomic_territories:fetchPoints', function(pointsCollection) + local rankCollection = {} + if territories ~= nil then + for i = 1, #pointsCollection, 1 do + table.sort(pointsCollection, function(a, b) return a.totalPoints > b.totalPoints end) + rankCollection[i] = { + title = 'πŸ’€ | Gang: ' .. pointsCollection[i].label, + description = 'πŸ† | Position: ' .. i, + metadata = { + '⭐ | All-Time Points: ' .. pointsCollection[i].totalPoints, + '⭐ | Monthly Points: ' .. pointsCollection[i].monthlyPoints, + '⭐ | Weekly Points: ' .. pointsCollection[i].weeklyPoints + } } - } + end + + lib.registerContext({ + id = 'listRankings', + menu = 'homePage', + title = 'devTomic | Rank List', + options = rankCollection, + }) + lib.showContext('listRankings') end - lib.registerContext({ - id = 'trecastrana', - menu = 'prvastrana', - title = 'devTomic | Rank List', - options = trTabela, - }) - lib.showContext('trecastrana') - end + end) end) -end) +end AddEventHandler('onResourceStop', function(resourceName) - if resourceName == GetCurrentResourceName() then - TriggerEvent('tomic_territories:progressBars', 'stop') - end -end) \ No newline at end of file + if resourceName == GetCurrentResourceName() then + TriggerEvent('tomic_territories:progressBars', 'stop') + progress = 0 + end +end) diff --git a/fxmanifest.lua b/fxmanifest.lua index 8127158..f9429f4 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -1,13 +1,12 @@ -- Made by @TomiΔ‡#9076 - fx_version 'cerulean' -games { 'gta5' } +game 'gta5' lua54 'yes' -author "devTomic (tomiichx)" -description "Territory System for Gangs" +author 'tomiichx' +description 'Territory System for Gangs' -version 'TomiΔ‡ Development | v1.0.0' +version 'v2.0' shared_scripts { '@es_extended/imports.lua', @@ -15,21 +14,9 @@ shared_scripts { 'shared.lua' } -client_scripts { - 'client.lua' -} +client_script 'client.lua' -server_scripts { +server_scripts { '@oxmysql/lib/MySQL.lua', - '@es_extended/locale.lua', - 'server.lua', -} - ---[[ -β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ•—β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘ -β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— -β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β–ˆβ–ˆβ–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β•šβ•β• -β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•— -β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–‘β•šβ•β•β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• -β–‘β–‘β–‘β•šβ•β•β–‘β–‘β–‘β–‘β•šβ•β•β•β•β•β–‘β•šβ•β•β–‘β–‘β–‘β–‘β–‘β•šβ•β•β•šβ•β•β–‘β•šβ•β•β•β•β•β–‘ -]] + 'server.lua' +} \ No newline at end of file diff --git a/territories.sql b/install.sql similarity index 84% rename from territories.sql rename to install.sql index de67ede..60e755f 100644 --- a/territories.sql +++ b/install.sql @@ -1,22 +1,24 @@ -CREATE TABLE IF NOT EXISTS `tomic_territories` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` text DEFAULT 'NewTerritory', - `owner` text DEFAULT 'noone', - `label` text DEFAULT 'NoOne', - `radius` int(11) DEFAULT 50, - `coords` longtext NOT NULL, - `type` text DEFAULT 'default', - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8mb4; - -INSERT IGNORE INTO `tomic_territories` (`id`, `name`, `owner`, `label`, `radius`, `coords`, `type`) VALUES - (1, 'Motel', 'gsf', 'GSF', 60, '{"z":33.239501953125,"y":3574.15380859375,"x":1567.4241943359376}', 'dealer'), - (2, 'Docks', 'noone', 'NoOne', 65, '{"x":1011.2307739257813,"z":39.15380859375,"y":-2867.156005859375}', 'market'), - (3, 'Plaza', 'noone', 'NoOne', 50, '{"x":-1838.6373291015626,"z":13.0029296875,"y":-1223.5911865234376}', 'dealer'), - (4, 'Galileo', 'noone', 'NoOne', 55, '{"z":327.6732177734375,"y":1110.7120361328126,"x":-429.21759033203127}', 'dealer'), - (5, 'Dump', 'noone', 'NoOne', 80, '{"z":19.13623046875,"x":-533.7362670898438,"y":-1682.756103515625}', 'market'); - -ALTER TABLE `jobs` - ADD `nedpoeni` INT(11) DEFAULT 0, - ADD `mespoeni` INT(11) DEFAULT 0, - ADD `poeni` INT(11) DEFAULT 0; +-- Fresh install in case this script hasn't been used previously on your server. + +CREATE TABLE IF NOT EXISTS `tomic_territories` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` text DEFAULT 'NewTerritory', + `owner` text DEFAULT 'noone', + `label` text DEFAULT 'NoOne', + `radius` int(11) DEFAULT 50, + `coords` longtext NOT NULL, + `type` text DEFAULT 'default', + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8mb4; + +INSERT IGNORE INTO `tomic_territories` (`id`, `name`, `owner`, `label`, `radius`, `coords`, `type`) VALUES + (1, 'Motel', 'gsf', 'GSF', 60, '{"z":33.239501953125,"y":3574.15380859375,"x":1567.4241943359376}', 'dealer'), + (2, 'Docks', 'noone', 'NoOne', 65, '{"x":1011.2307739257813,"z":39.15380859375,"y":-2867.156005859375}', 'market'), + (3, 'Plaza', 'noone', 'NoOne', 50, '{"x":-1838.6373291015626,"z":13.0029296875,"y":-1223.5911865234376}', 'dealer'), + (4, 'Galileo', 'noone', 'NoOne', 55, '{"z":327.6732177734375,"y":1110.7120361328126,"x":-429.21759033203127}', 'dealer'), + (5, 'Dump', 'noone', 'NoOne', 80, '{"z":19.13623046875,"x":-533.7362670898438,"y":-1682.756103515625}', 'market'); + +ALTER TABLE `jobs` + ADD `weeklyPoints` INT(11) DEFAULT 0, + ADD `monthlyPoints` INT(11) DEFAULT 0, + ADD `totalPoints` INT(11) DEFAULT 0; \ No newline at end of file diff --git a/server.lua b/server.lua index 63f0085..0c7f5a4 100644 --- a/server.lua +++ b/server.lua @@ -5,12 +5,13 @@ CreateThread(function() if data then territories = {} for i = 1, #data, 1 do - table.insert(territories, {id = data[i].id, name = data[i].name, owner = data[i].owner, radius = data[i].radius, label = data[i].label, type = data[i].type, coords = json.decode(data[i].coords), isTaking = false, progress = 0, cooldown = false, spawnano = false, radi = false} ) - exports.ox_inventory:RegisterStash('devTomic-Ter['..data[i].name..']['..data[i].id..']', 'devTomic | Territory: '..data[i].name, 50, 100000) - print('devTomic | Registered stash: devTomic-'..data[i].id..' | Territory: '..data[i].name..'') + table.insert(territories, { id = data[i].id, name = data[i].name, owner = data[i].owner, radius = data[i].radius, label = data[i].label, type = data[i].type, coords = json.decode(data[i].coords), isTaking = false, progress = 0, isCooldown = false }) + exports.ox_inventory:RegisterStash('devTomic-Ter[' .. data[i].name .. '][' .. data[i].id .. ']', 'devTomic | Territory: ' .. data[i].name, 50, 100000) + print('devTomic | Registered stash: devTomic-' .. data[i].id .. ' | Territory: ' .. data[i].name .. '') end end end) + checkForUpdates() end) ESX.RegisterServerCallback('tomic_territories:getTerritories', function(source, cb) @@ -18,31 +19,38 @@ ESX.RegisterServerCallback('tomic_territories:getTerritories', function(source, end) if shared.rankings then - ESX.RegisterServerCallback('tomic_territories:povucipoene', function(source, cb) - MySQL.query('SELECT * FROM jobs', {}, function(poeni) - if poeni then - cb(poeni) + ESX.RegisterServerCallback('tomic_territories:fetchPoints', function(source, cb) + MySQL.query('SELECT * FROM jobs', {}, function(cbResults) + if cbResults then + cb(cbResults) end end) end) end --- command to create -RegisterCommand(shared.command, function(source, args) +RegisterCommand(shared.command, function(source, args, rawCommand) local xPlayer = ESX.GetPlayerFromId(source) + if source == 0 then return print('devTomic | Command can only be used in-game!') end - if xPlayer.getGroup() == shared.group then - if args[1] == 'create' then - TriggerClientEvent('tomic_territories:createTerritory', source) - elseif args[1] == 'delete' then - TriggerClientEvent('tomic_territories:deleteTerritory', source) - end - else - xPlayer.showNotification('devTomic | You are not allowed to use this command!') + + if not inArray(shared.groups, xPlayer.getGroup()) then + return xPlayer.showNotification('devTomic | You do not have permission to use this command!') end -end) + + if args[1] == nil then + return xPlayer.showNotification('devTomic | Usage: /territory [create/delete]') + end + + if args[1] == 'create' then + TriggerClientEvent('tomic_territories:createTerritory', source) + end + + if args[1] == 'delete' then + TriggerClientEvent('tomic_territories:deleteTerritory', source) + end +end, false) RegisterNetEvent('tomic_territories:createTerritory') AddEventHandler('tomic_territories:createTerritory', function(territoryInfo) @@ -54,41 +62,27 @@ AddEventHandler('tomic_territories:createTerritory', function(territoryInfo) radius = territoryInfo.radius, label = 'NoOne', type = territoryInfo.type, - coords = json.encode({x = territoryInfo.coords.x, y = territoryInfo.coords.y, z = territoryInfo.coords.z}), - isTaking = false, + coords = territoryInfo.coords, progress = 0, - cooldown = false, - spawnano = false, - radi = false + isTaking = false, + isCooldown = false } - MySQL.Async.execute('INSERT INTO tomic_territories (id, name, type, coords, radius) VALUES (@id, @name, @type, @coords, @radius)', { + + MySQL.Async.execute( + "INSERT INTO tomic_territories (id, name, type, coords, radius) VALUES (@id, @name, @type, @coords, @radius)", { ['@id'] = territory.id, ['@name'] = territory.name, ['@type'] = territory.type, - ['@radius'] = territory.radius, - ['@coords'] = territory.coords, + ['@coords'] = json.encode(territory.coords), + ['@radius'] = territory.radius }, function(rowsChanged) if rowsChanged > 0 then - table.insert(territories, { - id = #territories + 1, - name = territoryInfo.name, - owner = 'noone', - radius = territoryInfo.radius, - label = 'NoOne', - type = territoryInfo.type, - coords = json.decode(territory.coords), - isTaking = false, - progress = 0, - cooldown = false, - spawnano = false, - radi = false - }) - exports.ox_inventory:RegisterStash('devTomic-Ter['..territory.name..']['..territory.id..']', 'devTomic | Territory: '..territory.name, 50, 100000) + table.insert(territories, territory) + exports.ox_inventory:RegisterStash("devTomic-Ter[" .. territory.name .. "][" .. territory.id .. "]", "devTomic | Territory: " .. territory.name, 50, 100000) + TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) + xPlayer.showNotification("devTomic | Territory created!") end end) - Wait(500) - TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) - xPlayer.showNotification('devTomic | Territory created!') end) RegisterNetEvent('tomic_territories:deleteTerritory') @@ -111,193 +105,186 @@ AddEventHandler('tomic_territories:deleteTerritory', function(territoryName) end) end) -RegisterNetEvent('tomic_territories:capturestart') -AddEventHandler('tomic_territories:capturestart', function(id, job, label, name, currentOwner) +RegisterNetEvent('tomic_territories:captureServer') +AddEventHandler('tomic_territories:captureServer', function(id, job, label, name, currentOwner) local _source = source local xPlayer = ESX.GetPlayerFromId(_source) local xPlayers = ESX.GetPlayers() - local vrijeme = os.date('%H') for i = 1, #xPlayers, 1 do local xPlayer = ESX.GetPlayerFromId(xPlayers[i]) + if xPlayer.job.name == currentOwner then - xPlayer.showNotification('devTomic | Territory: '..name..' is being attacked by another gang!') + xPlayer.showNotification('devTomic | Territory: ' .. name .. ' is being attacked by another gang!') end + if xPlayer.job.name == job then - xPlayer.showNotification('devTomic | Your gang started attacking territory '..name..'!') + xPlayer.showNotification('devTomic | Your gang started attacking territory ' .. name .. '!') end end for k, v in pairs(territories) do if v.id == id then - v.isTaking = true - v.cooldown = true - datakey = k - data = territories[k] + v.isTaking, v.isCooldown = true, true TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) - TriggerClientEvent('tomic_territories:blipblink', -1, id, job, label) - TriggerClientEvent('tomic_territories:captureprogress', source, datakey, data) - print(GetPlayerName(xPlayer.source)..' started capturing: '..name) + TriggerClientEvent('tomic_territories:updateBlips', -1, id, job, label) + TriggerClientEvent('tomic_territories:captureProgress', source, k, territories[k]) + print(GetPlayerName(xPlayer.source) .. ' started capturing: ' .. name) end end end) -RegisterNetEvent('tomic_territories:MethServer') -AddEventHandler('tomic_territories:MethServer', function(infoX, vozilo) +RegisterNetEvent('tomic_territories:sellDealer') +AddEventHandler('tomic_territories:sellDealer', function(itemObject) local xPlayer = ESX.GetPlayerFromId(source) - voziloInfo = vozilo - for i, v in pairs(territories) do - if v.id == infoX.data.id then - v.spawnano = true - v.radi = true - TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) - TriggerClientEvent('tomic_territories:MethClient', source, voziloInfo) - end + if xPlayer.getInventoryItem(itemObject.itemKey).count < itemObject.itemCount then + return xPlayer.showNotification('devTomic | You do not have that amount!') end -end) -RegisterNetEvent('tomic_territories:sellDealer') -AddEventHandler('tomic_territories:sellDealer', function(allInfo) - local xPlayer = ESX.GetPlayerFromId(source) - if xPlayer.getInventoryItem(allInfo.i).count >= allInfo.xCount then - if allInfo.xType == true then - xPlayer.removeInventoryItem(allInfo.i, allInfo.xCount) - xPlayer.addAccountMoney('black_money', allInfo.xWorth * allInfo.xCount) - else - xPlayer.removeInventoryItem(allInfo.i, allInfo.xCount) - xPlayer.addMoney(allInfo.xWorth * allInfo.xCount) - end + if itemObject.itemCurrency then + xPlayer.addAccountMoney('black_money', itemObject.itemWorth * itemObject.itemCount) else - xPlayer.showNotification('devTomic | You do not have that amount!') + xPlayer.addMoney(itemObject.itemWorth * itemObject.itemCount) end + + xPlayer.removeInventoryItem(itemObject.itemKey, itemObject.itemCount) end) RegisterNetEvent('tomic_territories:buyMarket') -AddEventHandler('tomic_territories:buyMarket', function(allInfo) +AddEventHandler('tomic_territories:buyMarket', function(itemObject) local xPlayer = ESX.GetPlayerFromId(source) - if allInfo.xType == true then - if xPlayer.getAccount('black_money').money >= allInfo.xWorth then - if xPlayer.canCarryItem(allInfo.i, allInfo.xCount) then - xPlayer.removeAccountMoney('black_money', allInfo.xWorth * allInfo.xCount) - xPlayer.addInventoryItem(allInfo.i, allInfo.xCount) - else - xPlayer.showNotification('devTomic | You do not have any space in your inventory!') - end - else - xPlayer.showNotification('devTomic | You do not have enough black money!') + if itemObject.itemCurrency then + if xPlayer.getAccount('black_money').money < itemObject.itemWorth * itemObject.itemCount then + return xPlayer.showNotification('devTomic | You do not have enough black money!') end + + if not xPlayer.canCarryItem(itemObject.itemKey, itemObject.itemCount) then + return xPlayer.showNotification('devTomic | You do not have any space in your inventory!') + end + + xPlayer.removeAccountMoney('black_money', itemObject.itemWorth * itemObject.itemCount) else - if xPlayer.getMoney() >= allInfo.xWorth then - if xPlayer.canCarryItem(allInfo.i, allInfo.xCount) then - xPlayer.removeMoney(allInfo.xWorth * allInfo.xCount) - xPlayer.addInventoryItem(allInfo.i, allInfo.xCount) - else - xPlayer.showNotification('devTomic | You do not have any space in your inventory!') - end - else - xPlayer.showNotification('devTomic | You do not have enough money!') + if xPlayer.getMoney() < itemObject.itemWorth * itemObject.itemCount then + return xPlayer.showNotification('devTomic | You do not have enough money!') + end + + if not xPlayer.canCarryItem(itemObject.itemKey, itemObject.itemCount) then + return xPlayer.showNotification('devTomic | You do not have any space in your inventory!') end + + xPlayer.removeMoney(itemObject.itemWorth * itemObject.itemCount) end + + xPlayer.addInventoryItem(itemObject.itemKey, itemObject.itemCount) end) --- not working, but keep it.. xd (you can remove it if you want.. this was just for testing, it is not in use) --- RegisterNetEvent('tomic_territories:captureprogress') --- AddEventHandler('tomic_territories:captureprogress', function(id) --- for i, v in pairs(territories) do --- if v.id == id then --- TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) --- TriggerClientEvent('tomic_territories:captureprogress', source) --- if v.progress == 100 then --- v.isTaking = false --- MySQL.query('UPDATE tomic_territories SET owner = @owner WHERE id = @id', {id = id, owner = v.owner}) --- TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) --- end --- end --- end --- end) - -RegisterNetEvent('tomic_territories:capturecomplete') -AddEventHandler('tomic_territories:capturecomplete', function(id, job, label, prvivlasnik) +RegisterNetEvent('tomic_territories:captureComplete') +AddEventHandler('tomic_territories:captureComplete', function(terId, newOwner, newLabel, previousOwner) for i, v in pairs(territories) do - if v.id == id then - v.isTaking = false - v.owner = job - v.label = label - tername = v.name - MySQL.query('UPDATE tomic_territories SET owner = ? WHERE id = ?', {job, id}) - MySQL.query('UPDATE tomic_territories SET label = ? WHERE id = ?', {label, id}) + if v.id == terId then + v.isTaking, v.owner, v.label = false, newOwner, newLabel + + MySQL.query('UPDATE tomic_territories SET owner = ?, label = ? WHERE id = ?', { newOwner, newLabel, terId }) + if shared.rewards.on then - TriggerEvent('tomic_territories:reward', job, tername) + TriggerEvent('tomic_territories:rewardPlayers', newOwner, v.name) end + if shared.rankings then - --------------------------------------------------------------------------------------------------------- - MySQL.query('SELECT * FROM jobs WHERE name = @name', { ['@name'] = prvivlasnik }, function(x) - if x then - local poeni = x[1].poeni - novostanje = poeni - 2 - MySQL.query('UPDATE jobs SET nedpoeni = @poeni WHERE name = @name', { ['@name'] = prvivlasnik, ['@poeni'] = novostanje }) - MySQL.query('UPDATE jobs SET mespoeni = @poeni WHERE name = @name', { ['@name'] = prvivlasnik, ['@poeni'] = novostanje }) - end - end) - --------------------------------------------------------------------------------------------------------- - MySQL.query('SELECT * FROM jobs WHERE name = @name', { ['@name'] = job }, function(x) - if x then - local poeni = x[1].poeni - novostanje = poeni + 3 - MySQL.query('UPDATE jobs SET poeni = @poeni WHERE name = @name', { ['@name'] = job, ['@poeni'] = novostanje }) - MySQL.query('UPDATE jobs SET nedpoeni = @x WHERE name = @name', { ['@name'] = job, ['@x'] = novostanje }) - MySQL.query('UPDATE jobs SET mespoeni = @y WHERE name = @name', { ['@name'] = job, ['@y'] = novostanje }) + MySQL.query('SELECT * FROM jobs WHERE name IN (@prevOwner, @newOwner)', { ['@prevOwner'] = previousOwner, ['@newOwner'] = newOwner }, function(results) + for i = 1, #results do + local result = results[i] + local name = result.name + local points = result.totalPoints + + if name == previousOwner then + points = points - 2 + elseif name == newOwner then + points = points + 3 + end + + MySQL.query('UPDATE jobs SET weeklyPoints = @points, monthlyPoints = @points, totalPoints = @points WHERE name = @name', { ['@points'] = points, ['@name'] = name }) end end) - --------------------------------------------------------------------------------------------------------- end + TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) + Wait(shared.cooldown * 60000) - v.cooldown = false + + v.isCooldown = false + TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) end end end) -RegisterNetEvent('tomic_territories:reward') -AddEventHandler('tomic_territories:reward', function(job, tername) - local _source = source - local xPlayer = ESX.GetPlayerFromId(_source) +RegisterNetEvent('tomic_territories:rewardPlayers') +AddEventHandler('tomic_territories:rewardPlayers', function(terOwner, terName) local xPlayers = ESX.GetPlayers() for i = 1, #xPlayers, 1 do local xPlayer = ESX.GetPlayerFromId(xPlayers[i]) - if xPlayer.job.name == job then + if xPlayer.job.name == terOwner then xPlayer.addInventoryItem(shared.rewards.item, shared.rewards.count) - xPlayer.showNotification('devTomic | You got $'..shared.rewards.count..' as a reward for capturing: '..tername..'!') + xPlayer.showNotification('devTomic | You got $' .. shared.rewards.count .. ' as a reward for capturing: ' .. terName .. '!') end end end) -RegisterNetEvent('tomic_territories:updateTerritories') -AddEventHandler('tomic_territories:updateTerritories', function(territories) - for i, v in pairs(territories) do - TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) - end -end) - -RegisterNetEvent('tomic_territories:captureend') -AddEventHandler('tomic_territories:captureend', function(id) +RegisterNetEvent('tomic_territories:endCapturing') +AddEventHandler('tomic_territories:endCapturing', function(id) for i, v in pairs(territories) do if v.id == id then v.isTaking = false TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) + Wait(shared.cooldown * 60000) - v.cooldown = false + + v.isCooldown = false TriggerClientEvent('tomic_territories:updateTerritories', -1, territories) end end end) if shared.rankings then - function Resetuj(d, h, m) + function Reset(d, h, m) if d == 1 then - MySQL.query('UPDATE jobs SET nedpoeni = @x', { ['@x'] = 0 }) + MySQL.query('UPDATE jobs SET weeklyPoints = @points', { ['@points'] = 0 }) + end + end + + TriggerEvent('cron:runAt', 06, 00, Reset) +end + +function inArray(array, value) + for i, v in pairs(array) do + if v == value then + return true end end - TriggerEvent('cron:runAt', 06, 00, Resetuj) -end \ No newline at end of file + return false +end + +function checkForUpdates() + local currentVersion = GetResourceMetadata(GetCurrentResourceName(), 'version', 0) + PerformHttpRequest('https://api.github.com/repos/tomiichx/tomic_territories/releases/latest', function(code, response) + if code == 200 then + local returnedData = json.decode(response) + local latestVersion = returnedData.tag_name + local downloadLink = returnedData.html_url + + if currentVersion ~= latestVersion then + print('\n') + print('devTomic | There is a new update available for ' .. GetCurrentResourceName()) + print('devTomic | Your version: ' .. currentVersion .. ' | New version: ' .. latestVersion) + print('devTomic | Download it from: ' .. downloadLink) + print('\n') + else + print('devTomic | You are using the latest version of ' .. GetCurrentResourceName()) + end + + else + print('devTomic | There was an error while checking for updates.') + end + end, 'GET') +end diff --git a/shared.lua b/shared.lua index 2446a1e..ccb1eaf 100644 --- a/shared.lua +++ b/shared.lua @@ -1,13 +1,14 @@ shared = { + language = 'en', -- jezik / language (WIP) command = 'ter', -- komanda / command (Admin Only) // /ter (create/delete) - group = 'admin', -- grupa ili permisija / group required + groups = {'admin', 'superadmin'}, -- grupa ili permisija / group required rankings = false, -- rang lista za mafije / rank list and points for gangs? (true/false) (not user-friendly, yet.. but it's translated tho) capturing = 5, -- in minutes / u minutama cooldown = 30, -- in minutes / u minutama rewards = { -- reward is given only after successfully capturing the territory on = true, -- off (false) / on (true) item = 'black_money', -- item name - count = 1000, -- amount + count = 1000 -- amount }, itemsToBuy = { -- buyable items if territory type is 'market' ['bread'] = { @@ -19,7 +20,7 @@ shared = { label = 'πŸ’§ | Water', worth = 20, black = true, -- true = black money, false = cash - }, + } }, itemsToSell = { -- sellable items if territory type is 'dealer' ['marihuana'] = { @@ -41,21 +42,21 @@ shared = { label = 'πŸ”οΈ | Cocaine', worth = 750, black = true, -- true = black money, false = cash - }, - }, -} - -shared.gangs = { -- https://docs.fivem.net/docs/game-references/blips/ || gangs allowed to territories, aswell as their label (label not in use yet, but planned in future) and blip color - gsf = { -- posao / job - label = 'GSF', - blipboja = 69, -- boja blipa / blip color + } }, - ballas = { -- posao / job - label = 'Ballas', - blipboja = 58, -- boja blipa / blip color - }, - bloods = { -- posao / job - label = 'Bloods', - blipboja = 59, -- boja blipa / blip color + gangs = { -- https://docs.fivem.net/docs/game-references/blips/ || gangs allowed to territories, aswell as their label (label not in use yet, but planned in future) and blip color + gsf = { -- posao / job + blipColour = 69, -- boja blipa / blip color + }, + ballas = { -- posao / job + blipColour = 58, -- boja blipa / blip color + }, + bloods = { -- posao / job + blipColour = 59, -- boja blipa / blip color + } }, + translations = { -- WIP + ['en'] = {}, + ['hr'] = {} + } } \ No newline at end of file diff --git a/update.sql b/update.sql new file mode 100644 index 0000000..3fd87cf --- /dev/null +++ b/update.sql @@ -0,0 +1,6 @@ +-- Execute this to update the columns if you already have used the script before. + +ALTER TABLE `jobs` + CHANGE COLUMN `nedpoeni` `weeklyPoints` INT(11) DEFAULT 0, + CHANGE COLUMN `mespoeni` `monthlyPoints` INT(11) DEFAULT 0, + CHANGE COLUMN `poeni` `totalPoints` INT(11) DEFAULT 0; \ No newline at end of file