示例#1
0
    def test_per_turn(self):  # tbc
        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()

        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.reset_unit_actions_and_movement()

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

        civ.reset_unit_actions_and_movement()
        civ.currency_per_turn()

        self.assertEqual(archer.actions, 2)
        self.assertEqual(worker.actions, 2)
        self.assertEqual(archer.movement, 5)
        self.assertEqual(worker.movement, 4)

        self.assertEqual(civ.food, 99)
        self.assertEqual(civ.gold, 51)
        self.assertEqual(civ.science, 5)
示例#2
0
    def set_up(self, tile, worker_id):
        """
        Start civilisation.

        Create Worker.
        """
        worker = Worker(worker_id, 1, tile, self._id)
        worker.actions = 2
        tile.unit = worker
        self.units[worker_id] = worker
示例#3
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)
示例#4
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)
示例#5
0
    def test_worker_attributes(self):
        """Test that worker's attributes are initialised correctly."""
        hextile = Hex(0, 0, 0)

        worker = Worker("worker", 2, hextile, 1)

        self.assertEqual(worker._health, 110)
        self.assertEqual(worker._movement_range, 5)
        self.assertEqual(worker._cost, {'food': 2, 'gold': 0, 'science': 0})
示例#6
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)
示例#7
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)
示例#8
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)
示例#9
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)
示例#10
0
def main():
    """Test function."""
    with open(os.path.join("..", "config", "config.json")) as config_file:
        config = json.load(config_file)
    con = Connection(config["server"]["ip"], config["server"]["port"])
    con.open()
    hex = Hex(1, 2, -3)
    worker = Worker(1, hex)
    upgrade_action = UpgradeAction(worker)
    message = Message(upgrade_action, 1)
    con.send(message.serialise())
    message = con.recv()
    print(str(Message.deserialise(message)))
    con.close()
示例#11
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)
示例#12
0
    def test_worker_level_up(self):
        """Test that worker unit levels up and attributes increase."""
        hextile = Hex(0, 0, 0)
        hextile2 = Hex(1, 0, -1)

        worker1 = Worker("worker", 1, hextile, 1)
        worker2 = Worker("worker", 3, hextile2, 1)

        worker1.level_up()
        worker2.level_up()

        self.assertEqual(worker1._cost, {'food': 2, 'gold': 0, 'science': 0})
        self.assertEqual(worker1._health, 120)
        self.assertEqual(worker1._movement_range, 5)

        self.assertEqual(worker2._cost, {'food': 3, 'gold': 0, 'science': 0})
        self.assertEqual(worker2._health, 120)
        self.assertEqual(worker2._movement_range, 6)
示例#13
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