-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.lua
243 lines (191 loc) · 7.37 KB
/
server.lua
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
local maxTargets = 2
local timeForStart = 5000
local timeForTarget = 10000
local timeForIntermission = 10000
local transition = {
Initial = 1337,
None = 0,
IdleToStarting = 1,
StartingToRunning = 2,
RunningToIntermission = 3,
IntermissionToRunning = 4,
RunningToIdle = 5
}
local round = {
seed = 0,
state = 'idle', -- idle, starting, running, intermission
score = {}, -- key is a target index, value is an array where index is a team and value is a score at target
target = 1,
playersTeams = {},
playersAtTarget = {},
timerOfStart = timeForStart,
timerOfTarget = timeForTarget,
timerOfIntermission = timeForIntermission
}
-- Map of players randoms so we can assign them to teams
local booking = {}
-- Map of players steam id's to their current ids
local playersMigrationTable = {}
function onNet(name, cb)
RegisterServerEvent(name)
AddEventHandler(name, cb)
end
function emitSync(to, transition)
TriggerClientEvent('aslt:sync', to, round, transition)
end
function endOfRound()
return round.target >= maxTargets
end
function sourceSteamdId()
return GetPlayerIdentifier(source, 0)
end
onNet('plr', function()
Citizen.Trace('Player steamid: ' .. sourceSteamdId() .. ', id ' .. source .. '\n')
end)
onNet('aslt:start', function(seed)
round = {
seed = seed,
state = 'starting',
target = 1,
score = {0, 0},
playersTeams = {},
playersAtTarget = {},
timeForStart = timeForStart,
timerOfStart = timeForStart,
timeForTarget = timeForTarget,
timerOfTarget = timeForTarget,
timeForIntermission = timeForIntermission,
timerOfIntermission = timeForIntermission
}
booking = {}
playersMigrationTable = {}
emitSync(-1, transition.IdleToStarting)
end)
onNet('aslt:book', function(rnd)
booking[source] = rnd
playersMigrationTable[sourceSteamdId()] = source
end)
-- Request for initial sync
onNet('aslt:sync', function()
-- Lets see if player just reconnected so we can migrate it's data
if round.state ~= 'idle' then
local playerSteamId = sourceSteamdId()
if playersMigrationTable[playerSteamId] then
local oldPlayerId = playersMigrationTable[playerSteamId]
local newPlayerId = source
Citizen.Trace('Migrating player ' .. playerSteamId .. ', was ' .. oldPlayerId .. ', now ' .. newPlayerId .. '\n')
round.playersTeams[newPlayerId] = round.playersTeams[oldPlayerId]
round.playersTeams[oldPlayerId] = nil
round.playersAtTarget[oldPlayerId] = nil
round.playersAtTarget[newPlayerId] = false
playersMigrationTable[playerSteamId] = newPlayerId
end
end
emitSync(source, transition.Initial)
end)
-- Low overhead separate events for potentially spammy things
onNet('aslt:on-target', function()
round.playersAtTarget[source] = true
TriggerClientEvent('aslt:on-target', -1, source)
end)
onNet('aslt:off-target', function()
round.playersAtTarget[source] = false
TriggerClientEvent('aslt:off-target', -1, source)
end)
local syncThreshold = 1000
local lastTime = 0
local sinceLastSync = 9000
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
local currentTime = GetGameTimer()
local dt = currentTime - lastTime
local currentTransition = transition.None
local newTarget = false
sinceLastSync = sinceLastSync + dt
if round.state == 'starting' then
round.timerOfStart = round.timerOfStart - dt
if round.timerOfStart <= 0 then
newTarget = true
currentTransition = transition.StartingToRunning
round.state = 'running'
round.timerOfStart = timeForStart
-- assign to teams
local sortedPlayers = {}
for playerId, playerRnd in pairs(booking) do
table.insert(sortedPlayers, playerId)
end
table.sort(sortedPlayers, function(a, b) return booking[a] < booking[b] end)
for index, playerId in ipairs(sortedPlayers) do
round.playersTeams[playerId] = index % 2
end
end
elseif round.state == 'running' then
local teamsMembersOnTarget = {}
local playersOnTarget = 0
local leadingTeam = -1
local leadingTeamMembersOnTarget = 0
for playerId, onTarget in pairs(round.playersAtTarget) do
local playerTeam = round.playersTeams[playerId]
if onTarget then
playersOnTarget = playersOnTarget + 1
local teamMembersOnTarget = teamsMembersOnTarget[playerTeam]
if teamMembersOnTarget then
teamsMembersOnTarget[playerTeam] = teamMembersOnTarget + 1
else
teamsMembersOnTarget[playerTeam] = 1
end
if teamsMembersOnTarget[playerTeam] > leadingTeamMembersOnTarget then
leadingTeam = playerTeam
leadingTeamMembersOnTarget = teamsMembersOnTarget[playerTeam]
end
end
end
local contesting = true
local meanNumberOfPlayersOnTarget = playersOnTarget / 2
for team, membersOnTarget in pairs(teamsMembersOnTarget) do
if meanNumberOfPlayersOnTarget ~= membersOnTarget then
contesting = false
break
end
end
if playersOnTarget > 0 and not contesting then
round.timerOfTarget = round.timerOfTarget - dt
if round.timerOfTarget <= 0 then
if endOfRound() then
round.state = 'idle'
currentTransition = transition.RunningToIdle
else
round.state = 'intermission'
round.timerOfIntermission = timeForIntermission
currentTransition = transition.RunningToIntermission
end
local leadingTeamScore = round.score[leadingTeam + 1]
round.score[leadingTeam + 1] = leadingTeamScore + 1
round.timerOfTarget = timeForTarget
end
end
elseif round.state == 'intermission' then
round.timerOfIntermission = round.timerOfIntermission - dt
if round.timerOfIntermission <= 0 then
newTarget = true
currentTransition = transition.IntermissionToRunning
round.state = 'running'
round.target = round.target + 1
round.timerOfTarget = timeForTarget
round.playersAtTarget = {}
round.timerOfIntermission = timeForIntermission
end
end
if targetCreationPending then
round.timerOfTarget = timeForTarget
round.playersAtTarget = {}
end
local needsRoutineSync = sinceLastSync > syncThreshold and round.state ~= 'idle'
if currentTransition ~= transition.None or needsRoutineSync then
emitSync(-1, currentTransition)
sinceLastSync = 0
end
lastTime = currentTime
end
end)