Beispiel #1
0
    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
Beispiel #2
0
 def leave_channel(self, msg):
     """
     离开临时会话.(一方退出即终结临时会话)
     :param msg:
     :return:
     """
     debug("ChatSrv leave_channel", msg)
     user_id = msg.getParamInt('userId')
     target_user_id = msg.getParamInt('targetUserId')
     channel_key = _get_channel_key(user_id, target_user_id)
     if channel_key in self.channels:
         del self.channels[channel_key]
         debug("ChatSrv clean channel", channel_key)
         is_friend = _check_friend(user_id, target_user_id)
         if not is_friend:
             now = current_milli_time()
             msg_id = get_msg_id(user_id, target_user_id, now)
             content = ftstr.dumps({"code": "chat", "status": "leave_channel"})
             system_message = ChatMessage(user_id, target_user_id, 7, now, content)
             self.service.save_record(user_id, target_user_id, msg_id, system_message)
             # 回应对方离线
             self.write(target_user_id, user_id, msg_id, system_message)
             # TODO 清理临时聊天消息
         ftcore.runOnceDelay(0.1, self.__do_leave_game_message,user_id, target_user_id)
     if channel_key in self.tmp_channels:
         del self.tmp_channels[channel_key]
         debug("ChatSrv clean temp channel", channel_key)
Beispiel #3
0
 def sns_system_message(self, user_id, target_user_id, code):
     """
     收到添加好友邀请/添加好友邀请被通过.
     :param user_id:
     :param target_user_id:
     :param code:
     :return:
     """
     debug("ChatSrv sns_system_message", user_id, target_user_id, code)
     # resp = MsgPack()
     # resp.setCmd('chat')
     #1 xxx,请求添加你为好友(不存储/打招呼的语言-以上是打招呼的内容)
     #2 你的添加好友请求已被xxx通过(生成一条你们刚刚成为好友/你已经添加xxx,现在可以开始聊天了。)
     #3 对方已离开聊天,添加好友继续聊天
     #4 xxx(你),撤回了一条消息
     #5 你的消息不能够送达(被删除、屏蔽)
     #6 游戏结果
     now = current_milli_time()
     code_status = ["invite", "accept"][code-1]
     content = ftstr.dumps({"code": "sns", "status": code_status})
     system_message = ChatMessage(user_id, target_user_id, 7, now, content)
     msg_id = get_msg_id(user_id,target_user_id,now)
     # 申请不计入聊天消息
     if code == 1:
         self.service.save_record(user_id, target_user_id, msg_id, system_message)
     # 各回应一条
     self.write(user_id, target_user_id, msg_id, system_message)
     self.write(target_user_id, user_id, msg_id, system_message)
     return 1
Beispiel #4
0
 def sync_server_time(self, msg):
     """
     同步某些指定messageID的消息.
     """
     debug("ChatListSrv : sync_server_time : msg = ", msg)
     user_id = msg.getParamInt('userId')
     resp = MsgPack()
     resp.setCmd('chat')
     resp.setResult('action', 'sync_server_time')
     #TODO 切换到redis时间
     now = current_milli_time()
     resp.setResult('serverTime', now)
     resp.setResult('status', "ok")
     tyrpcconn.sendToUser(user_id, resp)
Beispiel #5
0
 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)
Beispiel #6
0
 def __do_leave_game_message(self, user_id, target_user_id):
     """
     清理多余的游戏邀请.
     """
     now = current_milli_time()
     before = now - LIFE_CYCLE*60*1000
     message_ids = self.service.do_leave_chat_record(user_id,target_user_id,before)
     resp = MsgPack()
     resp.setCmd('chat')
     resp.setResult('action', 'leave_game_message')
     resp.setResult('uid1', user_id)
     resp.setResult('uid2', target_user_id)
     resp.setResult('messageIds', message_ids)
     debug("ChatSrv __do_leave_game_message", user_id, target_user_id,resp)
     tyrpcconn.sendToUser(user_id, resp)
     tyrpcconn.sendToUser(target_user_id, resp)
Beispiel #7
0
 def check_channel(self, user_id, target_user_id):
     """
     创建临时会话.(双方各发一条,创建临时会话,防止骚扰)
     :return:
     """
     channel_key = _get_channel_key(user_id,target_user_id)
     debug("ChatGameSrv check_channel", channel_key, self.tmp_channels)
     if channel_key in self.tmp_channels:
         _ = self.tmp_channels[channel_key][0]
         if _ == user_id:
             now = self.tmp_channels[channel_key][1]
             msg_id = self.tmp_channels[channel_key][2]
             del self.tmp_channels[channel_key]
             debug("ChatGameSrv end_channel", user_id, target_user_id,now,msg_id)
             return True,now,msg_id
     now = current_milli_time()
     msg_id = get_msg_id(user_id, target_user_id, now)
     self.tmp_channels[channel_key] = [target_user_id,now,msg_id]
     debug("ChatGameSrv create_channel", user_id, target_user_id, now, msg_id)
     return False,now,msg_id
Beispiel #8
0
 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
Beispiel #9
0
 def verify_4(self, msg):
     content = msg.getParamStr("content")
     time = current_milli_time()
     msg_id = self.get_message_id(msg,time)
     return msg_id, content, time