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));
def handle_client_leave(client_obj): acc_details = client_obj.acc_details; if (acc_details != None): t = time.time(); if (db.set_attrib(acc_details.unique_id, "last_logout_time", t) == GeneralResult.SUCCESS): acc_details.last_logout_time = t; result, total = db.get_attrib(acc_details.unique_id, "total_login_time"); if (result == GeneralResult.SUCCESS): total += t - acc_details.last_login_time; db.set_attrib(acc_details.unique_id, "total_login_time", total); acc_details.total_login_time = total;
def handle_client_leave(client_obj): acc_details = client_obj.acc_details if (acc_details != None): t = time.time() if (db.set_attrib(acc_details.unique_id, "last_logout_time", t) == GeneralResult.SUCCESS): acc_details.last_logout_time = t result, total = db.get_attrib(acc_details.unique_id, "total_login_time") if (result == GeneralResult.SUCCESS): total += t - acc_details.last_login_time db.set_attrib(acc_details.unique_id, "total_login_time", total) acc_details.total_login_time = total
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))