-
Notifications
You must be signed in to change notification settings - Fork 6
/
callbacks.lua
164 lines (148 loc) · 4.4 KB
/
callbacks.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
-- This defines all the callbacks needed by server and client.
-- Callbacks are called when certain events happen.
VERSION = "0.6"
-- These are all possible commands clients of the server can send:
CMD = {
CHAT = 128,
MAP = 129,
START_GAME = 130,
GAMESTATE = 131,
NEW_CAR = 132,
MOVE_CAR = 133,
PLAYER_WINS = 134,
BACK_TO_LOBBY = 135,
LAPS = 136,
SERVERCHAT = 138,
STAT = 139,
}
function setServerCallbacks( server )
server.callbacks.received = serverReceived
server.callbacks.synchronize = synchronize
server.callbacks.authorize = function( user, msg ) return lobby:authorize( user, msg ) end
server.callbacks.userFullyConnected = newUser
server.callbacks.disconnectedUser = disconnectedUser
-- Called when there's an error advertising (only on NON-DEDICATED server!):
network.advertise.callbacks.advertiseWarnings = advertisementMsg
end
function setClientCallbacks( client )
-- set client callbacks:
client.callbacks.received = clientReceived
client.callbacks.connected = connected
client.callbacks.disconnected = disconnected
client.callbacks.otherUserConnected = otherUserConnected
client.callbacks.otherUserDisconnected = otherUserDisconnected
-- Called when user is authorized or not (in the second case, a reason is given):
client.callbacks.authorized = function( auth, reason ) menu:authorized( auth, reason ) end
end
-- Called when client is connected to the server
function connected()
lobby:show()
menu:closeConnectPanel()
end
-- Called on server when client is connected to server:
function newUser( user )
lobby:setUserColor( user )
server:setUserValue( user, "moved", true )
server:setUserValue( user, "roundsWon", 0 )
server:send( CMD.SERVERCHAT, WELCOME_MSG, user )
if DEDICATED then
utility.log( "[" .. os.time() .. "] New user: " ..
user.playerName .. " (" .. server:getNumUsers() .. ")" )
end
-- update advertisement:
updateAdvertisementInfo()
end
-- Called when client is disconnected from the server
function disconnected( msg )
menu:show()
if msg and #msg > 0 then
menu:errorMsg( "You have been kicked:", msg )
end
client = nil
server = nil
end
-- Called on server when user disconnects:
function disconnectedUser( user )
if DEDICATED then
utility.log( "[" .. os.time() .. "] User left: " ..
user.playerName .. " (" .. server:getNumUsers() .. ")" )
end
-- update advertisement:
updateAdvertisementInfo()
end
-- Called on server when new client is in the process of
-- connecting.
function synchronize( user )
-- If the server has a map chosen, let the new client know
-- about it:
lobby:sendMap( user )
if STATE == "Game" then
server:send( CMD.START_GAME, "", user )
game:synchronizeCars( user )
end
end
function otherUserConnected( user )
print("TEST!")
if client and client.authorized then
Sounds:play( "beep" )
end
end
function otherUserDisconnected( user )
stats:removeUser( user.id )
end
function serverReceived( command, msg, user )
if command == CMD.CHAT then
-- broadcast chat messages on to all players
server:send( command, user.playerName .. ": " .. msg )
elseif command == CMD.MOVE_CAR then
local x, y = msg:match( "(.*)|(.*)" )
print( "move car:", user.id, x, y, msg)
game:validateCarMovement( user.id, x, y )
end
end
function clientReceived( command, msg )
if command == CMD.CHAT then
chat:newLineSpeech( msg )
elseif command == CMD.SERVERCHAT then
chat:newLineServer( msg )
elseif command == CMD.MAP then
lobby:receiveMap( msg )
elseif command == CMD.START_GAME then
game:show()
elseif command == CMD.GAMESTATE then
game:setState( msg )
elseif command == CMD.NEW_CAR then
game:newCar( msg )
elseif command == CMD.MOVE_CAR then
game:moveCar( msg )
elseif command == CMD.PLAYER_WINS then
game:playerWins( msg )
elseif command == CMD.BACK_TO_LOBBY then
lobby:show()
elseif command == CMD.LAPS then
lobby:receiveLaps( msg )
elseif command == CMD.STAT then
stats:add( msg )
stats:show() -- in case it's not displaying yet, show the stats window
end
end
function updateAdvertisementInfo()
if server then
local players, num = network:getUsers()
if num then
serverInfo.numPlayers = num
end
if STATE == "Game" then
serverInfo.state = "Game"
else
serverInfo.state = "Lobby"
end
serverInfo.map = map:getName()
network.advertise:setInfo( utility.createServerInfo() )
end
end
function advertisementMsg( msg )
if STATE == "Lobby" then
lobby:newWarning( "Could not advertise your game online:\n" .. msg )
end
end