<?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.
 */

/**
 * Description of MisStatus
 * 任务状态信息
 * @author jgao
 */
class MisStatus {

    //put your code here
    /// <summary>
    /// id
    /// </summary>
    public $id = 0;
    /// <summary>
    /// 类型
    /// </summary>
    public $type = 0;
    /// <summary>
    /// 命令码
    /// </summary>
    public $cmd = 0;
    /// <summary>
    /// 参数集
    /// </summary>
    public $paras = array();
    /// <summary>
    /// 当前计数
    /// </summary>
    public $cur = 0;
    /// <summary>
    /// 最大计数
    /// </summary>
    public $max = 0;
    /// <summary>
    /// 触发任务
    /// </summary>
    public $next = 0;
    /// <summary>
    /// 最低等级
    /// </summary>
    public $minlv = 0;
    
    /**
     * 所属任务卡
     * @var type 
     */
    public $card = 0;
    
    /**
     * 扩展数据
     * @var type 
     */
    public $exdatas = array();

    function initialize($value) {
        CommUtil::loadObject($value, $this);
    }

    /// <summary>
    /// 是否完成
    /// </summary>
    public function isFinish() {
        return $this->cur >= $this->max;
    }

    /// <summary>
    /// 由原始模型执行初始化
    /// </summary>
    /// <param name="mo"></param>
    /// <returns></returns>
    public static function initFromMo($mo) {
        $mis = new MisStatus();
        $mis->id = $mo->id;
        $mis->type = $mo->type;
        $mis->cmd = $mo->cmd;
        $mis->paras = strsplit($mo->paras);
        $mis->cur = self::calcStatusCur($mo);
        $mis->max = $mo->num;
        $mis->next = $mo->next;
        $mis->minlv = $mo->minlv;

        return $mis;
    }

    /**
     * 是否状态型任务
     * @param type $cmd
     */
    static function isStatusType($cmd) {
        return
                $cmd == MissEventCode::Evn_User_Levelup ||
                $cmd == MissEventCode::SEvn_Habitat_Own ||
                $cmd == MissEventCode::SEvn_Farm_Own ||
                $cmd == MissEventCode::SEvn_Build_Own ||
                $cmd == MissEventCode::SEvn_Pet ||
                $cmd == MissEventCode::SEvn_Explore_GridOwn;
    }

    /**
     * 自动对齐可能出现统计失误的状态型任务计数
     */
    function autoCalcStatusCur() {
        if (self::isStatusType($this->cmd)) {
            $mo = dataCenter()->getMissionConst($this->id);
            if ($mo != null) {
                $cur = self::calcStatusCur($mo);
                if ($this->cur < $cur) {
                    $this->cur = $cur;
                }
                if ($this->max > $mo->num) {
                    $this->max = $mo->num;
                }
            }
        }
    }

    static function calcStatusCur($mo) {
        $paras = strsplit($mo->paras);
        $para0 = "";
        $para1 = "";
        $para2 = "";

        if (array_count($paras) >= 1) {
            $para0 = $paras[0];
        }

        if (array_count($paras) >= 2) {
            $para1 = $paras[1];
        }

        if (array_count($paras) >= 3) {
            $para2 = $paras[2];
        }

        switch ($mo->cmd) {
            case MissEventCode::Evn_User_Levelup: // 第一个状态事件
                return usrInfo()->baseInfo()->level;
            case MissEventCode::SEvn_Habitat_Own:
                return MissProc::GetBuildingNum(INIT_HABITID, $para0, $para1);
            case MissEventCode:: SEvn_Farm_Own:
                return MissProc::GetBuildingNum(INIT_Farm, $para0, $para1);
            case MissEventCode:: SEvn_Build_Own:
                return MissProc::GetBuildingNum($para0, $para1, $para2);
            case MissEventCode:: SEvn_Pet:
                return MissProc::GetPetNum($para0, $para1, $para2);
            case MissEventCode:: SEvn_Explore_GridOwn:
                return MissProc::GetExlporeGridNum($para0);
        }
        return 0;
    }

}
