Ejemplo n.º 1
0
def create(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST are allowed!')

    try:
        game_code = request.POST.get('game_code')
        board_name = request.POST.get('board_name')

        if not re.match("^[A-Za-z0-9]*$", game_code):
            url = reverse('GameAssistant:start_new', args=[board_name, 1])
            return HttpResponseRedirect(url)
        if Game.objects(game_code=game_code):
            url = reverse('GameAssistant:start_new', args=[board_name, 0])
            return HttpResponseRedirect(url)

        num_of_players = int(request.POST.get('num_of_players'))
        board_name = request.POST.get('board_name')
        board = board_factory(board_name)
        role_config = {}
        for key, value in request.POST.items():
            if key in board.role_index:
                role_config[key] = int(value)

        board.set_role_config(role_config)
        role_list = board.deal()

        if len(role_list) != num_of_players:
            url = reverse('GameAssistant:start_new', args=[board_name, 2])
            return HttpResponseRedirect(url)

        client_id = get_client_id_from_session(request)
        room_number = 0
        while True:
            room_number += 1
            if not Game.objects(room_number=room_number):
                break

        game = Game(client_id=client_id,
                    room_number=room_number,
                    board_name=board_name,
                    game_code=game_code,
                    num_of_players=num_of_players)
        for number in range(0, game.num_of_players):
            seat = Seat(seat_number=number + 1,
                        game_code=game_code,
                        role=role_list[number])
            game.game_seats.append(seat)
        game.save()

        url = reverse('GameAssistant:going_room')
        return HttpResponseRedirect(url)

    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running game.create! Details: {0}'.format(e))
Ejemplo n.º 2
0
def enter(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST are allowed!')

    try:
        game_code = request.POST.get('gamecode')

        if not re.match("^[A-Za-z0-9]*$", game_code):
            url = reverse('GameAssistant:home_index', args=[0])
            return HttpResponseRedirect(url)

        if not Game.objects(game_code=game_code):
            url = reverse('GameAssistant:home_index', args=[1])
            return HttpResponseRedirect(url)

        game = Game.objects(game_code=game_code).first()

        client_id = game.client_id

        if not Client.objects(client_id=client_id):
            url = reverse('GameAssistant:home_index', args=[4])
            return HttpResponseRedirect(url)

        client = Client.objects(client_id=client_id).first()

        if 'sessionid' in request.COOKIES:
            sessionid = request.COOKIES.get('sessionid')
            session = Session.objects.get(session_key=sessionid)
            if session and session.get_decoded().get('subclient_id'):
                subclient_id = session.get_decoded().get('subclient_id')
                if client.has_subclient(subclient_id):

                    url = reverse('GameAssistant:going_room_guest')
                    return HttpResponseRedirect(url)

        #If no cookie of subclient:
        subclient_id = client.generate_subclient_id()
        if client.add_subclient(subclient_id=subclient_id):
            request.session.set_expiry(60 * 60 * 24)
            request.session['subclient_id'] = subclient_id

            url = reverse('GameAssistant:going_room_guest')
            return HttpResponseRedirect(url)

        else:
            return HttpResponseBadRequest(
                'Unknown error happened! Failed to generate subuser!')

    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running subclient.enter! Details: {0}'.format(
                e))
Ejemplo n.º 3
0
def delete(request):
    try:
        if request.method != 'POST':
            return HttpResponseBadRequest('Only POST are allowed!')
        client_id = get_client_id_from_session(request)
        if Game.objects(client_id=client_id):
            game = Game.objects(client_id=client_id).first()
            game.delete()
            url = reverse('GameAssistant:start_profile', args=[''])
            return HttpResponseRedirect(url)
        return HttpResponseBadRequest('Game not existed!')
    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running game.delete! Details: {0}'.format(e))
Ejemplo n.º 4
0
def end(request):
    try:
        if request.method != 'POST':
            return HttpResponseBadRequest('Only POST are allowed!')
        client_id = get_client_id_from_session(request)
        if Game.objects(client_id=client_id):
            game = Game.objects(client_id=client_id).first()
            game.update(game_state=GameState.ended.value)
            url = reverse('GameAssistant:going_room')
            return HttpResponseRedirect(url)
        return HttpResponseBadRequest('Game not existed!')
    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running game.end! Details: {0}'.format(e))
