<?php

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace loyalsoft;

/**
 * Description of HeroProc
 *
 * @author gwang
 */
class HeroProc {
    //put your code here

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法
     * @param Req $req
     */
    public static function procMain($req) {
        switch ($req->cmd) {
            case CmdCode::hero_swith:                                           # 6601 切换英雄
                return HeroProc::SwithHero();
            case CmdCode::hero_unlock:                                          # 6602 解锁英雄
                return self::UnlockHero();
            case CmdCode::hero_strengthenStar:                                  # 6603 角色升星
                return self::HeroStrengthenStar();
            default:
                Err(ErrCode::cmd_err);
        }
    }
    
    /**
     * 6603 角色升星
     * @return type
     */
    private static function HeroStrengthenStar(){
        list($heroId) = req()->paras;             
        
        $mo = GameConfig::hero_getItem($heroId);
        my_Assert($mo != null, ErrCode::err_const_no);
        
        my_Assert(StlUtil::dictHasProperty(ctx()->heros->Dic, $heroId), "找不到英雄配置数据!");
        my_Assert(ctx()->heros->Dic->$heroId->isUnlock == 1, "英雄未解锁!");
        
        $nextMo = null;       
        $typeList = GameConfig::heroType_typeId_getItemArray($mo->typeID);
        foreach ($typeList as $item) {
            if($item->starLv == $mo->starLv +1){
                $nextMo = $item;
                break;
            }
                
        }
        
        my_Assert($nextMo != null, "没有下一星级配置信息");
        
        $heroDebrisList = explode(',', $nextMo->heroDebris_need);
        $itemId = $heroDebrisList[0];
        $itemNum = $heroDebrisList[1];
        
        my_Assert(StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $itemNum, ErrCode::notenough_item);      
        ctx()->store->removeItem($itemId, $itemNum);
        
        $nextHeroId = $nextMo->id;
        
        $ins_hero = new Ins_Hero(ctx()->heros->Dic->$heroId);
        $ins_hero->Id = $nextHeroId;                     
        ctx()->heros->Dic->$nextHeroId = $ins_hero;
        
        StlUtil::dictRemove(ctx()->heros->Dic, $heroId);
                
        UserProc::updateUserInfo();
        return Resp::ok(array(                   
                    'store' => ctx()->store,
                    'heros'=>ctx()->heros,
        ));
    }
    
    /**
     * 购买角色
     * @return type
     */
    private static function BuyHero(){
        list($heroId) = req()->paras;                                        # 切换英雄id
        my_Assert(GameConfig::hero_getItem($heroId) != null, "找不到英雄配置数据!");
       
        ctx()->heros->Dic->$heroId->isUnlock = 1;

        return Resp::ok();
    }

    /**
     * 6602 解锁英雄
     * @return type
     */
    private static function UnlockHero() {
        list($heroId) = req()->paras;             
        
        $mo = GameConfig::hero_getItem($heroId);
        my_Assert($mo != null, ErrCode::err_const_no);
        
        my_Assert(StlUtil::dictHasProperty(ctx()->heros->Dic, $heroId), "找不到英雄配置数据!");
        my_Assert(ctx()->heros->Dic->$heroId->isUnlock == 0, "英雄已经解锁!");
        
        //$heroDebris_need = $mo->heroDebris_need;
        $heroDebrisList = explode(',', $mo->heroDebris_need);
        $itemId = $heroDebrisList[0];
        $itemNum = $heroDebrisList[1];
        
        my_Assert(StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $itemNum, ErrCode::notenough_item);
        
        ctx()->store->removeItem($itemId, $itemNum);
        
        ctx()->heros->Dic->$heroId->isUnlock = 1;//解锁
                           
        UserProc::updateUserInfo();
        return Resp::ok(array(                   
                    'store' => ctx()->store,
                    'heros'=>ctx()->heros,
        ));
       
    }

    /**
     * 6601 切换英雄
     */
    private static function SwithHero() {
        list($newHeroId) = req()->paras;                                        # 切换英雄id
        my_Assert(CommUtil::isPropertyExists(ctx()->heros->Dic, $newHeroId), "尚未获得此英雄!");
        ctx()->heros->CurrentHeroId = $newHeroId;
        return Resp::ok();
    }
}
