def execute(cls, author: Gamer, data: Dict): try: key = cls._choose_key() except IndexError: raise HttpRequestException( status=205, reason='there are too many games', ) try: cur_game = cls._create_game(key, author, data['type']) cur_game.save() cur_gig = cls._create_gig(author, cur_game, cls.INDEX_COLOR_TO_AUTHOR, chief=True) cur_gig.save() except Exception as e: raise HttpRequestException( class_error=HttpResponseServerError, reason=str(e), ) return { 'result': key, 'description': 'Игра и связь игра-человек успешно созданы', }
def validate(cls, user, data): if user.money < data['cost']: raise HttpRequestException( status=205, reason='Insufficient funds', ) try: gg = GiG.objects.get(gamer=user) except ObjectDoesNotExist: raise HttpRequestException( class_error=HttpResponseNotFound, reason='No communication game player', ) return dict( cost=data['cost'], gg=gg, )
def _get_game(cls, string_invite): game = cls.get_games_with_string_invite(string_invite) if game.is_block: raise HttpRequestException( reason='You are not allowed to enter this game', class_error=HttpResponseForbidden, ) return game
def get_games_with_string_invite(string_invite): """ Общий метод получения игры по инвайту """ try: return Game.objects.get(link=string_invite) except ObjectDoesNotExist: raise HttpRequestException( reason='Игра с такой string_invite не найдена', class_error=HttpResponseNotFound, )
def validate(cls, from_user, player_name): try: player = Gamer.objects.get(player_name) return dict( target=GiG.objects.get(gamer=player) ) except ObjectDoesNotExist: raise HttpRequestException( class_error=HttpResponseNotFound, reason='There are no such people', )
def validate(cls, player: Gamer): game = cls.game(player) all_gamers = cls.all_gamers(game) gig = GiG.objects.get(game=game, gamer=player) if not gig.chief: raise HttpRequestException( class_error=HttpResponseForbidden, reason='You are not the creator of the game', ) if game.cnt_gamers == 1 and not player.user.is_superuser: raise HttpRequestException( status=205, reason='You can\'t start playing alone', ) return dict( game=game, all_gamers=all_gamers, )
def validate(cls, player: Gamer): game, all_gamers = super().validate(player).values() if game.is_initialized: raise HttpRequestException( class_error=HttpResponseForbidden, reason='Game already started', ) return dict( game=game, all_gamers=all_gamers, )
def check_request(request, class_form=None, method='GET', stage=Stage.FULL): retval = None if stage['validate_form'] and class_form: form = class_form(request.__getattribute__(method)) if not form.is_valid(): raise HttpRequestException( class_error=HttpResponseBadRequest, reason=form.errors, ) retval = form.cleaned_data if stage['authenticated']: if not request.user.is_authenticated: raise HttpRequestException( class_error=HttpResponse, reason='Unauthorized', status=401, ) request.user = Gamer.objects.get(user=request.user) return retval
def game(player: Gamer) -> Game: try: active = reduce(lambda q, v: q | v, [ Q(status=Game.StatusGameChoices.CREATED), Q(status=Game.StatusGameChoices.INITIALIZED), Q(status=Game.StatusGameChoices.STARTED), ], Q()) return Game.objects.get( active, players__in=[player], ) except ObjectDoesNotExist: raise HttpRequestException( class_error=HttpResponseNotFound, reason='No current games', )