Esempio n. 1
0
    def add_new_player(self, jsonObject, playerId):
        """ Add new player to the game
        :param jsonObject: (json) json response from player
        """

        # Validate jsonObject
        error = None
        try:
            if "TeamName" not in jsonObject:
                error = "Missing 'teamName' parameter"
            elif len(jsonObject["TeamName"]) == 0:
                error = "'TeamName' cannot be an empty string"
            elif len(jsonObject["Characters"]) == 0:
                error = "list of classes can not be empty"
        except KeyError as e:
            error = "json response doesn't have the correct format"

        # If there is an error, return false and error
        if error:
            return False, error
        # Add player to game data
        new_team = Team(jsonObject['TeamName'])
        for character in jsonObject['Characters']:
            error = new_team.add_character(character)

        self.teams[new_team.id] = new_team

        self.playerInfos[playerId] = jsonObject
        self.playerInfos[playerId]["Id"] = playerId
        self.playerInfos[playerId]["TeamId"] = new_team.id

        # Return response (as a JSON object)
        return (True, new_team.toJson())
    def test_constructor(self):
        """ Test default constructor
        """
        character_json = {"characterName": "Player1", "classId": "warrior"}
        team1 = Team("Team1")

        self.assertTrue(team1.add_character(character_json))
        self.assertEqual(team1.size(), 1)
Esempio n. 3
0
    def __init__(self):
        """ Init the game object
        :param totalTurns: (int) max number of ticks in a game
        """

        self.totalTurns = gameConst.totalTurns
        self.turnsExecuted = 0

        self.queuedTurns = {}
        self.turnResults = {}
        self.teams = {}
        self.playerInfos = {}

        Character.remove_all_characters()
        Team.remove_all_teams()

        # Load map
        self.map = GameMap()