<?php

namespace loyalsoft;

/**
 * Description of TaskCardVo
 * 任务卡
 * @author gwang
 */
class Ins_TaskCard extends Object_ext {

    /**
     * @var type 这个物品在玩家身上唯一的id
     */
    public $uid = 0;

    /**
     * @var type 当前已完成步骤数量
     */
    public $count = 0;

    /**
     * @var type 查询用的id
     */
    public $typeId = 0;

    /**
     * @var int 任务状态未完成/已完成:0/1
     */
    public $state = 2;

    /**
     * @var array[]当前任务步骤数据 
     */
    public $curSteps;

    /**
     * 
     * @return type
     */
    public function mo() {
        $mo = GameConfig::taskcard_getItem($this->typeId);
        my_Assert(null != $mo, ErrCode::err_const_no);
        return $mo;
    }

    public function __construct($args = 0) {
        if (isInt($args)) {
            $this->typeId = $args;
            $arr = array();
            $cfg = GameConfig::taskcard_getItem($this->typeId);
            my_Assert(null != $cfg, ErrCode::err_const_no);
            foreach (explode(',', $cfg->tasksteps) as $taskStepTypeId) {
                $arr [] = new Ins_TaskStep($taskStepTypeId);
            }
            $this->curSteps = $arr;
        } else {
            parent::__construct($args);
        }
    }

    /**
     * @return boolean 分析任务卡是否已经完成所有步骤
     */
    public function IsFinish() {
        if ($this->state >= Enum_TaskCardStateType::finish) {                   # 已经完成
            return true;
        }
        $isFinish = true;
        foreach ($this->curSteps as &$tsp) {                                    # 初期里面只有一个任务
            $tsp = new Ins_TaskStep($tsp);
            if (!$tsp->isFinish()) {
                $isFinish = false;
                break;
            }
        }
        return $isFinish;
    }

    public function AddStep($stpid) {
        $exists = false;
        foreach ($this->curSteps as $stp) {
//            $stp = new Ins_TaskStep($stp);
            if ($stp->typeId == $stpid) {
                $exists = true;
            }
        }
        if (!$exists) {
            $newstp = new Ins_TaskStep($stpid);
            $this->curSteps[] = $newstp;
//            $newstp->doStartAct();
        }
    }

}
