Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gameover enhancement #79

Merged
merged 8 commits into from
Sep 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tele_giphy/game/migrations/0006_auto_20160903_2045.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 20 additions & 0 deletions tele_giphy/game/migrations/0007_auto_20160903_2056.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 20 additions & 0 deletions tele_giphy/game/migrations/0008_gameoverrecords_game_type.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 20 additions & 0 deletions tele_giphy/game/migrations/0009_auto_20160904_0348.py
Original file line number Diff line number Diff line change
@@ -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',
),
]
6 changes: 4 additions & 2 deletions tele_giphy/game/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 --\
Expand Down Expand Up @@ -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)
63 changes: 43 additions & 20 deletions tele_giphy/game/templates/game/gameover.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,48 @@
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class='text-center'>
<h1>Gameover Token: {{ token }}</h1>
<!-- Make Token a URL if game has ended and been deleted -->
<!-- {% if token|length == 4 %} -->
<!-- {% else %} -->
<!-- <h1>Gameover Token: <a href="{{ request_url }}">{{ token }}</a></h1> -->
<!-- {% endif %} -->

{% for player, actions in result.items %}
<h2>PLAYER: {{ player }}</h2>
<br>
{% for round in actions.rounds %}
<p>
<h3>Round {{ forloop.counter }}</h3>
<b>Text Entered:</b> {{ round.user_text }}<br>
<a href="{{ round.giphy_url }}"><img src="{{ round.giphy_url }}"></a>
</p>
{% endfor %}
<br>
{% endfor %}
{# Game ended without any rounds in hotseat #}
{% if result == "Game ended without any rounds" %}
<h1>{{ result }}</h1>
<br><br>
<h2>Has a doge!</h2>
<a href="{{ doge }}"><img src="{{ doge }}"></a>
{# If game is over and user tries to go to game over using game token #}
{% elif result|length == 0 %}
<h1>Game is over!<br><br>Use following token to see game record:<br><a href="{{ request_url }}">{{ token }}</a></h1>
<h2><b>Save the above token! This page may be deleted at any time.</b></h2>
{# Response for after game has just ended #}
{% else %}
<h1>Gameover Token: {{ token }}</h1>
{# Hotseat play, does not show usernames #}
{% if game_mode == "hotseat" %}
{% for player, actions in result.items %}
{% for round in actions.rounds %}
<br>
<p>
<h3>Round {{ forloop.counter }}</h3>
<h4>player {{ forloop.counter }}</h2>
<h4>Text Entered:</b> {{ round.user_text }}</h4><br>
<a href="{{ round.giphy_url }}"><img src="{{ round.giphy_url }}"></a>
</p>
{% endfor %}
{% endfor %}
{# Multiplayer play, shows usernames #}
{% elif game_mode == "multiplayer" %}
{% for player, actions in result.items %}
<h2>PLAYER: {{ player }}</h2>
<br>
{% for round in actions.rounds %}
<p>
<h3>Round {{ forloop.counter }}</h3>
<b>Text Entered:</b> {{ round.user_text }}<br>
<a href="{{ round.giphy_url }}"><img src="{{ round.giphy_url }}"></a>
</p>
{% endfor %}
<br>
{% endfor %}
{% endif %}
{% endif %}
</div>
<br>
<div class="footer">
Expand All @@ -35,4 +58,4 @@ <h3>Round {{ forloop.counter }}</h3>

</div>

{% endblock %} <!-- body -->
{% endblock %}
2 changes: 1 addition & 1 deletion tele_giphy/game/templates/game/hotseat_gameplay.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h3 class = "text-center">Your GIF</h3>

<div class = "row">
<div class = "col-md-4 col-md-offset-4 text-center">
<form action = "{% url 'game:pass_on' token %}" method = 'post'>
<form action = "{% url 'game:gameover' token %}" method = 'post'>
{% csrf_token %}
<button type="submit" class="btn btn-default">End Game</button>
</form>
Expand Down
2 changes: 1 addition & 1 deletion tele_giphy/game/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
url(r'^game_lobby/(?P<token>[0-9]+)/$', views.hotseat_gameplay, name='game_lobby'),
url(r'^game_lobby/(?P<token>[0-9]+)/choose_new_gif$', views.choose_new_gif, name='choose_new_gif'),
url(r'^game_lobby/(?P<token>[0-9]+)/next$', views.pass_on, name='pass_on'),
url(r'^gameover/(?P<token>[\w\-]+)/$', views.gameover, name = 'gameover'),
url(r'^gameover/(?P<token>[\w\-]+)/$', views.gameover, name='gameover'),
url(r'^game_lobby/(?P<token>[0-9]+)/$', views.multi_gameplay, name='multi_game_lobby'),
url(r'^game_lobby/(?P<token>[0-9]+)/choose_new_gif$', views.multi_choose_new_gif, name='multi_choose_new_gif'),
url(r'^game_lobby/(?P<token>[0-9]+)/next$', views.multi_pass_on, name='multi_pass_on'),
Expand Down
65 changes: 47 additions & 18 deletions tele_giphy/game/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}). <a href="{url}">Click here to join it.</a>'.format(
token=request.user.usergame.game, url=url))
'You are already part of a game ({token}). <a href="{url}">Click here to join it.</a>'.format(
token=request.user.usergame.game, url=url))
raise IntegrityError
except UserGame.DoesNotExist:
UserGame.objects.create(user=request.user, game=game)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand All @@ -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):
Expand All @@ -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):
Expand Down