Browse Source

Advancing phases works. Minimal usable version.

feature/js
Daniel Perelman 2 years ago
parent
commit
a65ab743f8
3 changed files with 44 additions and 11 deletions
  1. +11
    -9
      fear_tracker/models.py
  2. +3
    -0
      fear_tracker/templates/game.html
  3. +30
    -2
      fear_tracker/views.py

+ 11
- 9
fear_tracker/models.py View File

@@ -113,7 +113,7 @@ class Game(models.Model):
def _fear_cards_as_of_fear_phase(self, previous):
fear_phase = self._fear_phase(previous=previous)
if fear_phase:
return fear_phase.initial_fear // self.fear_per_card
return fear_phase.starting_fear // self.fear_per_card
else:
return 0

@@ -136,28 +136,30 @@ class Game(models.Model):
if current_phase:
current_phase.ended = current_time
current_phase.save()
initial_fear = current_phase.starting_fear +\
starting_fear = current_phase.starting_fear +\
current_phase.fear_this_phase()
else:
initial_fear = 0
(next_turn, next_phase) = self.next_turn_and_phase()
starting_fear = 0
(next_turn, next_phase_id) = self.next_turn_and_phase()
self.game_turn = next_turn
self.game_phase = next_phase
self.game_phase = next_phase_id
self.save()

next_phase = self.get_current_phase()
if next_phase is None:
Phase.objects.create(
next_phase = Phase.objects.create(
game=self,
game_turn=next_turn,
game_phase=next_phase,
initial_fear=initial_fear,
game_phase=next_phase_id,
starting_fear=starting_fear,
started=current_time)
else:
next_phase.started = current_time
next_phase.initial_fear = initial_fear
next_phase.starting_fear = starting_fear
next_phase.ended = None
next_phase.save()
self.player_set.update(ready=False)
return next_phase

@staticmethod
def get_name_for_phase_id(phase):


+ 3
- 0
fear_tracker/templates/game.html View File

@@ -24,6 +24,9 @@
</div>
<form action="#" method="POST">
{% csrf_token %}
<input type="hidden" name="game_turn" value="{{ turn }}">
<input type="hidden" name="game_phase" value="{{ phase_id }}">
<input type="hidden" name="phase_name" value="{{ phase }} phase of turn #{{ turn }}">
<div class="players">
{% for order, player in players.items %}
<div class="player player-{{ player.order }}">


+ 30
- 2
fear_tracker/views.py View File

@@ -2,6 +2,7 @@ from collections import OrderedDict
from io import BytesIO
import qrcode

from django.db import transaction
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.views.decorators.http import require_safe, require_http_methods
@@ -88,6 +89,7 @@ def qr_code(request, access_code):
return HttpResponse(output.getvalue(), content_type='image/png')


@transaction.atomic
@lookup_access_code
@require_http_methods(["HEAD", "GET", "POST"])
def game(request, game):
@@ -106,9 +108,16 @@ def game(request, game):
}
current_phase = game.get_current_phase()
if request.method == 'POST':
correct_phase =\
current_phase.game_turn == int(request.POST['game_turn'])\
and current_phase.game_phase == int(request.POST['game_phase'])
advance_phase = 'advance' in request.POST
for key, value in request.POST.items():
sections = key.split('-')
if key == 'csrfmiddlewaretoken' or sections[-1] == 'orig':
if key in ['csrfmiddlewaretoken',
'update', 'advance',
'game_turn', 'game_phase', 'phase_name']\
or sections[-1] == 'orig':
pass
elif sections[0] == 'player':
orig_value = request.POST.get(key + '-orig', None)
@@ -117,7 +126,7 @@ def game(request, game):
if sections[2] == 'visible':
player.visible = True
elif sections[2] == 'ready':
if value != orig_value:
if correct_phase and value != orig_value:
if value == 'true':
player.ready = True
player.changed_to_ready = True
@@ -140,6 +149,15 @@ def game(request, game):
f"#{effect_num} {effect_kind} " +
f"from {orig} to {amount} " +
"was not processed.")
elif not correct_phase or advance_phase:
old_phase = request.POST['phase_name']
errors.append(
f"{player.name} " +
f"({player.get_spirit_name()})'s " +
f"#{effect_num} {effect_kind} " +
"could not be change from " +
f"{orig} to {amount} " +
f"because {old_phase} has already ended.")
else:
fear_row, created = Fear.objects.get_or_create(
phase=current_phase,
@@ -165,6 +183,15 @@ def game(request, game):
raise Exception(key + ": " + value)
else:
raise Exception(key + ": " + value)
if advance_phase:
players_ready = all(player.ready for player in players.values())
if not players_ready:
errors.append("Cannot advance to next phase until " +
"all players are done with the current phase.")
else:
current_phase = game.advance_phase()
for player in players.values():
player.ready = False

for fear in current_phase.fear_set.order_by('effect').all():
players[fear.player.order].fear[fear.effect] = {
@@ -189,6 +216,7 @@ def game(request, game):
'access_code': game.access_code,
'turn': game.game_turn,
'phase': game.get_current_phase_name(),
'phase_id': game.game_phase,
'available_fear_cards': game.num_available_fear_cards(),
'fear_to_next_card': game.get_fear_to_next_card(),
'fear_this_phase': current_phase.fear_this_phase(),


Loading…
Cancel
Save