This commit is contained in:
Administrator 2022-08-28 16:42:40 +03:00
parent b5fb630a3c
commit d86c05fbe0
2 changed files with 20 additions and 0 deletions

View File

@ -133,6 +133,25 @@ def check_status(request):
}) })
@csrf_exempt
def get_fields(request):
if request.method == 'OPTIONS':
return JsonResponse({})
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)
my_field = list(player.field)
player2 = Player.objects.get(game_id=game_id, number=(1 - player.number))
another_field = list(player2.field.replace('o', ' '))
return JsonResponse({
'my_field': [my_field[x * 10: (x + 1) * 10] for x in range(0, 10)],
'opponent_field': [another_field[x * 10: (x + 1) * 10] for x in range(0, 10)],
'my_turn': player.game.turn == player.number,
'game_finished': 'o' not in player.field or 'o' not in player2.field
})
@csrf_exempt @csrf_exempt
def check_field_correct(request): def check_field_correct(request):
if request.method == 'OPTIONS': if request.method == 'OPTIONS':

View File

@ -27,4 +27,5 @@ urlpatterns = [
path('api/place_ships', views.place_ships), path('api/place_ships', views.place_ships),
path('api/shoot', views.shoot), path('api/shoot', views.shoot),
path('api/check_field_correct', views.check_field_correct), path('api/check_field_correct', views.check_field_correct),
path('api/get_fields', views.get_fields)
] ]