RyanUSNS

ClientSide - SOevents

Nov 16th, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.55 KB | None | 0 0
  1. ------------------------------------------------------------------------------------
  2. --  PROJECT:     Community, Integrity, Transparency
  3. --  RIGHTS:      All rights reserved by developers
  4. --  FILE:        CITsoEvents/soevents.lua
  5. --  PURPOSE:     State official events - Client side
  6. --  DEVELOPERS:  Ryan#USNS (IRC: Ryan1) (Discord: Ryan#9516)
  7. ------------------------------------------------------------------------------------
  8.  
  9. local t = function(...) return exports.CITt:t(...) or arg[1] or "error" end
  10. local progressTimer = nil
  11. local missionsInfo = {}
  12. local validResourcesAndLimit = {
  13.     ["Iron"] = 3400,
  14.     ["Oil"] = 970,
  15.     ["Fish"] = 2500,
  16.     ["Wheat"] = 1700,
  17.     ["Herbs"] = 750,
  18.     ["Hemp"] = 750,
  19.     ["Meat"] = 1875,
  20.     ["Explosive"] = 150,
  21.     ["Gold"] = 60,
  22.     ["Chemicals"] = 60,
  23.     ["Nitrous"] = 3500,
  24.     ["Electronics"] = 75,
  25. }
  26.  
  27. function createEventGUI()
  28.     local screenW, screenH = guiGetScreenSize()
  29.     eventsWindow = guiCreateWindow((screenW - 420) / 2, (screenH - 367) / 2, 420, 367, t("State Official Event Panel"), false)
  30.     guiWindowSetSizable(eventsWindow, false)
  31.     guiSetAlpha(eventsWindow, 1.00)
  32.     guiSetVisible(eventsWindow, false)
  33.     eventsCombo = guiCreateComboBox(164, 25, 246, 84, "", false, eventsWindow)
  34.     missionsInfo = {
  35.         ["Delivery Clothes"] = t("Requires a Trucker vehicle. You will take the clothes from a place and deliver it to the clothes shop with 95%%+ or you will have to re-take the clothes, if you leave your vehicle, you must re-take the mission. First one to finish the mission wins."),
  36.         ["Delivery Glasses"] = t("Requires a Delivery Man vehicle, you will take the glass from a place and deliver it to the shop with 95%%+ or you will have to re-take the glass, if you leave your vehicle, you must re-take the mission. First one to deliver it wins."),
  37.         ["Mystery Box"] = t("An SO will place the Mystery Box in a hidden place on the map and  then civilians will try to find it. First one to find it, wins the prize provided by State Official."),
  38.         ["From Danger to Safety"] = t("Requires a Bus Driver Vehicle's. You will take the people from a shooting to a safe zone. When the first player delivery them ( all other players will have 90 seconds to complete. The bus that received less damage will be the winner."),
  39.         ["Awesome Bag"] = t("Voluotedrs will, try fild a lost bag. If you find it, you must hand it over to the police Department or the civilian headquarters."),
  40.         ["Delivery Fish"] = t("Requires any boat. You will take the fish from the ocean and come back to shore with 95%%+ or you will back to sea for catch the fish. If you leave your boat, you will fail the mission. The first to complete the mission will be the winner."),
  41.     }
  42.     for name, info in pairs(missionsInfo) do
  43.         guiComboBoxAddItem(eventsCombo, name)
  44.     end
  45.     helpLabel1 = guiCreateLabel(10, 25, 144, 23, t("Select event from box:"), false, eventsWindow)
  46.     guiLabelSetVerticalAlign(helpLabel1, "center")
  47.     infoLabelB = guiCreateLabel(10, 48, 400, 61, t("No event selected..."), false, eventsWindow)
  48.     guiLabelSetHorizontalAlign(infoLabelB, "left", true)
  49.     mainTabPanel = guiCreateTabPanel(10, 109, 400, 171, false, eventsWindow)
  50.     -- Pickup information tab
  51.     pickupTab = guiCreateTab(t("Pickup informations"), mainTabPanel)
  52.     pickCoordsLabel = guiCreateLabel(10, 45, 72, 26, t("Coordinates:"), false, pickupTab)
  53.     guiLabelSetVerticalAlign(pickCoordsLabel, "center")
  54.     pickCoordsEdit = guiCreateEdit(82, 44, 308, 26, "", false, pickupTab)
  55.     guiEditSetReadOnly(pickCoordsEdit, true)
  56.     getPickPosButton = guiCreateButton(10, 9, 380, 25, t("Get my Position"), false, pickupTab)
  57.     pickLocLabel = guiCreateLabel(10, 75, 72, 26, t("Location:"), false, pickupTab)
  58.     guiLabelSetHorizontalAlign(pickLocLabel, "center", false)
  59.     guiLabelSetVerticalAlign(pickLocLabel, "center")
  60.     pickBlipLabel = guiCreateLabel(10, 106, 72, 26, t("Blip ID:"), false, pickupTab)
  61.     guiLabelSetHorizontalAlign(pickBlipLabel, "center", false)
  62.     guiLabelSetVerticalAlign(pickBlipLabel, "center")
  63.     pickLocEdit = guiCreateEdit(82, 75, 308, 26, "", false, pickupTab)
  64.     guiEditSetReadOnly(pickLocEdit, true)
  65.     pickBlipEdit = guiCreateEdit(82, 106, 308, 26, "53", false, pickupTab)
  66.     -- Delivery information tab
  67.     deliveryTab = guiCreateTab(t("Delivery informations"), mainTabPanel)
  68.     delCoordsLabel = guiCreateLabel(10, 45, 72, 26, t("Coordinates:"), false, deliveryTab)
  69.     guiLabelSetVerticalAlign(delCoordsLabel, "center")
  70.     delCoordsEdit = guiCreateEdit(82, 44, 308, 26, "", false, deliveryTab)
  71.     guiEditSetReadOnly(delCoordsEdit, true)
  72.     getDelPosButton = guiCreateButton(10, 9, 380, 25, t("Get my Position"), false, deliveryTab)
  73.     delLocLabel = guiCreateLabel(10, 75, 72, 26, t("Location:"), false, deliveryTab)
  74.     guiLabelSetHorizontalAlign(delLocLabel, "center", false)
  75.     guiLabelSetVerticalAlign(delLocLabel, "center")
  76.     delBlipLabel = guiCreateLabel(10, 106, 72, 26, t("Blip ID:"), false, deliveryTab)
  77.     guiLabelSetHorizontalAlign(delBlipLabel, "center", false)
  78.     guiLabelSetVerticalAlign(delBlipLabel, "center")
  79.     delLocEdit = guiCreateEdit(82, 75, 308, 26, "", false, deliveryTab)
  80.     guiEditSetReadOnly(delLocEdit, true)
  81.     delBlipEdit = guiCreateEdit(82, 106, 308, 26, "53", false, deliveryTab)
  82.     -- Quiz tab
  83.     quizTab = guiCreateTab(t"Quiz", mainTabPanel)
  84.     startQuizBtn = guiCreateButton(269, 112, 121, 25, t"Ask", false, quizTab)
  85.     quizField = guiCreateEdit(50, 17, 340, 20, "", false, quizTab)
  86.     quizLabel = guiCreateLabel(10, 18, 40, 14, t"Quiz:", false, quizTab)
  87.     answerLabel = guiCreateLabel(10, 48, 50, 15, t"Answer:", false, quizTab)
  88.     answerField = guiCreateEdit(60, 47, 330, 20, "", false, quizTab)
  89.     cancelBtn = guiCreateButton(15, 112, 121, 25, t"Cancel Quiz", false, quizTab)
  90.     rewardLabel = guiCreateLabel(10, 78, 50, 15, "Reward:", false, quizTab)
  91.     rewardField = guiCreateEdit(60, 78, 330, 20, "", false, quizTab)
  92.     -- Other controls
  93.     startEventButton = guiCreateButton(297, 290, 113, 27, t("Start Event"), false, eventsWindow)
  94.     stopEventButton = guiCreateButton(10, 332, 113, 27, t("Stop Event"), false, eventsWindow)
  95.     clearInfoButton = guiCreateButton(157, 332, 113, 27, t("Clear Informations"), false, eventsWindow)
  96.     closeButton = guiCreateButton(297, 332, 113, 27, t("Close Window"), false, eventsWindow)
  97.     timeEdit = guiCreateEdit(157, 290, 113, 27, t("Time"), false, eventsWindow)
  98.     amountEdit = guiCreateEdit(10, 290, 113, 27, t("Reward Amount"), false, eventsWindow)
  99.     guiSetProperty(timeEdit, "ValidationString", "^[0-9]*$")
  100.     --guiSetProperty(amountEdit, "ValidationString", "^[0-9]*$")
  101.     guiSetProperty(delBlipEdit, "ValidationString", "^[0-9]*$")
  102.     guiSetProperty(pickBlipEdit, "ValidationString", "^[0-9]*$")
  103.     guiSetProperty(rewardField, "ValidationString", "^[0-9]*$")
  104.     --Progress Bar
  105.     progressBar = guiCreateProgressBar((screenW/2) - 350,(screenH/2) - 25, 700, 57, false)
  106.     guiSetVisible(progressBar, false)
  107.     -- Event handlers
  108.     addEventHandler("onClientGUIClick", closeButton, toggleWindow, false)
  109.     addEventHandler("onClientGUIClick", startEventButton, startEvent, false)
  110.     addEventHandler("onClientGUIClick", stopEventButton, cancelThisEvent, false)
  111.     addEventHandler("onClientGUIClick", clearInfoButton, resetData, false)
  112.     addEventHandler("onClientGUIClick", getPickPosButton, insertCoordinates, false)
  113.     addEventHandler("onClientGUIClick", getDelPosButton, insertCoordinates, false)
  114.     addEventHandler("onClientGUIComboBoxAccepted", eventsCombo, selectMission, false)
  115.     addEventHandler("onClientGUIClick", timeEdit, clearEdit, false)
  116.     addEventHandler("onClientGUIClick", amountEdit, clearEdit, false)
  117.     addEventHandler("onClientGUIClick", startQuizBtn, startQuiz, false)
  118.     addEventHandler("onClientGUIClick", cancelBtn, stopQuiz, false)
  119. end
  120.  
  121.  
  122. function toggleWindow()
  123.     if (not eventsWindow) then
  124.         createEventGUI()
  125.     end
  126.     local state = not guiGetVisible(eventsWindow)
  127.     guiSetVisible(eventsWindow, state)
  128.     showCursor(state)
  129. end
  130. addEvent("CITsoEvents.toggleWindow", true)
  131. addEventHandler("CITsoEvents.toggleWindow", root, toggleWindow)
  132.  
  133. function startEvent()
  134.     local r = guiComboBoxGetSelected(eventsCombo)
  135.     if (r < 0) then
  136.         outputChatBox("Please select an event from combo box.", 255, 0, 0)
  137.         return false
  138.     end
  139.     if (getElementDimension(localPlayer) ~= 0) then
  140.         outputChatBox("You need to be in main dimension to create events.", 200, 0, 0)
  141.         return false
  142.     end
  143.     local p = split(guiGetText(pickCoordsEdit):gsub(" ", ""), ",")
  144.     if (#p < 3) then
  145.         outputChatBox("Please enter pickup coordinates.", 255, 0, 0)
  146.         return false
  147.     end
  148.     local px, py, pz, pBlip = p[1], p[2], p[3], guiGetText(pickBlipEdit)
  149.     if (not tonumber(pBlip)) then
  150.         return false
  151.     end
  152.     local timer = guiGetText(timeEdit)
  153.     local reward = guiGetText(amountEdit)
  154.     local eventName = guiComboBoxGetItemText(eventsCombo, r)
  155.     local info = guiGetText(infoLabelB)
  156.     if (tonumber(pBlip) > 63) then
  157.         outputChatBox("The blip ID can only be between 0-63.", 255, 0, 0)
  158.         return false
  159.     end
  160.     if (not tonumber(timer) or timer == "" or tonumber(timer) == 0) then
  161.         outputChatBox("Please enter time in minutes at second edit box.", 255, 0, 0)
  162.         return false
  163.     end
  164.     if (not tonumber(reward) or reward == "") then
  165.         if (not reward:find(":")) then
  166.             outputChatBox("Please enter prize money (or resourceName:amountOfResource) in first edit box.", 255, 0, 0)
  167.             return false
  168.         end
  169.         local spl = split(reward, ":")
  170.         if (not validResourcesAndLimit[spl[1]]) then
  171.             outputChatBox("Invalid resource name entered", 255, 0, 0)
  172.             return false
  173.         end
  174.         if (not spl[2] or not tonumber(spl[2]) or (tonumber(spl[2]) and tonumber(spl[2]) < 1)) then
  175.             outputChatBox("Invalid resource amount entered", 255, 0, 0)
  176.             return false
  177.         end
  178.         if (validResourcesAndLimit[spl[1]] < tonumber(spl[2])) then
  179.             outputChatBox("You are exceeding the maximum limit of "..spl[1]..". Max Limit: "..validResourcesAndLimit[spl[1]], 255, 0, 0)
  180.             return false
  181.         end
  182.     end
  183.     if (tonumber(reward) and tonumber(reward) > 100000) then
  184.         outputChatBox("Maximum prize is $100,000.", 255, 0, 0)
  185.         return false
  186.     end
  187.     if (tonumber(reward) and tonumber(reward) < 1000) then
  188.         outputChatBox("Minimum prize is $1,000.", 255, 0, 0)
  189.         return false
  190.     end
  191.     if (eventName == "Mystery Box") then
  192.         triggerServerEvent("CITsoEvents.startMission", resourceRoot, eventName, info, reward, timer, px, py, pz)
  193.         resetData()
  194.         return true
  195.     end
  196.     local d = split(guiGetText(delCoordsEdit):gsub(" ", ""), ",")
  197.     if (#d < 3) then
  198.         outputChatBox("Please enter delivery coordinates.", 255, 0, 0)
  199.         return false
  200.     end
  201.     local dx, dy, dz, dBlip = d[1], d[2], d[3], guiGetText(delBlipEdit)
  202.     if (tonumber(dBlip) > 63) then
  203.         outputChatBox("The blip ID can only be between 0-63.", 255, 0, 0)
  204.         return false
  205.     end
  206.     if (getDistanceBetweenPoints2D(px, py, dx, dy) <= 100) then
  207.         outputChatBox("The delivery position is very close to pickup position, pick different delivery position.", 200, 0, 0)
  208.         return false
  209.     end
  210.     triggerServerEvent("CITsoEvents.startMission", resourceRoot, eventName, info, reward, timer, px, py, pz, pBlip, dx, dy, dz, dBlip)
  211.     resetData()
  212. end
  213.  
  214. function cancelThisEvent()
  215.     triggerServerEvent("CITsoEvents.stopEvent", resourceRoot)
  216. end
  217.  
  218. function resetData()
  219.     guiSetText(pickCoordsEdit, "")
  220.     guiSetText(pickLocEdit, "")
  221.     guiSetText(delCoordsEdit, "")
  222.     guiSetText(delLocEdit, "")
  223.     guiSetText(infoLabelB, t("No event selected..."))
  224.     guiSetText(pickBlipEdit, "53")
  225.     guiSetText(delBlipEdit, "53")
  226.     guiSetText(amountEdit, t("Reward Amount"))
  227.     guiSetText(timeEdit, t("Time"))
  228.     guiComboBoxSetSelected(eventsCombo, -1)
  229. end
  230.  
  231. function insertCoordinates()
  232.     local x, y, z = getCameraMatrix()
  233.     local z = getGroundPosition(x, y, z) + 0.5
  234.     local city = exports.CITmapMisc:getZoneName2(x, y, z, true)
  235.     local zone = exports.CITmapMisc:getZoneName2(x, y, z)
  236.     if (source == getPickPosButton) then
  237.         guiSetText(pickCoordsEdit, math.ceil(tonumber(x), 3)..", "..math.ceil(tonumber(y), 3)..", "..math.ceil(tonumber(z), 3))
  238.         guiSetText(pickLocEdit, city..", "..zone)
  239.         return true
  240.     end
  241.     guiSetText(delCoordsEdit, math.ceil(tonumber(x), 3)..", "..math.ceil(tonumber(y), 3)..", "..math.ceil(tonumber(z), 3))
  242.     guiSetText(delLocEdit, city..", "..zone)
  243. end
  244.  
  245. function selectMission()
  246.     local r = guiComboBoxGetSelected(eventsCombo)
  247.     local name = guiComboBoxGetItemText(eventsCombo, r)
  248.     missionsInfo = {
  249.         ["Delivery Clothes"] = "Requires a Trucker vehicle. You will take the clothes from a place and deliver it to the clothes shop with 95%%+ or you will have to re-take the clothes, if you leave your vehicle, you must re-take the mission. First one to finish the mission wins.",
  250.         ["Delivery Glasses"] = "Requires a Delivery Man vehicle, you will take the glass from a place and deliver it to the shop with 95%%+ or you will have to re-take the glass, if you leave your vehicle, you must re-take the mission. First one to deliver it wins.",
  251.         ["Mystery Box"] = "An SO will place the Mystery Box in a hidden place on the map and  then civilians will try to find it. First one to find it, wins the prize provided by State Official.",
  252.         ["From Danger to Safety"] = "Requires a Bus Driver Vehicle's. You will take the people from a shooting to a safe zone. When the first player delivery them ( all other players will have 90 seconds to complete. The bus that received less damage will be the winner.",
  253.         ["Awesome Bag"] = "Voluotedrs will, try fild a lost bag. If you find it, you must hand it over to the police Department or the civilian headquarters.",
  254.         ["Delivery Fish"] = "Requires any boat. You will take the fish from the ocean and come back to shore with 95%%+ or you will back to sea for catch the fish. If you leave your boat, you will fail the mission. The first to complete the mission will be the winner.",
  255.     }
  256.     guiSetText(infoLabelB, (missionsInfo[name] or "Couldn't fetch info, select again."))
  257. end
  258.  
  259. function clearEdit()
  260.     local text = guiGetText(source)
  261.     if (text == t("Time") or text == t("Reward Amount")) then
  262.         guiSetText(source, "")
  263.     end
  264. end
  265.  
  266. function loadingCargoDG()
  267.     if (not progressBar) then
  268.         createEventGUI()
  269.     end
  270.     setElementData(localPlayer, "lc", 1)
  271.     guiSetVisible(progressBar, true)
  272.     progressTimer = setTimer(addProgress, 100, 100)
  273.     exports.CIThelp:modTextBar("loadingCargo", t("Loading cargo..."), 255, 167, 0)
  274. end
  275. addEvent("CITsoEvents.addProgressBar", true)
  276. addEventHandler("CITsoEvents.addProgressBar", root, loadingCargoDG)
  277.  
  278. function addProgress()     
  279.     guiProgressBarSetProgress(progressBar, guiProgressBarGetProgress(progressBar) + 1)                 
  280.     if (guiProgressBarGetProgress(progressBar) == 100) then
  281.         triggerServerEvent("CITsoEvents.showMarker", resourceRoot)
  282.         guiSetVisible(progressBar, false)
  283.         guiProgressBarSetProgress(progressBar, 0)
  284.         progressTimer = nil
  285.         setElementData(localPlayer, "lc", nil)
  286.         exports.CIThelp:modTextBar("loadingCargo", "")
  287.     end
  288. end
  289.  
  290. function killProgressBar()
  291.     if (not progressBar) then
  292.         return false
  293.     end
  294.     if (progressTimer) then
  295.         killTimer(progressTimer)
  296.         progressTimer = nil
  297.     end
  298.     guiSetVisible(progressBar, false)
  299.     guiProgressBarSetProgress(progressBar, 0)
  300. end
  301. addEvent("CITsoEvents.killProgressBar", true)
  302. addEventHandler("CITsoEvents.killProgressBar", root, killProgressBar)
  303.  
  304. function outputEventMessage(str)
  305.     if (tostring(getElementData(localPlayer, "CITset.eventoutput")) == "Yes") then
  306.         outputChatBox(str, 255, 167, 0)
  307.     end
  308. end
  309. addEvent("CITsoEvents.OEM", true)
  310. addEventHandler("CITsoEvents.OEM", root, outputEventMessage)
  311.  
  312. function startQuiz(button)
  313.     if (button ~= "left") then
  314.         return false
  315.     end
  316.     local reward = guiGetText(rewardField)
  317.     local quiz = guiGetText(quizField)
  318.     local answer = guiGetText(answerField)
  319.     local timeToStop = guiGetText(timeEdit)
  320.     if (#answer == 0 or #quiz == 0) then   
  321.         exports.CIThelp:dm(t("Missing parts need to be filled."), 190, 0, 0)
  322.         return false
  323.     end
  324.     reward = ((reward and reward ~= "" and tonumber(reward)) or 0)
  325.     if (reward > 7000) then
  326.         exports.CIThelp:dm(t("The reward should not be less than 0 or more than $%s.", "7000"), 190, 0, 0)
  327.         return false
  328.     end
  329.     timeToStop = ((timeToStop and timeToStop ~= "" and tonumber(timeToStop)) or 0)
  330.     if (timeToStop > 10) then
  331.         exports.CIThelp:dm(t("The time should not be less than 0 or more than %s minutes.", "10"), 190, 0, 0)
  332.         return false
  333.     end
  334.     triggerServerEvent("CITsoEvents.startQuiz", resourceRoot, quiz, answer, (reward ~= 0 and reward), ((timeToStop ~= 0 and (timeToStop * 60000)) or (60000 * 5)))
  335. end
  336.  
  337. function stopQuiz(button)
  338.     if (button ~= "left") then
  339.         return false
  340.     end
  341.     triggerServerEvent("CITsoEvents.stopQuiz", resourceRoot)
  342. end
  343.  
  344. function receiveQuiz(msg, r, g, b)
  345.     if ((getElementData(localPlayer, "CITset.soquiz") or "") ~= "Yes") then
  346.         return false
  347.     end
  348.     outputChatBox(msg, r, g, b, true)
  349. end
  350. addEvent("CITsoEvents.receiveQuiz", true)
  351. addEventHandler("CITsoEvents.receiveQuiz", resourceRoot, receiveQuiz)
  352.  
  353. local mysteryBagObj = nil
  354. local mysteryBagTimer = nil
  355.  
  356. function checkUAVMysteryBag()
  357.     if (not mysteryBagObj) then
  358.         if (mysteryBagTimer) then
  359.             killTimer(mysteryBagTimer)
  360.             mysteryBagTimer = nil
  361.         end
  362.         mysteryBagObj = nil
  363.         return false
  364.     end
  365.     if (getElementData(localPlayer, "freecam") or getElementData(localPlayer, "camerafar")) then
  366.         if (getElementAlpha(mysteryBagObj) == 255) then
  367.             setElementAlpha(mysteryBagObj, 0)
  368.         end
  369.     elseif (getElementAlpha(mysteryBagObj) == 0) then
  370.         setElementAlpha(mysteryBagObj, 255)
  371.     end
  372. end
  373.  
  374. function mysteryBagAddTimer(obj)
  375.     mysteryBagObj = obj
  376.     mysteryBagTimer = setTimer(checkUAVMysteryBag, 750, 0)
  377. end
  378. addEvent("CITsoEvents.mysteryBagStarted", true)
  379. addEventHandler("CITsoEvents.mysteryBagStarted", root, mysteryBagAddTimer)
  380.  
  381. function mysteryBagStop()
  382.     if (mysteryBagTimer) then
  383.         killTimer(mysteryBagTimer)
  384.     end
  385.     mysteryBagTimer = nil
  386.     mysteryBagObj = nil
  387. end
  388. addEvent("CITsoEvents.mysteryBagStopped", true)
  389. addEventHandler("CITsoEvents.mysteryBagStopped", root, mysteryBagAddTimer)
Advertisement
Add Comment
Please, Sign In to add comment