Skip to content

Commit

Permalink
refactor(ship): make speed a Ship and shipType attribute (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bgeninatti committed Apr 9, 2021
1 parent a777727 commit 20e69b3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
7 changes: 5 additions & 2 deletions pythonium/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ def distances_to_planets(
)

def nearby_planets(
self, point: Tuple[int, int], turns: int = 1
self,
point: Tuple[int, int],
turns: int = 1,
speed: int = cfg.ship_speed,
) -> List[Planet]:
"""
Return all the planet that are ``turns`` away or less,
Expand All @@ -96,7 +99,7 @@ def nearby_planets(
distances = self.distances_to_planets(point)
return list(
filter(
lambda p: distances[p.position] <= cfg.ship_speed * turns,
lambda p: distances[p.position] <= speed * turns,
self.planets.values(),
)
)
Expand Down
7 changes: 4 additions & 3 deletions pythonium/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def action_ship_move(self, ship, target):
ship.position, target
)

if distance_to_target <= cfg.ship_speed:
if distance_to_target <= ship.speed:
to = target
target = None
else:
Expand All @@ -502,8 +502,8 @@ def action_ship_move(self, ship, target):
(target[1] - ship.position[1]) / distance_to_target,
)
to = (
int(ship.position[0] + direction[0] * cfg.ship_speed),
int(ship.position[1] + direction[1] * cfg.ship_speed),
int(ship.position[0] + direction[0] * ship.speed),
int(ship.position[1] + direction[1] * ship.speed),
)

ship.position = to
Expand Down Expand Up @@ -716,6 +716,7 @@ def action_planet_build_ship(self, planet, ship_type):
max_cargo=ship_type.max_cargo,
max_mc=ship_type.max_mc,
attack=ship_type.attack,
speed=ship_type.speed,
)

planet.megacredits -= ship_type.cost.megacredits
Expand Down
4 changes: 3 additions & 1 deletion pythonium/game_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
max_cargo=1200,
max_mc=10 ** 3,
attack=0,
speed=cfg.ship_speed,
),
ShipType(
name="war",
cost=Transfer(megacredits=1000, pythonium=600),
max_cargo=100,
max_mc=10 ** 3,
attack=100,
speed=cfg.ship_speed,
),
)

Expand Down Expand Up @@ -223,6 +225,7 @@ def init_players(self, players, galaxy):
max_cargo=ship_type.max_cargo,
max_mc=ship_type.max_mc,
attack=ship_type.attack,
speed=ship_type.speed,
)
galaxy.add_ship(ship)
return galaxy
Expand Down Expand Up @@ -337,7 +340,6 @@ def get_context(self, galaxy, players, turn):
# FIXME: Can the user change the ship types attribute?
return {
"ship_types": self.ship_types,
"ship_speed": cfg.ship_speed,
"tolerable_taxes": cfg.tolerable_taxes,
"happypoints_tolerance": cfg.happypoints_tolerance,
"score": score,
Expand Down
5 changes: 5 additions & 0 deletions pythonium/ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class Ship:
See :attr:`ShipType.attack`
"""

speed: int = attr.ib(converter=int)
"""
Ship's speed in ly per turn
"""

# State in turn
megacredits: int = attr.ib(converter=int, default=0, init=False)
"""
Expand Down
5 changes: 5 additions & 0 deletions pythonium/ship_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ class ShipType:
Attack of the ship. It will be used to resolve conflicts.
"""

speed: int = attr.ib(converter=int)
"""
Ship's speed in ly per turn
"""

def __repr__(self):
return self.name
1 change: 1 addition & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def build(cls, **kwargs):
max_cargo=cls.faker.pyint(),
max_mc=cls.faker.pyint(),
attack=cls.faker.pyint(),
speed=cls.faker.pyint(),
)


Expand Down
1 change: 1 addition & 0 deletions tests/game_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def build_galaxy(self, players):
max_cargo=ship_type.max_cargo,
max_mc=ship_type.max_mc,
attack=ship_type.attack,
speed=ship_type.speed,
)
galaxy.add_ship(ship)

Expand Down
6 changes: 4 additions & 2 deletions tests/test_run_turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,15 @@ def test_ship_move(game, initial_position, destination):

game.action_ship_move(ship, destination)

if distance_to_target <= cfg.ship_speed:
if distance_to_target <= ship.speed:
assert ship.position == destination
assert ship.target is None
else:
new_distance_to_origin = game.galaxy.compute_distance(
ship.position, initial_position
)
# TODO: Somehow also test the direction. Now only the traveled distance is tested
assert math.isclose(new_distance_to_origin, cfg.ship_speed, abs_tol=1)
assert math.isclose(new_distance_to_origin, ship.speed, abs_tol=1)
assert ship.target == destination


Expand Down Expand Up @@ -422,6 +422,7 @@ def test_ship_to_ship_conflict(game, ships_args):
max_cargo=ship_type.max_cargo,
max_mc=ship_type.max_mc,
attack=ship_type.attack,
speed=ship_type.speed,
)
)
game.galaxy.ships = ships
Expand Down Expand Up @@ -508,6 +509,7 @@ def test_planet_conflict(game, planet_args, ships_args):
max_cargo=ship_type.max_cargo,
max_mc=ship_type.max_mc,
attack=ship_type.attack,
speed=ship_type.speed,
)
)

Expand Down

0 comments on commit 20e69b3

Please sign in to comment.