Example #1
0
def join_game(access_code, player_name):
    """
    Find a game with the access code, and add the player to it
    """

    # find the game or error
    game = db_util.get_game(access_code)
    if game is None:
        send('game not found')
        return

    if game['state'] != 'waiting_for_players':
        send('game is in progress')
        return

    # create a player
    player = db_util.create_player(player_name)

    # add to the list of players
    db.games.update_one({'access_code': access_code},
                        {'$push': {
                            'players': player
                        }})

    join_room(access_code)
    emit('lobby_update',
         {'players': db_util.get_players_in_lobby(access_code)},
         room=access_code)
Example #2
0
 def test_join_closed_lobby(self):
     self.client1.emit("create_game", "player1")
     access_code = self.client1.get_received()[0]['args']
     self.client1.emit('close_lobby', access_code)
     self.client2.emit("join_game", access_code, "player2")
     response = self.client2.get_received()[0]['args']
     self.assertEqual(response, 'game is in progress')
     self.assertNotIn('player2', db_util.get_players_in_lobby(access_code))
Example #3
0
    def test_join_game(self):
        self.client1.emit("create_game", "player1")
        access_code = self.client1.get_received()[0]['args']
        self.client2.emit("join_game", access_code, "player2")
        self.assertTrue(
            len(db.games.find_one({'access_code': access_code})['players']) ==
            2)

        lobby_players = db_util.get_players_in_lobby(access_code)
        self.assertIn('player1', lobby_players)
        self.assertIn('player2', lobby_players)
        self.assertNotIn('player3', lobby_players)

        received = self.client1.get_received()
        self.assertIn('player1', received[0]['args'][0]['players'])
Example #4
0
def updatePositions():
	ps = db.get_players_in_lobby()
	"""
	ret = {}
	i = 0
	for p in ps:
		ii = 0
		ret2 = {}
		while ii < len(p):
			if ii !=1:
				ret[ii] = p[ii]
			else:
				ret[ii] = {0:p[ii][0],1:p[ii][1]}
			ii+= 1
		ret[i] = ret2
		i+= 1
	return json.dumps(ret) #sends all player names and coordinates
	"""
	return json.dumps(ps)
def updatePositions():
	ps = db.get_players_in_lobby()
	return json.dumps(ps)