123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- -- 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
|