Ejemplo n.º 5
0
def edit(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST are allowed!')
    try:
        new_name = request.POST.get('new_name')
        game_code = request.POST.get('game_code')
        seat_number = request.POST.get('seat_number')
        client_id = get_client_id_from_session(request)

        client = Client.objects(client_id=client_id).first()
        game = Game.objects(game_code=game_code).first()
        seat = game.game_seats.filter(seat_number=seat_number).first()
        current_seat_state = seat.seat_state
        current_seat_user_id = seat.user_id

        if current_seat_state == SeatState.subuser.value:
            if client.update_subclient(subclient_id=current_seat_user_id,
                                       subclient_name=new_name):
                url = reverse('GameAssistant:going_room')
                return HttpResponseRedirect(url)
            else:
                return HttpResponseBadRequest(
                    "Unknown error happened! Failed to update subclient!")

        url = reverse('GameAssistant:going_room')
        return HttpResponseRedirect(url)

    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running client.edit! Details: {0}'.format(e))
Ejemplo n.º 6
0
def remove(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST are allowed!')
    try:
        game_code = request.POST.get('game_code')
        seat_number = request.POST.get('seat_number')
        sessionid = request.COOKIES.get('sessionid')
        session = Session.objects.get(session_key=sessionid)
        client_id = session.get_decoded().get('client_id')
        game = Game.objects(game_code=game_code).first()

        seat = game.game_seats.filter(seat_number=seat_number).first()
        current_seat_state = seat.seat_state
        if current_seat_state == SeatState.subuser.value:
            if game.update_seat(seat_number=seat_number,
                                user_id='',
                                seat_state=SeatState.empty.value):
                url = reverse('GameAssistant:going_room')
                return HttpResponseRedirect(url)
            else:
                return HttpResponseBadRequest(
                    "Unknown error happened! Failed to update game!")

        url = reverse('GameAssistant:going_room')
        return HttpResponseRedirect(url)

    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running client.remove! Details: {0}'.format(
                e))
Ejemplo n.º 7
0
def unsit(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST are allowed!')
    try:
        game_code = request.POST.get('game_code')
        seat_number = request.POST.get('seat_number')
        sessionid = request.COOKIES.get('sessionid')
        session = Session.objects.get(session_key=sessionid)
        subclient_id = session.get_decoded().get('subclient_id')
        client_id = subclient_id.split('@', 1)[-1]
        game = Game.objects(game_code=game_code).first()

        if user_is_seated(subclient_id, game):
            seat = game.game_seats.filter(seat_number=seat_number).first()
            current_seat_state = seat.seat_state
            current_seat_user_id = seat.user_id
            if (current_seat_state != SeatState.empty.value) and (
                    current_seat_user_id == subclient_id):
                if game.update_seat(seat_number=seat_number,
                                    user_id='',
                                    seat_state=SeatState.empty.value):
                    url = reverse('GameAssistant:going_room_guest')
                    return HttpResponseRedirect(url)
                else:
                    return HttpResponseBadRequest(
                        "Unknown error happened! Failed to update game!")

        url = reverse('GameAssistant:going_room_guest')
        return HttpResponseRedirect(url)

    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running subclient.unsit! Details: {0}'.format(
                e))
Ejemplo n.º 8
0
def get_game_infor(request):
    try:
        client_id = get_client_id_from_session(request)
        if Game.objects(client_id=client_id):
            game = Game.objects(client_id=client_id).first()
            ret = {}
            ret['RoomNumber'] = game.room_number
            ret['BoardName'] = game.board_name
            ret['GameCode'] = game.game_code
            ret['GameState'] = game.game_state
            ret['NumberOfPlayers'] = game.num_of_players
            ret['WsId'] = game.websocket_id()
            return JsonResponse(ret, safe=False)
        return HttpResponseBadRequest('Game not existed!')
    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running game.get_game_infor! Details: {0}'.
            format(e))
