Exemple #1
0
    def test_unlock_research(self):
        """Test the unlock_research function."""
        civ = Civilisation("myCiv", grid, logger)
        civ.science = 50
        civ.unlock_research(1)

        self.assertEqual(civ._tree._nodes[1].unlocked, True)
Exemple #2
0
    def test_move_unit_to_hex(self):
        """Test the move_unit_to_hex method."""
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        hextile2 = Hex(1, 0, -1)
        archer = Archer(1, 1, hextile, "myCiv")
        archer.actions = 2
        civ.move_unit_to_hex(archer, hextile2)

        self.assertEqual(archer.position, hextile2)
        self.assertEqual(archer.actions, 1)
Exemple #3
0
    def test_is_dead(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        archer = Archer(1, 1, hextile, "myCiv")
        archer.health = 0
        civ.units[archer.id] = archer
        archer._civilisation = civ
        civ.is_dead(archer)

        self.assertEqual(archer.health, 0)
        self.assertNotIn(archer, civ.units)
Exemple #4
0
    def test_set_up(self):
        """Test the set_up function."""
        hextile = Hex(0, 0, 0)
        civ = Civilisation("myCiv", grid, logger)

        civ.set_up(hextile, "worker")
        worker = civ.units["worker"]

        self.assertEqual(worker.actions, 2)
        self.assertEqual(hextile.unit, worker)
        self.assertEqual(civ.units["worker"], worker)
Exemple #5
0
    def test_attack_unit(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        archer = Archer(1, 1, hextile, "myCiv")
        hextile2 = Hex(1, 0, -1)
        archer.actions = 2
        swordsman = Swordsman(1, 1, hextile2, "notMyCiv")
        civ.attack_unit(archer, swordsman)

        self.assertEqual(swordsman.health, 110)
        self.assertEqual(round(archer.health), 85)
        self.assertEqual(archer.actions, 1)
Exemple #6
0
    def test_cost_of_units(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        worker = Worker("worker", 1, hextile, "myCiv")
        civ.units[worker.id] = worker

        archer = Archer(1, 1, hextile, "myCiv")
        civ.units[archer.id] = archer

        cost = civ.cost_of_units()
        self.assertEqual(cost["food"], 3)
        self.assertEqual(cost["gold"], 0)
        self.assertEqual(cost["science"], 0)
Exemple #7
0
    def test_reset_unit_actions_and_movement(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        worker = Worker("worker", 1, hextile, "myCiv")
        civ.units[worker.id] = worker
        archer = Archer(1, 1, hextile, "myCiv")
        civ.units[archer.id] = archer

        civ.reset_unit_actions_and_movement()

        self.assertEqual(archer.actions, 2)
        self.assertEqual(worker.actions, 2)
        self.assertEqual(archer.movement, 5)
        self.assertEqual(worker.movement, 4)
Exemple #8
0
    def test_buy_unit(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        worker = Worker("worker", 1, hextile, "myCiv")
        worker.actions = 5
        civ.units[worker.id] = worker

        grid.create_grid()
        civ.build_city_on_tile(worker, 1)
        city = civ.cities[1]
        unit = civ.buy_unit(city, Archer, 1, 1)

        self.assertEqual(civ.units[1], unit)
        self.assertEqual(civ.gold, 65)
Exemple #9
0
    def test_movement_cost_of_path(self):
        """Test the movement_cost_of_path function."""
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        hextile2 = Hex(1, 0, -1)
        hextile3 = Hex(1, 1, -2)
        hextile._terrain = Terrain(TerrainType.FLAT, BiomeType.GRASSLAND)
        hextile2._terrain = Terrain(TerrainType.HILL, BiomeType.GRASSLAND)
        hextile3._terrain = Terrain(TerrainType.HILL, BiomeType.DESERT)
        # costs are 1, 2 and 3 respectively
        hexes = [hextile, hextile2, hextile3]
        archer = Archer(1, 1, hextile, "myCiv")
        archer.actions = 2
        cost = civ.movement_cost_of_path(hexes)

        self.assertEqual(cost, 6)
    def test_unlock_node(self):
        """Test the unlock_node function."""
        civ = Civilisation("myCiv", grid, logger)
        tree = ResearchTree(civ)
        tree.unlock_node(1)
        unlock = tree._nodes[1].unlocked

        self.assertEqual(unlock, True)
 def test_unlockable(self):
     """Test the unlockable function."""
     civ = Civilisation("myCiv", grid, logger)
     tree = ResearchTree(civ)
     unlock1 = tree.unlockable(1)
     self.assertEqual(unlock1, True)
     unlock2 = tree.unlockable(5)
     self.assertEqual(unlock2, False)
    def test_add_node(self):
        """Test the add_node function."""
        civ = Civilisation("myCiv", grid, logger)
        tree = ResearchTree(civ)
        length = len(tree._nodes)

        tree.add_node(10, "test", False, 5)
        self.assertEqual(len(tree._nodes), length + 1)
Exemple #13
0
 def shuffleraces(self):
     while (len(self.civilisations) < 6):
         race = self.racepool.pop(randint(0, len(self.racepool)))
         power = self.powerpool.pop(randint(0, len(self.racepool)))
         self.civilisations.append({
             "civilisation":
             Civilisation(race, power, self.map),
             "reward":
             0
         })
Exemple #14
0
    def test_build_city_on_tile(self):
        """Test the build_city_on_tile function."""
        hextile = Hex(0, 0, 0)
        civ = Civilisation("myCiv", grid, logger)
        worker = Worker("worker", 1, hextile, 1)
        worker.actions = 2

        grid.create_grid()

        gold = civ.gold
        actions = worker.actions

        civ.build_city_on_tile(worker, 1)

        city = civ.cities[1]

        self.assertEqual(civ.gold, gold - 25)
        self.assertEqual(worker.actions, actions - 1)
        self.assertEqual(civ.cities[1], city)
        self.assertEqual(civ.tiles[worker.position], civ._id)
Exemple #15
0
    def test_calculate_vision(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        worker = Worker("worker", 1, hextile, "myCiv")
        worker.actions = 2
        civ.units[worker.id] = worker

        grid.create_grid()
        civ.build_city_on_tile(worker, 1)
        hextile2 = grid.get_neighbour_in_direction(hextile, 2)
        worker.position = hextile2

        civ.build_structure(worker, BuildingType.UNIVERSITY, 1)
        civ.calculate_vision()

        self.assertEqual(len(civ._vision), 44)
Exemple #16
0
    def test_currency_per_turn(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        worker = Worker("worker", 1, hextile, "myCiv")
        worker.actions = 5
        civ.units[worker.id] = worker
        archer = Archer(1, 1, hextile, "myCiv")
        civ.units[archer.id] = archer

        grid.create_grid()
        civ.build_city_on_tile(worker, 1)
        hextile2 = grid.get_neighbour_in_direction(hextile, 2)
        worker.position = hextile2
        civ.build_structure(worker, BuildingType.UNIVERSITY, 1)

        hextile3 = grid.get_neighbour_in_direction(hextile, 4)
        worker.position = hextile3
        civ.build_structure(worker, BuildingType.FARM, 2)

        civ.currency_per_turn()
        self.assertEqual(civ.food, 99)
        self.assertEqual(civ.gold, 51)
        self.assertEqual(civ.science, 5)
def read_civs_from_json_file(era_name, time_period):
    try:
        filepath = 'civilisations/' + era_name.lower(
        ) + '/' + time_period.lower() + '/civilisations.json'
        with open((filepath), 'r') as f:
            json_data = json.load(f)
        civs = [
            Civilisation(civ['name'], civ['region'], civ['established'],
                         civ['end_date'], civ['gov_type'], civ['religion'],
                         civ['image'], civ['fun_fact'], civ['info'])
            for civ in json_data
        ]
        return civs
    except IOError:
        abort(404)
Exemple #18
0
    def test_currency_of_buildings(self):
        civ = Civilisation("myCiv", grid, logger)
        hextile = Hex(0, 0, 0)
        worker = Worker("worker", 1, hextile, "myCiv")
        worker.actions = 5
        civ.units[worker.id] = worker

        grid.create_grid()
        civ.build_city_on_tile(worker, 1)
        hextile2 = grid.get_neighbour_in_direction(hextile, 2)
        worker.position = hextile2
        civ.build_structure(worker, BuildingType.UNIVERSITY, 1)

        hextile3 = grid.get_neighbour_in_direction(hextile, 4)
        worker.position = hextile3
        civ.build_structure(worker, BuildingType.FARM, 2)
        curr = civ.currency_of_buildings()

        self.assertEqual(curr["food"], 2)
        self.assertEqual(curr["gold"], -4)
        self.assertEqual(curr["science"], 5)
Exemple #19
0
    def test_civilisation_constructor(self):
        """Test the constructor for the civilisation class."""
        civ = Civilisation("myCiv", grid, logger)
        tree = civ._tree

        self.assertEqual(civ._id, "myCiv")
        self.assertEqual(civ._grid, grid)
        self.assertEqual(civ._logger, logger)
        self.assertEqual(civ._units, {})
        self.assertEqual(civ._cities, {})
        self.assertEqual(civ._tiles, {})
        self.assertEqual(civ._food, 100)
        self.assertEqual(civ._gold, 100)
        self.assertEqual(civ._science, 0)
        self.assertEqual(civ._tree, tree)
        self.assertEqual(civ._vision, {})
Exemple #20
0
    def add_player(self, message):
        """
        Add a new player to the game.

        :param message: The message object sent from the client.
        :return: The id of the new player
        """
        if len(self._civs) < self._num_players:
            user_id = database_API.User.insert(self._session,
                                               self._game_id,
                                               active=True, gold=100,
                                               food=100, science=0,
                                               production=0)
            self.add_civ(Civilisation(user_id, self._grid, self._logger))
            self._logger.info("New Civilisation joined with id " +
                              str(user_id))
            # NOTE: Not needed when loading from db
            location = random.choice(self._start_locations)
            del self._start_locations[self._start_locations.index(location)]
            unit_id = database_API.Unit.insert(self._session, user_id, 1,
                                               0, Worker.get_health(1),
                                               *location)
            self._civs[user_id].set_up(self._grid.get_hextile(location),
                                       unit_id)
            self._queues[user_id] = Queue()
            self._queues[user_id].put(UnitUpdate(
                self._civs[user_id].units[unit_id]))
            if(len(self._civs) == self._num_players):
                self._game_started = True
                self._turn_count += 1
                player_ids = [x for x in self._civs]
                for civ in self._civs:
                    self._queues[civ].put(PlayerJoinedUpdate(player_ids))
                self._current_player = list(self._civs.keys())[0]
                start_turn_update = StartTurnUpdate(self._current_player,
                                                    self._turn_count)
                for key in self._queues:
                    self._queues[key].put(start_turn_update)
                    unit = self._civs[key].units[list(self._civs[key].units.
                                                      keys())[0]]
                    self.populate_queues([unit])

            return self._game_id, user_id
        else:
            err = ServerError(GAME_FULL_ERROR)
            self._logger.error(err)
            return err
Exemple #21
0
    def test_upgrade_unit(self):
        """Test the upgrade_unit method."""
        civ = Civilisation("myCiv", grid, logger)
        civ.unlock_research(3)
        hextile = Hex(0, 0, 0)
        archer = Archer(1, 1, "myCiv", hextile)
        archer.actions = 2
        civ.upgrade_unit(archer)

        self.assertEqual(archer.level, 2)
        self.assertEqual(archer.actions, 1)
 def join_game(self):
     """Ask the server to join a game."""
     join_game_action = action.JoinGameAction()
     reply = self.send_action(join_game_action, self.con)
     if reply.type == "ServerError":
         self._log.error(reply.obj)
         # raise action.ServerError(reply.obj)
     else:
         self._log.info("Joined game player id = " + str(reply.obj))
         game_id, self.id = reply.obj
         grid = Grid(20)
         self._game_state = GameState(game_id, 1, grid, self._log)
         self._game_state._grid.create_grid()
         self._game_state._grid.static_map()
         civ = Civilisation(self.id, self._game_state._grid, self._log)
         self._game_state.add_civ(civ)
         self._game_state._my_id = self.id
    def test_research_tree_constructor_and_tree_setup(self):
        """
        Test the tree constructor and getters (and also the tree_setup.

        function which is called in the constructor).
        """
        civ = Civilisation("myCiv", grid, logger)
        tree = ResearchTree(civ)

        win = ResearchNode(9, "win", False, 50)
        worker1 = ResearchNode(0, "worker", True, 0)
        worker2 = ResearchNode(1, "worker", False, 10)
        worker3 = ResearchNode(2, "worker", False, 20)
        archer1 = ResearchNode(3, "archer", True, 0)
        archer2 = ResearchNode(4, "archer", False, 10)
        archer3 = ResearchNode(5, "archer", False, 20)
        swordsman1 = ResearchNode(6, "swordsman", True, 0)
        swordsman2 = ResearchNode(7, "swordsman", False, 10)
        swordsman3 = ResearchNode(8, "swordsman", False, 20)

        self.assertEqual(tree._civilisation, civ)
        self.assertEqual(
            sorted(tree._nodes),
            sorted({
                9: win,
                0: worker1,
                1: worker2,
                2: worker3,
                3: archer1,
                4: archer2,
                5: archer3,
                6: swordsman1,
                7: swordsman2,
                8: swordsman3
            }))
        self.assertEqual(tree._tier, {
            'Worker': 1,
            'Archer': 1,
            'Swordsman': 1
        })
Exemple #24
0
    def test_build_structure(self):
        """Test the build_structure function."""
        grid.create_grid()
        hextile = Hex(0, 0, 0)
        civ = Civilisation("myCiv", grid, logger)
        worker = Worker("worker", 1, hextile, "myCiv")
        worker.actions = 2

        grid.create_grid()
        gold = civ.gold
        actions = worker.actions

        civ.build_city_on_tile(worker, 1)
        hextile2 = grid.get_neighbour_in_direction(hextile, 2)
        worker.position = hextile2

        civ.build_structure(worker, BuildingType.UNIVERSITY, 1)
        building = civ.cities[hextile.city_id].buildings[1]

        # a city had to be built before the university so both costs
        # must be deducted from the total gold and actions
        self.assertEqual(civ.gold, gold - 35)
        self.assertEqual(worker.actions, actions - 2)
        self.assertEqual(worker.position.building, building)
Exemple #25
0
 def addciv(self, civilisation: Civilisation):
     civilisation.setplayer(self)
     self.civilisations.append(civilisation)
 def handle_player_joined_update(self, update):
     """Handle player joined update."""
     for id in update._players:
         if id not in self._game_state._civs:
             new_civ = Civilisation(id, self._game_state._grid, self._log)
             self._game_state._civs[id] = new_civ
            self._hud.draw_quick_surface(layouts)
            self._screen.blit(self._hud_quick_surface, (0, 0))
            pygame.display.flip()
            if self._menu_displayed:
                self._select_menu.display_menu()
        else:
            self._main_menu.display_menu()

    def render_hud(self):
        """Render heads up display."""
        while not self._shutdown:
            self._hud.draw()
            time.sleep(1)


if __name__ == "__main__":
    with open(os.path.join("..", "config", "config.json")) as config_file:
        config = json.load(config_file)
    map_ref = Grid(26)
    map_ref.create_grid()
    map_ref.static_map()
    logger = Logger("client.log", "client", config["logging"]["log_level"])
    logger = logger.get_logger()
    civ = Civilisation(1, map_ref, logger)
    game_state = GameState(1, 1, map_ref, logger)
    game_state.add_civ(civ)
    game_state.my_id = 1
    server_api = server_API.ServerAPI()
    game = Game(game_state, logger, server_api)
    game.start()