diff --git a/.gitignore b/.gitignore index f28dba1..deea292 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .idea static/ -media/ +media/uploads/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/README.md b/README.md index 12d3433..900bca3 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,23 @@ # chess_rpg_backend -Backend for chess rpg game +DEV branch for backend for chess rpg game +
+ +##### dev server for up to date endpoints(web socket not provided) + +- https://dev.akarpov.ru + +
### installation ```shell $ python3 manage.py makemigrations & python3 manage.py migrate +$ python3 manage.py loaddata media/dump_data/hero_model_fixture.json $ docker run -p 6379:6379 -d redis:5 ``` ### run ```shell -$ python3 manage.py runserver 0.0.0.0:8000 +$ daphne -b 0.0.0.0 -p 8000 chess_backend.asgi:application ``` ### Описание команд сокетов diff --git a/chess_backend/settings.py b/chess_backend/settings.py index a05c22c..035bb14 100644 --- a/chess_backend/settings.py +++ b/chess_backend/settings.py @@ -11,10 +11,14 @@ DEBUG = True ALLOWED_HOSTS = [] +if DEBUG: + ALLOWED_HOSTS = ["*"] + INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.auth", "django.contrib.contenttypes", + "django.contrib.staticfiles", "django.contrib.messages", # Packages "rest_framework", @@ -25,10 +29,26 @@ INSTALLED_APPS = [ ] if DEBUG: - INSTALLED_APPS.append("django.contrib.staticfiles") 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', + ], + }, + }, +] + + MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", diff --git a/chess_backend/urls.py b/chess_backend/urls.py index 956f93a..34b266d 100644 --- a/chess_backend/urls.py +++ b/chess_backend/urls.py @@ -22,8 +22,10 @@ schema_view = get_schema_view( permission_classes=(permissions.AllowAny,), ) -urlpatterns = [path("api/", include("game.urls"))] + static( - settings.MEDIA_URL, document_root=settings.MEDIA_ROOT +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: @@ -43,4 +45,4 @@ if settings.DEBUG: schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc", ), - ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + ] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..4bc884a --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,31 @@ +services: + daphne: + build: + dockerfile: ./build/Dockerfile + context: . + image: daphne + volumes: + - type: bind + source: ${ROOT_DIR}/logs + target: /app/logs + ports: + - "8080:8080" + - "8000:8000" + depends_on: + - postgres + tty: true + container_name: daphne_server + postgres: + image: postgres:latest + ports: + - "5432:5432" + environment: + - POSTGRES_USER=daphne + - POSTGRES_PASSWORD=daphne + - POSTGRES_DB=daphne + container_name: daphne_database + redis: + image: redis:latest + ports: + - "6379:6379" + container_name: daphne_redis \ No newline at end of file diff --git a/game/models.py b/game/models.py index 7976b25..222caff 100644 --- a/game/models.py +++ b/game/models.py @@ -97,7 +97,7 @@ class Hero(models.Model): added = models.DateTimeField(auto_now_add=True) type = models.CharField(blank=False, choices=HeroTypes.choices, max_length=7) - model = models.ForeignKey("HeroModelSet", on_delete=models.CASCADE) + model_f = models.ForeignKey("HeroModelSet", on_delete=models.CASCADE) health = models.IntegerField( validators=[MinValueValidator(1), MaxValueValidator(10)], blank=False ) @@ -108,14 +108,8 @@ class Hero(models.Model): validators=[MinValueValidator(1), MaxValueValidator(10)], blank=False ) - def idle_img(self): - return self.idle_img_f.image.url - - def attack_img(self): - return self.attack_img_f.image.url - - def die_img(self): - return self.die_img_f.image.url + def model(self): + return self.model_f.model.url def __str__(self): return f"{self.type} {self.player.name}" @@ -145,7 +139,7 @@ class Hero(models.Model): class HeroModelSet(models.Model): hero_type = models.CharField(blank=False, choices=HeroTypes.choices, max_length=7) - model = models.ImageField(upload_to="uploads/") + model = models.FileField(upload_to="uploads/") def __str__(self): return f"{self.hero_type} model file" diff --git a/media/dump_data/dump.jpg b/media/dump_data/dump.jpg new file mode 100644 index 0000000..4c60bc1 Binary files /dev/null and b/media/dump_data/dump.jpg differ diff --git a/media/dump_data/dump_1P13Svy.jpg b/media/dump_data/dump_1P13Svy.jpg new file mode 100644 index 0000000..4c60bc1 Binary files /dev/null and b/media/dump_data/dump_1P13Svy.jpg differ diff --git a/media/dump_data/dump_d6lH7IJ.jpg b/media/dump_data/dump_d6lH7IJ.jpg new file mode 100644 index 0000000..4c60bc1 Binary files /dev/null and b/media/dump_data/dump_d6lH7IJ.jpg differ diff --git a/media/dump_data/dump_mx4zxHq.jpg b/media/dump_data/dump_mx4zxHq.jpg new file mode 100644 index 0000000..4c60bc1 Binary files /dev/null and b/media/dump_data/dump_mx4zxHq.jpg differ diff --git a/media/dump_data/hero_model_fixture.json b/media/dump_data/hero_model_fixture.json new file mode 100644 index 0000000..76a6e19 --- /dev/null +++ b/media/dump_data/hero_model_fixture.json @@ -0,0 +1,34 @@ +[ + { + "model": "game.heromodelset", + "pk": 1, + "fields": { + "hero_type": "KING", + "model": "dump_data/dump.jpg" + } + }, + { + "model": "game.heromodelset", + "pk": 2, + "fields": { + "hero_type": "WARRIOR", + "model": "dump_data/dump_mx4zxHq.jpg" + } + }, + { + "model": "game.heromodelset", + "pk": 3, + "fields": { + "hero_type": "ARCHER", + "model": "dump_data/dump_d6lH7IJ.jpg" + } + }, + { + "model": "game.heromodelset", + "pk": 4, + "fields": { + "hero_type": "WIZARD", + "model": "dump_data/dump_1P13Svy.jpg" + } + } +] \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e538abf --- /dev/null +++ b/nginx.conf @@ -0,0 +1,44 @@ +server { + # Base settings for client body size + client_max_body_size 64M; + client_body_timeout 10; + send_timeout 10; + keepalive_timeout 10; + client_header_timeout 10; + client_body_buffer_size 64M; + client_header_buffer_size 64M; + client_max_header_size 64M; + + location /static { + allow all; + autoindex off; + root /var/www; + } + + location /media { + allow all; + autoindex off; + root /var/www; + } + + location / { + allow all; + proxy_pass http://127.0.0.1:8080; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + + listen 443 ssl; + + # ssl settings + # ssl_certificate /etc/nginx/ssl/nginx.crt; + # ssl_certificate_key /etc/nginx/ssl/nginx.key; + # ssl_protocols TLSv1 TLSv1.1 TLSv1.2; +} diff --git a/room/consumers.py b/room/consumers.py index 04c8fab..7c5e772 100644 --- a/room/consumers.py +++ b/room/consumers.py @@ -1,9 +1,14 @@ import json +import os +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() + from game.models import Deck from room.models import PlayerInQueue, Room, PlayerInRoom, GameState from room.services.room_create import create_room