callbacks.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. -- This defines all the callbacks needed by server and client.
  2. -- Callbacks are called when certain events happen.
  3. VERSION = "0.6"
  4. -- These are all possible commands clients of the server can send:
  5. CMD = {
  6. CHAT = 128,
  7. MAP = 129,
  8. START_GAME = 130,
  9. GAMESTATE = 131,
  10. NEW_CAR = 132,
  11. MOVE_CAR = 133,
  12. PLAYER_WINS = 134,
  13. BACK_TO_LOBBY = 135,
  14. LAPS = 136,
  15. SERVERCHAT = 138,
  16. STAT = 139,
  17. }
  18. function setServerCallbacks( server )
  19. server.callbacks.received = serverReceived
  20. server.callbacks.synchronize = synchronize
  21. server.callbacks.authorize = function( user, msg ) return lobby:authorize( user, msg ) end
  22. server.callbacks.userFullyConnected = newUser
  23. server.callbacks.disconnectedUser = disconnectedUser
  24. -- Called when there's an error advertising (only on NON-DEDICATED server!):
  25. network.advertise.callbacks.advertiseWarnings = advertisementMsg
  26. end
  27. function setClientCallbacks( client )
  28. -- set client callbacks:
  29. client.callbacks.received = clientReceived
  30. client.callbacks.connected = connected
  31. client.callbacks.disconnected = disconnected
  32. client.callbacks.otherUserConnected = otherUserConnected
  33. client.callbacks.otherUserDisconnected = otherUserDisconnected
  34. -- Called when user is authorized or not (in the second case, a reason is given):
  35. client.callbacks.authorized = function( auth, reason ) menu:authorized( auth, reason ) end
  36. end
  37. -- Called when client is connected to the server
  38. function connected()
  39. lobby:show()
  40. menu:closeConnectPanel()
  41. end
  42. -- Called on server when client is connected to server:
  43. function newUser( user )
  44. lobby:setUserColor( user )
  45. server:setUserValue( user, "moved", true )
  46. server:setUserValue( user, "roundsWon", 0 )
  47. server:send( CMD.SERVERCHAT, WELCOME_MSG, user )
  48. if DEDICATED then
  49. utility.log( "[" .. os.time() .. "] New user: " ..
  50. user.playerName .. " (" .. server:getNumUsers() .. ")" )
  51. end
  52. -- update advertisement:
  53. updateAdvertisementInfo()
  54. end
  55. -- Called when client is disconnected from the server
  56. function disconnected( msg )
  57. menu:show()
  58. if msg and #msg > 0 then
  59. menu:errorMsg( "You have been kicked:", msg )
  60. end
  61. client = nil
  62. server = nil
  63. end
  64. -- Called on server when user disconnects:
  65. function disconnectedUser( user )
  66. if DEDICATED then
  67. utility.log( "[" .. os.time() .. "] User left: " ..
  68. user.playerName .. " (" .. server:getNumUsers() .. ")" )
  69. end
  70. -- update advertisement:
  71. updateAdvertisementInfo()
  72. end
  73. -- Called on server when new client is in the process of
  74. -- connecting.
  75. function synchronize( user )
  76. -- If the server has a map chosen, let the new client know
  77. -- about it:
  78. lobby:sendMap( user )
  79. if STATE == "Game" then
  80. server:send( CMD.START_GAME, "", user )
  81. game:synchronizeCars( user )
  82. end
  83. end
  84. function otherUserConnected( user )
  85. print("TEST!")
  86. if client and client.authorized then
  87. Sounds:play( "beep" )
  88. end
  89. end
  90. function otherUserDisconnected( user )
  91. stats:removeUser( user.id )
  92. end
  93. function serverReceived( command, msg, user )
  94. if command == CMD.CHAT then
  95. -- broadcast chat messages on to all players
  96. server:send( command, user.playerName .. ": " .. msg )
  97. elseif command == CMD.MOVE_CAR then
  98. local x, y = msg:match( "(.*)|(.*)" )
  99. print( "move car:", user.id, x, y, msg)
  100. game:validateCarMovement( user.id, x, y )
  101. end
  102. end
  103. function clientReceived( command, msg )
  104. if command == CMD.CHAT then
  105. chat:newLineSpeech( msg )
  106. elseif command == CMD.SERVERCHAT then
  107. chat:newLineServer( msg )
  108. elseif command == CMD.MAP then
  109. lobby:receiveMap( msg )
  110. elseif command == CMD.START_GAME then
  111. game:show()
  112. elseif command == CMD.GAMESTATE then
  113. game:setState( msg )
  114. elseif command == CMD.NEW_CAR then
  115. game:newCar( msg )
  116. elseif command == CMD.MOVE_CAR then
  117. game:moveCar( msg )
  118. elseif command == CMD.PLAYER_WINS then
  119. game:playerWins( msg )
  120. elseif command == CMD.BACK_TO_LOBBY then
  121. lobby:show()
  122. elseif command == CMD.LAPS then
  123. lobby:receiveLaps( msg )
  124. elseif command == CMD.STAT then
  125. stats:add( msg )
  126. stats:show() -- in case it's not displaying yet, show the stats window
  127. end
  128. end
  129. function updateAdvertisementInfo()
  130. if server then
  131. local players, num = network:getUsers()
  132. if num then
  133. serverInfo.numPlayers = num
  134. end
  135. if STATE == "Game" then
  136. serverInfo.state = "Game"
  137. else
  138. serverInfo.state = "Lobby"
  139. end
  140. serverInfo.map = map:getName()
  141. network.advertise:setInfo( utility.createServerInfo() )
  142. end
  143. end
  144. function advertisementMsg( msg )
  145. if STATE == "Lobby" then
  146. lobby:newWarning( "Could not advertise your game online:\n" .. msg )
  147. end
  148. end