def room_voice_message(dynamic_id, voice_url): account_id = PlayerManager().query_account_id(dynamic_id) if not account_id: send.system_notice(dynamic_id, content.ENTER_DYNAMIC_LOGIN_EXPIRE) return room_manager = RoomManager() room_id = room_manager.query_player_room_id(account_id) if not room_id: send.system_notice(dynamic_id, content.ROOM_UN_ENTER) return room = room_manager.get_room(room_id) if not room: send.system_notice(dynamic_id, content.ROOM_UN_FIND) return player = room.get_player(account_id) if not player: func.log_error('[game] room_voice_message player is unvalid, account_id: {}, room_id: {}'.format( account_id, room_id)) send.system_notice(dynamic_id, content.ROOM_UN_ENTER) return t = func.time_get() if t - player.voice_message_t < 4: send.system_notice(dynamic_id, content.ROOM_CHAT_TO_FREQUENT) return player.voice_message_t = t send.voice_message_to_self(dynamic_id, voice_url) dynamic_id_list = room.get_room_dynamic_id_list() send.voice_message_to_all(dynamic_id_list, account_id, voice_url)
def clear_unvalid_room(self): t = func.time_get() expire_t = t - 2 * 24 * 3600 # 玩家正常创建的房间保存2天 online_expire_t = t - 2 * 24 * 3600 # 在线匹配房间保存2天 delete_id_list = [] for room_id, room in self._rooms.iteritems(): if not room: delete_id_list.append(room_id) continue if room.is_online_match() and room.create_time <= online_expire_t: delete_id_list.append(room_id) elif room.create_time <= expire_t: delete_id_list.append(room_id) # 通知game节点删除 request_all_game_node('remove_unvalid_room', delete_id_list) # 本节点删除, 数据库删除 for room_id in delete_id_list: try: sql = 'delete from {} where room_id={}'.format(dbname.DB_ROOM, room_id) dbexecute.execute(sql) except Exception as e: func.log_error('[gate] RoomProxyManager clear_unvalid_room db room_id: {}, failed: {}'.format( room_id, e.message )) try: if room_id in self._rooms: del self._rooms[room_id] except Exception as e1: func.log_error('[gate] RoomProxyManager clear_unvalid_room cache room_id: {}, failed: {}'.format( room_id, e1.message ))
def check_heart_tick_time_out(): t = func.time_get() user_manager = UserManager() all_heart_ticks = user_manager.all_heart_tick time_out_list = [] for account_id, pre_t in all_heart_ticks.items(): if t - pre_t >= 75: # 客户端30秒上传1次,75秒没有检测到,则判断为离线 time_out_list.append(account_id) if time_out_list: func.log_warn('[gate] check_heart_tick_time_out {} user time out, {}'.format( len(time_out_list), time_out_list )) user_manager.remove_heart_tick(time_out_list) request_all_game_node('heart_tick_time_out', time_out_list)
def log_gold(account_id, count, remain, origin_id): t = func.time_get() insert_data = { 'account_id': account_id, 'count': count, 'remain': remain, 'origin_id': origin_id, 'time': t } result = dbexecute.insert_record(**{'table': dbname.DB_LOG_GOLD, 'data': insert_data}) if not result: func.log_error('[gate] log_gold account_id: {}, count: {}, remain: {}, origin_id: {}, time: {}'.format( account_id, count, remain, origin_id, t ))
def calc_prepay_id(self): xml = self.get_req_xml() headers = {'Content-Type': 'application/xml'} r = requests.post(self._url, data=xml, headers=headers) re_xml = ElementTree.fromstring(r.text.encode('utf8')) xml_status = re_xml.getiterator('result_code')[0].text func.log_info('[game] WechatPay query_wechat_prepay_id xml_status: {}'.format(xml_status)) if xml_status != 'SUCCESS': self._error = content.RECHARGE_WECHAT_CONNECT_FAILED return prepay_id = re_xml.getiterator('prepay_id')[0].text func.log_info('[game] WechatPay prepay_id: {}'.format(prepay_id)) self._params['prepay_id'] = prepay_id self._params['package'] = 'Sign=WXPay' self._params['timestamp'] = str(func.time_get())
def send_poker_room_full(dynamic_id_list, statistic_list, give_up=False): response = game_poker_pb2.m_5105_toc() response.server_t = func.time_get() response.give_up = 1 if give_up else 0 for info in statistic_list: room_fulls = response.room_fulls.add() room_fulls.account_id = info['account_id'] room_fulls.rank = info['rank'] room_fulls.point_change = info['point_change'] room_fulls.win_count = info['win_count'] room_fulls.lose_count = info['lose_count'] room_fulls.bomb_count = info['bomb_count'] room_fulls.max_point = info['max_point'] func.log_info('[game] 5105 send_poker_room_full dynamic_id_list: {}, response: {}'.format( dynamic_id_list, response )) forward.push_object_game(5105, response.SerializeToString(), dynamic_id_list)
def save_order_to_db(pay, ingot, recharge_origin): proxy_id, account_id = pay.attach insert_data = { 'account_id': account_id, 'proxy_id': proxy_id, 'op_id': pay.order_id, 'money': pay.money, 'ingot': ingot, 'origin': recharge_origin, 'time': func.time_get() } _id = dbexecute.insert_auto_increment_record(**{ 'table': dbname.DB_RECHARGE, 'data': insert_data }) func.log_info('[gate] save_order_to_db _id: {}, account_id: {}, proxy_id: {}, money: {}, op_id: {}'.format( _id, account_id, proxy_id, pay.money, pay.order_id ))
def send_mahjong_room_full(dynamic_id_list, max_rounds, statistic_list, give_up=False): response = game_mahjong_pb2.m_5207_toc() response.server_t = func.time_get() response.max_rounds = max_rounds response.give_up = 1 if give_up else 0 for info in statistic_list: room_fulls = response.room_fulls.add() room_fulls.account_id = info['account_id'] room_fulls.rank = info['rank'] room_fulls.point_change = info['point_change'] room_fulls.max_point = info['max_point'] room_fulls.drawn_count = info['drawn_count'] room_fulls.win_count = info['win_count'] room_fulls.lose_count = info['lose_count'] room_fulls.help_count = info['help_count'] func.log_info('[game] 5207 send_mahjong_room_full dynamic_id_list: {}, response: {}'.format( dynamic_id_list, response )) forward.push_object_game(5207, response.SerializeToString(), dynamic_id_list)
def calc_prepay_id(self): xml = self.get_req_xml() headers = {'Content-Type': 'application/xml'} r = requests.post(self._url, data=xml, headers=headers) func.log_info('[game] wechatpay ret: {} url: {} xml: {}'.format( r, self._url, xml)) re_xml = ElementTree.fromstring(r.text.encode('utf8')) xml_status = re_xml.getiterator('result_code')[0].text func.log_info( '[game] WechatPay query_wechat_prepay_id xml_status: {}'.format( xml_status)) if xml_status != 'SUCCESS': self._error = content.RECHARGE_WECHAT_CONNECT_FAILED return prepay_id = re_xml.getiterator('prepay_id')[0].text func.log_info('[game] WechatPay prepay_id: {}'.format(prepay_id)) self._params['prepay_id'] = prepay_id self._params['package'] = 'Sign=WXPay' self._params['timestamp'] = str(func.time_get())
def _create_room(account_id, room_type, room_help, rounds): room_id = RoomProxyManager().generator_room_id() if room_id <= 0: return None room = RoomProxy() t = func.time_get() insert_data = { 'room_id': room_id, 'room_type': room_type, 'room_help': room_help, 'rounds': rounds, 'create_time': t, 'account_id': account_id, 'data': func.transform_object_to_pickle(None) } result = dbexecute.insert_record(**{'table': dbname.DB_ROOM, 'data': insert_data}) if result > 0: room.create(room_id, room_type, rounds, account_id, t) return room return None
def account_verify_channel(dynamic_id, address, user_name, channel_id, uuid, token, name, head_frame, head_icon, sex): func.log_info( '[auth] account_verify_channel dynamic_id: {}, user_name: {}, channel_id: {}, uuid: {}, head_frame: {}, head_icon: {}, sex: {}' .format(dynamic_id, user_name, channel_id, uuid, head_frame, head_icon, sex)) if not user_name or not uuid or channel_id < 0: send.system_notice(dynamic_id, content.ACCOUNT_LOGIN_ARGUMENT) return password = uuid sql = 'select * from {} where user_name="{}"'.format( dbname.DB_ACCOUNT, user_name) result = dbexecute.query_one(sql) if not result: account_id = _register_process(user_name, password, name, uuid, token, channel_id, sex, head_frame, head_icon) if not account_id: send.system_notice(dynamic_id, content.LOGIN_USER_CREATE_FAILED_51) return result = dbexecute.query_one(sql) if not result: send.system_notice(dynamic_id, content.LOGIN_USER_CREATE_FAILED_52) return #encrypt_password = func.encrypt_password(user_name, password, result['token_key']) #if encrypt_password != result['password']: # send.system_notice(dynamic_id, content.LOGIN_USER_CREATE_FAILED_53) # return #if not _check_weixin_token(uuid, token): # send.system_notice(dynamic_id, content.LOGIN_USER_CREATE_FAILED_54) # return account_id = result['account_id'] verify_key = _create_verify_key(account_id, result['token_key']) notice_gate_user_channel_login(account_id, verify_key, address, name=name, sex=sex, head_frame=head_frame, head_icon=head_icon) t = func.time_get() send.account_verify_channel(dynamic_id, t, account_id, verify_key)
def account_verify_official(dynamic_id, address, user_name, password): if not user_name or not password: send.system_notice(dynamic_id, content.ACCOUNT_NULL) return if not func.check_english(user_name): send.system_notice(dynamic_id, content.ACCOUNT_ENGLISH) return sql = 'select * from {} where user_name="{}"'.format( dbname.DB_ACCOUNT, user_name) result = dbexecute.query_one(sql) if not result: send.system_notice(dynamic_id, content.ACCOUNT_DO_NOT_EXIST) return encrypt_password = func.encrypt_password(user_name, password, result['token_key']) if encrypt_password != result['password']: send.system_notice(dynamic_id, content.ACCOUNT_PASSWORD_ERROR) return account_id = result['account_id'] verify_key = _create_verify_key(account_id, result['token_key']) notice_gate_user_login(account_id, verify_key, address) t = func.time_get() send.account_verify_official(dynamic_id, t, account_id, verify_key)
def _register_process(user_name, password, name, uuid, token_key, channel_id, sex, head_frame, head_icon): if token_key == '': token_key = _create_token_key(user_name, password) encrypt_password = func.encrypt_password(user_name, password, token_key) t = func.time_get() # 实时搜索最大的account_id sql = 'select `account_id` from {} order by `account_id` desc'.format( dbname.DB_ACCOUNT) result = dbexecute.query_one(sql) if not result: account_id = 380001 else: account_id = calc_account_id(result['account_id']) account_data = { 'account_id': account_id, 'uuid': uuid, 'cid': channel_id, 'user_name': user_name, 'password': encrypt_password, 'token_key': token_key, 'create_time': t, 'last_login': t, 'last_logout': t, 'name': name, 'sex': sex, 'head_frame': head_frame, 'head_icon': head_icon, 'gold': constant.GOLD_ORIGIN } if dbexecute.insert_record(**{ 'table': dbname.DB_ACCOUNT, 'data': account_data }) > 0: return account_id else: return 0
def generator_unique_order_id(money, account_id, dynamic_id): t = func.time_get() return '%s%d%d' % (str(money * 3 + func.random_get(30000, t)), account_id, dynamic_id)
def _create_token_key(user_name, password): m = hashlib.md5() m.update( str(user_name) + str(func.time_get()) + str(password) + str(func.random_get(100, 500000))) return m.hexdigest()
def is_expire(self): return func.time_get() - self._create_time >= rule.ROOM_EXPIRE
def generator_unique_order_id(money): t = func.time_get() return str(money * 3 + func.random_get(30000, t))
# coding:utf8 from twisted.internet import defer from firefly.server.globalobject import GlobalObject from firefly.utils.services import CommandService from app.util.common import func class GameCommandService(CommandService): def is_target_local(self, target_key): return target_key in self._targets game_service = GameCommandService(str(func.random_get(1, func.time_get()))) def game_service_handle(target): game_service.mapTarget(target) def push_object(target_key, msg, send_list): func.log_info('[push_object] target_key: {}, send_list: {}'.format(target_key, send_list)) def push_all_game(target_key, msg): pass # TODO: gameservice: push_all_game def request_gate_node(target_key, *args, **kwargs): return GlobalObject().remote['gate'].callRemote(target_key, *args, **kwargs)
# coding:utf8 from twisted.internet import defer from firefly.server.globalobject import GlobalObject from firefly.utils.services import CommandService from app.util.common import func class GameCommandService(CommandService): def is_target_local(self, target_key): return target_key in self._targets game_service = GameCommandService(str(func.random_get(1, func.time_get()))) def game_service_handle(target): game_service.mapTarget(target) def push_object(target_key, msg, send_list): func.log_info('[push_object] target_key: {}, send_list: {}'.format( target_key, send_list)) def push_all_game(target_key, msg): pass # TODO: gameservice: push_all_game def request_gate_node(target_key, *args, **kwargs): return GlobalObject().remote['gate'].callRemote(target_key, *args,
def heart_tick(self, account_id): self._heart_tick[account_id] = func.time_get()
def _create_verify_key(account_id, token_key): m = hashlib.md5() m.update( str(account_id) + str(token_key) + str(func.time_get()) + str(func.random_get(10000, 500000))) return m.hexdigest()