Ejemplo n.º 1
0
def create_room(client_id, room_id=None, insecure=False):
    lobby_route = router.get_lobby_route(insecure=insecure).get('server')

    resp = {}
    with client.connect_to_ws_addr(client_id, lobby_route) as ws_client:
        resp = ws_client.send_msg_with_expect_msgs(
            {
                'cmd': 'session',
                'op': 'open'
            }, [{
                'cmd': 'session',
                'op': 'opened'
            }, {
                'cmd': 'statistic',
                'appStats': matcher.MATCH_ANY
            }])

        resp = ws_client.send_msg_with_expect_msg({
            'cmd': 'conv',
            'op': 'start',
            'cid': room_id
        })
    return {
        'addr': resp.get('addr'),
        'error': resp.get('error'),
        'lobby_addr': lobby_route
    }
Ejemplo n.º 2
0
def fetch_rooms_in_lobby(client_id, **kwargs):
    lobby_route = router.get_lobby_route().get('server')

    resp = {}
    with client.connect_to_ws_addr(client_id, lobby_route) as ws_client:
        resp = ws_client.send_msg_with_expect_msgs(
            {
                'cmd': 'session',
                'op': 'open'
            }, [{
                'cmd': 'session',
                'op': 'opened'
            }, {
                'cmd': 'statistic',
                'appStats': matcher.MATCH_ANY
            }])

        resp = ws_client.send_msg_with_expect_msgs(
            {
                'cmd': 'lobby',
                'op': 'add'
            }, [{
                'cmd': 'lobby',
                'op': 'added'
            }, {
                'cmd': 'lobby',
                'op': 'room-list',
                'list': matcher.MATCH_ANY
            }])

    return resp
Ejemplo n.º 3
0
def join_room(client_id,
              room_id,
              conv_resp=matcher.MATCH_ANY,
              rejoin=False,
              random_join=False,
              expect_attr={},
              insecure=False):
    lobby_route = router.get_lobby_route(insecure=insecure).get('server')

    resp = {}
    with client.connect_to_ws_addr(client_id, lobby_route) as ws_client:
        resp = ws_client.send_msg_with_expect_msgs(
            {
                'cmd': 'session',
                'op': 'open'
            }, [{
                'cmd': 'session',
                'op': 'opened'
            }, {
                'cmd': 'statistic',
                'appStats': matcher.MATCH_ANY
            }])

        if random_join:
            resp = ws_client.send_msg_with_expect_msg(
                {
                    'cmd': 'conv',
                    'op': 'add-random',
                    'rejoin': rejoin,
                    'expectAttr': expect_attr
                },
                resp=conv_resp)
        else:
            resp = ws_client.send_msg_with_expect_msg(
                {
                    'cmd': 'conv',
                    'op': 'add',
                    'cid': room_id,
                    'rejoin': rejoin,
                    'randomJoin': random_join,
                    'expectAttr': expect_attr
                },
                resp=conv_resp)

    return {
        'addr': resp.get('addr'),
        'error': {
            'reasonCode': resp.get('reasonCode'),
            'detail': resp.get('detail')
        }
    }
Ejemplo n.º 4
0
def connect_lobby(client_id, insecure=False):
    lobby_route = router.get_lobby_route(insecure=insecure).get('server')

    ws_client = client.connect_to_ws_addr(client_id, lobby_route)
    ws_client.send_msg_with_expect_msgs({
        'cmd': 'session',
        'op': 'open'
    }, [{
        'cmd': 'session',
        'op': 'opened'
    }, {
        'cmd': 'statistic',
        'appStats': matcher.MATCH_ANY
    }])

    return ws_client
