<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace loyalsoft;

/**
 * Description of TaskProc
 *
 * @author c'y'zhao
 */
class TaskProc {

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法
     * @param Req $req
     */
    public static function procMain($req) {
        switch ($req->cmd) {
            case CmdCode::cmd_task_getInfo:                                     # 6201
                return TaskProc::getTaskInfo();
            case CmdCode::cmd_task_getReward:                                   # 6202
                return TaskProc::getReward();
            case CmdCode::cmd_task_getAchieveReward:                            # 6203 领取成就奖励
                return TaskProc::getAchieveReward();
            case CmdCode::cmd_task_receiveActiveTaskReward:                     # 6204 领取活动任务奖励
                return TaskProc::receiveActiveTaskReward();
            case CmdCode::cmd_task_receiveActivePointBoxReward:                 # 6205 领取活跃点宝箱奖励
                return TaskProc::receiveActivePointBoxReward();
            case CmdCode::cmd_task_prizesAllreceived:                           # 6206 任务奖励一键领取
                return TaskProc::taskPrizesAllreceived();
            case CmdCode::cmd_task_lookGuangGao:                                # 6207 观看广告 通知后端
                return TaskProc::LookGuangGao();
            default:
                Err(ErrCode::cmd_err);
        }
    }

    /**
     * 6207 观看广告 通知后端
     * @return type
     */
    public static function LookGuangGao() {
        //list($type) = req()->paras;
        TaskProc::OnLookGuanggao();

        UserProc::updateUserInfo();
        return Resp::ok(array("task" => ctx()->task,
        ));
    }

    /**
     *  6206 任务奖励一键领取
     */
    public static function taskPrizesAllreceived() {
        list($type) = req()->paras;

        $dic = ctx()->task->activeTaskInfo;
        $arr = array();
        if ($type == Enum_ActiveTaskType::Day7) {
            $startDay = TimeUtil::totalDays(ctx()->task->day7_startTs);
            $endDay = $startDay + 6;

            if (TimeUtil::totalDays() <= $endDay) {
                $num = TimeUtil::totalDays() - $startDay;
                foreach ($dic as $uid => $task) {
                    $t = new Ins_TaskStep_Active($task);
                    if ($t->mo()->type == $type && $t->mo()->day <= $num + 1 && $t->state == Enum_TaskCardStateType::finish) {
                        $arr[] = $uid;
                    }
                }
            }
        } else {
            foreach ($dic as $uid => $task) {
                $t = new Ins_TaskStep_Active($task);
                if ($t->mo()->type == $type && $t->state == Enum_TaskCardStateType::finish) {
                    $arr[] = $uid;
                }
            }
        }
        $point = 0;
        foreach ($arr as $uid) {
            $t = new Ins_TaskStep_Active(ctx()->task->activeTaskInfo->$uid);
            if ($t->mo()->activePoint != null) {
                $list = explode(',', $t->mo()->activePoint);
                $point += $list[1];
            }
            StoreProc::AddMultiItemInStore($t->mo()->prizes);
            if ($t->mo()->prizes != null) {
                $reward = explode(',', $t->mo()->prizes);
                if (GameConfig::item_getItem($reward[0])->itemType == 201) {
                    SystemProc::GetGem_GreaterOrangeQual_Active(req()->zoneid, ctx()->baseInfo->name, $reward[0]);
                }
            }
            $t->state = Enum_TaskCardStateType::drawed;
            ctx()->task->activeTaskInfo->$uid = $t;
        }

        switch ($type) {
            case Enum_ActiveTaskType::Day7:
                ctx()->task->day7Point += $point;
                break;
            case Enum_ActiveTaskType::DailyTask:
                ctx()->privateData()->battlePass_taskPoint += $point;
                ctx()->task->dailyTaskPoint += $point;
                TaskProc::OnReceiveTaskPrize();
                break;
            case Enum_ActiveTaskType::WeekTask:
                ctx()->privateData()->battlePass_taskPoint += $point;
                ctx()->task->weekTaskPoint += $point;
                TaskProc::OnReceiveTaskPrize();
                break;
            default:
                break;
        }

        if ($point != 0) {
            $str = "5," . $point;
            StoreProc::$reward[] = $str;
        }

        UserProc::updateUserInfo();
        return Resp::ok(array("task" => ctx()->task,
                    "store" => ctx()->store,
                    'reward' => StoreProc::$reward,
                    'reward_Gem' => StoreProc::$reward_Gem,
                    'reward_equip' => StoreProc::$reward_equip,
                    'gold' => ctx()->baseInfo->gold,
                    'cash' => ctx()->baseInfo->cash,
                    'heros' => ctx()->heros,
                    'privateState' => ctx()->privateState,
        ));
    }

