Skip to content

Commit

Permalink
Merge pull request #260 from johnpooch/develop
Browse files Browse the repository at this point in the history
Fix issue with can_retreat
  • Loading branch information
johnpooch authored May 17, 2021
2 parents df436f3 + ce29d78 commit 333a1e0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion adjudicator/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def can_retreat(self):
"""
for territory in self.territory.neighbours:
accessible = territory.accessible_by_piece_type(self)
unoccupied = not territory.occupied
unoccupied = not territory.occupied_after_processing
uncontested = not territory.bounce_occurred
if accessible and unoccupied and uncontested:
return True
Expand Down
16 changes: 16 additions & 0 deletions adjudicator/territory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from adjudicator.decisions.base import Outcomes
from . import decisions
from .state import register

Expand Down Expand Up @@ -75,6 +76,21 @@ def hold_strength(self):
def occupied(self):
return bool(self.piece)

@property
def occupied_after_processing(self):
"""
Whether the territory will be occupied after orders are processed.
Used to determine if pieces can retreat here.
"""
if any([p.order.outcome == Outcomes.SUCCEEDS for p in self.attacking_pieces]):
return False
if self.piece:
return not (
(self.piece.order.is_move and self.piece.order.outcome == Outcomes.SUCCEEDS)
or (self.piece.destroyed)
)
return False

def adjacent_to(self, territory):
return territory in self.neighbours

Expand Down
102 changes: 102 additions & 0 deletions core/tests/test_adjudicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,22 @@ def setUp(self):
season=Season.SPRING,
year=1900,
)
self.austria = models.Nation.objects.get(id='standard-austria-hungary')
self.england = models.Nation.objects.get(id='standard-england')
self.italy = models.Nation.objects.get(id='standard-italy')
self.russia = models.Nation.objects.get(id='standard-russia')
self.turkey = models.Nation.objects.get(id='standard-turkey')
self.patch_process_turn_apply_async()

self.adriatic_sea = models.Territory.objects.get(id='standard-adriatic-sea')
self.apulia = models.Territory.objects.get(id='standard-apulia')
self.rome = models.Territory.objects.get(id='standard-rome')
self.naples = models.Territory.objects.get(id='standard-naples')
self.piedmont = models.Territory.objects.get(id='standard-piedmont')
self.tuscany = models.Territory.objects.get(id='standard-tuscany')
self.tyrrhenian_sea = models.Territory.objects.get(id='standard-tyrrhenian-sea')
self.venice = models.Territory.objects.get(id='standard-venice')

self.livonia = models.Territory.objects.get(id='standard-livonia')
self.norway = models.Territory.objects.get(id='standard-norway')
self.st_petersburg = models.Territory.objects.get(id='standard-st-petersburg')
Expand Down Expand Up @@ -357,3 +368,94 @@ def test_disband(self):
self.assertEqual(order_st_petersburg.outcome, OutcomeType.SUCCEEDS)
self.assertTrue(piece.turn_disbanded, self.turn)
self.assertEqual(new_turn.piecestates.count(), 0)

def test_retreat_rome_not_auto_destroy(self):
self.turn.phase = Phase.ORDER
self.turn.save()
army_rome = self.turn.piecestates.create(
piece=models.Piece.objects.create(
game=self.game,
nation=self.austria,
type=PieceType.ARMY
),
territory=self.rome,
)
self.turn.piecestates.create(
piece=models.Piece.objects.create(
game=self.game,
nation=self.italy,
type=PieceType.ARMY
),
territory=self.naples,
)
self.turn.piecestates.create(
piece=models.Piece.objects.create(
game=self.game,
nation=self.italy,
type=PieceType.FLEET
),
territory=self.tyrrhenian_sea,
)
self.turn.piecestates.create(
piece=models.Piece.objects.create(
game=self.game,
nation=self.austria,
type=PieceType.ARMY
),
territory=self.tuscany,
)
self.turn.piecestates.create(
piece=models.Piece.objects.create(
game=self.game,
nation=self.austria,
type=PieceType.FLEET
),
territory=self.apulia,
)
self.turn.piecestates.create(
piece=models.Piece.objects.create(
game=self.game,
nation=self.austria,
type=PieceType.FLEET
),
territory=self.venice,
)
self.turn.orders.create(
nation=self.austria,
source=self.apulia,
target=self.adriatic_sea,
type=OrderType.MOVE,
)
self.turn.orders.create(
nation=self.austria,
source=self.rome,
type=OrderType.HOLD,
)
self.turn.orders.create(
nation=self.austria,
source=self.venice,
type=OrderType.HOLD,
)
self.turn.orders.create(
nation=self.italy,
source=self.tuscany,
target=self.piedmont,
type=OrderType.MOVE,
)
self.turn.orders.create(
nation=self.italy,
source=self.apulia,
target=self.rome,
type=OrderType.MOVE,
)
self.turn.orders.create(
nation=self.italy,
source=self.tyrrhenian_sea,
target=self.rome,
aux=self.naples,
type=OrderType.SUPPORT,
)
process_turn(self.turn)
army_rome.refresh_from_db()
self.assertFalse(army_rome.destroyed)
self.assertIsNone(army_rome.destroyed_message)

0 comments on commit 333a1e0

Please sign in to comment.