Skip to content

Commit

Permalink
Add battle tick logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jardiacaj committed Dec 1, 2017
1 parent 5324b59 commit 3e4ab73
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
18 changes: 11 additions & 7 deletions battle/battle_tick.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from django.db import transaction

from battle.models import Battle, BattleCharacterInTurn, BattleUnitInTurn, BattleContuberniumInTurn, \
BattleSoldierInTurn, Coordinates, Order, BattleUnit
BattleSoldierInTurn, Coordinates, Order
from context_managers import perf_timer
from finem_imperii.app_settings import BATTlE_TICKS_PER_TURN
from unit.models import WorldUnit

Expand Down Expand Up @@ -496,12 +497,15 @@ def check_end(battle: Battle):


def battle_tick(battle: Battle):
create_next_turn(battle)
unit_movement(battle)
unit_attack(battle)
if check_end(battle):
battle.current = False
battle.save()
with perf_timer(
"Tick {} for {}".format(battle.get_latest_turn().num, battle)
):
create_next_turn(battle)
unit_movement(battle)
unit_attack(battle)
if check_end(battle):
battle.current = False
battle.save()


def battle_turn(battle: Battle):
Expand Down
6 changes: 4 additions & 2 deletions context_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import time

import logging


@contextmanager
def perf_timer(name):
start_time = time.time()
print('[{}] ...'.format(name))
logging.info('[{}] ...'.format(name))
yield
elapsed = time.time() - start_time
print('[{}] in {} ms'.format(name, int(elapsed * 1000)))
logging.info('[{}] in {} ms'.format(name, int(elapsed * 1000)))
2 changes: 1 addition & 1 deletion unit/templates/unit/view_unit.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ <h4>Soldiers</h4>
{{ soldier.get_hunger_display }}
</td>
<td>

</td>
</tr>
{% endfor %}
Expand Down
4 changes: 4 additions & 0 deletions world/management/commands/initialize_world.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from django.core.management.base import BaseCommand, CommandError

from context_managers import perf_timer
Expand All @@ -12,6 +14,8 @@ def add_arguments(self, parser):
parser.add_argument('world_id', nargs='+', type=int)

def handle(self, *args, **options):
logging.getLogger().setLevel(logging.INFO)

for world_id in options['world_id']:
try:
world = World.objects.get(pk=world_id)
Expand Down
4 changes: 4 additions & 0 deletions world/management/commands/pass_turn.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from django.core.management.base import BaseCommand, CommandError

from context_managers import perf_timer
Expand All @@ -12,6 +14,8 @@ def add_arguments(self, parser):
parser.add_argument('world_id', nargs='+', type=int)

def handle(self, *args, **options):
logging.getLogger().setLevel(logging.INFO)

for world_id in options['world_id']:
try:
world = World.objects.get(pk=world_id)
Expand Down

0 comments on commit 3e4ab73

Please sign in to comment.