From 389a6d8eaf9537e0bd04ef291953688be3c2459e Mon Sep 17 00:00:00 2001 From: Aman Nidhi Date: Tue, 15 Oct 2019 21:23:42 +0530 Subject: [PATCH 1/2] Added Games init App --- phoenix/games/__init__.py | 0 phoenix/games/admin.py | 3 +++ phoenix/games/apps.py | 5 +++++ phoenix/games/migrations/__init__.py | 0 phoenix/games/models.py | 3 +++ phoenix/games/tests.py | 3 +++ phoenix/games/views.py | 3 +++ 7 files changed, 17 insertions(+) create mode 100644 phoenix/games/__init__.py create mode 100644 phoenix/games/admin.py create mode 100644 phoenix/games/apps.py create mode 100644 phoenix/games/migrations/__init__.py create mode 100644 phoenix/games/models.py create mode 100644 phoenix/games/tests.py create mode 100644 phoenix/games/views.py diff --git a/phoenix/games/__init__.py b/phoenix/games/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/phoenix/games/admin.py b/phoenix/games/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/phoenix/games/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/phoenix/games/apps.py b/phoenix/games/apps.py new file mode 100644 index 0000000..b74f62c --- /dev/null +++ b/phoenix/games/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class GamesConfig(AppConfig): + name = 'games' diff --git a/phoenix/games/migrations/__init__.py b/phoenix/games/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/phoenix/games/models.py b/phoenix/games/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/phoenix/games/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/phoenix/games/tests.py b/phoenix/games/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/phoenix/games/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/phoenix/games/views.py b/phoenix/games/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/phoenix/games/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From 379cdc28ab9352f65f97db222481ae2f222ece37 Mon Sep 17 00:00:00 2001 From: Aman Nidhi Date: Fri, 25 Oct 2019 15:20:49 +0530 Subject: [PATCH 2/2] Added Games App --- Pipfile | 2 +- Pipfile.lock | 66 ++++++++++++++------------- phoenix/games/constants.py | 18 ++++++++ phoenix/games/models.py | 87 +++++++++++++++++++++++++++++++++++- phoenix/games/serializers.py | 14 ++++++ phoenix/games/urls.py | 13 ++++++ phoenix/games/views.py | 37 ++++++++++++++- phoenix/settings.py | 3 +- 8 files changed, 203 insertions(+), 37 deletions(-) create mode 100644 phoenix/games/constants.py create mode 100644 phoenix/games/serializers.py create mode 100644 phoenix/games/urls.py diff --git a/Pipfile b/Pipfile index e2cbaa8..af84451 100644 --- a/Pipfile +++ b/Pipfile @@ -16,4 +16,4 @@ django-save-the-change = "*" bcrypt = "*" [requires] -python_version = "3.7" +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock index 7008010..9ea35de 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,11 +1,11 @@ { "_meta": { "hash": { - "sha256": "ce6fc666bd858d9b1fe7084607809f38ce151701f8fb7faf9c592372b7f22b73" + "sha256": "506de7723d9bd786446d1da7f24a5ee830e7471e1d98274cce51743d7938d458" }, "pipfile-spec": 6, "requires": { - "python_version": "3.7" + "python_version": "3.8" }, "sources": [ { @@ -47,36 +47,40 @@ }, "cffi": { "hashes": [ - "sha256:041c81822e9f84b1d9c401182e174996f0bae9991f33725d059b771744290774", - "sha256:046ef9a22f5d3eed06334d01b1e836977eeef500d9b78e9ef693f9380ad0b83d", - "sha256:066bc4c7895c91812eff46f4b1c285220947d4aa46fa0a2651ff85f2afae9c90", - "sha256:066c7ff148ae33040c01058662d6752fd73fbc8e64787229ea8498c7d7f4041b", - "sha256:2444d0c61f03dcd26dbf7600cf64354376ee579acad77aef459e34efcb438c63", - "sha256:300832850b8f7967e278870c5d51e3819b9aad8f0a2c8dbe39ab11f119237f45", - "sha256:34c77afe85b6b9e967bd8154e3855e847b70ca42043db6ad17f26899a3df1b25", - "sha256:46de5fa00f7ac09f020729148ff632819649b3e05a007d286242c4882f7b1dc3", - "sha256:4aa8ee7ba27c472d429b980c51e714a24f47ca296d53f4d7868075b175866f4b", - "sha256:4d0004eb4351e35ed950c14c11e734182591465a33e960a4ab5e8d4f04d72647", - "sha256:4e3d3f31a1e202b0f5a35ba3bc4eb41e2fc2b11c1eff38b362de710bcffb5016", - "sha256:50bec6d35e6b1aaeb17f7c4e2b9374ebf95a8975d57863546fa83e8d31bdb8c4", - "sha256:55cad9a6df1e2a1d62063f79d0881a414a906a6962bc160ac968cc03ed3efcfb", - "sha256:5662ad4e4e84f1eaa8efce5da695c5d2e229c563f9d5ce5b0113f71321bcf753", - "sha256:59b4dc008f98fc6ee2bb4fd7fc786a8d70000d058c2bbe2698275bc53a8d3fa7", - "sha256:73e1ffefe05e4ccd7bcea61af76f36077b914f92b76f95ccf00b0c1b9186f3f9", - "sha256:a1f0fd46eba2d71ce1589f7e50a9e2ffaeb739fb2c11e8192aa2b45d5f6cc41f", - "sha256:a2e85dc204556657661051ff4bab75a84e968669765c8a2cd425918699c3d0e8", - "sha256:a5457d47dfff24882a21492e5815f891c0ca35fefae8aa742c6c263dac16ef1f", - "sha256:a8dccd61d52a8dae4a825cdbb7735da530179fea472903eb871a5513b5abbfdc", - "sha256:ae61af521ed676cf16ae94f30fe202781a38d7178b6b4ab622e4eec8cefaff42", - "sha256:b012a5edb48288f77a63dba0840c92d0504aa215612da4541b7b42d849bc83a3", - "sha256:d2c5cfa536227f57f97c92ac30c8109688ace8fa4ac086d19d0af47d134e2909", - "sha256:d42b5796e20aacc9d15e66befb7a345454eef794fdb0737d1af593447c6c8f45", - "sha256:dee54f5d30d775f525894d67b1495625dd9322945e7fee00731952e0368ff42d", - "sha256:e070535507bd6aa07124258171be2ee8dfc19119c28ca94c9dfb7efd23564512", - "sha256:e1ff2748c84d97b065cc95429814cdba39bcbd77c9c85c89344b317dc0d9cbff", - "sha256:ed851c75d1e0e043cbf5ca9a8e1b13c4c90f3fbd863dacb01c0808e2b5204201" + "sha256:08f99e8b38d5134d504aa7e486af8e4fde66a2f388bbecc270cdd1e00fa09ff8", + "sha256:1112d2fc92a867a6103bce6740a549e74b1d320cf28875609f6e93857eee4f2d", + "sha256:1b9ab50c74e075bd2ae489853c5f7f592160b379df53b7f72befcbe145475a36", + "sha256:24eff2997436b6156c2f30bed215c782b1d8fd8c6a704206053c79af95962e45", + "sha256:2eff642fbc9877a6449026ad66bf37c73bf4232505fb557168ba5c502f95999b", + "sha256:362e896cea1249ed5c2a81cf6477fabd9e1a5088aa7ea08358a4c6b0998294d2", + "sha256:40eddb3589f382cb950f2dcf1c39c9b8d7bd5af20665ce273815b0d24635008b", + "sha256:5ed40760976f6b8613d4a0db5e423673ca162d4ed6c9ed92d1f4e58a47ee01b5", + "sha256:632c6112c1e914c486f06cfe3f0cc507f44aa1e00ebf732cedb5719e6aa0466a", + "sha256:64d84f0145e181f4e6cc942088603c8db3ae23485c37eeda71cb3900b5e67cb4", + "sha256:6cb4edcf87d0e7f5bdc7e5c1a0756fbb37081b2181293c5fdf203347df1cd2a2", + "sha256:6f19c9df4785305669335b934c852133faed913c0faa63056248168966f7a7d5", + "sha256:719537b4c5cd5218f0f47826dd705fb7a21d83824920088c4214794457113f3f", + "sha256:7b0e337a70e58f1a36fb483fd63880c9e74f1db5c532b4082bceac83df1523fa", + "sha256:853376efeeb8a4ae49a737d5d30f5db8cdf01d9319695719c4af126488df5a6a", + "sha256:85bbf77ffd12985d76a69d2feb449e35ecdcb4fc54a5f087d2bd54158ae5bb0c", + "sha256:8978115c6f0b0ce5880bc21c967c65058be8a15f1b81aa5fdbdcbea0e03952d1", + "sha256:8f7eec920bc83692231d7306b3e311586c2e340db2dc734c43c37fbf9c981d24", + "sha256:8fe230f612c18af1df6f348d02d682fe2c28ca0a6c3856c99599cdacae7cf226", + "sha256:92068ebc494b5f9826b822cec6569f1f47b9a446a3fef477e1d11d7fac9ea895", + "sha256:b57e1c8bcdd7340e9c9d09613b5e7fdd0c600be142f04e2cc1cc8cb7c0b43529", + "sha256:ba956c9b44646bc1852db715b4a252e52a8f5a4009b57f1dac48ba3203a7bde1", + "sha256:ca42034c11eb447497ea0e7b855d87ccc2aebc1e253c22e7d276b8599c112a27", + "sha256:dc9b2003e9a62bbe0c84a04c61b0329e86fccd85134a78d7aca373bbbf788165", + "sha256:dd308802beb4b2961af8f037becbdf01a1e85009fdfc14088614c1b3c383fae5", + "sha256:e77cd105b19b8cd721d101687fcf665fd1553eb7b57556a1ef0d453b6fc42faa", + "sha256:f56dff1bd81022f1c980754ec721fb8da56192b026f17f0f99b965da5ab4fbd2", + "sha256:fa4cc13c03ea1d0d37ce8528e0ecc988d2365e8ac64d8d86cafab4038cb4ce89", + "sha256:fa8cf1cb974a9f5911d2a0303f6adc40625c05578d8e7ff5d313e1e27850bd59", + "sha256:fb003019f06d5fc0aa4738492ad8df1fa343b8a37cbcf634018ad78575d185df", + "sha256:fd409b7778167c3bcc836484a8f49c0e0b93d3e745d975749f83aa5d18a5822f", + "sha256:fe5d65a3ee38122003245a82303d11ac05ff36531a8f5ce4bc7d4bbc012797e1" ], - "version": "==1.12.3" + "version": "==1.13.0" }, "chardet": { "hashes": [ diff --git a/phoenix/games/constants.py b/phoenix/games/constants.py new file mode 100644 index 0000000..7f10354 --- /dev/null +++ b/phoenix/games/constants.py @@ -0,0 +1,18 @@ +APP = 'app' +WEB = 'web' +MOBILE_WEB = 'mobile_web' +CHANNEL_CHOICES = [ + (APP, 'App'), + (WEB, 'Web'), + (MOBILE_WEB, 'Mobile Web'), +] + + +TICTACTOE_GAME = 'ticktactoe' +NIM_GAME = 'nim' +SUDOKU_GAME = 'sudoku' +GAME_TYPE_CHOICES = [ + (TICTACTOE_GAME, 'Ticktactoe'), + (NIM_GAME, 'Nim'), + (SUDOKU_GAME, 'Sudoku') +] diff --git a/phoenix/games/models.py b/phoenix/games/models.py index 71a8362..10222a0 100644 --- a/phoenix/games/models.py +++ b/phoenix/games/models.py @@ -1,3 +1,86 @@ -from django.db import models -# Create your models here. +from django.conf import settings +from django.core.exceptions import NON_FIELD_ERRORS, ValidationError +from django.db import models, transaction +from django import forms + +from phoenix.accounts.models import User +# from phoenix.games.managers.match_manager import MatchManager +from phoenix.games.constants import * + + +class BaseTimeClass(models.Model): + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + abstract = True + + +class Score(BaseTimeClass): + match_played = models.IntegerField(default=0) + match_won = models.IntegerField(default=0) + player = models.OneToOneField('Player', + on_delete=models.CASCADE, + related_name='score') + + def __str__(self): + return str(self.match_played) + + +class Player(BaseTimeClass): + + user_orig = models.OneToOneField(User, + on_delete=models.SET_NULL, + null=True) + + matches = models.ManyToManyField('Match', + through='MatchPlayerMembership', + related_name='player_list',) + + last_played = models.DateTimeField(auto_now_add=True, db_index=True) + user_agent = models.CharField(max_length=254, blank=True, null=True) + channel = models.CharField( + max_length=10, choices=CHANNEL_CHOICES, blank=True, null=True, db_index=True) + + class Meta: + verbose_name = ("Player") + verbose_name_plural = ("Players") + + def __str__(self): + return "players "+str(self.id) + + +class Match(BaseTimeClass): + game_type = models.CharField(choices=GAME_TYPE_CHOICES, + default=TICTACTOE_GAME, + max_length=20, + null=False,) + players = models.ManyToManyField('Player', + through='MatchPlayerMembership', + related_name='players',) + + def __str__(self): + return "Match #"+str(self.id) + + class Meta: + verbose_name_plural = "Matches" + + +class MatchPlayerMembership(BaseTimeClass): + + player = models.ForeignKey(Player, on_delete=models.CASCADE) + match = models.ForeignKey(Match, on_delete=models.CASCADE) + + is_first_player = models.BooleanField(default=False) + is_winner = models.BooleanField(default=False) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=['player', 'match'], name='unique appversion' + ) + ] + + def __str__(self): + return "MatchPlayerMembership #"+str(self.id) diff --git a/phoenix/games/serializers.py b/phoenix/games/serializers.py new file mode 100644 index 0000000..da2d71f --- /dev/null +++ b/phoenix/games/serializers.py @@ -0,0 +1,14 @@ +from django.contrib.auth import get_user_model +from django.contrib.sites.models import Site +from django.core.exceptions import NON_FIELD_ERRORS, ValidationError +from django.db.models import Q +from rest_framework import serializers + +from phoenix.games.models import Match + + +class MatchSerializer(serializers.ModelSerializer): + + class Meta: + model = Match + fields = '__all__' diff --git a/phoenix/games/urls.py b/phoenix/games/urls.py new file mode 100644 index 0000000..f76e0c8 --- /dev/null +++ b/phoenix/games/urls.py @@ -0,0 +1,13 @@ +from rest_framework import routers + +# from phoenix.games.views import ( +# PhoenixAuthViewSet +# ) + +# api_router = routers.DefaultRouter() + +# api_router.register(r'auth', PhoenixAuthViewSet, 'auth') + + +# urlpatterns = [ +# ] diff --git a/phoenix/games/views.py b/phoenix/games/views.py index 91ea44a..81b969b 100644 --- a/phoenix/games/views.py +++ b/phoenix/games/views.py @@ -1,3 +1,36 @@ -from django.shortcuts import render +from django.conf import settings +from django.contrib.auth import get_user_model +from django.contrib.sites.models import Site +from django.contrib.sites.shortcuts import get_current_site +from django.core import serializers +from django.db.models import Q +from django.template.loader import get_template +from rest_framework import generics, mixins, permissions, status, viewsets +from rest_framework.decorators import action +from rest_framework.response import Response -# Create your views here. +from phoenix.games.models import * +from phoenix.games.serializers import MatchSerializer + + +class MatchViewSet(viewsets.GenericViewSet): + + queryset = Match.objects.all() + + permission_classes_by_action = { + 'pusher_authentication': [permissions.AllowAny], + } + + def get_permissions(self): + try: + return [permission() for permission in self.permission_classes_by_action[self.action]] + except KeyError: + return [permission() for permission in self.permission_classes] + + def get_serializer_class(self): + # if self.action == 'current_user': + return MatchSerializer + + @action(detail=False, methods=['post']) + def pusher_authentication(self): + pass diff --git a/phoenix/settings.py b/phoenix/settings.py index 3c5ff87..6c63748 100644 --- a/phoenix/settings.py +++ b/phoenix/settings.py @@ -44,7 +44,8 @@ 'debug_toolbar', 'rest_framework', - 'phoenix.accounts' + 'phoenix.accounts', + 'phoenix.games', ] PASSWORD_HASHERS = (