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

Added Games init App #7

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ django-save-the-change = "*"
bcrypt = "*"

[requires]
python_version = "3.7"
python_version = "3.8"
66 changes: 35 additions & 31 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added phoenix/games/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions phoenix/games/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions phoenix/games/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class GamesConfig(AppConfig):
name = 'games'
18 changes: 18 additions & 0 deletions phoenix/games/constants.py
Original file line number Diff line number Diff line change
@@ -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')
]
Empty file.
86 changes: 86 additions & 0 deletions phoenix/games/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

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)
14 changes: 14 additions & 0 deletions phoenix/games/serializers.py
Original file line number Diff line number Diff line change
@@ -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__'
3 changes: 3 additions & 0 deletions phoenix/games/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
13 changes: 13 additions & 0 deletions phoenix/games/urls.py
Original file line number Diff line number Diff line change
@@ -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 = [
# ]
36 changes: 36 additions & 0 deletions phoenix/games/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

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
3 changes: 2 additions & 1 deletion phoenix/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
'debug_toolbar',

'rest_framework',
'phoenix.accounts'
'phoenix.accounts',
'phoenix.games',
]

PASSWORD_HASHERS = (
Expand Down