game2006api/webapp/mt/AttrHelper.php
aozhiwei b9430e4d5a 1
2024-01-23 17:31:45 +08:00

138 lines
3.7 KiB
PHP

<?php
namespace mt;
use phpcommon;
class AttrHelper {
public static function mergeAttr(&$tarAttrs, $srcAttrs)
{
foreach ($srcAttrs as $srcAttr) {
$found = false;
foreach ($tarAttrs as &$tarAttr) {
if ($tarAttr['attr_id'] == $srcAttr['attr_id'] &&
$tarAttr['type'] == $srcAttr['type']) {
$tarAttr['val'] += $srcAttr['val'];
$found = true;
break;
}
}//end for tarAttrs
if (!$found) {
array_push($tarAttrs, $srcAttr);
}
}
}
public static function addRandAttrs($cfgAttrs, $num, $baseAttrs, &$dbAttrs)
{
$matchedAttrs = array();
foreach ($cfgAttrs as $cfgAttr) {
$attrId = $cfgAttr[0];
$type = $cfgAttr[1];
if (array_find($dbAttrs, function($val) use ( $attrId, $type) {
return $val['attr_id'] == $attrId && $val['type'] == $type;
})) {
array_push($matchedAttrs, $cfgAttr);
} else if (array_find($baseAttrs, function($val) use ($attrId, $type) {
return $val['attr_id'] == $attrId && $val['type'] == $type;
})) {
array_push($matchedAttrs, $cfgAttr);
}
}
shuffle($matchedAttrs);
for ($i = 0; $i < $num; ++$i) {
if ($i < $matchedAttrs) {
$cfgAttr = $matchedAttrs[$i];
$attrId = $cfgAttr[0];
$type = $cfgAttr[1];
$val = rand($cfgAttr[2], $cfgAttr[3]);
$found = false;
foreach ($dbAttrs as &$attr) {
if ($attr['attr_id'] == $attrId &&
$attr['type'] == $type) {
$found = true;
}
}
if ($found) {
array_push($dbAttrs, array(
'attr_id' => $attrId,
'type' => $type,
'val' => $val
));
}
}
}
}
public static function getAbsVal($randAttr, $attrId)
{
if (!$randAttr) {
return null;
}
foreach ($randAttr as $attr){
if ($attr['attr_id'] == $attrId &&
$attr['type'] == kHAT_ABS_VAL) {
return $attr;
}
}
return null;
}
public static function getRateVal($randAttr, $attrId)
{
if (!$randAttr) {
return null;
}
foreach ($randAttr as $attr){
if ($attr['attr_id'] == $attrId &&
$attr['type'] == kHAT_RATE_VAL) {
return $attr;
}
}
return null;
}
public static function getRateValEx($randAttr, $attrId, $defVal = 0)
{
if (!$randAttr) {
return $defVal;
}
foreach ($randAttr as $attr){
if ($attr['attr_id'] == $attrId &&
$attr['type'] == kHAT_RATE_VAL) {
return $attr['val'] / 100.0;
}
}
return $defVal;
}
public static function getAttrVal($randAttr, $attrId, $defVal)
{
if (!$randAttr) {
return $defVal;
}
foreach ($randAttr as $attr){
if ($attr['attr_id'] == $attrId) {
return $attr['val'];
}
}
return $defVal;
}
public static function hasAttr($attr, $attrId)
{
if (!$randAttr) {
return false;
}
foreach ($randAttr as $attr){
if ($attr['attr_id'] == $attrId) {
return true;
}
}
return false;
}
}