Ejemplo n.º 5
0
def match_room(client_id,
               room_id,
               piggyback_id,
               conv_resp=matcher.MATCH_ANY,
               expect_attr={},
               insecure=False):
    lobby_route = router.get_lobby_route(insecure=insecure).get('server')

    resp = {}
    with client.connect_to_ws_addr(client_id, lobby_route) as ws_client:
        resp = ws_client.send_msg_with_expect_msgs(
            {
                'cmd': 'session',
                'op': 'open'
            }, [{
                'cmd': 'session',
                'op': 'opened'
            }, {
                'cmd': 'statistic',
                'appStats': matcher.MATCH_ANY
            }])

        resp = ws_client.send_msg_with_expect_msg(
            {
                'cmd': 'conv',
                'op': 'match-random',
                'piggybackPeerId': piggyback_id,
                'expectAttr': expect_attr
            },
            resp=conv_resp)

    return {
        'addr': resp.get('addr'),
        'error': {
            'reasonCode': resp.get('reasonCode'),
            'detail': resp.get('detail')
        }
    }
def start_process():
    """Driver function to run this script"""
    parser = argparse.ArgumentParser(
        description=
        "Command line client to comunicate with LeanCloud game service")
    parser.add_argument('--peerid',
                        default='2a',
                        dest="peerid",
                        help="Client peerId, default is \"2a\"")
    parser.add_argument(
        '--env',
        default='q0',
        dest="config_env",
        help="Which env in config.ini to use, default is \"qcloud\"")
    parser.add_argument('--protocol',
                        default='json.1',
                        dest="protocol",
                        help="Serialization protocol, default is \"json.1\"")
    parser.add_argument(
        '--addr',
        default=None,
        dest="server_addr",
        help="Server address connecting to directly, default is \"None\"")
    parser.add_argument('--roomid',
                        default=None,
                        dest="roomid",
                        help="Room id to join or create, default is \"None\"")
    parser.add_argument(
        '--create',
        action="store_true",
        default=False,
        dest="is_create_room",
        help="Create room when joined failed, default is \"False\"")
    parser.add_argument(
        '--insecure',
        action="store_true",
        default=False,
        dest="is_insecure",
        help=
        "Connect to game lobby and game server with insecure websocket, default is \"False\""
    )
    parser.add_argument(
        '--random-join',
        action="store_true",
        default=False,
        dest="is_random_join",
        help="Connect to game lobby and random join room, default is \"False\""
    )

    args = parser.parse_args()

    config.init_config(args.config_env)

    resp = None
    room_id = args.roomid
    server_addr = args.server_addr

    if args.is_create_room:
        resp = lobby_shortcuts.create_room(args.peerid,
                                           room_id=room_id,
                                           insecure=args.is_insecure)
    else:
        if room_id is not None:
            resp = lobby_shortcuts.join_room(args.peerid,
                                             room_id,
                                             insecure=args.is_insecure)
            if resp.get('error') and resp.get('error').get('reasonCode'):
                resp = None
        elif args.is_random_join:
            resp = lobby_shortcuts.join_room(args.peerid,
                                             room_id,
                                             insecure=args.is_insecure,
                                             random_join=True)
            if resp.get('error') and resp.get('error').get('reasonCode'):
                resp = None

        if server_addr is None and resp is None:
            server_addr = router.get_lobby_route(
                insecure=args.is_insecure).get('server')

    if server_addr is None:
        server_addr = resp.get('addr')

    print(colorama.Fore.YELLOW + "Connecting to %s" % server_addr)

    clt = client.client_builder() \
        .with_addr(server_addr) \
        .with_appid(config.APP_ID) \
        .with_protocol(args.protocol) \
        .with_peerid(args.peerid) \
        .build()
    clt.connect()

    while True:
        try:
            raw_str = input()
            if len(raw_str) != 0:
                cmd_msg_args = input_parser.parse_input_cmd_args(raw_str)
                clt.send_msg(cmd_msg_args)
            else:
                print(raw_str)
        except KeyboardInterrupt:
            break
        except Exception:
            print(colorama.Fore.RED +
                  "Got exception: %s" % traceback.print_exc())

    clt.close()
    client.close_all_opened_clients()
    print(colorama.Fore.GREEN + "Client closed")