hellochar/the_game_bazaar — pull request #67 by flawedmatrix (2013-05-03 14:13:00+00:00)
Title: Handle Disconnection

CHANGES
- Added Game controller method for removing players from the player list
- Refactored socket communication so that game_id, player_id, isHost, and map_id is generated onJoin, which is possible because the user is stored server side in the initial socket.io request
- This means all of the hacky game.js code dealing with game_id and player_id and storing temporary variables in divs in the HTML is redundant, so the code has been removed and tests changed to reflect the simpler interface
- This was necessary to make handling leave_room logic very simple
- Wrote tests in Jasmine, for game.js leave logic, wrote test for game controller method for removing players
- Wrote tests for Socket.io not only for leave room logic but for everything else
- Guests can no longer start the game by sending the 'start' packet
- Added mock library for Python (makes Mock testing easy)
- Added extra loading screen to prevent the start button from being visible until finishInitialization()

Here is how socket communication works now:
1. Client GET /game/{ gameid }
2. Server processes link, renders { gameid } back to client through the template and directly passing it into the Game() constructor
3. On Game.init() the client makes a socket.io request with the /game namespace and the 'id={ gameid }' query string
4. Server handles the initial connection, retrieving the request.user object (django magically handles the sessions and cookies to do this) and parsing the query string
5. Client then sends the 'join' message indicating the client wants to join the game
6. Server automatically adds the client to the '{ gameid }' room and adds the user to the game in the database. It then sends back a 'game_data' packet containing player_id, isHost, map_id, and player_list, which all gets used in the client.

tl;dr:
Loading screen
User disconnects/leaving room logic
Don't send the server the game_id and player_id; it already knows what they are.
Make sure you do pip install again