lr_occ/client/main_bk2.lua

325 lines
11 KiB
Lua
Raw Normal View History

2024-07-21 09:07:35 +07:00
ESX = nil
LR = {}
LR.__index = LR
function LR:Init()
local o = {}
setmetatable(o, LR)
o.Capturing = false
o.CurZone = nil
o.InZone = false
o.Blips = {}
o.RadiusBlips = {}
ESX.TriggerServerCallback("lr_occ:callback:getData", function(data)
print(ESX.DumpTable(data))
o.Zones = data[1]
o.DelayTime = data[2]
o:EventHandler()
o:PosThread()
o:Blip()
o:TimeThread()
end)
return o
end
function LR:PosThread()
Citizen.CreateThread(function()
while true do
coroutine.yield(0)
self.ped = PlayerPedId()
self.pedCoords = GetEntityCoords(self.ped, true)
self.CurZone = self:CheckPos()
if self.CurZone ~= nil then
local zoneCoords = Config.Occ[self.CurZone].Pos
local distance = #(self.pedCoords - zoneCoords)
DrawMarker(1, zoneCoords.x, zoneCoords.y, zoneCoords.z - 100.100, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
Config.Occ[self.CurZone].Radius * 2, Config.Occ[self.CurZone].Radius * 2, 200.0, 255, 0, 0, 100,
false, true, 2, nil, nil, false)
DrawMarker(1, zoneCoords.x, zoneCoords.y, zoneCoords.z - 1.100, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0,
2.0, 255, 0, 0, 100, false, true, 2, nil, nil, false)
if distance <= 5.5 and not self.Busy and not self.Capturing then
DisplayText("Nhấn [E] để chiếm khu vực này")
if IsControlJustPressed(0, 38) then
self.Busy = true
ESX.TriggerServerCallback("lr_occ:callback:canCapture", function(result)
self.Busy = false
if result == true then
self.Capturing = true
self:StartCapture(self.CurZone)
end
end, self.CurZone)
end
end
if (IsPedInAnyVehicle(self.ped, false) or IsPlayerFreeAiming(PlayerId()) or IsPlayerDead(PlayerId())) and self.Panel2Open then
self:TogglePanel2(false)
elseif not self.Panel2Open and not IsPedInAnyVehicle(self.ped, false) and not IsPlayerFreeAiming(PlayerId()) and not IsPlayerDead(PlayerId()) then
self:TogglePanel2(true)
end
else
Wait(2000)
end
end
end)
end
function LR:StartCapture(index)
Citizen.CreateThread(function()
local time = Config.Occ[self.CurZone].time
while self.Capturing do
coroutine.yield(0)
print(time)
if self.CurZone ~= nil then
Wait(1000)
time = time - 1
if time <= 0 then
self.Capturing = false
end
if IsPlayerDead(PlayerId()) then
self.Capturing = false
TriggerServerEvent("lr_occ:server:cancelCapture")
end
else
self.Capturing = false
TriggerServerEvent("lr_occ:server:cancelCapture")
end
end
end)
end
function LR:CheckPos()
for k, v in pairs(self.Zones) do
local distance = #(self.pedCoords - v.Pos)
if distance <= v.Radius then
if not self.InZone then
self.InZone = true
self:TogglePanel2(true)
--self:TogglePanel3(true)
end
return k
end
end
if self.InZone then
self.InZone = false
self:TogglePanel2(false)
--self:TogglePanel3(false)
end
return nil
end
function LR:TogglePanel2(t)
self.Panel2Open = t
SendNUIMessage({
event = "toggle-panel2",
data = t
})
end
function LR:SetDataPanel2(data)
SendNUIMessage({
event = "data-panel2",
data = data
})
end
function LR:TimeThread()
Citizen.CreateThread(function()
while true do
Wait(1000)
for k, v in pairs(self.Zones) do
if v.open then
if v.playerCaptureName ~= nil then
if v.CaptureTime > 0 then
v.CaptureTime = v.CaptureTime - 1
end
end
end
end
if self.DelayTime > 0 then
self.DelayTime = self.DelayTime - 1
end
if self.CurZone ~= nil then
local v = self.Zones[self.CurZone]
if v.open then
if self.DelayTime > 0 then
self:SetDataPanel2({
type = "blocked",
data = {
name = v.Label,
time = self.DelayTime
}
})
else
if v.playerCaptureName ~= nil then
self:SetDataPanel2({
type = "capturing",
data = {
name = v.Label,
point = v.point,
time = v.CaptureTime,
player = v.playerCaptureName
}
})
else
self:SetDataPanel2({
type = "allow",
data = {
name = v.Label,
point = v.point,
time = v.time
}
})
end
end
else
self:SetDataPanel2({
type = "closed",
data = {
name = v.Label
}
})
end
end
end
end)
end
function LR:EventHandler()
RegisterNetEvent("lr_occ:client:startCapture", function(index, data)
self.Zones[index] = data
self:ShowNotification("warning",
("%s đang bị chiếm bởi %s"):format(self.Zones[index].Label, self.Zones[index].playerCaptureName), true)
self:PulseBlip(index)
end)
RegisterNetEvent("lr_occ:client:cancelCapture", function(index, data)
local zone = self.Zones[index]
self:ShowNotification("warning",
("%s đã hủy chiếm khu vực %s, mọi người có thể tiến hành chiếm khu vực này ngay bây giờ"):format(
zone.playerCaptureName, zone.Label))
self.Zones[index].playerCaptureName = nil
self.Zones[index].playerCaptureSource = nil
self.Zones[index].playerCaptureTime = nil
self.Zones[index].CaptureTime = self.Zones[index].time
self:UnPulse()
end)
RegisterNetEvent("lr_occ:client:showNotification", function(...)
self:ShowNotification(...)
end)
RegisterNetEvent("lr_occ:client:captured", function(index, data)
if self.CurZone == index then
SetCurrentPedWeapon(PlayerPedId(), "WEAPON_UNARMED", true)
end
self:ShowNotification("success",
("%s đã chiếm thành công %s mang về %s điểm cho %s"):format(data.playerCaptureName, data.Label, data.point,
data.owner))
self.Zones[index] = data
self:UnPulse()
self.DelayTime = Config.Delay
self:Blip()
end)
RegisterNetEvent("lr_occ:client:openMain", function(index, data)
self:ShowNotification("success",
"Khu vực chính đã được mở, các băng đảng có thể tiến hành chiếm đóng ngay bây giờ !!")
self.Zones[index] = data
self.DelayTime = 600
self:Blip()
end)
end
function LR:ShowNotification(type, msg, playSound)
SendNUIMessage({
event = "notification",
data = {
type = type,
msg = msg,
playSound = playSound
}
})
end
function LR:PulseBlip(index)
local zoneCoords = Config.Occ[index].Pos
local Pblip = AddBlipForCoord(zoneCoords.x, zoneCoords.y, zoneCoords.z)
SetBlipSprite(Pblip, 161)
SetBlipScale(Pblip, 2.0)
SetBlipColour(Pblip, 1)
PulseBlip(Pblip)
self.PulseBlip = Pblip
end
function LR:UnPulse()
RemoveBlip(self.PulseBlip)
self.PulseBlip = nil
end
function LR:Blip()
for k, v in pairs(self.Blips) do
RemoveBlip(v)
end
self.Blips = {}
for k, v in pairs(self.RadiusBlips) do
RemoveBlip(v)
end
self.RadiusBlips = {}
for k, v in pairs(self.Zones) do
if v.open and v.owner == nil then
local blip = AddBlipForCoord(v.Pos.x, v.Pos.y, v.Pos.z)
SetBlipSprite(blip, 420)
SetBlipDisplay(blip, 2)
SetBlipScale(blip, 0.9)
SetBlipColour(blip, 1)
SetBlipAsShortRange(blip, true)
SetBlipHighDetail(blip, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString(("Khu Vực Chiếm Đóng [%s]"):format(k))
EndTextCommandSetBlipName(blip)
self.Blips[k] = blip
else
local blip = AddBlipForCoord(v.Pos.x, v.Pos.y, v.Pos.z)
SetBlipSprite(blip, 420)
SetBlipDisplay(blip, 2)
SetBlipScale(blip, 0.9)
SetBlipColour(blip, 5)
SetBlipAsShortRange(blip, true)
SetBlipHighDetail(blip, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString(("Khu Vực Chiếm Đóng [%s]"):format(string.upper(v.owner or k)))
EndTextCommandSetBlipName(blip)
self.Blips[k] = blip
end
local Ablip = AddBlipForRadius(v.Pos.x, v.Pos.y, v.Pos.z, v.Radius)
SetBlipColour(Ablip, 1)
SetBlipAlpha(Ablip, 100)
SetBlipHighDetail(Ablip, true)
SetBlipRotation(Ablip, 0.0)
SetBlipDisplay(Ablip, 4)
SetBlipAsShortRange(Ablip, false)
self.RadiusBlips[k] = Ablip
end
--self:PulseBlip("main")
print("LOADED BLIPS")
end
Citizen.CreateThread(function()
while ESX == nil do
Wait(0)
TriggerEvent("esx:getSharedObject", function(obj) ESX = obj end)
end
LR:Init()
end)
function DisplayText(text)
local width = string.len(text) + 0.0
DrawRect(1.0 - width / 200 / 2, 0.98, width / 200, 0.06, 245, 71, 72, 200)
SetTextScale(0.4, 0.4)
SetTextFont(ESX.FontId)
SetTextProportional(1)
SetTextColour(255, 255, 255, 255)
SetTextEntry("STRING")
SetTextCentre(1)
AddTextComponentString(text)
DrawText(1.0 - width / 200 / 2, 0.96)
end