def client_login(request): # Check if browser supports cookies request.session.set_test_cookie() if not request.session.test_cookie_worked(): return HttpResponseRedirect('/no_cookies/') request.session.delete_test_cookie() if request.method == "POST": form = ClientLoginForm(request.POST) if form.is_valid(): new_player_response = services.new_player(form.cleaned_data['game_code'], form.cleaned_data['player_name']) if new_player_response['error_code'] is None: services.set_session_args(request, player_pk=new_player_response['player'].pk, game_code=new_player_response['player'].game_state_code(), player_name=new_player_response['player'].player_name) context = {'player_name': new_player_response['player'].player_name, 'error_code': None} return render(request, 'game/start_game.html', context) else: error_code = new_player_response['error_code'] if request.method == 'GET': if request.session.get('player_pk'): form = ClientLoginForm({'game_code': request.session.get('game_code'), 'player_name': request.session.get('player_name')}) else: form = ClientLoginForm() error_code = None context = {'form': form, 'error_code': error_code} return render(request, 'game/client_login.html', context)
def client_login(request): if request.method == 'POST': form = ClientLoginForm(request.POST) if form.is_valid(): response = services.new_player(form.cleaned_data['game_code'], form.cleaned_data['player_name']) print(response) if response == "error": return render(request, 'game/client_login.html', {'form': form, 'error': True}) return HttpResponseRedirect('/game/clientsuccess/') if request.method == 'GET': form = ClientLoginForm() return render(request, 'game/client_login.html', {'form': form, 'error': False})
def new_player(request): """ Clients use this function to generate new players. Requires game_code and name query parameters """ game_code = request.query_params.get('game_code', None) name = request.query_params.get('name', None) if game_code and name is not None: player = services.new_player(game_code, name) if player is "error": return Response(status=404, data={'status_code': 404, 'error': 'The specified game_state resource was not found'}) serializer = PlayerExpandedSerializer(player) return Response(serializer.data) return Response(status=400, data={'status_code': 400, 'error': 'Incorrect query parameters supplied. Service requires game_code and name.'})