diff --git a/tele_giphy/game/migrations/0006_auto_20160903_2045.py b/tele_giphy/game/migrations/0006_auto_20160903_2045.py new file mode 100644 index 0000000..f827ace --- /dev/null +++ b/tele_giphy/game/migrations/0006_auto_20160903_2045.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-03 20:45 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0005_auto_20160901_0509'), + ] + + operations = [ + migrations.AddField( + model_name='gameoverrecords', + name='game_token', + field=models.ForeignKey(blank=True, db_column='game_token', default='', on_delete=django.db.models.deletion.CASCADE, to='game.Game', to_field='token'), + ), + migrations.AlterField( + model_name='game', + name='token', + field=models.CharField(max_length=16, unique=True), + ), + ] diff --git a/tele_giphy/game/migrations/0007_auto_20160903_2056.py b/tele_giphy/game/migrations/0007_auto_20160903_2056.py new file mode 100644 index 0000000..c7ed9d2 --- /dev/null +++ b/tele_giphy/game/migrations/0007_auto_20160903_2056.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-03 20:56 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0006_auto_20160903_2045'), + ] + + operations = [ + migrations.AlterField( + model_name='gameoverrecords', + name='game_token', + field=models.CharField(max_length=16), + ), + ] diff --git a/tele_giphy/game/migrations/0008_gameoverrecords_game_type.py b/tele_giphy/game/migrations/0008_gameoverrecords_game_type.py new file mode 100644 index 0000000..012a874 --- /dev/null +++ b/tele_giphy/game/migrations/0008_gameoverrecords_game_type.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-04 03:37 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0007_auto_20160903_2056'), + ] + + operations = [ + migrations.AddField( + model_name='gameoverrecords', + name='game_type', + field=models.CharField(default='hotseat', max_length=11), + ), + ] diff --git a/tele_giphy/game/migrations/0009_auto_20160904_0348.py b/tele_giphy/game/migrations/0009_auto_20160904_0348.py new file mode 100644 index 0000000..1615a54 --- /dev/null +++ b/tele_giphy/game/migrations/0009_auto_20160904_0348.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-04 03:48 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0008_gameoverrecords_game_type'), + ] + + operations = [ + migrations.RenameField( + model_name='gameoverrecords', + old_name='game_type', + new_name='mode', + ), + ] diff --git a/tele_giphy/game/models.py b/tele_giphy/game/models.py index 447d9d5..3595a29 100644 --- a/tele_giphy/game/models.py +++ b/tele_giphy/game/models.py @@ -8,7 +8,7 @@ # Keeps tabs on a game token, whether game has started, and ended class Game(models.Model): - token = models.CharField(max_length=16) + token = models.CharField(max_length=16, unique=True) game_active = models.BooleanField(default='False') game_over = models.BooleanField(default='False') current_round = models.IntegerField(default=1) @@ -18,7 +18,7 @@ def __str__(self): return self.token -# Keeps track of what game is attached to a user +# Keeps track of what game is attached to a user; class UserGame(models.Model): ''' USER --- USERGAME --\ @@ -49,3 +49,5 @@ class GameRound(models.Model): class GameOverRecords(models.Model): token = models.CharField(max_length=100) # This is specific to gameover, not same as Game.token records = models.CharField(max_length=500000) + game_token = models.CharField(max_length=16) + mode = models.CharField(max_length=11, default=HOTSEAT_MODE) diff --git a/tele_giphy/game/templates/game/gameover.html b/tele_giphy/game/templates/game/gameover.html index 340331a..baf6052 100644 --- a/tele_giphy/game/templates/game/gameover.html +++ b/tele_giphy/game/templates/game/gameover.html @@ -5,25 +5,48 @@
-

Gameover Token: {{ token }}

- - - - - - - {% for player, actions in result.items %} -

PLAYER: {{ player }}

-
- {% for round in actions.rounds %} -

-

Round {{ forloop.counter }}

- Text Entered: {{ round.user_text }}
- -

