<?php

namespace loyalsoft;

require_once __DIR__ . '/FightProc/SweepGatesProc.php';
require_once __DIR__ . '/FightProc/PVPProc.php';

/**
 * 战斗业务逻辑分发
 * @author gwang
 */
class FightProc {

    /**
     * 战斗逻辑分发
     * 所有的Proc中必须有这样一个方法
     */
    static function procMain() {
        switch (req()->cmd) {
            # 关卡-征战
            case CmdCode::cmd_fight_arenas_preFight:                            # 6800 关卡挑战 - 预先计算掉落
                return SweepGatesProc::Arenas_preFight();
            case CmdCode::cmd_fight_arenasfight:                                # 6801 挑战关卡
                return SweepGatesProc::Arenas_Fight();
            case CmdCode::cmd_fight_arenassweep:                                # [6802] 扫荡
                Err(ErrCode::function_notopen_msg);                             # 暂未开放扫荡功能. -gwang 2020.4.20
                return SweepGatesProc::Arenas_NewSweepFight();

            case CmdCode::cmd_fight_GetArenasActiveReward:                      # 6806 领取章节星级奖励
                return SweepGatesProc::DrawChapterStarsReward();

            // <editor-fold defaultstate="collapsed" desc="    玩家挑战    ">
            case CmdCode::cmd_fight_GetChallengeAdversaryInfo:                  # 6803 获取挑战对手的信息
                return PVPProc::GetChallengeAdversaryInfo();
            case CmdCode::cmd_fight_LogChallengeResult:                         # 6804 记录挑战结果
                return PVPProc::LogChallengeInfo();
            case CmdCode::cmd_fight_GetChallengeLog:                            # 6805 拉取挑战记录
                return PVPProc::GetChagllengeLog();

// </editor-fold>
            //  <editor-fold defaultstate="collapsed" desc="   PVP 模块 681x ">
            case CmdCode::cmd_fight_pvp_maininfo:                               # [6810] 竞技场 主界面拉取信息
                return PVPProc::pvpMainInfo();
            case CmdCode::cmd_fight_pvp_refresh:                                # [6811] 竞技场 刷新对手列表
                return PVPProc::pvp_Refresh();
            case CmdCode::cmd_fight_pvp_pk:                                     # [6812] 竞技场 挑战对手
                return PVPProc::pvp_PK();
            case CmdCode::cmd_fight_pvp_setTeam:                                # [6813] 竞技场 设定防守队伍
                return PVPProc::pvp_setTeam();
            case CmdCode::cmd_fight_pvp_buyTicket:                              # [6814] 竞技场 购买挑战票
                return PVPProc::pvp_buyticket();
            case CmdCode::cmd_fight_pvp_getrank:                                # [6815] 竞技场 查看排行榜
                return PVPProc::pvp_getRank();
            case CmdCode::cmd_fight_pvp_getLog:                                 # [6816] 竞技场 查看挑战记录
                return PVPProc::pvp_getFightLogs();
// </editor-fold>
            // <editor-fold defaultstate="collapsed" desc="   竞技商店 682x  ">
            case CmdCode::cmd_fight_pvpShop_getMainInfo:                        # [6820] 竞技场 竞技商店主界面
                return PVPProc::pvpShopMain();
            case CmdCode::cmd_fight_pvpShop_buy:                                # [6821] 竞技场 竞技商店购买道具
                return PVPProc::pvpShopBuy();
            case CmdCode::cmd_fight_pvpShop_refresh:                            # [6822] 竞技场 竞技商店刷新道具(扣钻)
                return PVPProc::pvpShopRefresh();
            // </editor-fold>

            case CmdCode::fight_worldBoss_enter:                                # 6831 世界BOSS战 - 进入
                return self::WorldBossEnter();
            case CmdCode::fight_worldBoss_refresh_rank :                        # 6834 查询世界boss伤害输出榜
                return self::GetWorldBossRank();
// ---------- 无尽塔相关 -------------------------
            case CmdCode::fight_endlessTower_Get:                               # 6841 查询无尽塔的进度
                return self::EndlessTower_Get();
            case CmdCode::fight_endlessTower_Up:                                # 6842 无尽塔升级
                return self::EndlessTower_Up();
            case CmdCode::fight_endlessTower_drawreward:                        # 6843 无尽塔,领取特殊奖励
                return self::EndlessTower_Drawreward();
            default:
                return Resp::err(ErrCode::cmd_err);
        }
    }

//
// <editor-fold defaultstate="collapsed" desc="  世界boss  ">

    static function WorldBossEnter() {
        list($bossid) = req()->paras;
        $gateId = 506000 + $bossid;
        TaskProc::OnPassWorldBoss($gateId);
        return Resp::ok();
    }

    /**
     * [6834] 查询世界boss伤害输出榜
     */
    static function GetWorldBossRank() {
        $zoneid = req()->zoneid;
        list($bossId) = req()->paras;                                           # 提取参数bossid.
        $key = MemKey_GameRun::Game_WorldBoss_X_Rank($zoneid, $bossId);
        var_dump($key);
        var_dump($bossId);
        var_dump(req()->paras);
        $rank = gMem()->zrange($key, 0, 50, true);
        return Resp::ok(array('rank' => $rank));
    }

