<?php

namespace loyalsoft;

/*
 * 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 CollegeProc
 *
 * @author cyzhao
 */
class CollegeProc {

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法
     */
    public static function procMain() {
        switch (req()->cmd) {
            case CmdCode::cmd_college_ActiveTaskCard://废弃                     # 7301 激活任务卡
                return CollegeProc::ActiveTaskCard();
            case CmdCode::cmd_college_ReceiveCollegeReward:                     # 7302 领取年级奖励
                return CollegeProc::ReceiveCollegeReward();
            case CmdCode::cmd_college_ReceiveCourseReward:                      # 7303 领取课程奖励
                return CollegeProc::ReceiveCourseReward();
            case CmdCode::cmd_college_ScoreExchangeItem:                        #7304 积分兑换道具
                return CollegeProc::ScoreExchangeItem();
            case CmdCode::cmd_college_GetCollegeData:
                return CollegeProc::GetCollegeData();
            default:
                Err(ErrCode::cmd_err);
        }
    }

    public static function GetCollegeData() {
        $req = req();
        //list($taskCardId) = $req->paras          
        return Resp::ok(array('store' => ctx()->store, 'college' => ctx()->college));
    }

    /**
     * 7301 激活---废弃
     */
    public static function ActiveTaskCard() {
        $req = req();
        list($taskCardId) = $req->paras;
        $collegeInfo = ctx()->college;
        $compMissCardDic = $collegeInfo->compMissCardDic;
        if (!StlUtil::dictHasProperty($compMissCardDic, $taskCardId)) {
            return Resp::err(ErrCode::college_notExistTaskCard);
        }

        if ($compMissCardDic->$taskCardId->type > Enum_TaskCardStateType::notopen) {
            return Resp::err(ErrCode::college_TaskCardStateError);
        }

        $cid = StoreProc::PutTaskCardInStore($taskCardId);                      // 存入背包
        NormalEventProc::OnTaskCard_Actived($cid, null);                        # 带入事件
        $compMissCardDic->$taskCardId->indexId = $cid;
        $compMissCardDic->$taskCardId->type = Enum_TaskCardStateType::ing;

        $taskCard = new Ins_TaskCard(ctx()->store->taskcards->$cid); # 任务卡对象
        $taskCard->state = Enum_TaskCardStateType::ing;

        TaskProc::OnGetSchoolTask();
        foreach ($taskCard->curSteps as &$tsp) {                                # 初期里面只有一个任务
            $tsp = new Ins_TaskStep($tsp);
            if ($tsp->isStatusType()) {                                         # 如果是道具收集类的, 扣除所收集道具
                $tsp->autoCalcStatusCur();
            }
        }
        if ($taskCard->IsFinish()) {                                            # 检查是否任务卡的所有步骤都已完成
            $taskCard->state = Enum_TaskCardStateType::finish;
            $compMissCardDic->$taskCardId->type = Enum_TaskCardStateType::finish;
            TaskProc::OnFinishSchoolTask();
            NormalEventProc::OnTaskCard_Finish($taskCard->uid, null);           # 广播卡完成事件
        }
        ctx()->college->compMissCardDic = $compMissCardDic;
        ctx()->store->taskcards->$cid = $taskCard;
        UserProc::updateUserInfo();

        //$compMissCardDic->$taskCardId->type = 2;

        return Resp::ok(array('store' => ctx()->store, 'college' => ctx()->college));         # 返回值更新背包
    }

