Skip to content

Instantly share code, notes, and snippets.

@jonathanpaulson
Created August 23, 2023 02:37
Show Gist options
  • Save jonathanpaulson/99bb4d5f39424aeebdccc9118a20068d to your computer and use it in GitHub Desktop.
Save jonathanpaulson/99bb4d5f39424aeebdccc9118a20068d to your computer and use it in GitHub Desktop.
diff --git a/base/views.py b/base/views.py
index 18b2147..49521c8 100644
--- a/base/views.py
+++ b/base/views.py
@@ -53,7 +53,7 @@ class SignupView(View):
user.set_password(user.password)
user.save()
- player = Player(user=user)
+ player = Player(user=user, email=user_form.cleaned_data['email'])
player.save()
user = authenticate(
diff --git a/games/forms.py b/games/forms.py
index 4b67220..752c344 100644
--- a/games/forms.py
+++ b/games/forms.py
@@ -31,6 +31,7 @@ class AttackForm(forms.Form):
)
target_x = forms.ChoiceField(choices=X_CHOICES)
target_y = forms.ChoiceField(choices=Y_CHOICES)
+ bomb = forms.BooleanField(required=False)
def __init__(self, *args, **kwargs):
other_teams = kwargs.pop('other_teams')
@@ -41,6 +42,9 @@ class AttackForm(forms.Form):
self.fields['target_y'].widget.attrs = {
'class': 'form-control col-md-1'
}
+ self.fields['bomb'].widget.attrs = {
+ 'class': 'form-control col-md-1'
+ }
self.fields['target_team'] = forms.ChoiceField(
choices=(
(team.id, team.player.user.username)
diff --git a/games/models.py b/games/models.py
index 523fb24..b23ec38 100644
--- a/games/models.py
+++ b/games/models.py
@@ -44,15 +44,17 @@ class Shot(models.Model):
x = models.IntegerField()
y = models.IntegerField()
+ bomb = models.BooleanField()
def __str__(self):
return 'Game {game_id} - '\
- '{attacking_team} attacked {defending_team} ({x}, {y})'.format(
+ '{attacking_team} attacked {defending_team} ({x}, {y}, {bomb})'.format(
game_id=self.game.id,
attacking_team=self.attacking_team.player.user.username,
defending_team=self.defending_team.player.user.username,
x=self.x,
- y=self.y
+ y=self.y,
+ bomb=self.bomb,
)
diff --git a/games/views.py b/games/views.py
index d1b15c3..010dbd1 100644
--- a/games/views.py
+++ b/games/views.py
@@ -186,13 +186,28 @@ class AttackView(View):
other_teams.append(team)
attack_form = AttackForm(request.POST, other_teams=other_teams)
+ print(attack_form, attack_form.is_valid())
if attack_form.is_valid():
- target_x = attack_form.cleaned_data['target_x']
- target_y = attack_form.cleaned_data['target_y']
+ target_x = int(attack_form.cleaned_data['target_x'])
+ target_y = int(attack_form.cleaned_data['target_y'])
+ bomb = attack_form.cleaned_data['bomb']
+ print(bomb, type(bomb))
target_team = attack_form.cleaned_data['target_team']
other_team = Team.objects.get(pk=target_team)
+ past_bombs = Shot.objects.filter(
+ game=game,
+ attacking_team=player_team,
+ bomb=True,
+ )
+ print(past_bombs)
+ if len(past_bombs) >= 10 and bomb:
+ messages.error(request, 'You\'ve already shot two bombs!')
+ return HttpResponseRedirect(
+ reverse('game', args=[game_id])
+ )
+
# Verify shot hasn't already been attempted
past_shots = Shot.objects.filter(
game=game,
@@ -202,20 +217,30 @@ class AttackView(View):
y=target_y
)
- if len(past_shots) > 0:
+ if len(past_shots) > 0 and (not bomb):
messages.error(request, 'You\'ve already shot there!')
return HttpResponseRedirect(
reverse('game', args=[game_id])
)
- shot = Shot(
- game=game,
- attacking_team=player_team,
- defending_team=other_team,
- x=target_x,
- y=target_y
- )
- shot.save()
+ squares = set()
+ if bomb:
+ for dx in [-1,0,1]:
+ for dy in [-1,0,1]:
+ squares.add((target_x+dx, target_y+dy))
+ else:
+ squares.add((target_x,target_y))
+ print(squares)
+ for x,y in squares:
+ shot = Shot(
+ game=game,
+ attacking_team=player_team,
+ defending_team=other_team,
+ x=x,
+ y=y,
+ bomb=bomb,
+ )
+ shot.save()
player_team.last_turn = game.turn
player_team.save()
@@ -227,7 +252,10 @@ class AttackView(View):
ship_tiles = set()
for ship in other_team.ships.all():
ship_tiles.update(set(ship.get_tiles()))
- other_team_hit = (int(target_x), int(target_y)) in ship_tiles
+ other_team_hit = False
+ for x,y in squares:
+ if (x,y) in ship_tiles:
+ other_team_hit = True
# Check for death
past_shot_tiles = set([
@@ -237,6 +265,7 @@ class AttackView(View):
defending_team=other_team
)
])
+ print(past_shot_tiles)
hit_tiles = past_shot_tiles.intersection(ship_tiles)
if len(hit_tiles) == len(ship_tiles):
other_team.alive = False
diff --git a/players/forms.py b/players/forms.py
index b4be167..5a96a59 100644
--- a/players/forms.py
+++ b/players/forms.py
@@ -10,7 +10,8 @@ class UserForm(forms.ModelForm):
model = User
fields = (
'username',
- 'password'
+ 'password',
+ 'email'
)
def __init__(self, *args, **kwargs):
@@ -21,6 +22,9 @@ class UserForm(forms.ModelForm):
self.fields['password'].widget = forms.PasswordInput(attrs={
'class': 'form-control',
})
+ self.fields['email'].widget = forms.EmailInput(attrs={
+ 'class': 'form-control',
+ })
class PlayerForm(forms.ModelForm):
diff --git a/players/models.py b/players/models.py
index fc69b4d..32ff67f 100644
--- a/players/models.py
+++ b/players/models.py
@@ -4,8 +4,10 @@ from django.db import models
class Player(models.Model):
user = models.OneToOneField(User)
+ email = models.TextField()
def __str__(self):
- return '{username}'.format(
- username=self.user.username
+ return '{username} {email}'.format(
+ username=self.user.username,
+ email=self.email,
)
diff --git a/templates/base/signup.html b/templates/base/signup.html
index cde5262..41bd188 100644
--- a/templates/base/signup.html
+++ b/templates/base/signup.html
@@ -25,7 +25,14 @@
{{ user_form.password }}
</div>
</div>
+ <div class="form-group">
+ <label for="{{ user_form.email.id_for_label }}" class="col-sm-2 control-label">Email:</label>
+ <div class="col-sm-10">
+ {{ user_form.email }}
+ </div>
+ </div>
+
<button type="submit" class="btn btn-default">Sign Up</button>
</form>
-{% endblock %}
\ No newline at end of file
+{% endblock %}
diff --git a/templates/games/attack_form.html b/templates/games/attack_form.html
index 76cda3b..6c3d87e 100644
--- a/templates/games/attack_form.html
+++ b/templates/games/attack_form.html
@@ -10,5 +10,8 @@
<div class="form-group">
{{ attack_form.target_y }}
</div>
+ <div class="form-group">
+ {{ attack_form.bomb }}
+ </div>
<input id="attack-form-submit" class="btn btn-default" type="submit" value="Shoot!">
-</form>
\ No newline at end of file
+</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment