Skip to content

Commit

Permalink
fix: async write errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TGRHavoc committed May 17, 2019
1 parent acc71d4 commit 64583fe
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 68 deletions.
43 changes: 43 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,9 +2,52 @@ Changelog
=========


v2.2.0 (30-10-2017)
-------------------

New
~~~
- Add live blips. [Jordan Dalton]

When blips are added and removed, they are now sent to the map so it can update itself.

I've also added some commands to allow users to add/remove blips from in-game with `blip remove` and `blip add <sprite> [name] [description]`

People are still getting errors about writing to sockets at the same time.. FFS.
- Add event handlers for blips. [Jordan Dalton]

Added add/update/remove events for blips.

They all take a dynamic object that represnts the blips to modify.

Fix
~~~
- Fixed native failing on restart. [Jordan Dalton]

When the resource would restart with players on the server, there would be an error thrown. This is now fixed..

Other
~~~~~
- Merge branch 'feature/live_blips' into develop. [Jordan Dalton]


v2.1.10 (29-10-2017)
--------------------

Fix
~~~
- Fixed Server_OnError error. [Jordan Dalton]

Now checking if the websocket is null before removing and disposing


v2.1.9 (29-10-2017)
-------------------

Changes
~~~~~~~
- Update changelog. [Jordan Dalton]

Fix
~~~
- Fixed server freezing issues. [Jordan Dalton]
Expand Down
52 changes: 52 additions & 0 deletions client/blips_client.lua
Expand Up @@ -62,3 +62,55 @@ AddEventHandler("livemap:getBlipsFromClient", function()
Citizen.Trace("Generated the blips!")
TriggerServerEvent("livemap:blipsGenerated", blipTable)
end)

RegisterCommand("blip", function(source, args, raw)

for i=1, #args do
print(i .. " = " .. args[i] .. " " .. type(args[i]))
end

if(args[1] == "add") then
local pos = GetEntityCoords( PlayerPedId(), not IsPlayerDead(PlayerId()) )
local spriteId = tonumber(args[2])

if spriteId == nil then
print("Sorry, spriteId must be a number")
return
end
local name, description = "", ""
if args[3] then
name = args[3]
end
if args[4] then
description = args[4]
end

print(spriteId .. " " .. name .. " " .. description .. " " .. pos.x .. " " .. pos.y .. " " .. pos.z)

local blip = {
sprite = spriteId,
pos = {
x = pos.x,
y = pos.y,
z = pos.z
},
name = name,
description = description
}

TriggerServerEvent("livemap:AddBlip", blip)

elseif(args[1] == "remove") then
local pos = GetEntityCoords(PlayerPedId(), not IsPlayerDead(PlayerId()))

TriggerServerEvent("livemap:RemoveClosestBlip", {x = pos.x, y = pos.y, z = pos.z})

elseif(args[1] == "update") then

else
print("Sorry, the available blip arguments are as follows:")
print("add <spriteId> [name] [descriptionn] -- Adds a blip at your current position")
print("remove [radius] -- Removed the closes blip within the radius")
print("update <spriteId> <name> <description> -- Updates the closes blip with this data")
end
end, true)
161 changes: 137 additions & 24 deletions server/blips_server.lua
Expand Up @@ -30,16 +30,19 @@ RegisterServerEvent("livemap:blipsGenerated")
A blip follows the format:
{
sprite = Number (required)
x = Float (required)
y = Float (required)
z = Float (required)
pos = Table (required){
x = Float (required)
y = Float (required)
z = Float (required)
}
name = String (optional)
description = String (optional)
}
sprite is the "spriteId" used when creating a blip.
x,y,z represents the position of the blip (rounded to 2dp)
x,y,z represents the position of the blip (rounded to 2dp) and is stored inside "pos"
name is the name of the blip shown on the UI
desscription is a description of what the blip does, shown on UI
]]
Expand All @@ -55,6 +58,9 @@ RegisterServerEvent("livemap:AddBlip")
]]
RegisterServerEvent("livemap:UpdateBlip")

RegisterServerEvent("livemap:RemoveClosestBlip")
RegisterServerEvent("livemap:RemoveBlip")



-- An array of blips that we have. This is encoded into JSON and sent over HTTP (when requested)
Expand Down Expand Up @@ -98,7 +104,7 @@ function blipsContainsBlip(id, x, y, z)
end

for k,v in ipairs(blips[id]) do
if v.x == x and v.y == y and v.z == z then
if v.pos.x == x and v.pos.y == y and v.pos.z == z then
return k
end
end
Expand Down Expand Up @@ -152,64 +158,160 @@ end


AddEventHandler("livemap:AddBlip", function(blip)
-- Only let the host of the session update the blips
if source ~= GetHostId() then
return
end

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.")
print("LiveMap Error: AddBlip cannot run because no sprite wasn't supplied.")
return
end

if not blip["pos"] or blip["pos"] == nil then
print("LiveMap Error:: AddBlip cannot run because the pos wasn't 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"]))
blip["sprite"] = nil

if blipsContainsBlip(id, blip.x, blip.y, blip.z) == -1 then
-- Conveert coordinates to 2dp
blip["pos"].x = tonumber(string.format("%.2f", blip["pos"].x))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
blip["pos"].z = tonumber(string.format("%.2f", blip["pos"].z))

if blipsContainsBlip(id, blip.pos.x, blip.pos.y, blip.pos.z) == -1 then
if blips[id] == nil then
blips[id] = {}
end

