Beispiel #1
0
    def json_to_species(cls, json_species):
        if len(json_species) == SPECIES_LENGTH:
            [[globals()[FOOD], species_food], [globals()[BODY], species_body],
             [globals()[POPULATION], species_pop],
             [globals()[TRAITS], json_species_traits]] = json_species
            fat_food = None
        elif len(json_species) == SPECIES_FAT_LENGTH:
            [[globals()[FOOD], species_food], [globals()[BODY], species_body],
             [globals()[POPULATION], species_pop],
             [globals()[TRAITS], json_species_traits],
             [globals()[FATFOOD], fat_food]] = json_species
            assert (species_body >= fat_food >= MIN_FATFOOD)
        else:
            raise AssertionError

        assert (all([
            MAX_FOOD >= species_food >= MIN_FOOD,
            MAX_BODY >= species_body >= MIN_BODY,
            MAX_POP >= species_pop >= MIN_POP,
            MAX_TRAITS >= len(json_species_traits)
        ]))

        species_traits = [
            cls.json_to_trait(trait) for trait in json_species_traits
        ]
        species_obj = Species(species_pop, species_food, species_body,
                              species_traits, fat_food)
        return species_obj
    def json_to_species(cls, json_species):
        """
        Converts a JSON Species+ into a Species.
        :param json_species: a JSON Species+ as specified by the data definition at
                             http://www.ccs.neu.edu/home/matthias/4500-s16/6.html
        :return: a Species object
        """
        gdict = globals()
        if len(json_species) == SPECIES_LENGTH:
            [[gdict[FOOD], species_food], [gdict[BODY], species_body],
             [gdict[POPULATION], species_pop],
             [gdict[TRAITS], json_species_traits]] = json_species
            fat_food = False
        else:
            [[gdict[FOOD], species_food], [gdict[BODY], species_body],
             [gdict[POPULATION], species_pop],
             [gdict[TRAITS], json_species_traits], [gdict[FATFOOD],
                                                    fat_food]] = json_species

        species_traits = [
            cls.json_to_trait(trait) for trait in json_species_traits
        ]
        species_obj = Species(species_pop, species_food, species_body,
                              species_traits, fat_food)
        species_obj.validate_attributes()
        return species_obj
    def json_to_player(cls, json_player):
        """
        Converts a JSON Player+ to a PlayerState
        :param json_player: a JSON Player+ as specified by the data definition at
                            http://www.ccs.neu.edu/home/matthias/4500-s16/8.html
        :return: a PlayerState object
        """
        gdict = globals()
        if len(json_player) == PLAYER_LENGTH:
            [[gdict[ID], player_id], [gdict[SPECIES], json_los],
             [gdict[BAG], food_bag]] = json_player
            cards = []
        else:
            [[gdict[ID], player_id], [gdict[SPECIES], json_los],
             [gdict[BAG], food_bag], [gdict[CARDS], cards]] = json_player

        player_species = [
            cls.json_to_species(json_species) for json_species in json_los
        ]
        player_hand = [cls.json_to_trait(trait_card) for trait_card in cards]
        player_obj = PlayerState(name=player_id,
                                 hand=player_hand,
                                 food_bag=food_bag,
                                 species=player_species)
        player_obj.validate_attributes()
        return player_obj
Beispiel #4
0
    def json_to_player(cls, json_player):
        if len(json_player) == PLAYER_LENGTH:
            [[globals()[ID], player_id], [globals()[SPECIES], json_los],
             [globals()[BAG], food_bag]] = json_player
            cards = []
        elif len(json_player) == PLAYER_PLUS_LENGTH:
            [[globals()[ID], player_id], [globals()[SPECIES], json_los],
             [globals()[BAG], food_bag], [globals()[CARDS],
                                          cards]] = json_player
        else:
            raise AssertionError

        assert (all([
            player_id >= MIN_PLAYER_ID, food_bag >= MIN_FOOD_BAG,
            isinstance(player_id, int),
            isinstance(food_bag, int),
            isinstance(json_los, list),
            isinstance(cards, list)
        ]))

        player_species = [
            cls.json_to_species(json_species) for json_species in json_los
        ]
        if cards:
            cards = [cls.json_to_trait(trait_card) for trait_card in cards]
        player_obj = PlayerState(name=player_id,
                                 hand=cards,
                                 food_bag=food_bag,
                                 species=player_species)
        return player_obj