-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.lua
More file actions
156 lines (124 loc) · 5.47 KB
/
client.lua
File metadata and controls
156 lines (124 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
-- if you've opted to disable player names in server.lua and
-- then the script will use this name instead
-- %i is replaced with the players' Server ID
local defaultDormantNameFormat = "Dormant Player #%i"
--- [[ CODENS HAPPENING HERE! DONT TOUCH! ]] ---
local playerBlipHandles = {}
local latestBlipsUpdate = {}
local evaluationPlayers = {}
exports("GetDormantBlipHandles", function()
return playerBlipHandles
end)
exports("GetLatestBlipsUpdate", function()
return latestBlipsUpdate
end)
local function has_value(tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
function table.filter(table, it)
local ret = {}
for key, value in pairs(table) do
if it(value, key, table) then
ret[key] = value
end
end
return ret
end
local function GetServerPlayerIds()
-- get all the players that are currently in the same instance
-- as the local player ("active" players)
local rawPlayers = GetActivePlayers()
-- make a new table for storing player server IDs
local playerIDs = {}
-- iterate raw_players using ipairs and insert their server ID
-- into the player_ids table
for index, player in ipairs(rawPlayers) do
table.insert(playerIDs, GetPlayerServerId(player))
end
return playerIDs
end
local function Evaluate()
-- iterate through the players needed to be removed
for _, player in ipairs(evaluationPlayers) do
-- iterate through every blip handle we currently have stored
for player, blip in pairs(playerBlipHandles) do
-- filter through the most recent blips update for blips belonging to this player
local results = table.filter(latestBlipsUpdate, function(blip, key, blips)
return blip[1] == player
end)
-- if there are no blips belonging to this player, attempt to delete and dispose of handle
if(#results == 0 and playerBlipHandles[player] ~= nil) then
-- check if the blip handle does actually exist, if so, delete it
if(DoesBlipExist(playerBlipHandles[player])) then
RemoveBlip(playerBlipHandles[player])
end
-- remove the player blip handle from storage
playerBlipHandles[player] = nil
end
end
end
evaluationPlayers = {}
end
-- this is called when a player leaves the server so clients can clean-up old blips
RegisterNetEvent("_bigmode:evaluateBlips")
AddEventHandler("_bigmode:evaluateBlips", function(player)
table.insert(evaluationPlayers, player)
end)
-- this is called when the server sends a new or updated batch of player data
RegisterNetEvent("_bigmode:updateBlips")
AddEventHandler("_bigmode:updateBlips", function(blips)
latestBlipsUpdate = blips
if(#evaluationPlayers >= 1) then
Evaluate()
end
Citizen.CreateThread(function()
(function()
-- iterate all dormant blips sent to us by the server
for index, blip in pairs(blips) do
local player = blip[1]
local playerPed = NetworkDoesEntityExistWithNetworkId(blip[2]) and NetworkGetEntityFromNetworkId(blip[2]) or nil
-- if the player isn't in our instance and isn't our local player, draw this blip
if((not DoesEntityExist(playerPed)) and player ~= GetPlayerServerId(PlayerId())) then
-- get the players name and coords from the blip
local playerName = blip[3]
local coords = blip[4]
-- sanity check in-case the player name isn't sent by server
if(coords == nil) then
coords = playerName
playerName = nil
end
-- check if a blip already exists on the map for this player and
-- if there is then use the stored handle, otherwise, create a
-- new one and add it to the playerBlipHandles table
local blip = 0
if(playerBlipHandles[player] ~= nil and DoesBlipExist(playerBlipHandles[player])) then
blip = playerBlipHandles[player]
-- update the blips position
SetBlipCoords(blip, coords[1], coords[2], coords[3])
else
-- create the new blip
blip = AddBlipForCoord(coords[1], coords[2], coords[3])
-- set the dormant players' blip properties
SetBlipAlpha(blip, 180)
SetBlipSprite(blip, 1)
SetBlipScale(blip, 0.8)
SetBlipShrink(blip, 1)
SetBlipCategory(blip, 7)
SetBlipDisplay(blip, 6)
-- store the blip handle in the playerBlipHandles table using the players' server ID as key
playerBlipHandles[player] = blip
end
-- set the blip name to the players' name, or the default dormant name if not sent by the server
BeginTextCommandSetBlipName("STRING")
AddTextComponentString(playerName ~= nil and playerName or string.format(defaultDormantNameFormat, player))
EndTextCommandSetBlipName(blip)
end
end
end)()
end)
end)