table.insert(blips[id], blip)
TriggerEvent("livemap:internal_AddBlip", id, blip)
print("Sending addBlip for " .. id)

else
print("Blip already exists at " .. blip.x .. "," .. blip.y .. "," .. blip.z)
print("Blip already exists at " .. blip.pos.x .. "," .. blip.pos.y .. "," .. blip.pos.z)
end
end)

AddEventHandler("livemap:UpdateBlip", function(blip)
-- Only let the host of the session update the blips
if source ~= GetHostId() then
return
end

if not blip["sprite"] or blip["sprite"] == nil then
-- We don't have a sprite id... we can't save it :(
print("LiveMap Error: UpdateBlip cannot run because no sprite was supplied.")
print("LiveMap Error: UpdateBlip cannot run because no sprite wasn't supplied.")
return
end

if not blip["pos"] or blip["pos"] == nil then
print("LiveMap Error:: UpdateBlip cannot run because the pos wasn't 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"]))
blip["sprite"] = nil

local index = blipsContainsBlip(id, blip.x, blip.y, blip.z)
blip["pos"].x = tonumber(string.format("%.2f", blip["pos"].x))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))

local index = blipsContainsBlip(id, blip.pos.x, blip.pos.y, blip.pos.z)

if index ~= -1 then
blips[id][index] = blip

TriggerEvent("livemap:internal_UpdateBlip", id, blip)

else
-- Blip doesn't exist, add it?
if blips[id] == nil then
blips[id] = {}
end

table.insert(blips[id], blip)

TriggerEvent("livemap:internal_AddBlip", id, blip)

end
end)

AddEventHandler("livemap:RemoveBlip", 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: RemoveBlip cannot run because no sprite wasn't supplied.")
return
end

if not blip["pos"] or blip["pos"] == nil then
print("LiveMap Error:: RemoveBlip cannot run because the pos wasn't supplied.")
return
end

local id = tostring(blip["sprite"])

blip["sprite"] = nil

blip["pos"].x = tonumber(string.format("%.2f", blip["pos"].x))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))

local index = blipsContainsBlip(id, blip.pos.x, blip.pos.y, blip.pos.z)

if index == -1 then
print("LiveMap Error: Cannot remove blip as it isn't in the table")
else
blips[id][index] = nil
TriggerEvent("livemap:RemoveBlip", id, blip)
end
end)

function getDistanceBetween(p1, p2)
-- Apparently math.pow doesn't exist (nil value)
local x = (p2.x - p1.x) * (p2.x - p1.x)
local y = (p2.y - p1.y) * (p2.y - p1.y)
local z = (p2.z - p1.z) * (p2.z - p1.z)

-- sqrt works though!
return math.sqrt(x + y + z)
end

AddEventHandler("livemap:RemoveClosestBlip", function(playerPos)
local currentClosest = nil
local sprite = nil
local currentDistance = 999999

for spriteId, blipArray in pairs(blips) do
for _, blip in pairs(blipArray) do
local distance = getDistanceBetween(playerPos, blip.pos)

if (distance < currentDistance) then
currentDistance = distance
currentClosest = blip
sprite = spriteId
end

end
end

if currentClosest == nil then
-- No blips at all?
return
end

print("Removing closest blip ("..sprite..") located at: " .. currentClosest.pos.x .. ", " .. currentClosest.pos.y .. ", " .. currentClosest.pos.z)

local index = blipsContainsBlip(sprite, currentClosest.pos.x, currentClosest.pos.y, currentClosest.pos.z)

if index == -1 then
print("LiveMap Error: Umm... The closest blip doesn't have an index.. Something seriously wrong here!!!")
else
blips[sprite][index] = nil
TriggerEvent("livemap:internal_RemoveBlip", sprite, currentClosest)
end

end)


AddEventHandler("livemap:blipsGenerated", function(blipTable)
local id = GetPlayerIdentifier(source, 0)

Expand All @@ -236,6 +338,17 @@ AddEventHandler("onResourceStart", function(name)

if blipData then
blips = json.decode(blipData)

-- Update all the old positions to use the new one :)
for spriteId, blipArray in pairs(blips) do
for _, blip in pairs(blipArray) do
if not blip["pos"] then
blip.pos = {x=blip.x, y=blip.y, z=blip.z}
blip.x, blip.y, blip.z = nil,nil,nil
end
end
end

print("loaded blip cache from file")
end
end
Expand Down
2 changes: 1 addition & 1 deletion server/wrapper.lua
Expand Up @@ -128,7 +128,7 @@ AddEventHandler("onResourceStart", function(name)
--print("NUMBER OF PLAYERS: " .. GetNumPlayerIndices())
if name == GetCurrentResourceName() then
if GetNumPlayerIndices() ~= 0 then -- There's players on the server :)
for i=0, GetNumPlayerIndices() do
for i=0, GetNumPlayerIndices()-1 do
local id = GetPlayerIdentifier(GetPlayerFromIndex(i), 0)
setStaticDataFor(GetPlayerFromIndex(i), id)
end
Expand Down
1 change: 1 addition & 0 deletions src/Live Map/Live Map.csproj
Expand Up @@ -55,6 +55,7 @@
<Compile Include="LiveMap.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SocketHandler.cs" />
<Compile Include="SocketWrapper.cs" />
<Compile Include="WebSocketServer.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 64583fe

Please sign in to comment.