Exemplo n.º 1
0
async def do_diamond_reward(member: Member, source_diamond, diamond=None):
    if not isinstance(member, Member):
        raise ValueError('Parameter member_cid must be a type of Member.')
    if source_diamond not in SOURCE_MEMBER_DIAMOND_LIST:
        raise ValueError('Parameter source_diamond must in %s.' %
                         str(SOURCE_MEMBER_DIAMOND_LIST))
    if diamond and not isinstance(diamond, int):
        raise TypeError('Parameter diamond must be type of int or long.')
    m_diamond = diamond
    if diamond is None:
        diamond_reward = await GameDiamondReward.find_one(
            dict(source=source_diamond))
        if diamond_reward:
            m_diamond = diamond_reward.quantity
    if not m_diamond:
        m_diamond = 0

    member.diamond += m_diamond
    member.updated_dt = datetime.datetime.now()

    diamond_detail = MemberDiamondDetail(member_cid=member.cid,
                                         diamond=m_diamond)
    diamond_detail.member_cid = member.cid
    diamond_detail.diamond = m_diamond
    diamond_detail.source = source_diamond
    diamond_detail.reward_datetime = datetime.datetime.now()
    diamond_detail.content = SOURCE_MEMBER_DIAMOND_DICT.get(source_diamond)

    await diamond_detail.save()
    await member.save()
    return True, m_diamond
Exemplo n.º 2
0
    async def post(self):
        """

        :return:
        {
            "code": [整型]返回码,
            "diamonds_num": [整型]会员总钻石数量
            "award_title": [字符串]奖励名称(旧title)
            "award_diamonds": [整型]本次奖励钻石数
        }
        """
        r_dict = {'code': 0}
        open_id = self.get_i_argument('open_id', None)
        try:
            if not open_id:
                r_dict['code'] = 1001
                return r_dict

            member = await find_member_by_open_id(open_id)
            if not member:
                r_dict['code'] = 1002
                return r_dict

            reward_info = []
            share_times_limit = (await GameDiamondReward.find_one(
                {'source':
                 SOURCE_MEMBER_DIAMOND_DAILY_SHARE_TIMES_LIMIT})).quantity
            value = get_cache_share_times(member.cid)
            if value < share_times_limit:
                end_datetime = datetime.datetime.now().replace(hour=23,
                                                               minute=59,
                                                               second=59,
                                                               microsecond=999)
                current_datetime = datetime.datetime.now()
                timeout = (end_datetime - current_datetime).seconds
                # 分享奖励
                _, m_diamond = await do_diamond_reward(
                    member, SOURCE_MEMBER_DIAMOND_DAILY_SHARE)
                # 设置更新已奖励次数
                set_cache_share_times(member.cid, timeout)
                if member.diamond is None:
                    member.diamond = m_diamond
                else:
                    member.diamond = member.diamond + m_diamond
                await member.save()
                reward_info = [{
                    'title':
                    SOURCE_MEMBER_DIAMOND_DICT.get(
                        SOURCE_MEMBER_DIAMOND_DAILY_SHARE),
                    'diamond_num':
                    m_diamond
                }]
            # 用户的钻石
            r_dict['diamonds_num'] = member.diamond
            r_dict['reward_info'] = reward_info
            r_dict['code'] = 1000
        except Exception:
            logger.error(traceback.format_exc())
        return r_dict
Exemplo n.º 3
0
async def update_diamond_reward(member_cid, source_diamond, diamond=0):
    diamond_detail = MemberDiamondDetail(member_cid=member_cid,
                                         diamond=diamond,
                                         source=source_diamond)
    diamond_detail.reward_datetime = datetime.datetime.now()
    diamond_detail.content = SOURCE_MEMBER_DIAMOND_DICT.get(source_diamond)

    await diamond_detail.save()
