Skip to content

Commit

Permalink
feat: update various blip mechanics
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TGRHavoc committed May 17, 2019
1 parent b1bb783 commit 5b5a193
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 28 deletions.
8 changes: 5 additions & 3 deletions client/blips_client.lua
Expand Up @@ -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)
Expand Down
127 changes: 102 additions & 25 deletions server/blips_server.lua
Expand Up @@ -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 <http://www.gnu.org/licenses/>.
]]

--[[
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?")
Expand All @@ -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
Expand All @@ -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
]]
Expand Down

0 comments on commit 5b5a193

Please sign in to comment.