<?php

namespace loyalsoft;

/**
 * Description of MapProc
 * 地图数据处理逻辑
 * @author gwang
 */
class MapProc {

    /**
     * 地图处理逻辑分发
     * 所有的Proc中必须有这样一个方法
     */
    static function procMain() {
        switch (req()->cmd) {
            case CmdCode::map_EnterFootHold:                                    #  [7501] 进入据点
                return self::EnterFootHold();
            case CmdCode::map_FixUpTransmission:                                #  [7502] 开启传送阵
                return self::FixUpTransmision();
            case CmdCode::map_UpdateExplorerationProgress:                      #  [7503] 更新探索进度
                return self::UpdateExplorationProgress();
            case CmdCode::map_UnlockMap:                                        #  [7504] 解锁据点
                return self::UnlockMap();
//            case CmdCode::map_getUnlockInfoList:                              #  [7505] 获取mapid下已经解锁的列表
//                return self::getUnlockInfoList();
            case CmdCode::map_unlockInfoSva:                                    #  [7505] 保存mapid下已经解锁的
                return self::unlockInfoSva();
            case CmdCode::map_reviceExplorerReward:                             #  [7506] 领取探索奖励
                return self::reviceExplorerReward();
            default:                                                            #  err: 未知的命令码
                return Resp::err(ErrCode::cmd_err);
        }
    }

    /**
     * 
     * @return Resp
     */
    public static function reviceExplorerReward() {
        $mapid = req()->paras[0];                                               # 提取参数		                                       
        $newMap = ctx()->newMap();

        $newMap->unlockedFootholds->$mapid->exploreRewardGeted = true;

        $cost = GameConfig::map_scene_getItem($mapid)->exploreReward;
        StoreProc::AddMultiItemInStore($cost);
        ctx()->newMap = $newMap;

        UserProc::updateUserInfo();
        return Resp::ok(
                array("newMap"=>$newMap,"cost"=>$cost,)
                );
    }

    /**
     * 7505
     * @return Resp
     */
    public static function unlockInfoSva() {
        $unlockStr = req()->paras[0];                                           # 提取参数		                                       
        $newMap = ctx()->newMap();
        if (!StlUtil::dictHasProperty($newMap, 'unlockMapTypeList')) {
            $newMap->unlockMapTypeList = new \stdClass();
        }

        $mapid = explode('_', $unlockStr)[0];        
        $type = explode('_', $unlockStr)[1];
        $typeid = explode('_', $unlockStr)[2];
        if (!StlUtil::dictHasProperty($newMap->unlockMapTypeList, $mapid)) {
            $newMap->unlockMapTypeList->$mapid = array();            
        }
        $tag = 1;
        $tylist = $newMap->unlockMapTypeList->$mapid;
        foreach ($tylist as $str) {
            $slist = explode('_', $str);
            if($slist[0] == $mapid && $slist[1] == $type && $slist[2]==$typeid){
                $tag = 0;
                break;
            }
        }
        
        if($tag){
            $newMap->unlockMapTypeList->$mapid[] = $unlockStr;
        
            my_Assert(StlUtil::dictHasProperty($newMap->unlockedFootholds,$mapid), ErrCode::map_Unlocked);
      
            $explorerNum = self::countFootHoldExplorerNum($mapid, $newMap);         
            $newMap->unlockedFootholds->$mapid->curExploreProgress = $explorerNum;

            //$type = explode('_', $unlockStr)[1];
            if ($type == 6 && $newMap->unlockedFootholds->$mapid->transmissionIsOk == false) {//临时代码，6代表传送带
                $newMap->unlockedFootholds->$mapid->transmissionIsOk = true;
            }
        }
                    
        ctx()->newMap = $newMap;
        UserProc::updateUserInfo();
        return Resp::ok(array("newMap"=>$newMap,"tag"=>$tag,));
    }

    /*
     * 计算探索度值
     */

    public static function countFootHoldExplorerNum($mapid, $newMap) {
        $explorer = 0;       
        if (StlUtil::dictHasProperty($newMap->unlockMapTypeList, $mapid)) {
            $list = $newMap->unlockMapTypeList->$mapid;
            $mapList = GameConfig::map_explorer_getItemArray($mapid);   
            
            $tempList = array();
            if ($mapList != null) {
                foreach ($mapList as $item) {
                    if($item->paras == null){
                        continue;
                    }
                    $cmd = $item->cmd;
                    $parasList = explode(',', $item->paras);                        
                    foreach ($list as $para) {                       
                        $strList = explode('_', $para);
                        $tempStr = $strList[1].'-'.$strList[2];
                        if(in_array($tempStr, $tempList)){
                            continue;
                        }
                                          
                        if ($strList[1] == $cmd && $strList[2] == $parasList[0]) {
                            $tempList[] = $tempStr;
                            $explorer += $parasList[1];
                        }
                    }
                }
            }
        }     
        
        return $explorer;
    }

    /**
     * 7504 解锁据点
     * @return Resp 
     */
    public static function UnlockMap() {
        $targetMapId = req()->paras[0];                                          # 提取参数
        $newMap = ctx()->newMap();
        # 检查目标地图是否已经解锁
        my_Assert(!isset($newMap->unlockedFootholds->$targetMapId), ErrCode::map_Unlocked);
        $mo = GameConfig::map_scene_getItem($targetMapId);
        $footHold = new Ins_FootHold();
        $footHold->mapId = $mo->mapId;
        $footHold->curMapType = $mo->mapType;
        $this->unlockedFootholds->$targetMapId = $footHold;                     #  添加解锁据点数据
        ctx()->newMap = $newMap;
        UserProc::updateUserInfo();
        return Resp::ok($newMap);
    }

    /**
     * 7501 进入据点
     * @return Resp 
     */
    public static function EnterFootHold() {
        $targetMapId = req()->paras[0];                                          # 提取参数
        $newMap = ctx()->newMap();
        # 检查目标地图是否已经解锁
        my_Assert(isset($newMap->unlockedFootholds->$targetMapId), ErrCode::map_NotUnlocked);
        $newMap->curMapId = $targetMapId;
        ctx()->newMap = $newMap;
        UserProc::updateUserInfo();
        return Resp::ok();
    }

    /**
     * 7502 开启传送阵
     *  @return Resp 
     */
    public static function FixUpTransmision() {
        
    }

    /**
     * 7503 更新探索进度
     *  @return Resp 
     */
    public static function UpdateExplorationProgress() {
        
    }

}
