<?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();
            case CmdCode::hero_resetUnlockHero:                                 # 6604 角色卡解锁英雄
                return self::ResetUnlockHero();    
            default:
                Err(ErrCode::cmd_err);
        }
    }
    
    private static function ResetUnlockHero() {
        list($heroId) = req()->paras;
        
        if(in_array($heroId, ctx()->heros->roleCardUnlockHeroList)){
            StlUtil::arrayRemove(ctx()->heros->roleCardUnlockHeroList, $heroId);
        }
              
        UserProc::updateUserInfo();
        return Resp::ok(array());
        
    }

    /**
     * 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;
        if (ctx()->heros->CurrentHeroId == $heroId) {
            ctx()->heros->CurrentHeroId = $nextHeroId;
        }
        StlUtil::dictRemove(ctx()->heros->Dic, $heroId);

        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'store' => ctx()->store,
                    'heros' => ctx()->heros,
                    'newHeroId' => $nextHeroId,
        ));
    }

    /**
     * 购买角色
     * @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);

        self::UnlockNewHero($heroId);

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'store' => ctx()->store,
                    'heros' => ctx()->heros,
                    'skillUnlockRecord' => ctx()->privateState->skillUnlockRecord,
        ));
    }

    public static function UnlockNewHero($heroId) {
        FightProc::skillUnlock_heroUnlock($heroId); //这个接口位置不能动

        ctx()->heros->Dic->$heroId->isUnlock = 1; //解锁
        ctx()->heros->Dic->$heroId->isNewHeadImgTip = 1;
    }

    public static function RoleCardUnlockHero($itemId, $num) {
        $heroDic = GameConfig::hero();
        $heroTypeId = 0;

        $tMo = null;
        foreach ($heroDic as $mo) {
            if ($itemId == $mo->roleCard) {
                $heroTypeId = $mo->typeID;
                $tMo = $mo;
                break;
            }
        }
        $tag = false;
        if ($heroTypeId != 0) {
            $dic = ctx()->heros->Dic;
            foreach ($dic as $heroItem) {
                $hero = GameConfig::hero_getItem($heroItem->Id);
                if ($hero->typeID == $heroTypeId && $heroItem->isUnlock == 1) {
                    $tag = true;
                    break;
                }
            }

            if ($tag) {//英雄已经解锁，则转为碎片
                for ($i = 0; $i < $num; $i++) {
                    if ($tMo->heroDebris_need != null) {
                        StoreProc::AddMultiItemInStore($tMo->heroDebris_need);
                    }
                }
            } else {//解锁英雄
                for ($i = 0; $i < $num; $i++) {
                    if ($i == 0) {
                        HeroProc::UnlockNewHero($tMo->id);
                        ctx()->heros->roleCardUnlockHeroList[] = $tMo->id;
                        
                        StoreProc::$reward[] = $itemId.',1'; 
                        //StoreProc::$reward_hero[] = $tMo->id;
                    } else {
                        StoreProc::AddMultiItemInStore($tMo->heroDebris_need);
                    }
                }
            }
        }
        
        UserProc::updateUserInfo();
    }

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

}
