Exemplo n.º 1
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)]
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)]
Exemplo n.º 5
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)
Exemplo n.º 6
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
    )

    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)]