game2006api/webapp/mt/HeroLevelAttr.php
2022-09-08 16:33:46 +08:00

217 lines
6.2 KiB
PHP

<?php
namespace mt;
require_once('mt/StrHelper.php');
require_once('mt/AttrHelper.php');
require_once('mt/Hero.php');
use phpcommon;
class HeroLevelAttr {
private static $BASE_ATTR = [kHAT_Hp,kHAT_Atk,kHAT_Def,kHAT_Critical,kHAT_CriDamage,kHAT_Dodge,kHAT_Ruduce];
public static function get($id)
{
return getXVal(self::getMetaList(), $id);
}
public static function getByLevel($level)
{
self::mustBeLevelHash();
return getXVal(self::$levelHash, $level, null);
}
public static function getByCoefficient($data,$index)
{
self::mustBeCoefficientHash($data);
return getXVal(self::$coefficientHash, $index, null);
}
public static function getCoefficientByLevel($level,$hero_id){
$meta = self::getByLevel($level);
return [
[
'attr_id'=>kHAT_Hp,
'val' => self::_getCoefficient($meta,$hero_id,'hp_rate')
], [
'attr_id'=>kHAT_Atk,
'val' => self::_getCoefficient($meta,$hero_id,'atk_rate')
], [
'attr_id'=>kHAT_Def,
'val' => self::_getCoefficient($meta,$hero_id,'def_rate')
], [
'attr_id'=>kHAT_Critical,
'val' => self::_getCoefficient($meta,$hero_id,'crit_atk_rate')
], [
'attr_id'=>kHAT_CriDamage,
'val' => self::_getCoefficient($meta,$hero_id,'damage_rate')
], [
'attr_id'=>kHAT_Dodge,
'val' => self::_getCoefficient($meta,$hero_id,'miss_rate')
], [
'attr_id'=>kHAT_Ruduce,
'val' => self::_getCoefficient($meta,$hero_id,'ruduce_rate')
],
];
}
private static function _getCoefficient($meta,$hero_id,$title){
if ($meta[$title.'2']){
$item = explode('|',$meta[$title.'2']);
foreach ($item as $val){
$item_1 = explode(':',$val);
if ($item_1[0] == $hero_id){
return $item_1[1];
}
}
return $meta[$title];
}
return $meta[$title];
}
public static function addRandAttrNew($heroDb,$type){
$heroMeta = Hero::get($heroDb['hero_id']);
$newAttrs = array();
if ($heroMeta) {
$meta = self::getByLevel($heroDb['hero_lv']+1);
foreach (self::$BASE_ATTR as $item){
if ($type == 1){
$val = self::getAttrValue($meta);
}else{
$val = self::getAttrValueMin($meta);
}
array_push(
$newAttrs,
[
'attr_id' => $item,
'val' => $val,
]);
}
}
$rand_attr = $attr = emptyReplace(json_decode($heroDb['rand_attr'], true), array());
if (!$rand_attr){
foreach ($newAttrs as $item){
array_push(
$rand_attr,
[
'attr_id' => $item['attr_id'],
'val' => 1+$item['val'],
]);
}
}else{
foreach ($rand_attr as &$item){
foreach ($newAttrs as $v){
if ($item['attr_id'] == $v['attr_id']){
$item['val'] = $item['val']*(1+$v['val']);
}
}
}
}
return $rand_attr;
}
public static function addRandAttr($level, &$dbAttrs)
{
$newAttrs = array();
foreach (self::getMetaList() as $meta) {
if ($meta['level'] == $level) {
if (in_array(
$meta['type'],
array(
kHAT_ABS_VAL,
kHAT_RATE_VAL
)
)) {
$val = self::getAttrValue($meta);
if ($val > 0) {
array_push(
$newAttrs,
array(
'attr_id' => $meta['attr_id'],
'type' => $meta['type'],
'val' => $val,
));
}
}
}
}
AttrHelper::mergeAttr($dbAttrs, $newAttrs);
return true;
}
protected static function getAttrValue($meta)
{
$strs = explode('|', $meta['attr_weight']);
$totalSpace = 0;
foreach ($strs as $tmpStr) {
$strs2 = explode(':', $tmpStr);
if (count($strs2) == 2) {
$totalSpace += $strs2[1];
}
}
if ($totalSpace > 0) {
$randSpace = rand(0, $totalSpace);
$currSpace = 0;
foreach ($strs as $tmpStr) {
$strs2 = explode(':', $tmpStr);
if (count($strs2) == 2) {
$currSpace += $strs2[1];
}
if ($randSpace <= $currSpace) {
return $strs2[0];
}
}
}
return 0;
}
protected static function getAttrValueMin($meta)
{
$strs = explode('|', $meta['attr_weight']);
foreach ($strs as $tmpStr) {
$strs2 = explode(':', $tmpStr);
return $strs2[0];
}
}
protected static function getMetaList()
{
if (!self::$metaList) {
self::$metaList = getMetaTable('heroLevelAttr@heroLevelAttr.php');
}
return self::$metaList;
}
protected static function mustBeLevelHash()
{
if (!self::$levelHash) {
self::$levelHash = array();
foreach (self::getMetaList() as $meta) {
self::$levelHash[$meta['level']] = $meta;
}
}
}
protected static function mustBeCoefficientHash($list)
{
if (!self::$coefficientHash) {
self::$coefficientHash = array();
foreach ($list as $val) {
self::$coefficientHash[$val['attr_id']] = $val;
}
}
}
protected static $metaList;
protected static $levelHash;
protected static $coefficientHash;
}