- {% endfor %} -
- {% endfor %} + {# Game ended without any rounds in hotseat #} + {% if result == "Game ended without any rounds" %} +

{{ result }}

+

+

Has a doge!

+ + {# If game is over and user tries to go to game over using game token #} + {% elif result|length == 0 %} +

Game is over!

Use following token to see game record:
{{ token }}

+

Save the above token! This page may be deleted at any time.

+ {# Response for after game has just ended #} + {% else %} +

Gameover Token: {{ token }}

+ {# Hotseat play, does not show usernames #} + {% if game_mode == "hotseat" %} + {% for player, actions in result.items %} + {% for round in actions.rounds %} +
+

+

Round {{ forloop.counter }}

+

player {{ forloop.counter }}

+

Text Entered: {{ round.user_text }}


+ +

+ {% endfor %} + {% endfor %} + {# Multiplayer play, shows usernames #} + {% elif game_mode == "multiplayer" %} + {% for player, actions in result.items %} +

PLAYER: {{ player }}

+
+ {% for round in actions.rounds %} +

+

Round {{ forloop.counter }}

+ Text Entered: {{ round.user_text }}
+ +

+ {% endfor %} +
+ {% endfor %} + {% endif %} + {% endif %}

-{% endblock %} \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/tele_giphy/game/templates/game/hotseat_gameplay.html b/tele_giphy/game/templates/game/hotseat_gameplay.html index 3bfce3f..c7249e7 100644 --- a/tele_giphy/game/templates/game/hotseat_gameplay.html +++ b/tele_giphy/game/templates/game/hotseat_gameplay.html @@ -80,7 +80,7 @@

Your GIF

-
+ {% csrf_token %}
diff --git a/tele_giphy/game/urls.py b/tele_giphy/game/urls.py index eea09b8..6f19f71 100644 --- a/tele_giphy/game/urls.py +++ b/tele_giphy/game/urls.py @@ -15,7 +15,7 @@ url(r'^game_lobby/(?P[0-9]+)/$', views.hotseat_gameplay, name='game_lobby'), url(r'^game_lobby/(?P[0-9]+)/choose_new_gif$', views.choose_new_gif, name='choose_new_gif'), url(r'^game_lobby/(?P[0-9]+)/next$', views.pass_on, name='pass_on'), - url(r'^gameover/(?P[\w\-]+)/$', views.gameover, name = 'gameover'), + url(r'^gameover/(?P[\w\-]+)/$', views.gameover, name='gameover'), url(r'^game_lobby/(?P[0-9]+)/$', views.multi_gameplay, name='multi_game_lobby'), url(r'^game_lobby/(?P[0-9]+)/choose_new_gif$', views.multi_choose_new_gif, name='multi_choose_new_gif'), url(r'^game_lobby/(?P[0-9]+)/next$', views.multi_pass_on, name='multi_pass_on'), diff --git a/tele_giphy/game/views.py b/tele_giphy/game/views.py index 64e3218..66e944b 100644 --- a/tele_giphy/game/views.py +++ b/tele_giphy/game/views.py @@ -65,8 +65,8 @@ def _attach_user_to_game(game, request): UserGame.objects.get(user=request.user) url = reverse('game:pre_game_room', args=(request.user.usergame.game,)) messages.error(request, - 'You are already part of a game ({token}). Click here to join it.'.format( - token=request.user.usergame.game, url=url)) + 'You are already part of a game ({token}). Click here to join it.'.format( + token=request.user.usergame.game, url=url)) raise IntegrityError except UserGame.DoesNotExist: UserGame.objects.create(user=request.user, game=game) @@ -109,7 +109,8 @@ def pre_game_room(request, token): This is where players come to wait until the game can start """ users = User.objects.filter(usergame__game__token=token) - return render(request, 'game/pre_game_room.html', {"token": token, "users": users}) + return render(request, 'game/pre_game_room.html', { + "token": token, "users": users}) def start_game(request, token): @@ -162,9 +163,6 @@ def choose_name(request): def hotseat_gameplay(request, token): # if roundnumber of game is 1 (first turn) g = Game.objects.get(token=token) - if g.current_round == 4: - print(g.gameround_set.all()) - return HttpResponseRedirect(reverse('game:gameover', args=(token,))) if g.current_round > 1: received_gif = g.gameround_set.get(round_number=g.current_round - 1).giphy_url else: @@ -250,12 +248,32 @@ def gameover(request, token): # Checks what kind of token is passed and fetch object # End of game token if len(token) == 4: - g = get_object_or_404(Game, token=token) + # Check if gameover already happened, if so display postGameToken + try: + gameover = GameOverRecords.objects.get(game_token=token) + # If game ended before actions were made + if len(gameover.records) == 2: + return render(request, 'game/gameover.html', { + "result": "Game ended without any rounds", + "doge": gif_random('doge').json()['data']['image_url']}) + # If game ended with actions + result_url = reverse('game:gameover', args=(gameover.token,)) + return render(request, 'game/gameover.html', { + "result": '', + "token": gameover.token, + "request_url": result_url}) + # Game has not ended + except GameOverRecords.DoesNotExist: + g = get_object_or_404(Game, token=token) + # (Maybe) gameover records token elif len(token) > 4: g = get_object_or_404(GameOverRecords, token=token) - # + # Type of game that's being played + game_mode = g.mode + + # Processes gameRounds and stores it if isinstance(g, Game): # Fetch game round records, ordered by origin user and round number game_rounds = g.gameround_set.all().order_by('origin_user', 'round_number') @@ -274,18 +292,21 @@ def gameover(request, token): {'user_text': user_text, 'giphy_url': gTurn.giphy_url}) + # Stores a json of all players actions in post-gameover model + postGameToken = str(uuid4()) + result_json = json.dumps(result) + GameOverRecords.objects.create( + token=postGameToken, + records=result_json, + game_token=g.token, + mode=game_mode) + # Signout of user session, delete user and game user = request.user django_logout(request) if user.is_authenticated: user.delete() - - # Stores a json of all players actions in post-gameover model - postGameToken = str(uuid4()) - result_json = json.dumps(result) - GameOverRecords.objects.get_or_create( - token=postGameToken, - defaults={'records': result_json}) + g.delete() # Gets Previously stored gameover records elif isinstance(g, GameOverRecords): @@ -294,9 +315,17 @@ def gameover(request, token): else: raise Http404 - # result_url = reverse('gameover', args=(postGameToken,)) - - return render(request, 'game/gameover.html', {"result": result, "token": postGameToken}) + # If game ended before actions were made + if len(result) < 1: + return render(request, 'game/gameover.html', { + "result": "Game ended without any rounds", + "doge": gif_random('doge').json()['data']['image_url']}) + + # Gameover screen + return render(request, 'game/gameover.html', { + "result": result, + "token": postGameToken, + "game_mode": game_mode}) def multi_choose_new_gif(request, token):