Example #1
0
def handle_all_messages(m):
    if (m.mid == _MID.RECV_CLIENT_ATTEMPT_REGISTER):
        result = db.add_user_account(m.params[0], m.params[1]);
        msg.send(m.sock, m.client_obj, msg.build(_MID.SEND_CLIENT_ATTEMPT_REGISTER_RESULT, result));

    elif (m.mid == _MID.RECV_CLIENT_ATTEMPT_LOGIN):
        result, unique_id = db.find_user_account(m.params[0], m.params[1]);
        if (result == LoginResult.SUCCESS):
            acc_details = AccDetails();
            acc_details.parent_client = m.client_obj;
            acc_details.unique_id = unique_id;
            m.client_obj.acc_details = acc_details;
            update_acc_details(m.client_obj, unique_id);

            t = time.time();
            db.set_attrib(unique_id, "last_login_time", t);
            acc_details.last_login_time = t;

            result, total = db.get_attrib(unique_id, "total_login_time");
            if (result == GeneralResult.SUCCESS):
                acc_details.total_login_time = total;

        msg.send(m.sock, m.client_obj, msg.build(_MID.SEND_CLIENT_ATTEMPT_LOGIN_RESULT, result));

    elif (m.mid == _MID.RECV_REQUEST_FOR_CLIENT_ACCOUNT_DETAILS):
        acc_details = m.client_obj.acc_details;
        result = GeneralResult.ERROR;
        username = "";
        gold = -1;
        if (acc_details and acc_details.parent_client == m.client_obj):
            result = GeneralResult.SUCCESS;
            username = acc_details.username;
            gold = acc_details.gold;

        msg.send(m.sock, m.client_obj,
                 msg.build(_MID.SEND_CLIENT_ACCOUNT_DETAILS, result, username, gold));

    elif (m.mid == _MID.RECV_REQUEST_TO_BUY_BOOSTER_PACK):
        acc_details = m.client_obj.acc_details;
        result = GeneralResult.UNKNOWN_ERROR;
        gold = -1;
        if (m.client_obj.acc_details != None):
            cost = 0;
            if (m.params[0] == 0):
                cost = 450;
            elif (m.params[0] == 1):
                cost = 800;
            elif (m.params[0] == 2):
                cost = 2000;
            if (cost != 0):
                gold = acc_details.gold - cost;
                if (gold >= 0):
                    result = db.set_acc_gold(acc_details.unique_id, gold);
                    if (result == GeneralResult.SUCCESS):
                        acc_details.gold = gold;
                else:
                    result = GeneralResult.ERROR;

        msg.send(m.sock, m.client_obj,
                 msg.build(_MID.SEND_REQUEST_TO_BUY_BOOSTER_PACK_RESULT, result, gold));
Example #2
0
def got_msg(sock, client_obj, byte_data):
    message = msg.extract_message(sock, client_obj, byte_data);
    if (message != None):
        mid = message.mid;
        params = message.params;
        np = len(params);

        msg.log(message);
        
        callback.process_message(message);

        return;

        if (verify_params(mid, _MID.RECV_CLIENT_REGISTER_USER_PASS, np)):
            print("username: %s, password: %s" % (params[0], params[1]));
            db.add_user_account(params[0], params[1]);

        elif (verify_params(mid, _MID.RELAY_TEST, np)):
            msg.send(sock, client_obj, msg.build(_MID.RELAY_TEST, client_obj.id, client_obj.ip, client_obj.c_tcp_port, client_obj.c_udp_port));

        elif (verify_params(mid, _MID.SEND_CLIENT_ID, np)):
            pass;

        elif (verify_params(mid, _MID.RECV_CLIENT_BINDED_UDP_PORT, np)):
            client_obj.c_udp_port = params[0];

        elif (verify_params(mid, _MID.RECV_UDP_PEER_BIND_PORT_SUCCESS, np)):
            client_obj.joined_game.received_udp_bind_port(client_obj.game_client, params[0], params[1], params[2]);

        elif (verify_params(mid, _MID.RECV_UDP_PEER_BIND_PORT_FAILED, np)):
            client_obj.joined_game.received_udp_bind_port(client_obj.game_client, params[0], params[1], -1);

        elif (verify_params(mid, _MID.RECV_PEER_CONNECT_SUCCESS, np)):
            client_obj.joined_game.received_connect_success(client_obj.game_client, params[0], params[1]);

        elif (verify_params(mid, _MID.BEGIN_RELAY_TEST, np)):
            msg.send(sock, client_obj, msg.build(_MID.RELAY_TEST, client_obj.id, client_obj.ip, client_obj.c_tcp_port, client_obj.c_udp_port));
    else:
        print("received msg (raw: %s, len: %d) has an unknown MID" % (byte_data, byte_data.__len__()));
Example #3
0
def handle_all_messages(m):
    if (m.mid == _MID.RECV_CLIENT_ATTEMPT_REGISTER):
        result = db.add_user_account(m.params[0], m.params[1])
        msg.send(m.sock, m.client_obj,
                 msg.build(_MID.SEND_CLIENT_ATTEMPT_REGISTER_RESULT, result))

    elif (m.mid == _MID.RECV_CLIENT_ATTEMPT_LOGIN):
        result, unique_id = db.find_user_account(m.params[0], m.params[1])
        if (result == LoginResult.SUCCESS):
            acc_details = AccDetails()
            acc_details.parent_client = m.client_obj
            acc_details.unique_id = unique_id
            m.client_obj.acc_details = acc_details
            update_acc_details(m.client_obj, unique_id)

            t = time.time()
            db.set_attrib(unique_id, "last_login_time", t)
            acc_details.last_login_time = t

            result, total = db.get_attrib(unique_id, "total_login_time")
            if (result == GeneralResult.SUCCESS):
                acc_details.total_login_time = total

        msg.send(m.sock, m.client_obj,
                 msg.build(_MID.SEND_CLIENT_ATTEMPT_LOGIN_RESULT, result))

    elif (m.mid == _MID.RECV_REQUEST_FOR_CLIENT_ACCOUNT_DETAILS):
        acc_details = m.client_obj.acc_details
        result = GeneralResult.ERROR
        username = ""
        gold = -1
        if (acc_details and acc_details.parent_client == m.client_obj):
            result = GeneralResult.SUCCESS
            username = acc_details.username
            gold = acc_details.gold

        msg.send(
            m.sock, m.client_obj,
            msg.build(_MID.SEND_CLIENT_ACCOUNT_DETAILS, result, username,
                      gold))

    elif (m.mid == _MID.RECV_REQUEST_TO_BUY_BOOSTER_PACK):
        acc_details = m.client_obj.acc_details
        result = GeneralResult.UNKNOWN_ERROR
        gold = -1
        if (m.client_obj.acc_details != None):
            cost = 0
            if (m.params[0] == 0):
                cost = 450
            elif (m.params[0] == 1):
                cost = 800
            elif (m.params[0] == 2):
                cost = 2000
            if (cost != 0):
                gold = acc_details.gold - cost
                if (gold >= 0):
                    result = db.set_acc_gold(acc_details.unique_id, gold)
                    if (result == GeneralResult.SUCCESS):
                        acc_details.gold = gold
                else:
                    result = GeneralResult.ERROR

        msg.send(
            m.sock, m.client_obj,
            msg.build(_MID.SEND_REQUEST_TO_BUY_BOOSTER_PACK_RESULT, result,
                      gold))