<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace loyalsoft;

/**
 * 【四舍五入】
 * 战力 = (基础攻击力 * 攻击力系数 + 基础生命值 * 生命值系数 + （1+暴击率） * （1+暴击伤害） * 攻击力系数 + 肉食恢复加成 * （1+肉食恢复效果）*生命值系数+伤害减免值 * （1+伤害减免效果）* 生命值系数)*(1+装备系数+角色系数)
 */
class FightPower_Calculate {

    public static function GetFightPower() {
        $file = __NAMESPACE__ . '\\' . 'E_PredicateActionType';
        $reflectionClass = new \ReflectionClass($file);
        $constants = $reflectionClass->getConstants();

        $dic = new \stdClass();
        foreach ($constants as $name => $predicateActionType) {
            $dic->$predicateActionType = 0;
        }

        $CurrentHeroId = ctx()->heros->CurrentHeroId;
        $mo = GameConfig::hero_getItem($CurrentHeroId);
        my_Assert($mo != null, ErrCode::err_const_no);

//【1.当前英雄表里的基础攻击 2.装备品质解锁的攻击力 3.装备里镶嵌的宝石攻击力 4.组成套装的攻击力5.启灵攻击力 6.英雄自带词条】 7.装备等级带来的攻击力/生命
        //英雄带的                       
        $hero_special_predicate = explode(',', $mo->special_predicate);
        foreach ($hero_special_predicate as $hero_predicateId) {
            self::predicateActionParamVal($hero_predicateId, $dic);
        }

        //装备
        $attck = 0;
        $hp = 0;
        $equipDic = ctx()->store->equipLocation;

        $suitDic = new \stdClass();
        $equipParam = 0;
        foreach ($equipDic as $index => $equipUid) {
            //等级
            $ins_Equip = new Ins_Equip(ctx()->store->equip->$equipUid);
            $equipParam += GameConfig::equip_power_getItem($ins_Equip->mo()->rarity, $ins_Equip->qual, $ins_Equip->mo()->position)->power;
            if ($ins_Equip->mo()->position % 2 == 1) {              
                $attck += GameConfig::equip_levelupgrade_getItem($ins_Equip->mo()->rarity, $ins_Equip->qual, $ins_Equip->mo()->position, $ins_Equip->level)->attckNum;            
            } elseif ($ins_Equip->mo()->position % 2 == 0) {
                $hp += GameConfig::equip_levelupgrade_getItem($ins_Equip->mo()->rarity, $ins_Equip->qual, $ins_Equip->mo()->position, $ins_Equip->level)->hpNum;
            }
            //品阶解锁
            $str = explode(';', $ins_Equip->mo()->qual_predicateId);
            foreach ($str as $val) {
                $str2 = explode(',', $val);
                if ($ins_Equip->qual >= $str2[0]) {
                    self::predicateActionParamVal($str2[1], $dic);
                }
            }

            //套装
            $suitId = $ins_Equip->mo()->suitId;
            if (StlUtil::dictHasProperty($suitDic, $suitId)) {
                $suitDic->$suitId += 1;
            } else {
                $suitDic->$suitId = 1;
            }

            //宝石
            if ($ins_Equip->qual >= 4 && $ins_Equip->gemSetSlot != null) {
                foreach ($ins_Equip->gemSetSlot as $index => $gemUid) {
                    if ($gemUid != 0) {
                        $gem_predicateId = ctx()->store->gemStore->$gemUid->predicateId;
                        self::predicateActionParamVal($gem_predicateId, $dic);
                    }
                }
            }
        }

        foreach ($suitDic as $suitId => $num) {
            $suitMo = GameConfig::equip_suit_getItem($suitId);
            my_Assert($suitMo != null, ErrCode::err_const_no);
            if ($num >= $suitMo->num) {
                self::predicateActionParamVal($suitMo->effect, $dic);
            }
        }

        $arr = GameConfig::evolve();
        $evolveMaxId_left = ctx()->gates->evolveMaxId_left;
        $evolveMaxId_right = ctx()->gates->evolveMaxId_right;
        foreach ($arr as $key => $value) {
            isEditor() and $value = new \sm_evolve();
            if ($key <= $evolveMaxId_left) {
                self::predicateActionParamVal($value->predicateId, $dic);
                //continue;             
            }

            if ($value->specificEvolveId > 0 && $value->specificEvolveId <= $evolveMaxId_right) {
                self::predicateActionParamVal($value->specificPredicateId, $dic);
                //continue;
            }
        }

        //战力 = (
        //          基础攻击力 * 攻击力系数 
        //          + 基础生命值 * 生命值系数
        //          + （1 + 暴击率） * （1 + 暴击伤害） * 攻击力系数
        //          + 肉食恢复加成 * （1 + 肉食恢复效果）* 生命值系数
        //          + 伤害减免值 * （1 + 伤害减免效果）* 生命值系数
        //         ) * (1 + 装备系数 + 角色系数)
        $attackParas = glc()->fightPower_MulAtk;
        $hpParas = glc()->fightPower_MulHp;
        $fightPower_bitHitParas = GameConfig::glc2()->fightPower_bitHitParas;

//        $addDamage_ActionKey = E_PredicateActionType::addDamage;
//        $dic->$addDamage_ActionKey += $mo->attack;
//        $dic->$addDamage_ActionKey += $attck;
        
        $addAtk_ActionKey = E_PredicateActionType::addAtk;
        $addAtk = $dic->$addAtk_ActionKey;
                
        $base_addDamage = $addAtk + $mo->attack +$attck;                           // 基础攻击


        $addHp_ActionKey = E_PredicateActionType::addHp;
        $dic->$addHp_ActionKey += $mo->hp;
        $dic->$addHp_ActionKey += $hp;
        $base_addHp = $dic->$addHp_ActionKey;                                   // 基础生命值


        $addBigHitRate_ActionKey = E_PredicateActionType::addBigHitRate;
        $dic->$addBigHitRate_ActionKey += $mo->bigHit_rate;
        $baojilv = $dic->$addBigHitRate_ActionKey;                              // 暴击率

        $addBigHitVal_ActionKey = E_PredicateActionType::addBigHitVal;
        $dic->$addBigHitVal_ActionKey += $mo->bigHit_Val;
        $baojishanghai = $dic->$addBigHitVal_ActionKey;                         // 暴击伤害


        $mulDamage_ActionKey = E_PredicateActionType::mulDamage;
        $mulDamageVal = $dic->$mulDamage_ActionKey;                             // 攻击里加成百分比

        $mulAtk_ActionKey = E_PredicateActionType::mulAtk;
        $mulAtk = $dic->$mulAtk_ActionKey;
        
        $mulHp_ActionKey = E_PredicateActionType::mulHp;
        $mulHpVal = $dic->$mulHp_ActionKey;                                     // 生命额外加成

        $addEatFood_ActionKey = E_PredicateActionType::addEatFood;
        $roushihuifu = $dic->$addEatFood_ActionKey;
        $mulEatFood_ActionKey = E_PredicateActionType::mulEatFood;
        $roushihuifu_xiaoguo = $dic->$mulEatFood_ActionKey;
        $addDecDamage_ActionKey = E_PredicateActionType::addDecDamage;
        $shanghaijianmian = $dic->$addDecDamage_ActionKey;
        $mulDecDamage_ActionKey = E_PredicateActionType::mulDecDamage;
        $shanghaijianmian_xiaoguo = $dic->$mulDecDamage_ActionKey;

        $mulEquipBasicProps_damage_ActionKey = E_PredicateActionType::mulEquipBasicProps_damage;
        $mulEquipBasicProps_damage_val = $dic->$mulEquipBasicProps_damage_ActionKey;
        $mulEquipBasicProps_hp_ActionKey = E_PredicateActionType::mulEquipBasicProps_hp;
        $mulEquipBasicProps_hp_val = $dic->$mulEquipBasicProps_hp_ActionKey;

        $gongji = round($base_addDamage * (1 + $mulDamageVal+$mulAtk + $mulEquipBasicProps_damage_val), 0);
        $shengming = round($base_addHp * (1 + $mulHpVal + $mulEquipBasicProps_hp_val), 0);

        $fightPower = (
                $gongji * $attackParas                                          // 基础攻击力 * 攻击力系数 
                + $shengming * $hpParas                                         // 基础生命值 * 生命值系数
                + (1 + $baojilv) * (1 + $baojishanghai) * $attackParas * $fightPower_bitHitParas // （1 + 暴击率） * （1 + 暴击伤害） * 攻击力系数
                + $roushihuifu * (1 + $roushihuifu_xiaoguo) * $hpParas          // 肉食恢复加成 * （1 + 肉食恢复效果）* 生命值系数
                + $shanghaijianmian * (1 + $shanghaijianmian_xiaoguo) * $hpParas // 伤害减免值 * （1 + 伤害减免效果）* 生命值系数
                ) * (1 + $equipParam + $mo->mulFightPower);                     // (1 + 装备系数 + 角色系数)
        //
        
//        var_dump('---基础攻击力-------------------'.$gongji);
//        var_dump('---攻击力系数-------------------'.$attackParas);
//        var_dump('---基础生命值-------------------'.$shengming);
//        var_dump('---生命值系数-------------------'.$hpParas);
//        
//        var_dump('---暴击率-------------------'.$baojilv);
//        var_dump('---暴击伤害-------------------'.$baojishanghai);
//        var_dump('---暴击系数-------------------'.$fightPower_bitHitParas);
//        var_dump('---肉食恢复加成-----------------'.$roushihuifu);
//        var_dump('---肉食恢复效果-----------------'.$roushihuifu_xiaoguo);
//        
//        var_dump('---伤害减免值-----------------'.$shanghaijianmian);
//        var_dump('---伤害减免效果-----------------'.$shanghaijianmian_xiaoguo);
//        var_dump('---装备系数-----------------'.$equipParam);
//        var_dump('---角色系数-----------------'.$mo->mulFightPower);
//        
//        var_dump('---战力-----------------'.$fightPower);
//        
//        var_dump('---base_addDamage攻击-------------------'.$base_addDamage);
//        var_dump('攻击里加成百分比---------------'.$mulDamageVal);
//        var_dump('mulEquipBasicProps_damage_val----'.$mulEquipBasicProps_damage_val);                     
    
               
        return round($fightPower, 0);
    }

