示例#1
0
    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': 'Игра и связь игра-человек успешно созданы',
        }
示例#2
0
 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,
     )
示例#3
0
    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
示例#4
0
 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,
         )
示例#5
0
 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',
         )
示例#6
0
    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,
        )
示例#7
0
    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,
        )
示例#8
0
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
示例#9
0
 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',
         )