This commit is contained in:
Administrator 2022-08-24 23:34:04 +03:00
parent eb2e18c740
commit a2bb905def

View File

@ -1,3 +1,5 @@
import json
from django.http import JsonResponse
# Create your views here.
from django.utils import timezone
@ -32,8 +34,9 @@ def new_game(request):
def attend_game(request):
if request.method == 'OPTIONS':
return JsonResponse({})
game_id = request.POST['game_id']
attend_token = request.POST['attend_token']
data = json.loads(request.body.decode('utf-8'))
game_id = data['game_id']
attend_token = data['attend_token']
player = Player.objects.get(game_id=game_id, attend_token=attend_token)
if player.token is not None:
return JsonResponse({}, status=403)
@ -48,12 +51,13 @@ def attend_game(request):
def place_ships(request):
if request.method == 'OPTIONS':
return JsonResponse({})
game_id = request.POST['game_id']
token = request.POST['token']
data = json.loads(request.body.decode('utf-8'))
game_id = data['game_id']
token = data['token']
player = Player.objects.get(game_id=game_id, token=token)
if player.field != ' ' * 100:
return JsonResponse({}, status=403)
player.field = request.POST['field']
player.field = data['field']
if check_field(player.field):
player.save()
return JsonResponse({})
@ -65,8 +69,9 @@ def place_ships(request):
def check_opponent(request):
if request.method == 'OPTIONS':
return JsonResponse({})
game_id = request.POST['game_id']
token = request.POST['token']
data = json.loads(request.body.decode('utf-8'))
game_id = data['game_id']
token = data['token']
player = Player.objects.get(game_id=game_id, token=token)
player2 = Player.objects.filter(game_id=game_id, number=(1 - player.number)).first()
if player2 is None:
@ -78,14 +83,15 @@ def check_opponent(request):
def shoot(request):
if request.method == 'OPTIONS':
return JsonResponse({})
game_id = request.POST['game_id']
token = request.POST['token']
data = json.loads(request.body.decode('utf-8'))
game_id = data['game_id']
token = data['token']
player = Player.objects.get(game_id=game_id, token=token)
if player.game.turn != player.number:
return JsonResponse({}, status=403)
player2 = Player.objects.get(game_id=game_id, number=(1 - player.number))
h = request.POST['h']
v = request.POST['v']
h = data['h']
v = data['v']
pos = h * 10 + v
if player2.field[pos] == 'x' or player2.field[pos] == '.':
return JsonResponse({}, status=403)
@ -117,8 +123,9 @@ def shoot(request):
def check_status(request):
if request.method == 'OPTIONS':
return JsonResponse({})
game_id = request.POST['game_id']
token = request.POST['token']
data = json.loads(request.body.decode('utf-8'))
game_id = data['game_id']
token = data['token']
player = Player.objects.get(game_id=game_id, token=token)
player2 = Player.objects.get(game_id=game_id, number=1 - player.number)
return JsonResponse({
@ -131,6 +138,8 @@ def check_status(request):
def check_field_correct(request):
if request.method == 'OPTIONS':
return JsonResponse({})
data = json.loads(request.body.decode('utf-8'))
correct = check_field(data['field'])
return JsonResponse({
'correct': check_field(request.POST['field'])
'correct': correct
})