From 5b5a19353bc119d4627668b79d834f93e7043ac5 Mon Sep 17 00:00:00 2001 From: Jordan Dalton Date: Mon, 18 Sep 2017 23:08:51 +0100 Subject: [PATCH] feat: update various blip mechanics Like a lot of stuff here 1. Blips get saved when server stops 2. Blips get loaded on resource start 3. Blip coords are now rounded to 2dp 4. Blip indexes are now strings (had some issues when they were numbers.. fucking hate Lua) 5. Added some new event handlers 5a. AddBlip = Adds a blip to the blips table 5b. UpdateBlip = Updates a blip in the table --- client/blips_client.lua | 8 ++- server/blips_server.lua | 127 ++++++++++++++++++++++++++++++++-------- 2 files changed, 107 insertions(+), 28 deletions(-) diff --git a/client/blips_client.lua b/client/blips_client.lua index fb1613a..9a33344 100644 --- a/client/blips_client.lua +++ b/client/blips_client.lua @@ -39,16 +39,18 @@ AddEventHandler("livemap:getBlipsFromClient", function() for _,spriteId in pairs(blip_types) do local blip = GetFirstBlipInfoId(spriteId) - if DoesBlipExist(blip) then -- If the first blip exists, we're going to need an entry + if DoesBlipExist(blip) then -- If the first blip exists, we're going to need an entry blipTable[spriteId] = {} end while (DoesBlipExist(blip)) do local x,y,z = table.unpack(GetBlipCoords(blip)) - Citizen.Trace("Blip1: " .. x .. " " .. y .. " " .. z .. "") + -- Damn! There's no way to get the fucking display text for the blip :( table.insert(blipTable[spriteId], { - x = x, y = y, z = z + x = tonumber(string.format("%.2f", x)), -- Round them to 2dp + y = tonumber(string.format("%.2f", y)), + z = tonumber(string.format("%.2f", z)) }) blip = GetNextBlipInfoId(spriteId) diff --git a/server/blips_server.lua b/server/blips_server.lua index c6d3f0a..a8faca0 100644 --- a/server/blips_server.lua +++ b/server/blips_server.lua @@ -16,52 +16,108 @@ You should have received a copy of the GNU General Public License along with this program in the file "LICENSE". If not, see . ]] ---[[ - Blips!!! - This file should expose the "blips.json" file to that sexy new http handler -]] - RegisterServerEvent("livemap:blipsGenerated") +RegisterServerEvent("livemap:AddBlip") +RegisterServerEvent("livemap:UpdateBlip") -local cache = {} +local blips = {} -- Set this to the player who did the "blips generate" command. Should stop -- randomers triggering the event :) local playerWhoGeneratedBlips = nil -function cacheIsEmpty() - for _, _ in pairs(cache) do +function blipsIsEmpty() + for _, _ in pairs(blips) do return false end return true end +function blipsContainsBlip(id, x, y, z) + if blips[id] == nil then + return -1 + end + + for k,v in ipairs(blips[id]) do + if v.x == x and v.y == y and v.z == z then + return k + end + end + + return -1 +end + function sendBlips(res) -- Restrict the origin if set, otherwise allow everyone res.writeHead(200, { ["Access-Control-Allow-Origin"] = GetConvar("livemap_access_control", "*")} ) - - if not cacheIsEmpty() then - res.send(json.encode(cache)) + + if not blipsIsEmpty() then + res.send(json.encode(blips)) return else - -- Send cached blips - local blipFile = GetConvar("blip_file", "server/blips.json") - local blipData = LoadResourceFile(GetCurrentResourceName(), blipFile) + res.send(json.encode({ + error = "blip cache is empty" + })) + end +end + +AddEventHandler("livemap:AddBlip", function(blip) + if not blip["sprite"] or blip["sprite"] == nil then + -- We don't have a sprite id... we can't save it :( + print("LiveMap Error: AddBlip cannot run because no sprite was supplied.") + return + end + local id = tostring(blip["sprite"]) + + blip["x"] = tonumber(string.format("%.2f", blip["x"])) + blip["z"] = tonumber(string.format("%.2f", blip["z"])) + blip["y"] = tonumber(string.format("%.2f", blip["y"])) - if not blipData then - print("LIVE MAP ERROR: No blip file... Have you generated them yet?") - res.send(json.encode({ error = "no blips found" }) ) - return + if blipsContainsBlip(id, blip.x, blip.y, blip.z) == -1 then + if blips[id] == nil then + blips[id] = {} end - cache = json.decode(blipData) - res.send(blipData) + table.insert(blips[id], blip) + else + print("Blip already exists at " .. blip.x .. "," .. blip.y .. "," .. blip.z) + end +end) + +AddEventHandler("livemap:UpdateBlip", function(blip) + if not blip["sprite"] or blip["sprite"] == nil then + -- We don't have a sprite id... we can't save it :( + print("LiveMap Error: AddBlip cannot run because no sprite was supplied.") + return + end + local id = tostring(blip["sprite"]) + + blip["x"] = tonumber(string.format("%.2f", blip["x"])) + blip["z"] = tonumber(string.format("%.2f", blip["z"])) + blip["y"] = tonumber(string.format("%.2f", blip["y"])) + + local index = blipsContainsBlip(id, blip.x, blip.y, blip.z) + + if index ~= -1 then + blips[id][index] = blip + else + -- Blip doesn't exist, add it? + if blips[id] == nil then + blips[id] = {} + end + + table.insert(blips[id], blip) + end +end) + +function saveBlips() + if blipsIsEmpty() then + print("Blips are empty... No need to save") + return end -end -function saveBlips(blipTable) local blipFile = GetConvar("blip_file", "server/blips.json") - local saved = SaveResourceFile(GetCurrentResourceName(), blipFile, json.encode(blipTable, {indent = true}), -1) + local saved = SaveResourceFile(GetCurrentResourceName(), blipFile, json.encode(blips, {indent = true}), -1) if not saved then print("LiveMap couldn't save the blips to file.. Maybe the directory isn't writable?") @@ -77,10 +133,12 @@ AddEventHandler("livemap:blipsGenerated", function(blipTable) return end - saveBlips(blipTable) - cache = blipTable + blips = blipTable + + saveBlips() end) + RegisterCommand("blips", function(source, args, rawCommand) local playerId = GetPlayerIdentifier(source, 0) if args[1] == "generate" then @@ -94,6 +152,25 @@ RegisterCommand("blips", function(source, args, rawCommand) end, true) +AddEventHandler("onResourceStop", function(name) + if name == GetCurrentResourceName() then + saveBlips() -- Make sure the blips file is saved + end +end) + +AddEventHandler("onResourceStart", function(name) + if name == GetCurrentResourceName() then + local blipFile = GetConvar("blip_file", "server/blips.json") + local blipData = LoadResourceFile(GetCurrentResourceName(), blipFile) + + if blipData then + blips = json.decode(blipData) + print("loaded blip cache from file") + end + end +end) + + --[[ Handle HTTP requests. Mainly used for getting the blips via ajax from the UI ]]