Ejemplo n.º 9
0
def get_seats(request):
    try:
        client_id = get_client_id_from_session(request)
        user_id = get_user_id_from_session(request)
        if Game.objects(client_id=client_id):
            game = Game.objects(client_id=client_id).first()
            user_seated = 1 if user_is_seated(user_id, game) else 0
            ret = []
            for seat in game.game_seats:
                nickname = 'Waiting'
                user_seated_here = False
                if seat.seat_state == SeatState.superuser.value:
                    nickname = Client.objects(
                        client_id=seat.user_id).first().client_name
                    if client_id == user_id:
                        user_seated_here = True
                elif seat.seat_state == SeatState.subuser.value:
                    nickname = Client.objects(
                        client_id=client_id).first().subclients.filter(
                            subclient_id=seat.user_id).first().subclient_name
                    if user_id == seat.user_id:
                        user_seated_here = True
                ret_single = {
                    'SeatNumber': seat.seat_number,
                    'GameCode': game.game_code,
                    'GameState': game.game_state,
                    'SeatState': seat.seat_state,
                    'NickName': nickname,
                    'UserSeated': user_seated,
                    'UserSeatedHere': user_seated_here
                }
                if game.game_state & GameState.ended.value:
                    ret_single.update({'Role': seat.role})
                ret.append(ret_single)

            return JsonResponse(ret, safe=False)
        return HttpResponseBadRequest('Game not existed!')
    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running game.get_seats! Details: {0}'.format(
                e))
Ejemplo n.º 10
0
def get_user_infor(request):
    try:
        client_id = get_client_id_from_session(request)
        if Game.objects(client_id=client_id):
            game = Game.objects(client_id=client_id).first()
            ret = {}
            ret['UserName'] = get_user_name_from_session(request)
            ret['GameState'] = game.game_state
            if game.game_state & (GameState.started.value +
                                  GameState.ended.value):
                role = get_user_role(get_user_id_from_session(request), game)
                description = board_factory(
                    game.board_name).get_description(role)
                ret['Role'] = role
                ret['Description'] = description

            return JsonResponse(ret, safe=False)
        return HttpResponseBadRequest('Game not existed!')
    except Exception as e:
        return HttpResponseBadRequest(
            'Unknown error while running game.get_user_infor! Details: {0}'.
            format(e))
Ejemplo n.º 11
0
def exit(request):
    if request.method != 'POST':
        return HttpResponseBadRequest('Only POST are allowed')
    if 'client_id' in request.session:
        #To clean the subclients temporarily:
        client_id = get_client_id_from_session(request)
        if not Game.objects(client_id=client_id):
            client = Client.objects(client_id=client_id).first()
            client.clear_subclients()

        request.session.flush()
        url = reverse('GameAssistant:sign_in', args=[''])
        return HttpResponseRedirect(url)

    return HttpResponseBadRequest('Unknown error while running start.exit!')
Ejemplo n.º 12
0
        def wrapper(request, *callback_args, **callback_kwargs):
            try:
                sessionid = request.COOKIES.get('sessionid')
                session = Session.objects.get(session_key=sessionid)

                if auth_level == 'superuser':
                    client_id = session.get_decoded().get('client_id')
                    if not state_codes:
                        if Game.objects(client_id=client_id):
                            url = reverse('GameAssistant:start_profile',
                                          args=['0'])
                            return HttpResponseRedirect(url)
                    else:
                        if not Game.objects(client_id=client_id):
                            url = reverse('GameAssistant:start_profile',
                                          args=['1'])
                            return HttpResponseRedirect(url)
                        else:
                            game = Game.objects(client_id=client_id).first()
                            if not (game.game_state & state_codes):
                                return HttpResponseBadRequest(
                                    'Illegal request becuase of illegal game state!'
                                )
                    return func(request, *callback_args, **callback_kwargs)

                elif auth_level == 'subuser':
                    subclient_id = session.get_decoded().get('subclient_id')
                    client_id = subclient_id.split('@', 1)[-1]
                    if not state_codes:
                        if Game.objects(client_id=client_id):
                            url = reverse('GameAssistant:home_index',
                                          args=['2'])
                            return HttpResponseRedirect(url)
                    else:
                        if not Game.objects(client_id=client_id):
                            url = reverse('GameAssistant:home_index',
                                          args=['3'])
                            return HttpResponseRedirect(url)
                        else:
                            game = Game.objects(client_id=client_id).first()
                            if not (game.game_state & state_codes):
                                return HttpResponseBadRequest(
                                    'Illegal request becuase of illegal game state!'
                                )
                    return func(request, *callback_args, **callback_kwargs)

                else:
                    return HttpResponseBadRequest(
                        'Illegal request becuase of illegal authentication level!'
                    )

            except Exception as e:
                return HttpResponseBadRequest(
                    'Unknown error while running utils.game_state! Details: {0}'
                    .format(e))