    /**
     *  6205 领取活跃点宝箱奖励
     */
    public static function receiveActivePointBoxReward() {
        list($type, $typeId) = req()->paras;

        $point = 0;
        switch ($type) {
            case Enum_ActiveTaskType::Day7:
                $point = ctx()->task->day7Point;
                break;
            case Enum_ActiveTaskType::DailyTask:
                $point = ctx()->task->dailyTaskPoint;
                break;
            case Enum_ActiveTaskType::WeekTask:
                $point = ctx()->task->weekTaskPoint;
                break;
            default:
                break;
        }

        my_Assert($point >= $typeId, ErrCode::task_CanotReviced_ActivePointBox);

        $mo = GameConfig::activepointreward_getItem($type, $typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        StoreProc::AddMultiItemInStore($mo->reward);

        $strId = $type . "-" . $typeId;
        my_Assert(!in_array($strId, ctx()->task->activePointReceived), ErrCode::task_RepeatReviced_ActivePointBox);
        ctx()->task->activePointReceived[] = $strId;

        UserProc::updateUserInfo();
        return Resp::ok(array("task" => ctx()->task,
                    "store" => ctx()->store,
                    "reward" => StoreProc::$reward,
                    'reward_Gem' => StoreProc::$reward_Gem,
                    'reward_equip' => StoreProc::$reward_equip,
                    'gold' => ctx()->baseInfo->gold,
                    'cash' => ctx()->baseInfo->cash,
                    'heros' => ctx()->heros,
        ));
    }

    /**
     * 6204 领取活动任务奖励
     * @return type
     */
    public static function receiveActiveTaskReward() {
        list($uid) = req()->paras;

        my_Assert(StlUtil::dictHasProperty(ctx()->task->activeTaskInfo, $uid), ErrCode::task_NoExist);

        $ins_activeTaskInfo = new Ins_TaskStep_Active(ctx()->task->activeTaskInfo->$uid);
        if ($ins_activeTaskInfo->mo()->type == Enum_ActiveTaskType::Day7) {
            my_Assert(ctx()->task->day7_startTs > 0, ErrCode::task_NotOpen_Day7Happy);
            $startDay = TimeUtil::totalDays(ctx()->task->day7_startTs);
            $endDay = $startDay + 6;
            my_Assert(TimeUtil::totalDays() <= $endDay, ErrCode::task_DownTsLimit);

            $num = TimeUtil::totalDays() - $startDay;
            my_Assert($ins_activeTaskInfo->mo()->day <= $num + 1, ErrCode::task_DownTsLimit);
        }
        my_Assert($ins_activeTaskInfo->cur >= $ins_activeTaskInfo->max(), ErrCode::task_CanotPriceReviced);
        my_Assert($ins_activeTaskInfo->state == Enum_TaskCardStateType::finish, ErrCode::task_CanotPriceReviced);

        if ($ins_activeTaskInfo->mo()->prizes != null) {
            $reward = explode(',', $ins_activeTaskInfo->mo()->prizes);
            if (GameConfig::item_getItem($reward[0])->itemType == 201) {
                SystemProc::GetGem_GreaterOrangeQual_Active(req()->zoneid, ctx()->baseInfo->name, $reward[0]);
            }
        }

        StoreProc::AddMultiItemInStore($ins_activeTaskInfo->mo()->prizes);
        ctx()->task->activeTaskInfo->$uid->state = Enum_TaskCardStateType::drawed;
        ctx()->task->activeTaskInfo->$uid->drawTs = now();

        //金蛇任务特殊处理
        self::InitFlipCardTask($uid);
        //主线任务
        self::InitNextMainTask($uid);

        $activePointArr = explode(',', $ins_activeTaskInfo->mo()->activePoint);
        switch ($ins_activeTaskInfo->mo()->type) {
            case Enum_ActiveTaskType::Day7:
                ctx()->task->day7Point += $activePointArr[1];
                StoreProc::$reward[] = $ins_activeTaskInfo->mo()->activePoint;
                break;
            case Enum_ActiveTaskType::DailyTask:
                ctx()->privateData()->battlePass_taskPoint += $activePointArr[1];
                ctx()->task->dailyTaskPoint += $activePointArr[1];
                StoreProc::$reward[] = $ins_activeTaskInfo->mo()->activePoint;
                TaskProc::OnReceiveTaskPrize();
                break;
            case Enum_ActiveTaskType::WeekTask:
                ctx()->privateData()->battlePass_taskPoint += $activePointArr[1];
                ctx()->task->weekTaskPoint += $activePointArr[1];
                StoreProc::$reward[] = $ins_activeTaskInfo->mo()->activePoint;
                TaskProc::OnReceiveTaskPrize();
                break;
            default:
                break;
        }


        GuideProc::EquipLvUp_Guide_Trigger($ins_activeTaskInfo->typeId);

        UserProc::updateUserInfo();
        return Resp::ok(array("task" => ctx()->task,
                    "store" => ctx()->store,
                    'reward' => StoreProc::$reward,
                    'reward_Gem' => StoreProc::$reward_Gem,
                    'reward_equip' => StoreProc::$reward_equip,
                    'gold' => ctx()->baseInfo->gold,
                    'cash' => ctx()->baseInfo->cash,
                    'heros' => ctx()->heros,
                    'privateState' => ctx()->privateState,
                    'tili_ts' => ctx()->baseInfo->tili_ts,
                    "tili" => ctx()->baseInfo->tili,
        ));
    }

    static function InitNextMainTask($uid) {
        $ins_activeTaskInfo = new Ins_TaskStep_Active(ctx()->task->activeTaskInfo->$uid);
        if ($ins_activeTaskInfo->mo()->type != Enum_ActiveTaskType::MainTask) {
            return;
        }

        $nextId = $ins_activeTaskInfo->mo()->next;
        if ($nextId != null) {
            StlUtil::dictRemove(ctx()->task->activeTaskInfo, $uid);
            self::MainTaskInit($nextId);
        }
    }

    static function InitFlipCardTask($uid) {
        $ins_activeTaskInfo = new Ins_TaskStep_Active(ctx()->task->activeTaskInfo->$uid);
        if ($ins_activeTaskInfo->mo()->type != Enum_ActiveTaskType::FlipCardLotteryTask) {
            return;
        }
        if ($ins_activeTaskInfo->mo()->next == 0) {
            return;
        }

        $nextId = $ins_activeTaskInfo->mo()->next;
        if ($nextId != null) {
            $ins_TaskStep_Active = new Ins_TaskStep_Active($nextId);
            $ins_TaskStep_Active->uid = $uid;
            $ins_TaskStep_Active->cur = $ins_activeTaskInfo->cur;
            $ins_TaskStep_Active->state = Enum_TaskCardStateType::ing;
            ctx()->task->activeTaskInfo->$uid = $ins_TaskStep_Active;

            self::autoRecoverState_activeTask();
        }
    }

    /**
     * 6203 领取成就奖励
     * @return type
     */
    public static function getAchieveReward() {
        list($typeId) = req()->paras;

        $mo = GameConfig::achieve_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        my_Assert(StlUtil::dictHasProperty(ctx()->task->achieveDic, $typeId), ErrCode::err_const_no);
        $achieveDic = ctx()->task->achieveDic->$typeId;

        $list = explode(',', $mo->condition);
        $length = count($list);
        my_Assert($achieveDic->val >= $achieveDic->max, ErrCode::task_CanotPriceReviced);
        my_Assert($achieveDic->received < $achieveDic->max, ErrCode::task_PriceRepeatReviced);

        $index = 0;
        foreach ($list as $k => $num) {
            if ($num == $achieveDic->max) {
                $index = $k;
                break;
            }
        }
        //领取奖励
        $pList = explode(';', $mo->reward);
        StoreProc::AddMultiItemInStore($pList[$index]);

        //下一个max    
        if ($index + 1 < $length) {
            $achieveDic->max = $list[$index + 1];
        }
        ctx()->task->achieveDic->$typeId = $achieveDic;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    "achieve" => ctx()->task->achieveDic,
                    "cash" => ctx()->baseInfo->cash,
        ));
    }

    public static function getTaskInfo() {
        //list($type) = req()->paras; //武器uid

        return Resp::ok(array("task" => ctx()->task,
        ));
    }

    static function ResetTask() {
//        self::DailyTaskReset();
//        self::ClearDay7Task();                      
//        $week = date("w");
//        if ($week == 1) {//每周一 第一次登录的时候刷新         
//            self::WeekTaskReset();            
//        }
    }

    static function ClearDay7Task() {
        if (ctx()->task->day7_startTs > 0) {
            $startDay = TimeUtil::totalDays(ctx()->task->day7_startTs);
            $endDay = $startDay + 6;
            if (TimeUtil::totalDays() > $endDay) {
                self::ClearActiveTask(Enum_ActiveTaskType::Day7);
                self::clearActivePointRewardReceived(Enum_ActiveTaskType::Day7);
                ctx()->task->day7_startTs = 0;
            }
        }
    }

    /**
     * 7日狂欢数据重置
     */
    public static function Day7TaskReset() {
//        if (!FightProc::isFunUnlock(6)) {
//            return;
//        }

        if (ctx()->task->day7_startTs == 0) {
            $tag = true;
            foreach (ctx()->task->activeTaskInfo as $uid => $val) {
                $taskMo = GameConfig::activeTask_getItem($val->typeId);
                if ($taskMo->type == 1) {
                    $tag = false;
                    break;
                }
            }

            if ($tag) {//7日的数据初始化
                ctx()->task->day7Point = 0;
                ctx()->task->day7_startTs = now();
                self::initActiveTask(Enum_ActiveTaskType::Day7);
                self::autoRecoverState_activeTask();
            }
        }
    }

    static function ClearActiveTask($type) {
        $arr = array();
        foreach (ctx()->task->activeTaskInfo as $uid => $val) {
            $taskMo = GameConfig::activeTask_getItem($val->typeId);
            if ($taskMo->type == $type) {
                $arr[] = $uid;
            }
        }
        if (count($arr) > 0) {
            foreach ($arr as $uid) {
                StlUtil::dictRemove(ctx()->task->activeTaskInfo, $uid);
            }
        }
    }

    /**
     * 清理活跃点宝箱领取记录
     * @param type $type
     */
    static function clearActivePointRewardReceived($type = 1) {
        $reward = ctx()->task->activePointReceived;
        $arr = array();
        foreach ($reward as $val) {
            $str = explode('-', $val);
            if ($str[0] == $type) {
                $arr[] = $val;
            }
        }
        if (count($arr) > 0) {
            foreach ($arr as $sId) {
                StlUtil::arrayRemove(ctx()->task->activePointReceived, $sId);
            }
        }
    }

    static function initActiveTask($type) {
        $day7List = GameConfig::activeTask_type_getItemArray($type);
        $length = ctx()->task->activeTaskLength;
        foreach ($day7List as $key => $task) {
            $length += 1;
            $ins_TaskStep_Active = new Ins_TaskStep_Active($task->id);
            $ins_TaskStep_Active->uid = $length;
            $ins_TaskStep_Active->state = Enum_TaskCardStateType::ing;
            ctx()->task->activeTaskInfo->$length = $ins_TaskStep_Active;
        }
        ctx()->task->activeTaskLength = $length;
    }

    /**
     * 日常
     */
    static function DailyTaskReset($unlock = false) {
        if (!$unlock && !FightProc::isFunUnlock(5)) {//之所以这有两个判断是因为这个 活动解锁的代码监测位置问题，fightProc 里解锁监测代码在 $gateInfo->tz_state = $index;之前了 这个位置没有办法解锁
            return;
        }

        self::ClearActiveTask(Enum_ActiveTaskType::DailyTask);
        self::initActiveTask(Enum_ActiveTaskType::DailyTask);
        self::autoRecoverState_activeTask();
        ctx()->task->dailyTaskPoint = 0;
        self::clearActivePointRewardReceived(Enum_ActiveTaskType::DailyTask);
    }

    /**
     * 周常
     */
    static function WeekTaskReset($unlock = false) {
        if (!$unlock && !FightProc::isFunUnlock(5)) {//之所以这有两个判断是因为这个 活动解锁的代码监测位置问题，fightProc 里解锁监测代码在 $gateInfo->tz_state = $index;之前了 这个位置没有办法解锁
            return;
        }

        self::ClearActiveTask(Enum_ActiveTaskType::WeekTask);
        self::initActiveTask(Enum_ActiveTaskType::WeekTask);

        self::autoRecoverState_activeTask();
        ctx()->task->weekTaskPoint = 0;
        self::clearActivePointRewardReceived(Enum_ActiveTaskType::WeekTask);
    }

    static function MainTaskInit($id) {
        $length = ctx()->task->activeTaskLength;
        $length += 1;
        $ins_TaskStep_Active = new Ins_TaskStep_Active($id);
        $ins_TaskStep_Active->uid = $length;
        $ins_TaskStep_Active->state = Enum_TaskCardStateType::ing;
        ctx()->task->activeTaskInfo->$length = $ins_TaskStep_Active;
        ctx()->task->activeTaskLength = $length;
        self::autoRecoverState_activeTask();
    }

    static function checkMainTask() {
        $activeTaskInfo = ctx()->task->activeTaskInfo;
        $tag = false;
        foreach ($activeTaskInfo as $key => $task) {
            $ins_TaskStep_Active = new Ins_TaskStep_Active($task);
            if ($ins_TaskStep_Active->mo()->type == Enum_ActiveTaskType::MainTask) {
                $tag = true;
                break;
            }
        }
        if (!$tag) {
            TaskProc::MainTaskInit(glc()->FirstMainTaskId);
        }
        self::autoRecoverState_activeTask();
    }