    /**
     * 获取词条对应的参数值
     * @param type $predicateId
     */
    public static function predicateActionParamVal($predicateId, &$dic) {
        $predicateMo = GameConfig::predicate_getItem($predicateId);
        my_Assert(null != $predicateMo, ErrCode::err_const_no);
        switch ($predicateMo->actionType) {
            case E_PredicateActionType::mulEquipBasicProps://所有装备基础属性+5%---基础攻击力/生命 攻击力取actionParam1 生命actionParam2            
                if ($predicateMo->actionParam1 != null) {
                    $actionKey = E_PredicateActionType::mulEquipBasicProps_damage;
                    $actionVal = $predicateMo->actionParam1;
                    $dic->$actionKey += $actionVal;
                }

                if ($predicateMo->actionParam2 != null) {
                    $actionKey = E_PredicateActionType::mulEquipBasicProps_hp;
                    $actionVal = $predicateMo->actionParam2;
                    $dic->$actionKey += $actionVal;
                }

                break;
            case E_PredicateActionType::addBigHitRateAndVal://增加X%暴击率且增加Y%暴击伤害 actionParam1 X  actionParam1 Y
                $actionKey1 = E_PredicateActionType::addBigHitRate;
                $actionKey2 = E_PredicateActionType::addBigHitVal;
                $actionVal1 = $predicateMo->actionParam1;
                $actionVal2 = $predicateMo->actionParam2;

                $dic->$actionKey1 += $actionVal1;
                $dic->$actionKey2 += $actionVal2;
                break;
            default:
                $actionKey = $predicateMo->actionType;
                $actionVal = $predicateMo->actionParam1;

                if (StlUtil::dictHasProperty($dic, $actionKey)) {
                    $dic->$actionKey += $actionVal;
                }
                break;
        }
    }
}
