Example #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)
Example #2
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)
Example #3
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})
Example #4
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
Example #5
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)
Example #6
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)
Example #7
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)
Example #8
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()
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
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)