def put_into_redis(self, request_json, data, type): if isinstance(data['answer'], int): data['answer'] = 0 if data['answer'] == 1 else 1 data['answer_str'] = '是' if data['answer'] == 0 else '否' data['choices'] = ['是', '否'] data['analytic'] = data['analyse'] else: if data['answer'] == 'A': data['answer'] = 0 if data['answer'] == 'B': data['answer'] = 1 if data['answer'] == 'C': data['answer'] = 2 if data['answer'] == 'D': data['answer'] = 3 data['choices'] = [ 'A:{0}'.format(data['answerA']), 'B:{0}'.format(data['answerB']), 'C:{0}'.format(data['answerC']), 'D:{0}'.format(data['answerD']) ] data['answer_str'] = data['choices'][data['answer']] data_str = str(data) redis.setex(name=self.get_session_name_by_type( request_json['chatbotUserId'], type), time=3600, value=data_str) return data_str
def treat_guess_number(self, number): guess_number_data = redis.get(get_dark_guess_number_session_name(self.chatbot_user_id)) if guess_number_data is None: chatbots.get(self.chatbot_user_id).send_text('你要不先对我说:**游戏:猜数字:开启') return False data = eval(redis.get(get_dark_guess_number_session_name(self.chatbot_user_id)).decode()) if number == '': self.display_guess_number(data) return True current_number = data['current_number'] tried_record = data['tried_record'] if len(number) != self.digit_count or not number.isdigit(): chatbots.get(self.chatbot_user_id).send_text( '咱这个数字答案是{0}位数字,我寻思着你在这输的啥JB玩意儿呢!'.format(self.digit_count)) return True a, b = self.calculate_guess_number(current_number, number) if a == self.digit_count: chatbots.get(self.chatbot_user_id).send_text( '哇你好屌,正确答案就是:「{0}」,秀秀秀!'.format(str(current_number))) user_login.get_luck_point_by_sender_id(50, self.user_id) return False tried_record.append({ "try_number": number, "a": a, "b": b }) data['try_time'] += 1 if data['try_time'] >= self.turn: chatbots.get(self.chatbot_user_id).send_text( '游戏结束,正确答案是:「{0}」,小朋友你不行啊!'.format(str(current_number))) return False redis.setex(name=get_dark_guess_number_session_name(self.chatbot_user_id), time=3600, value=str(data)) self.display_guess_number(data) return True
def start_guess_number(self): if redis.get(get_dark_guess_number_session_name(self.chatbot_user_id)) is None: chatbots.get(self.chatbot_user_id).send_text('正在生成数字......') guess_number_session_data = self.build_guess_number() redis.setex(name=get_dark_guess_number_session_name(self.chatbot_user_id), time=3600, value=str(guess_number_session_data)) self.display_guess_number( data=eval(redis.get(get_dark_guess_number_session_name(self.chatbot_user_id)).decode())) number = '' while self.treat_guess_number(number): number = self.listener.ask(guess_number_operator, '输入任意数字') shut_down_guess_number(self.chatbot_user_id)
def start_dark_maze(self): if redis.get(get_dark_maze_session_name(self.chatbot_user_id)) is None: chatbots.get(self.chatbot_user_id).send_text('正在生成迷宫......') maze_session_data = self.build_maze() chatbots.get(self.chatbot_user_id).send_text('正在生成人物......') redis.setex(name=get_dark_maze_session_name(self.chatbot_user_id), time=3600, value=str(maze_session_data)) self.listener.current_answer = ' ' while self.treat_maze(self.listener.current_answer): self.listener.ask(maze_operator, '输入wsad移动') shut_down_dark_maze(self.chatbot_user_id)
def give_the_lucky_point_to(self, count, sender_id): founded_luck_point = self.get_luck_point_by_sender_id(sender_id) if sender_id.startswith('/dark_buddy#'): redis.setex(name=self.get_temp_user_money_name(sender_id), time=3600, value=founded_luck_point['value'] + count) else: user_status = { 'status_id': 2, 'status_code': 'luck_point', 'user_id': founded_luck_point['user_id'], 'value': founded_luck_point['value'] + count } update_user_status(user_status)
def get_game_session_data(self, request_json): game_session_data = redis.get( self.get_dark_spy_session_name(request_json['chatbotUserId'])) if game_session_data is None: game_session_data = DarkSpyGameSessionData() game_session_data = json.dumps(game_session_data, cls=DarkSpyGameSessionDataEncoder, indent=4) redis.setex(name=self.get_dark_spy_session_name( request_json['chatbotUserId']), time=3600, value=game_session_data) # 参考 DarkSpyGameSessionData.decode(eval(json.dumps(game_session_data, cls=DarkSpyGameSessionDataEncoder, indent=4))) game_session_data = DarkSpyGameSessionData.decode( eval(game_session_data)) return game_session_data
def put_words(self, words): content = "" i = 0 items = words.items() backitems = [[v[1], v[0]] for v in items] backitems.sort(reverse=True) for word in [backitems[i][1] for i in range(0, len(backitems))]: if i >= 20: break i += 1 content += "[{0}](dtmd://dingtalkclient/sendMessage?content={1})/".format( word, quote(word)) redis.setex(name="tianhao:dark_buddy:dark_ji_word_cloud_words", time=3600 * 12, value=content[:-1]) return content
def do_handle(self, request_object, request_json): sender_id = request_json['senderId'] sender_nick = request_json['senderNick'] redis.setex(name=self.get_sign_in_lock_name(sender_id), time=60, value=sender_nick) btns = [] btn = CardItem( title='点击注册', url='http://{2}/dark_buddy/web/bindid/{0}?uuid={1}'.format( sender_id, uuid.uuid1(), public_ip)) btns.append(btn) title = "欢迎注册" text = "# {0}你好, 请点击下方按钮注册。".format(sender_nick) action_card = ActionCard(title=title, text=text, btns=btns, btn_orientation=1) chatbots.get( request_json['chatbotUserId']).send_action_card(action_card)
def put_image_by_uuid(self, image): id = uuid.uuid1() data = self.image_to_base64(image) redis.setex(name=self.get_redis_key(id), time=300, value=data) return id
def put_image(self, image): data = image_factory.image_to_base64(image) redis.setex(name=self.get_redis_key(), time=3600 * 12, value=data) return
def set_listener_session_choices(self, choices): redis.setex(name=self.get_dark_listener_session_name(), time=3600, value=str(choices))
def init_luck_point_4_temp_user(self, count, sender_id): if redis.get(self.get_temp_user_money_name(sender_id)): pass redis.setex(name=self.get_temp_user_money_name(sender_id), time=3600, value=count)