def export_member_game_history(cond: dict):
    """
    获取该用户的对战历史
    :param cond:
    :return:
    """
    member = Member.sync_find_one(cond)
    if not member:
        return

    workbook = xlsxwriter.Workbook('%s的游戏历史明细.xlsx' % member.nick_name)
    worksheet = workbook.add_worksheet('game_history')

    history_list = MemberGameHistory.sync_find({
        'member_cid': member.cid,
        'created_dt': {
            '$gte': datetime.now().replace(2019, 1, 1, 0, 0, 0, 0)
        }
    }).sort('created_dt').to_list(None)

    cols = ['时间', '所处段位', '状态', '钻石增减']
    for col_index, col_name in enumerate(cols):
        worksheet.write(0, col_index + 1, col_name)

    print(history_list)
    for index, his in enumerate(history_list):
        cols = [
            datetime2str(his.created_dt),
            TYPE_DAN_GRADE_DICT.get(his.dan_grade, str(his.dan_grade)),
            STATUS_RESULT_CHECK_POINT_DICT.get(his.status),
            str(his.result)
        ]
        for col_index, col_name in enumerate(cols):
            worksheet.write(index + 1, col_index + 1, col_name)

    worksheet2 = workbook.add_worksheet('diamond_detail')
    details = MemberDiamondDetail.sync_find({
        'member_cid': member.cid
    }).sort('created_dt').to_list(None)
    cols = ['时间', '奖励来源', '奖励类型', '钻石增减']
    for col_index, col_name in enumerate(cols):
        worksheet2.write(0, col_index + 1, col_name)

    for index, detl in enumerate(details):
        cols = [
            datetime2str(detl.reward_datetime),
            SOURCE_MEMBER_DIAMOND_DICT.get(detl.source), detl.content,
            detl.diamond
        ]
        for col_index, col_name in enumerate(cols):
            worksheet2.write(index + 1, col_index + 1, col_name)

    workbook.close()
Exemplo n.º 5
0
    async def post(self):
        r_dict = {'code': 0}
        open_id = self.get_i_argument('open_id', None)
        try:
            if open_id:
                member = await find_member_by_open_id(open_id)
                if member:
                    award_title = ''
                    award_diamonds = ''
                    share_times = await GameDiamondReward.find_one(
                        filtered={'source': SOURCE_MEMBER_DIAMOND_DAILY_SHARE_TIMES_LIMIT})
                    value = get_cache_share_times(member.cid)
                    if value < share_times.quantity:
                        end_datetime = datetime.datetime.now().replace(hour=23, minute=59, second=59, microsecond=999)
                        current_datetime = datetime.datetime.now()
                        timeout = (end_datetime - current_datetime).seconds
                        # 分享奖励
                        _, m_diamond = await do_diamond_reward(member, SOURCE_MEMBER_DIAMOND_DAILY_SHARE)
                        # 设置更新已奖励次数
                        set_cache_share_times(member.cid, timeout)
                        if member.diamond is None:
                            member.diamond = m_diamond
                        else:
                            member.diamond = member.diamond + m_diamond
                        await member.save()
                        award_title = SOURCE_MEMBER_DIAMOND_DICT.get(SOURCE_MEMBER_DIAMOND_DAILY_SHARE)
                        award_diamonds = m_diamond
                    # 用户的钻石
                    r_dict['diamonds_num'] = member.diamond
                    r_dict['award_title'] = award_title
                    r_dict['award_diamonds'] = award_diamonds
                    r_dict['code'] = 1000
                else:
                    r_dict['code'] = 1002
            else:
                r_dict['code'] = 1001
        except Exception:
            logger.error(traceback.format_exc())

        return r_dict
Exemplo n.º 6
0
    async def post(self):
        r_dict = {'code': 0}
        open_id = self.get_i_argument('open_id')
        if not open_id:
            r_dict['code'] = 1001
            return r_dict

        try:
            member = await find_member_by_open_id(open_id)
            origin_diamond = member.diamond
            member_quantity = RedisCache.get(KEY_CACHE_APPLET_ONLINE_MEMBER)
            if not member_quantity:
                member_quantity = await Member.count(
                    dict(status=STATUS_USER_ACTIVE, open_id={'$ne': None}))
                RedisCache.set(KEY_CACHE_APPLET_ONLINE_MEMBER,
                               member_quantity,
                               timeout=5 * 60)
            else:
                member_quantity = int(member_quantity)
            r_dict['online_member_count'] = member_quantity
            r_dict['can_friend_share'] = True if (
                await MemberGameHistory.count({
                    'member_cid': member.cid,
                    'runaway_index': None
                })) > 0 else False

            reward_info = []
            if not member.dan_grade:  # 新手奖励
                member.dan_grade = TYPE_DAN_GRADE_ONE
                _, m_diamond = await do_diamond_reward(
                    member, SOURCE_MEMBER_DIAMOND_NOVICE)

                reward_info.append({
                    'title':
                    SOURCE_MEMBER_DIAMOND_DICT.get(
                        SOURCE_MEMBER_DIAMOND_NOVICE),
                    'diamond_num':
                    m_diamond
                })

            # 每日登录奖励
            start_datetime = datetime.datetime.now().replace(hour=0,
                                                             minute=0,
                                                             second=0,
                                                             microsecond=0)
            end_datetime = datetime.datetime.now().replace(hour=23,
                                                           minute=59,
                                                           second=59,
                                                           microsecond=99999)
            count = await MemberDiamondDetail.count({
                "member_cid": member.cid,
                "source": SOURCE_MEMBER_DIAMOND_LOGIN_EVERYDAY,
                "reward_datetime": {
                    '$lte': end_datetime,
                    '$gte': start_datetime
                },
            })
            if count == 0:
                _, m_diamond = await do_diamond_reward(
                    member, SOURCE_MEMBER_DIAMOND_LOGIN_EVERYDAY)

                reward_info.append({
                    'title':
                    SOURCE_MEMBER_DIAMOND_DICT.get(
                        SOURCE_MEMBER_DIAMOND_LOGIN_EVERYDAY),
                    'diamond_num':
                    m_diamond
                })

            # 排行榜奖励
            rank_reward = await wechat_utils.do_daily_ranking_award(member)
            if rank_reward:
                reward_info.append(rank_reward)

            r_dict['reward_info'] = reward_info
            r_dict['origin_diamond'] = origin_diamond
            r_dict['final_diamond'] = member.diamond
            r_dict['avatar_url'] = member.avatar
            r_dict['dan_grade_title'] = await get_dan_grade_by_index(
                member.dan_grade)
            r_dict['code'] = 1000
        except Exception:
            logger.error(traceback.format_exc())

        return r_dict