//    static function MainTaskCheck_oldUserDate() {
//        $dic = ctx()->task->activeTaskInfo;
//        $tag = false;
//        foreach ($dic as $uid => $task) {
//            $taskItem = new Ins_TaskStep_Active($task);
//            if ($taskItem->mo()->type == Enum_ActiveTaskType::MainTask) {
//                $tag = true;
//                break;
//            }
//        }
//
//        if (!$tag) {        
//            self::MainTaskInit(glc()->FirstMainTaskId);          
//        }
//    }
// <editor-fold defaultstate="collapsed" desc="不同活动任务监测信息-">

    /**
     * 登录
     */
    static function OnLogin_Daily() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::DailyLogin, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 改名字
     */
    static function OnReName() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::ReName, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 登录第X天
     */
    static function OnLogin_day7() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OnLogin_X, Enum_PropelType::set, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 登录第X天
     */
    static function OnLogin_Accumulate() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OnLogin_Accumulate, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }
    
    static function OnLogin_Accumulate_FlipCard() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OnLogin_Accumulate_FlipCard, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 通关第X关
     */
    static function OnPassGate_X($gateId) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::PassGate_X, Enum_PropelType::add, 1, array($gateId));
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 通关第X关
     */
    static function OnPassGate_X_state($gateId) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::PassGate_X_state, Enum_PropelType::add, 1, array($gateId));
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 参与主线关卡1次
     */
    static function OnFightNumMainGate() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::FightNumMainGate, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 参与挑战副本1次
     */
    static function OnFightNumChallengeGate() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::FightNumChallengeGate, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 通关挑战关卡第X关
     */
    static function OnPassLayer_ChallengeGate($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::PassLayer_ChallengeGate, Enum_PropelType::max, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 通关竞速副本5次
     */
//    static function OnPassNumRaceGate() {
//        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::PassNumRaceGate, Enum_PropelType::add, 1, array());
//        self::CheckActiveTaskConditions($taskEventArg);
//    }

    /**
     * 完成5次启灵	
     */
    static function OnCompleteNumQiLing() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::CompleteNumQiLing, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 完成1次关键启灵
     */
    static function OnCompleteNumSpecialQiLing() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::CompleteNumSpecialQiLing, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 镶嵌3个普通品质以上宝石 	
     */
    static function OnSetSpecialQualGem() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::SetSpecialQualGem, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 合成宝石2次
     */
    static function OnComposeNumGem() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::ComposeNumGem, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 合成宝石2次
     */
    static function OnComposeNumGem_state() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::ComposeNumGem_state, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 任意1个法术等级达到2  
     */
    static function OnAnySkillUpLevel_X() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::AnySkillUpLevel_X, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 任意1件装备等级达到2     
     */
    static function OnAnyEquipUpLevel_X() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::AnyEquipUpLevel_X, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 全身装备等级达到3级以上  
     */
    static function OnAllEquipUpLevel_X() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::AllEquipUpLevel_X, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 任意装备等级升级1次    
     */
    static function OnEquipLevelUpNum() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::EquipLevelUpNum, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 装备合成
     */
    static function OnEquipComposeNum() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::EquipComposeNum, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 击败怪物总数达到1000   
     */
    static function OnKillCommonNumMonster($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::KillCommonNumMonster, Enum_PropelType::add, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 击败领袖怪物总数达到1000   
     */
    static function OnKillleaderNumMonster($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::KillleaderNumMonster, Enum_PropelType::add, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 累计获取10000金币    
     */
    static function OnAccumulateGoldNum($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::AccumulateGoldNum, Enum_PropelType::add, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 累计消耗10000体力    
     */
    static function OnAccumulateTiliNum($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::AccumulateTiliNum, Enum_PropelType::add, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 累计消耗10000钻石  
     */
    static function OnConsumeCashNum($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::ConsumeCashNum, Enum_PropelType::add, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 开启1次璀璨宝石箱
     */
    static function OnOpenNumBrightBox($boxId, $num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OpenNumBrightBox, Enum_PropelType::add, $num, array($boxId));
        self::CheckActiveTaskConditions($taskEventArg);
    }

    static function OnOpenNumBrightBox_state($boxId, $num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OpenNumBrightBox_state, Enum_PropelType::add, $num, array($boxId));
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 每日商店购买物品1次    
     */
    static function OnBuyNumDailyShop() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::BuyNumDailyShop, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 每日商店购买物品1次 状态类的   
     */
    static function OnBuyNumDailyShop_state() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::BuyNumDailyShop_state, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 开任意宝箱X次
     */
    static function OnOpenNumAnyBox($num) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OpenNumAnyBox, Enum_PropelType::add, $num, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 购买或观看广告获得体力X次
     */
    static function OnBuyTiliOrLookGuanggao() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::BuyTiliOrLookGuanggao, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 观看广告
     */
    static function OnLookGuanggao() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::LookGuanggao, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 任意充值X次
     */
    static function OnRecharge() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::Recharge, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 206: 进入第X关;参数1:关卡ID;参数2:进入次数
     */
    static function OnFightGate_X($gateId) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::FightGate_X, Enum_PropelType::add, 1, array($gateId));
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 207: 通关主线挑战第X关的X模式1次;参数1:关卡ID;参数2:难度(1普通,2困难,3噩梦)
     */
    static function OnPassMainChallengeGate_X_Num($gateId, $state) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::PassMainChallengeGate_X_Num, Enum_PropelType::add, 1, array($gateId, $state));
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 208: 领取排行榜元宝奖励X次;参数1:无;参数2:次数(数值)
     */
    static function OnRankPrize_Num() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::RankPrize_Num, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 209: 玩家等级到达X级;参数1:无;参数2:等级(数值)
     */
    static function OnUserLevel_X($lv) {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::UserLevel_X, Enum_PropelType::set, $lv, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 扫荡
     */
    static function OnFightSweepGate() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::FightSweepGate, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 领取任务奖励
     */
    static function OnReceiveTaskPrize() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::OnReceiveTaskPrize, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 购买铜钱一次
     */
    static function OnBuyGold() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::BuyGold, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 领取战力榜元宝奖励X次;参数1:无;参数2:次数(数值)
     */
    static function OnFightRankPrize_Num() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::FightRankPrize_Num, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 封印技能
     */
    static function OnCloseSkill() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::CloseSkill, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }

    /**
     * 玩家领取X次章节宝箱
     */
    static function OnReceiveGateBoxReward() {
        $taskEventArg = new Ins_TaskEventArgs(Enum_ActiveTaskCmdType::ReceiveGateBoxReward, Enum_PropelType::add, 1, array());
        self::CheckActiveTaskConditions($taskEventArg);
    }
    
// </editor-fold>
//
// <editor-fold defaultstate="collapsed" desc="活动任务是否完成的检测-">

    /**
     * 检测任务是否完成 
     * @param Ins_TaskEventArgs $taskParam
     */
    static function CheckActiveTaskConditions($taskParam) {
        $activeTaskDic = ctx()->task->activeTaskInfo;
        foreach ($activeTaskDic as $uid => &$task) {
            $ins_TaskStep_Active = new Ins_TaskStep_Active($task);
            if ($ins_TaskStep_Active->state > Enum_TaskCardStateType::ing) {
                continue;
            }

            if ($ins_TaskStep_Active->mo()->cmd == $taskParam->taskType) {
                if ($ins_TaskStep_Active->isStatusType()) {//校验状态类的                                      
                    $cur = $ins_TaskStep_Active->calcStatusCur();
                    if ($cur != $ins_TaskStep_Active->cur) {
                        $ins_TaskStep_Active->cur = $cur;
                        if ($ins_TaskStep_Active->cur >= $ins_TaskStep_Active->max()) {
                            $ins_TaskStep_Active->cur = $ins_TaskStep_Active->max();
                        }
                    }
                } else {
                    $tag = true;
                    if ($ins_TaskStep_Active->mo()->cmd == Enum_ActiveTaskCmdType::AnySkillUpLevel_X && count($taskParam->paras) > 0 && $ins_TaskStep_Active->mo()->paras != null) {
                        $parasArr = $taskParam->paras;
                        $lvArr = explode(',', $ins_TaskStep_Active->mo()->paras);
                        $needLv = $lvArr[0];
                        $num = 0;
                        foreach ($parasArr as $lv) {
                            if ($lv > $needLv) {
                                $num += 1;
                            }
                        }
                        $ins_TaskStep_Active->cur = $num;
                        if ($ins_TaskStep_Active->cur >= $ins_TaskStep_Active->max()) {
                            $ins_TaskStep_Active->cur = $ins_TaskStep_Active->max();
                        }
                        $tag = false;
                    } else {
                        if ($ins_TaskStep_Active->mo()->paras != null) {
                            $parasArr = $taskParam->paras;
                            if (count($parasArr) > 0) {
                                $arr = explode(',', $ins_TaskStep_Active->mo()->paras);
                                $index = 0;
                                foreach ($arr as $s) {
                                    if ($s != $parasArr[$index]) {
                                        $tag = false;
                                        break;
                                    }
                                    $index += 1;
                                }
                            } else {
                                $tag = false;
                            }
                        }

                        if ($tag) {
                            $ins_TaskStep_Active->propel($taskParam);
                            if ($ins_TaskStep_Active->cur >= $ins_TaskStep_Active->max()) {
                                $ins_TaskStep_Active->cur = $ins_TaskStep_Active->max();
                            }
                        }
                    }
                }

                if ($ins_TaskStep_Active->isFinish()) {
                    $ins_TaskStep_Active->state = Enum_TaskCardStateType::finish;
                }

                ctx()->task->activeTaskInfo->$uid = $ins_TaskStep_Active;
            }
        }

        //ctx()->task->activeTaskInfo = $activeTaskDic;
        UserProc::updateUserInfo();                                             # 更新玩家数据
    }

    /**
     * 自动修复状态型任务
     */
    static function autoRecoverState_activeTask() {
        $activeTaskDic = ctx()->task->activeTaskInfo;
        foreach ($activeTaskDic as $uid => &$task) {
            $ins_TaskStep_Active = new Ins_TaskStep_Active($task);
            if ($ins_TaskStep_Active->state > Enum_TaskCardStateType::ing) {
                continue;
            }
            if ($ins_TaskStep_Active->isStatusType()) {
                $cur = $ins_TaskStep_Active->calcStatusCur();
                if ($cur != $ins_TaskStep_Active->cur) {
                    $ins_TaskStep_Active->cur = $cur;
                    if ($ins_TaskStep_Active->cur >= $ins_TaskStep_Active->max()) {
                        $ins_TaskStep_Active->cur = $ins_TaskStep_Active->max();
                    }
                }
            }

            if ($ins_TaskStep_Active->isFinish()) {
                $ins_TaskStep_Active->state = Enum_TaskCardStateType::finish;
            }

            ctx()->task->activeTaskInfo->$uid = $ins_TaskStep_Active;
        }

        UserProc::updateUserInfo();                                             # 更新玩家数据
    }

// </editor-fold>
//      
}
