game2006api/webapp/mt/ChipAttr.php
2022-08-18 16:31:33 +08:00

152 lines
4.6 KiB
PHP

<?php
namespace mt;
require_once('mt/AttrHelper.php');
require_once('mt/StrHelper.php');
require_once('mt/Item.php');
use phpcommon;
class ChipAttr {
const ROLE_CHIP_SUBTYPE = 1;
const GUN_CHIP_SUBTYPE = 2;
protected static $metaList;
protected static $attrPoolList;
protected static function getMetaList()
{
if (!self::$metaList) {
self::$metaList = getMetaTable('chip@chip.php');
}
return self::$metaList;
}
protected static function getAttrPoolList()
{
if (!self::$attrPoolList) {
self::$attrPoolList = getMetaTable('chipAttrPool@chipAttrPool.php');
}
return self::$attrPoolList;
}
public static function getAttrPool($attr){
$attr_pool = array();
foreach (self::getAttrPoolList() as $item){
if ($item['attr_id'] == $attr['attr_id'] && $item['attr_type'] == $attr['chip_type'] ){
$attr_pool = $item;
}
}
if ($attr_pool){
$attr_pool['weight'] = explode('|',$attr_pool['weight']);
foreach ($attr_pool['weight'] as $key =>$item){
$attr_pool['weight'][$key] = explode(':',$item);
}
$weight = 0;
$tempData = array ();
foreach ($attr_pool['weight'] as $one) {
$weight += $one[1];
for ($i = 0; $i < $one[1]; $i++) {
$tempData[] = $one;
}
}
$k = rand(0, $weight -1);
$attr_pool['attr_pool_number'] = $tempData[$k][0];
unset($attr_pool['weight']);
}
return $attr_pool;
}
public static function generateAttrRandom($itemMeta,$chip_grade){
if ($itemMeta['sub_type'] == self::ROLE_CHIP_SUBTYPE){
$chip = self::getNodeChip();
return self::_randomNum($itemMeta,$chip_grade,$chip);
}
if ($itemMeta['sub_type'] == self::GUN_CHIP_SUBTYPE){
$chip = self::getGunChip();
return self::_randomNum($itemMeta,$chip_grade,$chip);
}
return array();
}
public static function getAttrById($attr_id,$attr_type){
if($attr_type == self::ROLE_CHIP_SUBTYPE){
$chip = self::getNodeChip();
}
if($attr_type == self::GUN_CHIP_SUBTYPE){
$chip = self::getGunChip();
}
foreach ($chip as $item) {
if ($item['attr_id'] == $attr_id && $item['chip_type'] == $attr_type){
return $item;
}
}
}
public static function generateOneAttr($type){
// $itemMeta = Item::get($item_id);
// return self::getAttrPool(self::getAttrByItemId($itemMeta['id']));
// $attr_pool = self::getAttrPool(self::getAttrByItemId($itemMeta['id']));
// $attr = emptyReplace(json_decode($chip['rand_attr'], true), array());
// array_push($attr,$attr_pool);
// return $attr;
if ($type == self::ROLE_CHIP_SUBTYPE){
$chip = self::getNodeChip();
$attr = $chip[ array_rand($chip)];
return self::getAttrPool($attr);
}
if ($type == self::GUN_CHIP_SUBTYPE){
$chip = self::getGunChip();
$attr = $chip[ array_rand($chip)];
return self::getAttrPool($attr);
}
return array();
}
private static function _randomNum($itemMeta,$chip_grade,$chipList){
$attr_pool = array();
array_push($attr_pool,self::getAttrPool(self::getAttrByItemId($itemMeta['id'])));
if ($chip_grade>=3&&$chip_grade<5){
$num = 1;
}
if ($chip_grade>=5){
$num = 2;
}
for ($i=1;$i<=$num;$i++){
$attr = $chipList[ array_rand($chipList)];
array_push($attr_pool,self::getAttrPool($attr));
}
return $attr_pool ;
}
private static function getNodeChip(){
$node_chip = array();
foreach (self::getMetaList() as $item){
if ($item['chip_type'] == self::ROLE_CHIP_SUBTYPE){
array_push($node_chip,$item);
}
}
return $node_chip;
}
private static function getGunChip(){
$gun_chip = array();
foreach (self::getMetaList() as $item){
if ($item['chip_type'] == self::GUN_CHIP_SUBTYPE){
array_push($gun_chip,$item);
}
}
return $gun_chip;
}
private static function getAttrByItemId($item_id){
foreach (self::getMetaList() as $item){
if ($item['item_id'] == $item_id){
return $item;
}
}
}
}