From 79a4d75730ef120de3f13c313c1d7647b65233be Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sat, 9 Jul 2022 14:12:29 +0300 Subject: [PATCH] created logic to process hero movement, removed drf_yasg --- chess_backend/settings.py | 21 ++++++++----------- chess_backend/urls.py | 39 ----------------------------------- game/services/deck_handler.py | 7 +++++-- room/consumers.py | 1 - room/services/game_logic.py | 28 ++++++++++++++++++++++--- 5 files changed, 39 insertions(+), 57 deletions(-) diff --git a/chess_backend/settings.py b/chess_backend/settings.py index 035bb14..f74280c 100644 --- a/chess_backend/settings.py +++ b/chess_backend/settings.py @@ -28,21 +28,18 @@ INSTALLED_APPS = [ "room", ] -if DEBUG: - INSTALLED_APPS.append("drf_yasg") - TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, diff --git a/chess_backend/urls.py b/chess_backend/urls.py index 34b266d..e60cf14 100644 --- a/chess_backend/urls.py +++ b/chess_backend/urls.py @@ -1,48 +1,9 @@ from django.conf import settings from django.conf.urls.static import static -from django.template.defaulttags import url from django.urls import path, include, re_path -# openapi schema -from rest_framework import permissions -from drf_yasg.views import get_schema_view -from drf_yasg import openapi - - -schema_view = get_schema_view( - openapi.Info( - title="Snippets API", - default_version="v1", - description="Test description", - terms_of_service="https://www.google.com/policies/terms/", - contact=openapi.Contact(email="contact@snippets.local"), - license=openapi.License(name="BSD License"), - ), - public=True, - permission_classes=(permissions.AllowAny,), -) - urlpatterns = ( [path("api/", include("game.urls"))] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ) - -if settings.DEBUG: - urlpatterns += [ - re_path( - r"^swagger(?P\.json|\.yaml)$", - schema_view.without_ui(cache_timeout=0), - name="schema-json", - ), - re_path( - r"^swagger/$", - schema_view.with_ui("swagger", cache_timeout=0), - name="schema-swagger-ui", - ), - re_path( - r"^redoc/$", - schema_view.with_ui("redoc", cache_timeout=0), - name="schema-redoc", - ), - ] diff --git a/game/services/deck_handler.py b/game/services/deck_handler.py index 8de6dee..0b89104 100644 --- a/game/services/deck_handler.py +++ b/game/services/deck_handler.py @@ -10,10 +10,9 @@ def create_first_deck(player: Player): [None, None, None, None, None, None, None, None], ] types = ( - ["KING"] + ["KING", "WIZARD"] + ["ARCHER" for _ in range(4)] + ["WARRIOR" for _ in range(6)] - + ["WIZARD" for _ in range(2)] + [random.choice(HeroTypes.choices[:2])[0] for _ in range(3)] ) for t in types: @@ -26,6 +25,10 @@ def create_first_deck(player: Player): pos_x = 4 pos_y = 0 positions[0][4] = hero + elif t == "WIZARD": + pos_x = 3 + pos_y = 0 + positions[0][3] = hero else: pos_x = random.randint(0, 7) pos_y = random.randint(0, 1) diff --git a/room/consumers.py b/room/consumers.py index e9f39e6..df43f2b 100644 --- a/room/consumers.py +++ b/room/consumers.py @@ -4,7 +4,6 @@ import django from asgiref.sync import sync_to_async from channels.generic.websocket import AsyncWebsocketConsumer -from channels.layers import get_channel_layer os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chess_backend.settings") django.setup() diff --git a/room/services/game_logic.py b/room/services/game_logic.py index cc474f8..f0439e3 100644 --- a/room/services/game_logic.py +++ b/room/services/game_logic.py @@ -1,3 +1,25 @@ -def move_handler(prev_x, prev_y, x, y, room, player): - # TODO: implement move logic + check user identity - pass \ No newline at end of file +from room.models import HeroInGame, Room, PlayerInRoom + + +def _check_move_position(x: int, y: int, room: Room, p_first: bool): + hero = HeroInGame.objects.filter(x=x, y=y, room=room) + if not hero.exists(): + return False + elif hero.first() + + +def move_handler(prev_x: int, prev_y: int, x: int, y: int, room: Room, player: PlayerInRoom): + try: + hero = HeroInGame.objects.get(x=prev_x, y=prev_y, room=room, player=player) + except HeroInGame.DoesNotExist: + return False + + h_t = hero.hero.type + if h_t == "KING": + if abs(prev_x - x) != 1 or abs(prev_y - y) != 1: + return False + elif h_t == "WARRIOR": + if player.first: + if x - prev_x == 1 and y - prev_y == 0: + _check_move_position(x, y, room, True) +