Exemplo n.º 7
0
    async def post(self):
        r_dict = {'code': 0}
        open_id = self.get_i_argument('open_id')
        if not open_id:
            r_dict['code'] = 1001
            return r_dict

        try:
            member = await find_member_by_open_id(open_id)
            if not member:
                r_dict['code'] = 1002
                return r_dict

            vault_bank = await VaultDiamondBank.find_one(
                {"quantity": {
                    '$ne': None
                }})
            if not vault_bank:
                r_dict['code'] = 1003
                return r_dict

            second_limit = vault_bank.minutes * 60
            start_datetime = datetime.datetime.now().replace(hour=0,
                                                             minute=0,
                                                             second=0,
                                                             microsecond=0)
            end_datetime = datetime.datetime.now().replace(hour=23,
                                                           minute=59,
                                                           second=59,
                                                           microsecond=999)
            count = await MemberDiamondDetail.count({
                "member_cid": member.cid,
                "source": SOURCE_MEMBER_DIAMOND_VAULT_REWARD,
                "reward_datetime": {
                    '$lte': end_datetime,
                    '$gte': start_datetime
                },
            })

            if count != 0 and 0 < vault_bank.times < count:
                r_dict['code'] = 1004  # 达到领取上限
                return r_dict

            # 最近一次获取奖励的记录
            member_diamond_detail = await MemberDiamondDetail.find({
                "member_cid":
                member.cid,
                "source":
                SOURCE_MEMBER_DIAMOND_VAULT_REWARD
            }).sort([('reward_datetime', DESC)]).limit(1).to_list(1)

            if not member_diamond_detail:
                award_member_diamond = vault_bank.quantity
            else:
                member_diamond_detail = member_diamond_detail[0]
                # 距离上一次领取奖励的时间差
                spacing_seconds = (
                    datetime.datetime.now() -
                    member_diamond_detail.reward_datetime).seconds
                if spacing_seconds >= second_limit:
                    award_member_diamond = vault_bank.quantity
                else:
                    need_quantity = (vault_bank.quantity /
                                     second_limit) * spacing_seconds  # 获得的奖励数量
                    award_member_diamond = int(need_quantity)

            diamond_detail = MemberDiamondDetail(
                member_cid=member.cid,
                diamond=award_member_diamond,
                source=SOURCE_MEMBER_DIAMOND_VAULT_REWARD)
            diamond_detail.reward_datetime = datetime.datetime.now()
            diamond_detail.content = SOURCE_MEMBER_DIAMOND_DICT.get(
                SOURCE_MEMBER_DIAMOND_VAULT_REWARD)
            await diamond_detail.save()

            member.diamond = member.diamond + award_member_diamond  # +金库奖励的钻石
            member.updated_dt = datetime.datetime.now()
            await member.save()

            r_dict = {
                'code': 1000,
                'remain_seconds': second_limit,
                'total_seconds': second_limit,
                'can_get_diamond': 0,
                'total_diamond': vault_bank.quantity,
                'member_diamond': member.diamond
            }
        except Exception:
            logger.error(traceback.format_exc())

        return r_dict