def doReportGameResult(self, userId, mini_game_id, msg): """ 小游戏比赛服上报游戏结果. mo.setResult('winnerId', self.winnerId) mo.setResult('userId_a', self.userId_a) mo.setResult('userId_b', self.userId_b) mo.setResult('table_msgId', self.table_msgId) """ debug("ChatGameSrv : report_game_result : msg = ",userId,mini_game_id, msg, type(msg)) msg = ftstr.loads(msg) user_id = msg.get('userId_a') target_user_id = msg.get('userId_b') win_user_id = msg.get('winnerId') if isinstance(user_id, int) and user_id > 0 and isinstance(target_user_id, int) and target_user_id > 0 and \ isinstance(win_user_id, int): msg_id = msg.get('table_msgId') if not msg_id or msg_id == -1: now = current_milli_time() msg_id = get_msg_id(user_id, target_user_id, now) content = ftstr.dumps({'miniGameId': mini_game_id, 'code': 'result'}) game_message = GameChatMessage(user_id, target_user_id, 2, now, content) self.service.save_record(user_id, target_user_id, msg_id, game_message) debug("ChatGameSrv report_game_result", user_id, target_user_id, msg_id, win_user_id) self.service.save_record_result(user_id, target_user_id, msg_id, win_user_id) record = self.service.get_record(msg_id) if record: # 结果推送给双方 game_message = GameChatMessage.load_info(record) push_game_message(user_id, target_user_id, msg_id, game_message) # 所有的对战消息都在sns中存储为对战记录 _upload_vs_record(user_id, target_user_id, mini_game_id, win_user_id) return 1 return 0
def load(cls, userId, matchId): # jstr = daobase.executeUserCmd(userId, 'hget', cls.buildKey(userId), matchId) jstr = daobase.getMatchPlayerInfo(userId, matchId) if jstr: d = ftstr.loads(jstr) return MatchPlayerInfo(matchId, userId).fromDict(d) return None
def test1(): from chat.entity.models import ChatMessage msg = ChatMessage(10000, 10001, 0, 123, '{"text":"hello"}') print msg print msg.pack() from chat.entity.models import GameChatMessage msg2 = GameChatMessage(10000, 10001, 0, 123, '{"miniGameId":601,"code":"invite"}') print msg2 print msg2.pack() info = { "time": 111, "userId": 10000, "targetUserId": 10001, "msgType": 2, "content": '{"miniGameId":"6033","code":"invite"}' } msg3 = GameChatMessage.load_info(info) print msg3.pack() msg3.code = "accept" print msg3.code print msg3.pack() if isinstance(msg3, dict): print "msg3 is dict" rec = msg3.pack() print rec msg4 = GameChatMessage.load_info(json.loads(rec)) msg4 = GameChatMessage.load_info(ftstr.loads(rec)) print msg4.pack()
def initRobotSetting(): global RESOURCE_ROBOTS if len(RESOURCE_ROBOTS) == 0: res = loadResource('robot_info.json') rinfo = ftstr.loads(res) RESOURCE_ROBOTS = rinfo['robots']
def do_leave_chat_record(self, user_id, target_user_id, score): msg_list = self.record_dao.get_next_record_list( user_id, target_user_id, score) debug("do_leave_chat_record", msg_list) datas = len(msg_list) - 1 i = 0 msg_ids = [] while i < datas: msg_id = msg_list[i] info = self.get_record(msg_list[i]) time = info.get("time") msg_type = info.get("msgType") content = info.get("content") uid = info.get("userId") tid = info.get("targetUserId") content_obj = ftstr.loads(content) mini_game_id = content_obj.get("miniGameId") ex_code = content_obj.get("code") if ex_code == "invite": info = {"miniGameId": mini_game_id, "code": "leave"} content = ftstr.dumps(info) info = { "time": time, "userId": uid, "targetUserId": tid, "msgType": msg_type, "content": content } self.save_record(user_id, target_user_id, msg_id, info) msg_ids.append(msg_id) i += 2 return msg_ids
def verify_2(self, msg): content = msg.getParamStr("content") obj = ftstr.loads(content) message_id = obj.get("msgId","") if message_id: code = obj.get("code", "refuse") info = self.service.get_record(message_id) if info: time = info.get("time") content = info.get("content") mini_game_id = ftstr.loads(content).get("miniGameId") info = {"miniGameId": mini_game_id, "code": code} content = ftstr.dumps(info) else: raise StrangerException() else: # TODO 校验minigameID及code time = current_milli_time() message_id = self.get_message_id(msg,time) return message_id, content, time
def talk_message1(self, msg): """ 聊天消息. :param msg: userId,targetUserId,msgType,content,cid :return: msgId,ok """ debug("ChatSrv talk_message", msg) user_id = msg.getParamInt('userId') target_user_id = msg.getParamInt('targetUserId') msg_type = msg.getParamInt('msgType') content = msg.getParamStr('content') #客户端消息序号 sn = msg.getParamStr('sn') is_friend = _check_friend(user_id,target_user_id) debug("ChatSrv talk_message", user_id, target_user_id, msg_type, content, sn, is_friend) resp = MsgPack() resp.setCmd('chat') resp.setResult('action', 'send_message') try: msg_id, content, now = self.do_verify(msg) channel_key = _get_channel_key(user_id, target_user_id) if not is_friend and channel_key not in self.channels : # 临时会话已关闭,不允许发送消息 resp.setResult("status", "notFriend") else: obj = ftstr.loads(content) game_code = obj.get("code", "") if game_code == "" or game_code == "invite": info = { "time": now, "userId": user_id, "targetUserId": target_user_id, "msgType": msg_type, "content": content } else: # 游戏更新不更改初始来源 info = { "time": now, "userId": target_user_id, "targetUserId": user_id, "msgType": msg_type, "content": content } self.write(user_id,target_user_id,msg_id,info) self.service.save_record(user_id, target_user_id, msg_id, info) resp.setResult('sn', sn) resp.setResult('time', now) resp.setResult('msgId', msg_id) resp.setResult('status', "ok") except TYBizException, e: resp.setResult("status", "error") resp.setError(e.errorCode, e.message)
def save_record_result(self, user_id, target_user_id, message_id,win_user_id): debug("save record: ", user_id, target_user_id,message_id, win_user_id) info = DaoChatRecordHash.HGET(0, message_id) if info: debug("save record: ", user_id, target_user_id, info, type(info)) content = info.get("content") # TODO 应该都是字符串吧 content_obj = ftstr.loads(content) content_obj["code"] = "result" content_obj["winUserId"] = win_user_id info["content"]=ftstr.dumps(content_obj) DaoChatRecordHash.HSET(0, message_id, info)
def querySNS(api, params, sign=False): code = 200 try: if _DEBUG: debug('querySNS->', api, params) code, page = tyadapter.queryAdapter(_ADAPTER_SNS, params, {}, api) if _DEBUG: debug('querySNS->', api, code, page) return ftstr.loads(page) except: ftlog.error() return {'error': code}
def talk_message(self, msg): """ 聊天消息. :param msg: userId,targetUserId,msgType,content,cid :return: msgId,ok """ debug("ChatSrv talk_message", msg) user_id = msg.getParamInt('userId') target_user_id = msg.getParamInt('targetUserId') msg_type = msg.getParamInt('msgType') content = msg.getParamStr('content') # 客户端消息序号 sn = msg.getParamStr('sn') is_friend = _check_friend(user_id, target_user_id) debug("ChatSrv talk_message", user_id, target_user_id, msg_type, content, sn, is_friend) resp = MsgPack() resp.setCmd('chat') resp.setResult('action', 'send_message') try: channel_key = _get_channel_key(user_id, target_user_id) if not is_friend and channel_key not in self.channels: # 临时会话已关闭,不允许发送消息 resp.setResult("status", "notFriend") else: game_obj = ftstr.loads(content) message_id = game_obj.get("msgId", "") if message_id: # 游戏应答 code = game_obj.get("code", "refuse") info = self.service.get_record(message_id) if info: chat_message = GameChatMessage.load_info(info) chat_message.code = code else: raise StrangerException() else: time = current_milli_time() message_id = get_msg_id(user_id,target_user_id,time) chat_message = ChatMessage(user_id,target_user_id,msg_type,time,content) self.write(user_id, target_user_id, message_id, chat_message) self.service.save_record(user_id, target_user_id, message_id, chat_message) resp.setResult('sn', sn) # resp.setResult('time', time) resp.setResult('msgId', message_id) resp.setResult('status', "ok") except TYBizException, e: resp.setResult("status", "error") resp.setError(e.errorCode, e.message)
def doAudit(self, request, result): mo = MsgPack() mi = self.checkExchange.check(request) if mi.error: mo.setError(1, mi.error) else: params = { 'userId': mi.userId, 'result': result, 'realGameId': tyglobal.gameId(), 'exchangeId': mi.exchangeId, 'extOrderId': 'dummy_jd_num_audit', '_test_': 1, } url = tyglobal.httpGame( ) + '/api/hall5/exchange/auditCallback?' + ftstr.toHttpStr(params) _, ret = fthttp.queryHttp('GET', url, None, None, 5) if ret and ret[0] == '{': mo.updateResult(ftstr.loads(ret)) else: mo.setError(1, ret) return mo
def _init_game(self): _obj = ftstr.loads(self._content) self._miniGameId_ = _obj.get("miniGameId") self._code_ = _obj.get("code") self._winUserId_ = _obj.get("winUserId", "-1")