    static function SettleWorldBoss($zoneId, $bossid) {
        $key = MemKey_GameRun::Game_WorldBoss_X_Rank($zoneId, $bossid);
        $range = gMem()->zrange($key, 0, -1);                                   # 全部取出(暂时这么写吧)
        $i = 1;
        foreach ($range as $uid_name) {
            $rwd = $bossid == 2 ? self::getWorldbossRankReward2($i) : self::getWorldbossRankReward($i);
            $uid = substr($uid_name, 0, strpos($uid_name, "__"));
            EmailProc::SendWorldBossReward($zoneId, $uid, $i++, $rwd->rankName, $rwd->reward);
        }

        CLog::info("世界Boss($bossid)已结算,未更新结算标记(防止重复发放).");
    }

    /**
     * 按排名查询上榜奖励配置对象
     * @param type $index
     * @return \sm_worldboss_rankreward
     */
    static function getWorldbossRankReward($index) {
        $arr = (array) GameConfig::worldboss_rankreward();
        foreach ($arr as $i => $v) {
            isEditor() and $v = new \sm_worldboss_rankreward();
            if ($index >= $v->minRank && $index <= $v->maxRank) {
                return $v;
            }
        }
        return $arr[max(array_keys($arr))];
    }

    /**
     * 按排名查询上榜奖励配置对象
     * @param type $index
     * @return \sm_worldboss_rankreward2
     */
    static function getWorldbossRankReward2($index) {
        $arr = (array) GameConfig::worldboss_rankreward2();
        foreach ($arr as $i => $v) {
            isEditor() and $v = new \sm_worldboss_rankreward2();
            if ($index >= $v->minRank && $index <= $v->maxRank) {
                return $v;
            }
        }
        return $arr[max(array_keys($arr))];
    }

// </editor-fold>
//
// <editor-fold defaultstate="collapsed" desc=" 无尽塔 ">

    /**
     * [6841] 查询无尽塔的进度
     */
    static function EndlessTower_Get() {
        return Resp::ok(array('index' => ctx()->privateData()->endlessTower));
    }

    /**
     * [6842] 无尽塔升级
     */
    static function EndlessTower_Up() {
        $gateId = 600001;
        list( $index) = req()->paras;
        my_Assert($index == ctx()->privateData()->endlessTower, ErrCode::Fight_endlessTower_index);
        ctx()->privateData()->endlessTower++;                                   # 指向下一关
        // 发放奖励
        $mo = GameConfig::endless_tower_getItem($index);                        # 取配置信息
        my_Assert(isset($mo), ErrCode::err_const_no);                           # 防御
        $rsArr = str_replace(',', ';', $mo->rewardList);                        # 修复下奖励格式, ,=>;
        $rewardStr = str_replace('_', ',', $rsArr);                             # _ => ,
        $err = StoreProc::AddMultiItemInStore($rewardStr);                      # 发放奖励
        my_Assert(ErrCode::ok == $err, $err);                                   # 防御代码
        UserProc::updateUserInfo();                                             # 更新
        TaskProc::OnPassEndlessCarboN($gateId, $index);
        var_dump("无尽塔");
        return Resp::ok(array('index' => ctx()->privateData()->endlessTower,
                    'baseInfo' => ctx()->base(),
                    'store' => ctx()->store()
        ));
    }

    static function EndlessTower_Drawreward() {
        list($index) = req()->paras;
//        my_Assert($index % 5 == 0, ErrCode::paras_err);                         # 领取指定关卡的特殊奖励(逢5/0才有)
        my_Assert(ctx()->privateData()->endlessTower >= $index, "请先完成挑战再来领奖!"); # 防御未通关直接领奖(刷接口)
        my_Assert(!in_array($index, ctx()->privateData()->endlessTower_rewarded), "已经领取!"); # 防御已经领取
        $mo = GameConfig::endless_tower_getItem($index);
        my_Assert(isset($mo), ErrCode::err_const_no);
        my_Assert(strlen($mo->rewardOne) > 0, "该关卡无特殊奖励!");
        $rs = str_replace(',', ';', $mo->rewardOne);                            # 奖励串格式修复,
        $rewardStr = str_replace('_', ',', $rs);
        $err = StoreProc::AddMultiItemInStore($rewardStr);                      # 发放奖励
        my_Assert(ErrCode::ok == $err, $err);                                   # 防御
        ctx()->privateData()->endlessTower_rewarded[] = $index;                 # 更新记录
        UserProc::updateUserInfo();
        return Resp::ok(array(#                                                 # 返回
                    'baseInfo' => ctx()->base(),
                    'store' => ctx()->store()
        ));
    }

// </editor-fold>
//

    /**
     * 清理每日字段
     */
    static function ClearByDay() {
        SweepGatesProc::ClearGateTimes();                                        # 清理每个难度副本的每日战斗次数
        $pvp = new Info_UserPVP(ctx()->pvp);
        $pvp->refreshDailyData();
        $pvp->sendRewardEmail(req()->zoneid, req()->uid, PVPProc::GetCurSeasonID());
        ctx()->pvp = $pvp;
        ctx()->privateState->materialDailyChallengeRecord = new \stdClass(); # 充值材料副本挑战记录
    }

}
