Skip to content

Commit

Permalink
Merge pull request #251 from johnpooch/develop
Browse files Browse the repository at this point in the history
0.5.0
  • Loading branch information
johnpooch authored Apr 29, 2021
2 parents 2e8e34c + 3d686ac commit 88df40b
Show file tree
Hide file tree
Showing 14 changed files with 561 additions and 132 deletions.
7 changes: 6 additions & 1 deletion adjudicator/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ def __init__(self, state, nation, source, **kwargs):
self.nation = nation
self.source = source
self.state = state
self.outcome = Outcomes.SUCCEEDS

@property
def outcome(self):
if self.piece.retreating:
return Outcomes.FAILS
return Outcomes.SUCCEEDS


class Hold(Order):
Expand Down
8 changes: 8 additions & 0 deletions adjudicator/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ def process(state):
else:
build.outcome = Outcomes.FAILS

if state.phase == Phase.RETREAT:
for piece in state.pieces:
if piece.retreating and (piece.order.outcome == Outcomes.FAILS):
piece.destroyed = True
piece.destroyed_message = (
'Destroyed because piece must retreat but retreat order failed.'
)

# TODO test
# TODO split into sub function
# Set captured_by for territories if fall orders
Expand Down
19 changes: 19 additions & 0 deletions core/migrations/0009_piece_turn_destroyed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.1.7 on 2021-04-29 15:19

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0008_auto_20210426_1518'),
]

operations = [
migrations.AddField(
model_name='piece',
name='turn_destroyed',
field=models.ForeignKey(blank=True, help_text='The turn during which this piece was destroyed.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='core.turn'),
),
]
19 changes: 16 additions & 3 deletions core/models/nation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.contrib.auth.models import User
from django.db import models
from django.utils.functional import cached_property

from core.models.base import PerTurnModel, Phase, SurrenderStatus

Expand Down Expand Up @@ -160,6 +161,14 @@ def supply_centers(self):
territory__supply_center=True
)

@cached_property
def num_supply_centers(self):
return self.supply_centers.count()

@cached_property
def num_pieces(self):
return self.pieces.count()

@property
def unoccupied_controlled_home_supply_centers(self):
"""
Expand Down Expand Up @@ -187,7 +196,7 @@ def supply_delta(self):
Returns:
* `int`
"""
return self.supply_centers.count() - self.pieces.count()
return self.num_supply_centers - self.num_pieces

@property
def num_builds(self):
Expand Down Expand Up @@ -233,18 +242,22 @@ def pieces_to_order(self):
raise Exception('Should not be called during build phase')
return self.turn.piecestates.filter(piece__nation=self.nation)

@cached_property
def num_pieces_to_order(self):
return self.pieces_to_order.count()

@property
def num_orders(self):
if self.turn.phase == Phase.BUILD:
return max(self.num_builds, self.num_disbands)
return self.pieces_to_order.count()
return self.num_pieces_to_order

@property
def num_orders_remaining(self):
if self.turn.phase == Phase.BUILD:
num_orders = max(self.num_builds, self.num_disbands)
return max(0, num_orders - self.orders.count())
return self.pieces_to_order.count() - self.orders.count()
return self.num_pieces_to_order - self.orders.count()

def copy_to_new_turn(self, turn):
"""
Expand Down
10 changes: 10 additions & 0 deletions core/models/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class Piece(HygienicModel):
'be a retreat/disband phase or a build/disband phase.'
)
)
turn_destroyed = models.ForeignKey(
'Turn',
null=True,
blank=True,
on_delete=models.CASCADE,
related_name='+',
help_text=_(
'The turn during which this piece was destroyed.'
)
)

def __str__(self):
return f'{self.type} ({self.nation}) {self.id}'
Expand Down
Loading

0 comments on commit 88df40b

Please sign in to comment.