def test_material_requirements(self): wifi = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type'], MOCK_REWARD_WIFI['stats'], 1, MOCK_REWARD_WIFI['rewards']) reward_list = RewardList() reward_list.add_pending_reward(wifi) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS))), 0) wifi2 = Reward('test', MOCK_REWARD_WIFI['type'], MOCK_REWARD_WIFI['stats'], MOCK_REWARD_WIFI['materials'], MOCK_REWARD_WIFI['rewards']) reward_list.add_pending_reward(wifi2) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS))), 1)
def test_sorting_rewards_by_requirement(self): reward1 = Reward('Name1', 'Contract', stats={'Contract': 2}, materials=3, rewards=[]) reward2 = Reward('Name2', 'Contract', stats={'Tech': 3, 'Artifact': 4}, materials=5, rewards=[]) reward3 = Reward('Name3', 'Contract', stats={'Memento': 3, 'Artifact': 4}, materials=0, rewards=[]) reward_list = RewardList() reward_list.add_pending_reward(reward1) reward_list.add_pending_reward(reward2) reward_list.add_pending_reward(reward3) self.assertEqual(reward_list.list_reward_names(), ['Name1', 'Name2', 'Name3']) reward_list.sort_rewards_by_requirements_sum() self.assertEqual(reward_list.list_reward_names(), ['Name2', 'Name3', 'Name1'])
def test_reward_requirement(self): wifi = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type'], MOCK_REWARD_WIFI['stats'], MOCK_REWARD_WIFI['materials'], MOCK_REWARD_WIFI['rewards']) satellite = Reward(MOCK_REWARD_SATELLITE['name'], MOCK_REWARD_SATELLITE['type'], MOCK_REWARD_SATELLITE['stats'], MOCK_REWARD_SATELLITE['materials'], MOCK_REWARD_SATELLITE['rewards']) reward_list = RewardList() reward_list.add_pending_reward(wifi) reward_list.add_pending_reward(satellite) self.assertEqual(len(reward_list.pending_rewards), 2) self.assertEqual(len(reward_list.obtained_rewards), 0) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS))), 1) reward_list.obtain_reward(MOCK_REWARD_WIFI['name']) self.assertEqual(len(reward_list.pending_rewards), 1) self.assertEqual(len(reward_list.obtained_rewards), 1) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS))), 1)
def user_reward(): data = request.get_json() if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) user_id = get_user_id_by_cookie(request) be_reward_user_id = data['be_reward_user_id'] article_id = data['article_id'] reward_money = int(data['reward_money']) if exist_user(user_id) and exist_user(be_reward_user_id) and exist_article( article_id): # 货币的添加减少部分 user = User.query.filter(User.id == user_id).first() # 先判断用户是否够钱支付 if user.vir_money >= reward_money: try: user.vir_money -= reward_money reward_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) article = Article.query.filter( Article.article_id == article_id).first() remark = "给文章(" + str(article.title) + ")打赏" reward_money1 = "-" + str(reward_money) reward = Reward(user_id, be_reward_user_id, reward_money1, reward_time, article_id, remark) db.session.add(reward) db.session.commit() user = User.query.filter(User.id == be_reward_user_id).first() user.vir_money += reward_money remark1 = "你的文章(" + str(article.title) + ")得到奖励" reward1 = Reward(be_reward_user_id, user_id, reward_money, reward_time, article_id, remark1) db.session.add(reward1) db.session.commit() # 添加进记录表(打赏记录) # reward = Reward(user_id, be_reward_user_id, reward_money, reward_time, article_id, remark) # TODO 发通知 return jsonify({ "code": "200", "msg": "打赏成功", "reward_num": get_reward_number(article_id) }) except: db.session.rollback() return jsonify({"code": "203"}) else: return jsonify({"code": "203", "msg": "金币不足,请充值"}) else: return jsonify({"code": "203", "msg": "用户或文章不存在"})
def _handle_message(payload): _pretty_print(payload) # try to extract a message from the payload and check if it was successful message = message_service.extract_message(payload) if not message: return # message should be sent in a public or private channel if not message_service.has_valid_channel_type(message): return # message should contain the reward keyword/emoji if not message_service.contains_keyword(message): return tagged_users = message_service.get_tagged_users(message) for tagged_user in tagged_users: # if the user giving rewards has reached the daily limit, stop giving rewards if not reward_service.can_give_reward(message.user): break # otherwise give the reward and send a notification to the receiver reward = Reward(message.user, tagged_user, datetime.now()) if reward_service.give_reward(reward): message_service.send_reward_notification( message.user, tagged_users[0] )
def test_create_reward_with_empty_fields(self): new_reward = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type']) self.assertEqual(new_reward.reward_type, MOCK_REWARD_WIFI['type']) self.assertEqual(new_reward.name, MOCK_REWARD_WIFI['name']) self.assertEqual(new_reward.stat_requirement.get_value('Contract'), 0) self.assertEqual(new_reward.material_requirement, 0) self.assertEqual(new_reward.reward_requirement, [])
def test_reward_type(self): wifi = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type'], MOCK_REWARD_WIFI['stats'], MOCK_REWARD_WIFI['materials'], MOCK_REWARD_WIFI['rewards']) reward_list = RewardList() reward_list.add_pending_reward(wifi) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS))), 1) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS), team='RD')), 1) self.assertEqual(len(reward_list.possible_rewards(stats=Stats(MAXED_STATS), team='Exploration')), 0)
def test_create_and_fill_reward_list(self): wifi = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type']) reward_list = RewardList() self.assertEqual(len(reward_list.pending_rewards), 0) self.assertEqual(len(reward_list.obtained_rewards), 0) reward_list.add_pending_reward(wifi) self.assertEqual(len(reward_list.pending_rewards), 1) self.assertEqual(len(reward_list.obtained_rewards), 0) # getting reward self.assertEqual(reward_list.get_pending_reward(MOCK_REWARD_WIFI['name']).name, MOCK_REWARD_WIFI['name'])
def test_obtain_reward(self): wifi = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type']) reward_list = RewardList() reward_list.add_pending_reward(wifi) self.assertEqual(len(reward_list.pending_rewards), 1) self.assertEqual(len(reward_list.obtained_rewards), 0) self.assertEqual(reward_list.is_obtained(MOCK_REWARD_WIFI['name']), False) reward_list.obtain_reward(MOCK_REWARD_WIFI['name']) self.assertEqual(len(reward_list.pending_rewards), 0) self.assertEqual(len(reward_list.obtained_rewards), 1) self.assertEqual(reward_list.is_obtained(MOCK_REWARD_WIFI['name']), True)
def test_reward_name_listing(self): wifi = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type'], MOCK_REWARD_WIFI['stats'], 1, MOCK_REWARD_WIFI['rewards']) reward_list = RewardList() self.assertEqual(reward_list.list_reward_names(), []) reward_list.add_pending_reward(wifi) self.assertEqual(reward_list.list_reward_names(), [MOCK_REWARD_WIFI['name']]) self.assertEqual(reward_list.list_obtained_reward_names(), []) self.assertEqual(reward_list.list_pending_reward_names(), [MOCK_REWARD_WIFI['name']]) reward_list.obtain_reward(MOCK_REWARD_WIFI['name']) self.assertEqual(reward_list.list_obtained_reward_names(), [MOCK_REWARD_WIFI['name']]) self.assertEqual(reward_list.list_pending_reward_names(), []) self.assertEqual(reward_list.list_reward_names(), [MOCK_REWARD_WIFI['name']])
def before_request(): user_id = get_user_id_by_cookie(request) if user_id is not None: user = User.query.filter(User.id == user_id).first() # 如果今天还是首次登录,就加金币,改状态 if user.first_login == 1: user.vir_money += 5 user.first_login = 0 # 添加记录 reward_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 如果打赏用户是0,article_id 也是0, 说明是系统打赏的 reward = Reward(user_id, user_id, 5, reward_time, 0, "系统打赏") db.session.add(reward) db.session.commit()
def token(account): redis = get_redis_cli() uid = uuid.uuid4() result = User.query.filter_by(work_id=account).first() if result.first_login == 1: first_login = 1 # 先增加虚拟货币数,然后记录下来 result.vir_money += 5 # 添加记录 reward_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 如果打赏用户是0,article_id 也是0, 说明是系统打赏的 reward = Reward(result.id, result.id, 5, reward_time, 0, "系统打赏") db.session.add(reward) else: first_login = 0 result.first_login = 0 db.session.commit() list = { "id": result.id, "nick_name": result.nick_name, "work_id": result.work_id, "tel": result.tel, "gender": result.gender, "avatar": result.avatar, "mail": result.mail, "vir_money": result.vir_money, "follow": result.follow, "followers": result.followers, "profile": result.profile, "reward_addr": result.reward_addr, "effect": result.effect, "status": result.status } # key为 uid, value为 str(list), 有效期604800s redis.set("user:"******"user:"******"avatar": result.avatar, "effect": result.effect, "first_login": first_login })) response.set_cookie("token", str(uid)) return response
def load_reward(reward_name, reward_dict): reward = reward_dict.copy() reward_type = reward['type'] reward.setdefault('materials', 0) reward.setdefault('stats', empty_stats) reward.setdefault('rewards', []) reward.setdefault('obtainable', True) reward.setdefault('essential', False) stats = reward['stats'] materials = reward['materials'] rewards = reward['rewards'] obtainable = reward['obtainable'] essential = reward['essential'] new_reward = Reward(reward_name, reward_type, stats, materials, rewards, obtainable, essential) return new_reward
def test_create_reward(self): new_reward = Reward(MOCK_REWARD_WIFI['name'], MOCK_REWARD_WIFI['type'], MOCK_REWARD_WIFI['stats'], MOCK_REWARD_WIFI['materials'], MOCK_REWARD_WIFI['rewards']) self.assertEqual(new_reward.reward_type, MOCK_REWARD_WIFI['type']) self.assertEqual(new_reward.name, MOCK_REWARD_WIFI['name']) self.assertEqual(new_reward.stat_requirement.get_value('Contract'), MOCK_REWARD_WIFI['stats']['Contract'])
def test_reward_total_requirements(self): reward = Reward('myName', 'Contract', stats={'Tech': 3, 'Artifact': 4}, materials=5, rewards=[]) self.assertEqual(reward.stat_requirement.sum_stat(), 3+4) self.assertEqual(reward.total_requirements(), 3+4+5)