示例#1
0
    def handle(self, *args, **options):

        print '================================'
        print 'recover_goukonevent_score'
        print '================================'
        is_write = (args[0] if args else None) in ('1', 1)

        model_mgr = ModelRequestMgr()
        config = BackendApi.get_current_battleeventconfig(
            model_mgr, using=settings.DB_READONLY)
        mid = config.mid
        eventmaster = BackendApi.get_battleevent_master(
            model_mgr, mid, using=settings.DB_READONLY)
        eventrankmaster_list = BackendApi.get_battleevent_rankmaster_by_eventid(
            model_mgr, mid, using=settings.DB_READONLY)
        eventrankmaster_dict = dict([
            (eventrankmaster.rank, eventrankmaster)
            for eventrankmaster in eventrankmaster_list
        ])

        uid = PlayerExp.max_value('id')
        LIMIT = 500
        while 0 < uid:
            filters = {'mid': eventmaster.id, 'uid__lte': uid}

            scorerecord_list = BattleEventScore.fetchValues(
                filters=filters,
                order_by='-uid',
                limit=LIMIT,
                offset=0,
                using=settings.DB_READONLY)
            rankrecord_list = BattleEventRank.getByKey(
                [
                    BattleEventRank.makeID(scorerecord.uid, eventmaster.id)
                    for scorerecord in scorerecord_list
                ],
                using=settings.DB_READONLY)
            rankrecord_dict = dict([(rankrecord.uid, rankrecord)
                                    for rankrecord in rankrecord_list])

            for scorerecord in scorerecord_list:
                uid = min(uid, scorerecord.uid)

                if scorerecord.point_total < 1:
                    print '%s...zero' % scorerecord.uid
                    continue

                rankrecord = rankrecord_dict.get(scorerecord.uid)
                if rankrecord is None:
                    print '%s...rank None' % scorerecord.uid
                    continue

                eventrankmaster = eventrankmaster_dict.get(rankrecord.rank)
                if eventrankmaster is None:
                    print '%s...rank Unknown' % scorerecord.uid
                    continue

                p_key = BattleEventScorePerRank.makeID(
                    scorerecord.uid,
                    BattleEventScorePerRank.makeMid(mid, eventrankmaster.rank))
                if BackendApi.get_model(model_mgr,
                                        BattleEventScorePerRank,
                                        p_key,
                                        using=settings.DB_DEFAULT):
                    print '%s...Already' % scorerecord.uid
                    continue

                if is_write:
                    try:
                        db_util.run_in_transaction(Command.tr_write,
                                                   eventmaster,
                                                   eventrankmaster,
                                                   rankrecord).write_end()
                    except:
                        print '%s...ERROR' % scorerecord.uid
                        continue
                print '%s...SUCCESS' % scorerecord.uid
            uid -= 1
    def __proc_normal(self, model_mgr, redisdb, config, eventmaster,
                      rankmaster_dict, now, logintime, cdate):
        """通常のバトルイベント.
        """
        eventrankmaster = rankmaster_dict.get(eventmaster.rankstart)

        print 'start for normal'

        print '================================'
        print 'save level.'
        rediskey = "battleevent_preparation"
        redisdb.delete(rediskey)

        uid = PlayerExp.max_value('id')
        LIMIT = 500
        while 0 < uid:
            pipe = redisdb.pipeline()

            uidlist = range(max(0, uid - LIMIT) + 1, uid + 1)
            playerexplist = PlayerExp.getByKey(uidlist,
                                               using=settings.DB_READONLY)
            tutorialend_uid_list = [
                model.id for model in PlayerTutorial.getByKey(
                    uidlist, using=settings.DB_READONLY)
                if model.tutorialstate == Defines.TutorialStatus.COMPLETED
            ]
            for playerexp in playerexplist:
                if not playerexp.id in tutorialend_uid_list:
                    continue
                pipe.zadd(rediskey, playerexp.id, playerexp.level)

            pipe.execute()
            uid -= LIMIT

        print '================================'
        print 'allocate start.'
        data = redisdb.zrevrange(rediskey,
                                 0,
                                 1,
                                 withscores=True,
                                 score_cast_func=RedisModel.value_to_int)
        if data:
            level = data[0][1]
            groupid = 0
            while 0 < level:
                uidlist = [
                    RedisModel.value_to_int(uid)
                    for uid in redisdb.zrangebyscore(rediskey, level, level)
                    if RedisModel.value_to_int(uid)
                ]
                if uidlist:
                    random.shuffle(uidlist)
                    model_mgr, group = db_util.run_in_transaction(
                        Command.tr_write, groupid, eventmaster,
                        eventrankmaster, uidlist, now)
                    model_mgr.write_end()
                    if group and not group.fixed:
                        groupid = group.id
                    else:
                        groupid = 0
                level -= 1

        print '================================'
        print 'save rankuidset.'
        for rankmaster in rankmaster_dict.values():
            print "eventid=%s, rank=%s" % (rankmaster.eventid, rankmaster.rank)
            BackendApi.save_battleevent_rankuidset(rankmaster.eventid,
                                                   rankmaster.rank)

        # 後片付け.
        redisdb.delete(rediskey)

        print '================================'
        print 'all done.'