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

feat(esx_shops): ✨ Add support for multiple paymenth methods, revert … #26

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
192 changes: 85 additions & 107 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,121 +1,99 @@
local hasAlreadyEnteredMarker, lastZone
local currentAction, currentActionMsg, currentActionData = nil, nil, {}

local currentZone, TextUI, inMenu

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be consistent with variable naming. Why is TextUI capitalized?

local function openShopMenu(zone)
local elements = {
{unselectable = true, icon = "fas fa-shopping-basket", title = TranslateCap('shop') }
}

for i=1, #Config.Zones[zone].Items, 1 do
local item = Config.Zones[zone].Items[i]
if not Config.Zones[zone] then return end
local elements = {}
for i, item in ipairs(Config.Zones[zone].Items) do
elements[i] = {
label = ('%s - <span style="color:green;">%s</span>'):format(item.label,
TranslateCap('shop_item', ESX.Math.GroupDigits(item.price))),
name = item.name,
value = 1,
type = "slider",
min = 1,
max = 10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good idea to add min and max as optionals to the config.

}
end

inMenu = true
ESX.UI.Menu.Open("default", GetCurrentResourceName(), "SHOP_MENU", {
title = TranslateCap('shop'),
align = Config.MenuAlign,
elements = elements
}, function(data, menu)
if not currentZone then
return menu.close()
end

TriggerServerEvent("esx_shops:buyItem", data.current.name, data.current.value, currentZone)
end, function(data, menu)
inMenu = false
menu.close()
end)
end

elements[#elements+1] = {
icon = "fas fa-shopping-basket",
title = ('%s - <span style="color:green;">%s</span>'):format(item.label, TranslateCap('shop_item', ESX.Math.GroupDigits(item.price))),
itemLabel = item.label,
item = item.name,
price = item.price
}
end
function AddShopBlip(pos, settings)
if not settings.ShowBlip then return end

ESX.OpenContext("right", elements, function(menu,element)
local elements2 = {
{unselectable = true, icon = "fas fa-shopping-basket", title = element.title},
{icon = "fas fa-shopping-basket", title = TranslateCap('amount'), input = true, inputType = "number", inputPlaceholder = TranslateCap('amount_placeholder'), inputMin = 1, inputMax = 25},
{icon = "fas fa-check-double", title = TranslateCap('confirm'), val = "confirm"}
}
local blip = AddBlipForCoord(pos)

ESX.OpenContext("right", elements2, function(menu2,element2)
local amount = menu2.eles[2].inputValue
ESX.CloseContext()
TriggerServerEvent('esx_shops:buyItem', element.item, amount, zone)
end, function(menu)
currentAction = 'shop_menu'
currentActionMsg = TranslateCap('press_menu', ESX.GetInteractKey())
currentActionData = {zone = zone}
end)
end, function(menu)
currentAction = 'shop_menu'
currentActionMsg = TranslateCap('press_menu', ESX.GetInteractKey())
currentActionData = {zone = zone}
end)
end
SetBlipSprite(blip, settings.Type)
SetBlipScale(blip, settings.Size)
SetBlipColour(blip, settings.Color)
SetBlipAsShortRange(blip, true)

local function hasEnteredMarker(zone)
currentAction = 'shop_menu'
currentActionMsg = TranslateCap('press_menu', ESX.GetInteractKey())
currentActionData = {zone = zone}
BeginTextCommandSetBlipName("STRING")
AddTextComponentSubstringPlayerName(TranslateCap('shops'))
EndTextCommandSetBlipName(blip)
end

local function hasExitedMarker(zone)
currentAction = nil
ESX.CloseContext()
function createShopPoint(pos, zone, ShowMarker)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be consistent with variable naming. Why is ShowMarker capitalized?

ESX.Point:new({
coords = pos,
distance = Config.DrawDistance,
enter = function()
end,
leave = function()
ESX.HideUI()
end,

inside = function(point)
if ShowMarker then
DrawMarker(Config.MarkerType, point.coords.x, point.coords.y, point.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Config.MarkerSize.x,
Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g,
Config.MarkerColor.b, 100, false, true, 2, false, nil, nil, false)
end

currentZone = point.currDistance < Config.MarkerSize.x and zone
if currentZone and not inMenu and not TextUI then
TextUI = true
ESX.TextUI(Translate("press_menu", ESX.GetInteractKey()))
elseif (not currentZone or inMenu) then
if not currentZone and inMenu then
ESX.UI.Menu.Close("default", GetCurrentResourceName(), "SHOP_MENU")
inMenu = false
end

if TextUI then
ESX.HideUI()
TextUI = false
end
end
end
})
end

-- Create Blips
CreateThread(function()
for k,v in pairs(Config.Zones) do
for i = 1, #v.Pos, 1 do
if not v.ShowBlip then return end

local blip = AddBlipForCoord(v.Pos[i])

SetBlipSprite (blip, v.Type)
SetBlipScale (blip, v.Size)
SetBlipColour (blip, v.Color)
SetBlipAsShortRange(blip, true)

BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(TranslateCap('shops'))
EndTextCommandSetBlipName(blip)
end
end
end)

-- Enter / Exit marker events
CreateThread(function()
while true do
local sleep = 1500

local playerCoords = GetEntityCoords(ESX.PlayerData.ped)
local isInMarker, currentZone = false, nil

for k,v in pairs(Config.Zones) do
for i = 1, #v.Pos, 1 do
local distance = #(playerCoords - v.Pos[i])

if distance < Config.DrawDistance then
sleep = 0
if v.ShowMarker then
DrawMarker(Config.MarkerType, v.Pos[i], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 100, false, true, 2, false, nil, nil, false)
end
if distance < 2.0 then
isInMarker = true
currentZone = k
lastZone = k
end
end
end
end

if isInMarker and not hasAlreadyEnteredMarker then
hasAlreadyEnteredMarker = true
hasEnteredMarker(currentZone)
ESX.TextUI(currentActionMsg)
end

if not isInMarker and hasAlreadyEnteredMarker then
hasAlreadyEnteredMarker = false
ESX.HideUI()
hasExitedMarker(lastZone)
end

Wait(sleep)
end
for zone, v in pairs(Config.Zones) do
for i = 1, #v.Pos, 1 do
local pos = v.Pos[i]
AddShopBlip(pos, v)
createShopPoint(pos, zone, v.ShowMarker)
end
end
end)

ESX.RegisterInteraction("shop_menu", function()
openShopMenu(currentActionData.zone)
openShopMenu(currentZone)
end, function()
return currentAction and currentAction == 'shop_menu'
end)
return not inMenu
end)
Loading