def on_join(self): game_id = self.session['game_id'] user = self.session['user'] # Join the game_id room on the sockets side self.join(str(game_id)) # Add the user to the game on the database side game, players_json, player_id = Game.add_user_to_game(game_id, user) self.session['player_id'] = player_id # The host of the game is the person that created the game # WHICH HAPPENS TO BE player_id == 0 IN OUR IMPLEMENTATION # Perhaps we should fix this one day isHost = self.session['isHost'] = (player_id == 0) player_list = [v for v in players_json] # Send the new player information needed to join the map selfData = { 'isHost': isHost, 'map_id': game.map.id, 'player_list': player_list, 'player_id': player_id } self.emit('game_data', selfData) # Send all other players information needed to add the current # player to the game data = { 'username': user.username, 'timestamp': self.get_time(), 'player_id': self.session['player_id'] } self.emit_to_room(str(self.session['game_id']), 'join', data)
def test_add_user_to_game(self): game, _ = Game.create_new_game(self.aMap.id, self.user) # Perform the join logic _, players_json, player_id = Game.add_user_to_game(game.id, self.joiner) # Check a bunch of conditions self.assertEqual(len(players_json), 3) self.assertEqual(players_json[0], "hosting_player") self.assertEqual(players_json[1], "joining_player")