    /**
     * 7302 领取一个年级的奖励
     */
    public static function ReceiveCollegeReward() {
        $req = req();
        list($collegeId) = $req->paras;

        $collegeInfo = ctx()->college;
        $mo = GameConfig::college_getItem($collegeId);
        my_Assert(count($collegeInfo->comCourseList) == count(explode(',', $mo->cList)), ErrCode::college_courseNumError);

        StoreProc::AddMultiItemInStore($mo->prize);      # 发放奖励

        $collegeInfo->collegeRewardList[] = $collegeId;

//        $collegeInfo = ctx()->college;
//        $dic = GameConfig::college_getItem($collegeInfo->collegeId);
//        $tag = true;
//        $cList = explode(',', $dic->cList);
//        foreach ($cList as $cId) {
//            if (!in_array($cId, $collegeInfo->comCourseList)) {
//                $tag = false;
//            }
//        }
//
//        if (!$tag) {
//            return Resp::err(ErrCode::college_courseNumError);
//        }
//        StoreProc::AddMultiItemInStore($dic->prize);      # 发放奖励
//
//        $college = new Info_College(ctx()->college);
//        $college->updataCourseNew();

        ctx()->college = $collegeInfo;
        UserProc::updateUserInfo();

        return Resp::ok(array('store' => ctx()->store,
                    'gold' => ctx()->baseInfo->gold,
                    'cash' => ctx()->baseInfo->cash,
                    'lvl' => ctx()->baseInfo->level,
                    'college' => ctx()->college));
    }

    /**
     * 7303 领取课程奖励
     */
    public static function ReceiveCourseReward() {
        $req = req();
        list($collegeId, $courseId) = $req->paras;

        $collegeInfo = ctx()->college;
        my_Assert(!in_array($courseId, $collegeInfo->comCourseList), ErrCode::college_CourseRewardRepeatReceive);

        $mo = GameConfig::course_getItem($courseId);
        $list = explode(',', $mo->misslist);

        //my_Assert(StlUtil::dictHasProperty($collegeInfo->ingTaskCardDic, $courseId), ErrCode::college_CourseNotUnlocked);

        foreach ($list as $id) {
            my_Assert(StlUtil::dictHasProperty($collegeInfo->ingTaskCardDic->$collegeId->$courseId, $id) && $collegeInfo->ingTaskCardDic->$collegeId->$courseId->$id->type == Enum_TaskCardStateType::drawed, ErrCode::college_TaskCardStateError);
        }

        StoreProc::AddMultiItemInStore($mo->prize);      # 发放奖励
        TaskProc::OnFinishAllSchoolTaskOfGrade($courseId);

        $collegeInfo->comCourseList[] = $courseId;

//        $tag = true;
//        foreach ($collegeInfo->compMissCardDic as $key => $value) {
//            if ($value->type != Enum_TaskCardStateType::drawed) {
//                $tag = false;
//                break;
//            }
//        }
//
//        if (!$tag) {
//            return Resp::err(ErrCode::college_taskCardNumError);
//        }
//
//        StoreProc::AddMultiItemInStore(GameConfig::course_getItem($collegeInfo->courseId)->prize);      # 发放奖励
//        TaskProc::OnFinishAllSchoolTaskOfGrade($collegeInfo->courseId);
//
//        $college = new Info_College(ctx()->college);
//        $college->updataCourse();

        ctx()->college = $collegeInfo;
        UserProc::updateUserInfo();

        return Resp::ok(array('store' => ctx()->store,
                    'gold' => ctx()->baseInfo->gold,
                    'cash' => ctx()->baseInfo->cash,
                    'lvl' => ctx()->baseInfo->level,
                    'college' => ctx()->college
        ));
    }

    /**
     * 7304 积分兑换道具
     */
    public static function ScoreExchangeItem() {
        $req = req();
        list($id) = $req->paras;

        $scoreRewardList = ctx()->college->scoreRewardList;
        my_Assert(in_array($id, $scoreRewardList), ErrCode::college_taskScoreRewardRepeatReceive);

        $score = ctx()->college->score;
        my_Assert($score >= $id, ErrCode::college_taskScoreRewardRepeatReceive);

        $mo = GameConfig::score_getItem($id);
        ctx()->college->scoreRewardList[] = $id;
        StoreProc::AddMultiItemInStore($mo->items);
        UserProc::updateUserInfo();

        return Resp::ok(array('store' => ctx()->store,
                    'college' => ctx()->college,
        ));
    }

}
