-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlobby.lua
More file actions
469 lines (393 loc) · 10.8 KB
/
lobby.lua
File metadata and controls
469 lines (393 loc) · 10.8 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
local panel = require( "panel" )
local lobby = {
camMoveTime = 5,
currentMapString = nil,
ready = false,
--locked = false,
timers = {},
countdown = nil,
}
lobby.colors = {
{ 0, 0, 0 },
{ 25, 25, 25 },
{ 255, 255, 255 },
{ 255, 0, 0 },
{ 0, 255, 0 },
{ 0, 0, 255 },
{ 255, 255, 0 },
{ 0, 255, 255 },
{ 255, 0, 255 },
{ 128, 0, 0 },
{ 0, 128, 0 },
{ 0, 0, 128 },
{ 255, 128, 0 },
{ 128, 255, 0 },
{ 128, 0, 255 },
{ 255, 0, 128 },
{ 0, 255, 128 },
{ 0, 128, 255 },
{ 255, 128, 128 },
{ 128, 255, 128 },
{ 128, 128, 255 },
{ 255, 128, 128 },
{ 128, 255, 128 },
{ 128, 128, 255 },
{ 0, 128, 128 },
{ 128, 0, 128 },
{ 128, 128, 0 },
{ 0, 64, 128 },
{ 0, 128, 64 },
{ 64, 0, 128 },
{ 128, 0, 64 },
{ 64, 128, 0 },
{ 128, 64, 0 },
}
local scr
-- The first level in the list to display:
local levelListStart = 1
local levelNameList = nil
local ERROR_MSG = nil
local ERROR_TIMER = 0
function lobby:init()
scr = ui:newScreen( "lobby" )
scr:addPanel( "topPanel",
0, 0,
love.graphics.getWidth(), 35 )
scr:addFunction( "topPanel", "close", 20, 0, "Leave", "q", lobby.close )
scr:addFunction( "topPanel", "stats", 3*love.graphics.getWidth()/4-40, 0, "Toggle Statistics", "t",
function() lobby:toggleStats() end )
scr:addFunction( "topPanel", "ready", love.graphics.getWidth() - 100, 0, "Ready", "r",
function() lobby:toggleReady() end )
self.userPanel = panel:new( 0, 0, 300, 18 )
end
function lobby:show()
print("Starting lobby")
STATE = "Lobby"
--self.currentMapString = nil
--self.locked = false
self.timers = {}
self.countdown = nil
if server then
for k, u in pairs( server:getUsers() ) do
server:setUserValue( u, "ready", false )
server:setUserValue( u, "ingame", false )
end
if DEDICATED then
--[[local t = Timer:new( 5, function() dedicated:chooseMap() end )
table.insert( self.timers, t )
dedicated:postMatchLock()]]
dedicated:chooseMap()
end
updateAdvertisementInfo()
end
if client then
ui:setActiveScreen( scr )
self.ready = false
-- In case I was a server before, remove the server settings:
if levelNameList ~= nil then
scr:removeFunction( "topPanel", "start" )
scr:removeList( levelNameList.name )
levelNameList = nil
end
chat:show()
if map.loaded then
map:zoomOut()
end
ERROR_TIMER = 0
end
-- If I'm the server, then let me choose the map:
if server and client then
self:createLevelList()
levelListStart = 1
scr:addFunction( "topPanel", "start", love.graphics.getWidth()/2 - 20, 0, "Start", "s",
function() lobby:attemptGameStart() end )
end
end
function lobby:update( dt )
map:update( dt )
if map.loaded then
self.camMoveTime = self.camMoveTime + dt
if self.camMoveTime > 4 then
local x = math.random( map.Boundary.minX, map.Boundary.maxX )
local y = math.random( map.Boundary.minY, map.Boundary.maxY )
map:camSwingToPos( x, y, 2 )
map:camZoom( 0.05 + math.random( 0, 1 )*0.05, 2)
self.camMoveTime = self.camMoveTime - 4
end
end
-- Continue any timers:
for k, t in ipairs( self.timers ) do
-- if the timer fires, delete it:
if t:updateAndFire( dt ) then
table.remove( self.timers, k )
end
end
if self.countdown then
self.countdown = self.countdown - dt
if self.countdown < 5 and not self.sentCountdownTimes[math.ceil(self.countdown)] then
if math.ceil(self.countdown) > 0 then
server:send( CMD.SERVERCHAT, math.ceil(self.countdown))
self.sentCountdownTimes[math.ceil(self.countdown)] = true
end
end
end
if ERROR_TIMER > 0 then
ERROR_TIMER = ERROR_TIMER - dt
end
if client then
stats:update( dt )
end
end
function lobby:draw()
map:draw()
lobby:drawUserList()
if ERROR_TIMER > 0 then
ERROR_PANEL:draw()
love.graphics.setColor( 255,128,0, 200 )
love.graphics.printf( ERROR_MSG, 60, love.graphics.getHeight() - 65,
love.graphics.getWidth() - 120, "center" )
end
if client then
stats:draw()
end
end
function lobby:drawUserList()
-- Print list of users:
love.graphics.setColor( 255,255,255, 255 )
local users, num = network:getUsers()
local x, y = 20, 60
local i = 1
if client and users then
--love.graphics.setColor( 0, 0, 0, 128 )
--love.graphics.rectangle( "fill", x - 5, y - 5, 300, num*20 + 5 )
for k, u in pairs( users ) do
self.userPanel:draw( x - 5, y - 3 )
love.graphics.setColor( 255,255,255, 255 )
love.graphics.printf( i .. ":", x, y, 20, "right" )
love.graphics.printf( u.playerName, x + 25, y, 250, "left" )
if u.customData.ready == true then
love.graphics.setColor( 128, 255, 128, 255 )
local dx = love.graphics.getFont():getWidth( u.playerName ) + 30
love.graphics.print( "[Ready]", x + dx, y )
end
y = y + 20
i = i + 1
end
end
end
function lobby:keypressed( key )
end
function lobby:mousepressed( button, x, y )
end
function lobby:close()
network:closeConnection()
end
function lobby:createLevelList()
local levelnames = love.filesystem.getDirectoryItems( "maps" )
if levelNameList then
scr:removeList( levelNameList.name )
end
local list = {}
--[[if levelListStart > 1 then
local entry = { txt="Up", event=function() lobby:moveLevelList( -1 ) end, key = "u" }
table.insert( list, entry )
end]]
-- Display up to 8 items in the list:
for i = 1, #levelnames do
if levelnames[i]:match("(.*).stl") or levelnames[i]:match("(.*).STL") then
local entry = {
txt=levelnames[i],
event=function() self:chooseMap( levelnames[i] ) end,
}
table.insert( list, entry )
end
end
--[[if levelListStart + 7 < #levelnames then
local entry = { txt="Down", event=function() lobby:moveLevelList( 1 ) end, key = "d" }
table.insert( list, entry )
end]]
levelNameList = scr:newList( love.graphics.getWidth() - 300, 60, 160, list, 9 )
end
function lobby:chooseMap( levelname )
-- SERVER ONLY!
if not server then return end
print("Loading map: " .. levelname )
self.currentMapString = nil
-- Mapstring is the map, in serialized form:
local mapstring
if not DEDICATED then
mapstring = love.filesystem.read( "maps/" .. levelname )
else
local f = io.open( "maps/" .. levelname, "r")
if f then
mapstring = f:read("*all")
f:close()
end
end
map:setName( levelname )
map:newFromString(mapstring)
if map.loaded then
-- Remember for later:
self.currentMapString = mapstring
self:sendMap()
print("\t->loaded!" )
updateAdvertisementInfo()
end
end
function lobby:sendMap( user )
-- SERVER ONLY!
if not server then return end
if self.currentMapString then
-- Remove linebreaks and replace by pipe symbol for sending.
--mapstring = self.currentMapString:gsub( "\n", "|" )
if user then
-- Send to single user?
server:send( CMD.MAP, self.currentMapString, user )
server:send( CMD.LAPS, tostring(LAPS), user )
else
-- Broadcast to all:
server:send( CMD.MAP, self.currentMapString )
server:send( CMD.LAPS, tostring(LAPS) )
end
end
end
function lobby:receiveMap( mapstring )
-- CLIENT ONLY!
if not client then return end
if server then return end
-- Re-add line breaks (fallback for earlier versions:)
if not mapstring:find( "\n" ) then
mapstring = mapstring:gsub( "|", "\n" )
end
map:newFromString( mapstring )
end
function lobby:receiveLaps( laps )
laps = tonumber(laps)
game.numberOfLaps = laps
end
function lobby:toggleReady()
if client then
local ready = client:getUserValue( "ready" )
client:setUserValue( "ready", not ready )
end
end
function lobby:toggleStats()
if stats.display then
stats:hide()
else
stats:show()
end
end
function lobby:attemptGameStart()
-- SERVER ONLY!
if not server then return end
if not map.loaded then
if not DEDICATED then
local commands = {}
commands[1] = { txt = "Ok", key = "y" }
scr:newMsgBox( "Cannot start:", "No valid map file loaded.", nil, nil, nil, commands)
end
return false
end
local allReady = true
local usersReady = 0
local users = network:getUsers()
for k, u in pairs( users ) do
if u.customData.ready then
usersReady = usersReady + 1
else
allReady = false
end
end
-- ON DEDICATED ONLY!
if DEDICATED and not allReady then
if usersReady == 0 then
if self.countdown then
self.countdown = nil
server:send( CMD.SERVERCHAT, "Server halted countdown. No one is ready." )
end
else
if self.countdown == nil then
self.countdown = COUNTDOWN
self.sentCountdownTimes = {}
server:send( CMD.SERVERCHAT, "Round starts in " .. COUNTDOWN .. " seconds. Get ready to play! ('R' to join this round.)" )
end
end
end
-- If all clients are ready, then they must also all be synchronized.
-- So we're ok to start.
if allReady or (self.countdown and self.countdown <= 0 ) then
lobby:startGame()
return true
elseif not DEDICATED then
if usersReady >= 1 then
local commands = {}
commands[1] = { txt = "Yes", key = "y", event = function() lobby:startGame() end }
commands[2] = { txt = "No, wait", key = "n" }
scr:newMsgBox( "Are you sure you want to start?", "Some users aren't ready yet. They won't be part of the round if you start now.", nil, nil, nil, commands)
else
local commands = {}
commands[1] = { txt = "Ok", key = "y" }
scr:newMsgBox( "Cannot start:", "No one is ready yet.", nil, nil, nil, commands)
end
end
return false
end
function lobby:startGame()
--self.locked = true -- don't let any more users join!
server:send( CMD.START_GAME )
--lobby:kickAllWhoArentReady()
self.countdown = nil
for k, u in pairs( server:getUsers() ) do
if u.customData.ready then
server:setUserValue( u, "ingame", true ) -- everyone else is spectating!
end
end
end
function lobby:authorize( user, authorizationRequest )
--[[if self.locked then
return false, "Game already started."
else
return true
end]]
-- Let only users running the correct version authorize:
if authorizationRequest == VERSION then
return true
else
return false, "Version mismatch. You're running version " .. authorizationRequest ..
", server version is " .. VERSION .. "."
end
end
function lobby:setUserColor( user )
-- SERVER ONLY!
if server then
math.randomseed( os.time() )
local col = lobby.colors[ math.random(#lobby.colors) ]
server:setUserValue( user, "red", col[1] )
server:setUserValue( user, "green", col[2] )
server:setUserValue( user, "blue", col[3] )
server:setUserValue( user, "body", math.random(NUM_CAR_IMAGES) )
end
end
function lobby:errorMsg( msg )
local commands = {}
commands[1] = { txt = "Ok", key = "y" }
scr:newMsgBox( "Error loading map:", msg, nil, nil, nil, commands)
end
function lobby:kickAllWhoArentReady()
if server then
local users = network:getUsers()
for k, u in pairs( users ) do
if not u.customData.ready then
server:kickUser( u, "Game started, and you were not ready." )
end
end
end
end
function lobby:newWarning( msg )
ERROR_MSG = msg
ERROR_TIMER = 10
ERROR_PANEL = panel:new( 60, love.graphics.getHeight() - 70,
love.graphics.getWidth() - 120, love.graphics.getFont():getHeight()*2 + 10 )
end
return lobby