Example #1
0
    def process_request(self, request):
        super(UnpackAndVerifyData, self).process_request(request)

        if request.path.startswith('/api/'):
            return

        if not server.active:
            return HttpResponse(status=502)

        char_id = getattr(request, '_char_id', None)
        if char_id:
            if request.path != '/resume/':
                p = Player(char_id)
                login_id = p.get_login_id()
                if not login_id:
                    msg = ReLoginResponse()
                    msg.ret = errormsg.SESSION_EXPIRE
                    data = pack_msg(msg)
                    return HttpResponse(data, content_type='text/plain')

                if login_id != request._game_session.login_id:
                    # NEED RE LOGIN
                    msg = ReLoginResponse()
                    msg.ret = errormsg.LOGIN_RE
                    data = pack_msg(msg)
                    return HttpResponse(data, content_type='text/plain')

                p.refresh()

            ap = ActivePlayers()
            ap.set(request._char_id)
Example #2
0
def login(request):
    req = request._proto

    try:
        account_data = make_account_dict_from_message(req.login)
    except Exception as e:
        raise SanguoException(errormsg.BAD_MESSAGE, 0, 'Login', e.args[0])

    account_data['server_id'] = server.id

    try:
        res = api_account_login(data=account_data)
    except APIFailure:
        raise SanguoException(errormsg.SERVER_FAULT, 0, 'Login',
                              'APIFailure. api_account_login')

    if res['ret'] != 0:
        raise SanguoException(
            res['ret'], 0, 'Login',
            'login, api_account_login, ret = {0}'.format(res['ret']))

    account_id = res['data']['account_id']
    char_id = res['data']['char_id']
    new_token = res['data']['new_token']

    request._account_id = account_id
    request._server_id = server.id

    login_signal.send(
        sender=None,
        char_id=char_id,
        real_login=True,
    )

    if char_id:
        request._char_id = char_id
    else:
        request._char_id = None

    session = GameSession(request._account_id, request._server_id,
                          request._char_id)

    if char_id:
        Player(char_id).set_login_id(session.login_id)

    request._game_session = session
    session = crypto.encrypt(session_dumps(session))

    response = StartGameResponse()
    response.ret = 0

    response.login.MergeFrom(make_login_response_msg(req.login, new_token))
    response.need_create_new_char = char_id == 0

    sync = SyncResponse()
    sync.ret = 0
    sync.utc_timestamp = arrow.utcnow().timestamp

    return [pack_msg(response, session), pack_msg(sync)]
Example #3
0
    def process_request(self, request):
        try:
            super(UnpackAndVerifyData, self).process_request(request)
        except VersionCheckFailure:
            version_msg = VersionCheckResponse()
            version_msg.ret = 0
            version_msg.version = version.version
            return HttpResponse(pack_msg(version_msg),
                                content_type='text/plain')

        if request.path.startswith('/api/'):
            return

        if not server.active:
            return HttpResponse(status=502)

        char_id = getattr(request, '_char_id', None)
        if char_id:
            if request.path != '/resume/':
                p = Player(char_id)
                login_id = p.get_login_id()
                if not login_id:
                    msg = ReLoginResponse()
                    msg.ret = errormsg.SESSION_EXPIRE
                    data = pack_msg(msg)
                    return HttpResponse(data, content_type='text/plain')

                if login_id != request._game_session.login_id:
                    # NEED RE LOGIN
                    msg = ReLoginResponse()
                    msg.ret = errormsg.LOGIN_RE
                    data = pack_msg(msg)
                    return HttpResponse(data, content_type='text/plain')

                p.refresh()

            ap = ActivePlayers()
            ap.set(request._char_id)
Example #4
0
def create_character(request):
    req = request._proto

    data = {
        'account_id': request._account_id,
        'server_id': server.id,
        'name': req.name
    }

    try:
        res = api_character_create(data)
    except APIFailure:
        raise SanguoException(
            errormsg.SERVER_FAULT,
            0,
            'Character Create',
            'APIFailure, api_character_create'
        )

    if res['ret'] != 0:
        raise SanguoException(
            res['ret'],
            0,
            'Character Create',
            'api_character_create, ret = {0}'.format(res['ret'])
        )

    char_id = res['data']['char_id']

    login_signal.send(
        sender=None,
        char_id=char_id
    )

    request._char_id = char_id

    game_session = request._game_session
    game_session.char_id = char_id

    new_session = crypto.encrypt(session_dumps(game_session))

    Player(char_id).set_login_id(game_session.login_id)

    response = CreateCharacterResponse()
    response.ret = 0
    return pack_msg(response, new_session)
Example #5
0
def resume(request):
    sync = SyncResponse()
    sync.ret = 0
    sync.utc_timestamp = arrow.utcnow().timestamp

    login_signal.send(
        sender=None,
        char_id=request._char_id,
    )

    new_session = GameSession(request._account_id, request._server_id, request._char_id)
    encrypted_session = crypto.encrypt(session_dumps(new_session))

    Player(request._char_id).set_login_id(new_session.login_id)

    response = ResumeResponse()
    response.ret = 0
    return [pack_msg(response, encrypted_session), pack_msg(sync)]
Example #6
0
def create_character(request):
    req = request._proto

    data = {
        'account_id': request._account_id,
        'server_id': server.id,
        'name': req.name
    }

    res = api_character_create(data)
    if res['ret'] != 0:
        raise SanguoException(
            res['ret'], 0, 'Character Create',
            'api_character_create, ret = {0}'.format(res['ret']))

    char_id = res['data']['char_id']
    try:
        char_initialize(request._account_id, server.id, char_id, req.name)
    except Exception as e:
        data = {
            'char_id': char_id,
        }
        api_character_failure(data)
        raise e

    login_signal.send(
        sender=None,
        char_id=char_id,
        real_login=True,
    )

    request._char_id = char_id

    game_session = request._game_session
    game_session.char_id = char_id

    new_session = crypto.encrypt(session_dumps(game_session))

    Player(char_id).set_login_id(game_session.login_id)

    response = CreateCharacterResponse()
    response.ret = 0
    return pack_msg(response, new_session)