created logic to process hero movement, removed drf_yasg

This commit is contained in:
Alexander Karpov 2022-07-09 14:12:29 +03:00
parent 7812e982b0
commit 79a4d75730
5 changed files with 39 additions and 57 deletions

View File

@ -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",
],
},
},

View File

@ -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<format>\.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",
),
]

View File

@ -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)

View File

@ -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()

View File

@ -1,3 +1,25 @@
def move_handler(prev_x, prev_y, x, y, room, player):
# TODO: implement move logic + check user identity
pass
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)