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