game2006api/webapp/controller/ChipController.class.php
hujiabin eaed9c83ef 1
2022-10-27 16:25:03 +08:00

858 lines
30 KiB
PHP

<?php
require_once('models/Chip.php');
require_once('models/Hero.php');
require_once('models/Gun.php');
require_once('models/User.php');
require_once('mt/ChipAttr.php');
require_once('services/FormulaService.php');
require_once('services/PropertyChgService.php');
require_once('services/LogService.php');
use models\Chip;
use models\Hero;
use models\Gun;
use models\User;
use services\FormulaService;
use services\LogService;
use phpcommon\SqlHelper;
class ChipController extends BaseAuthedController
{
public function chipList()
{
$type = getReqVal('type','');
if (! $type){
$this->_rspErr(1,'Please enter instructions');
return ;
}
$chipList = Chip::all($type);
$chipDtoList = array();
foreach ($chipList as $item) {
array_push($chipDtoList, Chip::toDto($item));
}
$this->_rspData(array(
'data' => $chipDtoList,
));
}
public function chipOrder(){
$type = getReqVal('type','');
if (! $type){
$this->_rspErr(1,'Please enter instructions');
return ;
}
$order = getReqVal('order','asc');
$chipList = Chip::all($type);
$chipDtoList = array();
foreach ($chipList as $item) {
array_push($chipDtoList, Chip::toDto($item));
}
$data = $this->_orderByChip($chipDtoList,$order);
$this->_rspData(array(
'data' => $data,
));
}
public function chipDetails(){
$token_id = getReqVal('token_id','');
if (! $token_id){
$this->_rspErr(1,'Please enter instructions');
return ;
}
$chip = Chip::getChipByTokenId($token_id);
if ($chip['supper_state'] == 1){
$chip['strength_temporary'] = \services\FormulaService::Hero_Chip_NFT_Maximum_Physical_Strength($chip['chip_grade'],$chip['lucky_temporary']);
$chip['mint'] = strval(\services\FormulaService::Chip_Need_Mint_Cost($chip['chip_grade'])-$chip['upgrade_mint']);
}
if ($chip['inlay_state'] == 1){
$tili = $chip['strength_max']-$chip['strength'];
$chip['mint'] = \services\FormulaService::Chip_Demount_Mint($tili);
}
$chip = Chip::toDto($chip);
$this->_rspData(array(
'data' => $chip,
));
}
public function beforeInlay(){
$type = getReqVal('type','');
if (! $type){
$this->_rspErr(1,'Please enter instructions');
return ;
}
$chipList = Chip::all($type);
$chipDtoList = array();
foreach ($chipList as $item) {
if ($item['supper_state']==0 && $item['inlay_state']==0){
array_push($chipDtoList, Chip::toDto($item));
}
}
$this->_rspData(array(
'data' => $chipDtoList,
));
}
public function inlayChip(){
$type = trim(getReqVal('type', 0));
$unique_id = trim(getReqVal('unique_id', 0));
$token_id = trim(getReqVal('token_id', 0));
if (!$type || ! $token_id || ! $unique_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
if ($type == 1){
if (! $this->_doInlayHero($unique_id,$token_id)){
return;
}
$hero = Hero::toDto(Hero::find($unique_id));
$propertyChgService->addHeroChg();
$this->_rspData([
'data'=>$hero,
'property_chg' => $propertyChgService->toDto(),
]);
}else{
if (! $this->_doInlayGun($unique_id,$token_id)){
return;
}
$gun = Gun::toDto(Gun::find($unique_id));
$propertyChgService->addGunChg();
$this->_rspData([
'data'=>$gun,
'property_chg' => $propertyChgService->toDto(),
]);
}
}
public function demountChip(){
$type = trim(getReqVal('type', 0));
$unique_id = trim(getReqVal('unique_id', 0));
$token_id = trim(getReqVal('token_id', 0));
if (!$type || ! $token_id || ! $unique_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
if ($type == 1){
if (!$this->_doDemount($unique_id,$token_id)){
return;
}
$hero = Hero::toDto(Hero::find($unique_id));
$propertyChgService->addHeroChg();
$this->_rspData([
'data'=>$hero,
'property_chg' => $propertyChgService->toDto(),
]);
}else{
if (! $this->_doDemountGun($unique_id,$token_id)){
return;
}
$gun = Gun::toDto(Gun::find($unique_id));
$propertyChgService->addGunChg();
$this->_rspData([
'data'=>$gun,
'property_chg' => $propertyChgService->toDto(),
]);
}
}
public function replaceChip(){
$type = trim(getReqVal('type', 0));
$unique_id = trim(getReqVal('unique_id', 0));
$token_id_new = trim(getReqVal('token_id_new', 0));
$token_id_old = trim(getReqVal('token_id_old', 0));
if (! $token_id_new ||! $token_id_old || ! $unique_id || !$type) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
if ($type == 1){
if (! $this->_doDemount($unique_id,$token_id_old)){
return;
}
if (! $this->_doInlayHero($unique_id,$token_id_new)){
return;
}
$hero = Hero::toDto(Hero::find($unique_id));
$propertyChgService->addHeroChg();
$this->_rspData([
'data'=>$hero,
'property_chg' => $propertyChgService->toDto(),
]);
}else{
if (! $this->_doDemountGun($unique_id,$token_id_old)){
return;
}
if (! $this->_doInlayGun($unique_id,$token_id_new)){
return;
}
$gun = Gun::toDto(Gun::find($unique_id));
$propertyChgService->addGunChg();
$this->_rspData([
'data'=>$gun,
'property_chg' => $propertyChgService->toDto(),
]);
}
}
public function aKeyInlayChip(){
$type = trim(getReqVal('type', 0));
$unique_id = trim(getReqVal('unique_id', 0));
if ( ! $type || ! $unique_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
if ($type == 1){
$hero_or_gun = Hero::find($unique_id);
}else{
$hero_or_gun = Gun::find($unique_id);
}
if (! $hero_or_gun){
$this->_rspErr(1, 'Not enough hero or gun');
return;
}
$limit = 0;
if (! $hero_or_gun['chip_ids'] ){
$limit = 4;
}
if ($hero_or_gun['chip_ids'] && !strpos($hero_or_gun['chip_ids'], '|')){
$limit = 3;
}
if ($hero_or_gun['chip_ids'] && strpos($hero_or_gun['chip_ids'], '|')){
$chipIdsArr = explode('|',$hero_or_gun['chip_ids']);
$limit = 4-count($chipIdsArr);
}
$chipList = Chip::all($type);
if (! $chipList){
$this->_rspErr(1, 'Not enough chip');
return;
}
$usable = array();
foreach ($chipList as $item) {
if ($item['supper_state']==0 && $item['inlay_state']==0){
array_push($usable, $item);
}
}
$res = $this->_getHignGradeLimit($usable,$limit);
if (! $res){
$this->_rspErr(1, 'The chip slot is full');
return;
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
if ($type == 1){
foreach ($res as $val){
$this->_doInlayHero($unique_id,$val['token_id']);
}
$hero_new = Hero::toDto(Hero::find($unique_id));
$propertyChgService->addHeroChg();
$this->_rspData([
'data'=>$hero_new,
'property_chg' => $propertyChgService->toDto(),
]);
}else{
foreach ($res as $val){
$this->_doInlayGun($unique_id,$val['token_id']);
}
$gun_new = Gun::toDto(Gun::find($unique_id));
$propertyChgService->addGunChg();
$this->_rspData([
'data'=>$gun_new,
'property_chg' => $propertyChgService->toDto(),
]);
}
}
public function beforeSynthetic(){
$token_id = trim(getReqVal('token_id', 0));
if (! $token_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$chip = Chip::getChipByTokenId($token_id);
if ($chip['supper_state'] == 1 || $chip['inlay_state'] == 1 || $chip['state'] == -1){
$this->_rspErr(1, "The chip can't synthetic");
return;
}
if ($chip['chip_grade'] == 15){
$this->_rspErr(1, "Chip Lv upper limit");
return;
}
$chip = Chip::toDto($chip);
foreach ($chip['rand_attr'] as $k=>$val){
$attr = \mt\ChipAttr::getAttrById($val['attr_id'],$val['chip_type']);
$chip['rand_attr_after'][$k] =[
'attr_id'=>$val['attr_id'],
'type'=>$val['type'],
'val'=>$attr['lv'.($chip['chip_grade']+1)]*$val['attr_pool_num'],
'chip_type'=>$val['chip_type']
];
}
$chip['upgrade_cost']=\services\FormulaService::getChipUpgradeCost($chip['chip_grade']+1);
$this->_rspData(['data'=>$chip]);
}
public function selectChip(){
$token_id = trim(getReqVal('token_id', 0));
$token_ids = explode(' ',$token_id);
if (count($token_ids)<0){
$this->_rspErr(1, 'Please enter instructions');
return;
}
$chip_main = Chip::getChipByTokenId($token_ids[0]);
$chip_param = Chip::getChipByTokenId($token_ids[1]);
if ($chip_param['supper_state'] == 1 || $chip_param['inlay_state'] == 1 || $chip_param['state'] == -1){
$this->_rspErr(1, "The chip can't select");
return;
}
if ($chip_param['chip_grade']>$chip_main['chip_grade']){
$this->_rspErr(1, "The selected material is too high grade");
return;
}
$cost = \services\FormulaService::getChipCumulativeCost($chip_param['chip_grade']);
$this->_rspData(['data'=>$cost]);
}
public function syntheticChip(){
$token_id_main = trim(getReqVal('token_id_main', 0));
$token_id_param = array_filter(explode(' ',getReqVal('token_id_param', 0)));
if (! $token_id_main || count($token_id_param)<=0 ) {
$this->_rspErr(1, 'Please enter instructions token_id_param');
return;
}
$chip_main = Chip::getChipByTokenId($token_id_main);
$upgrade_cost = \services\FormulaService::getChipUpgradeCost($chip_main['chip_grade']+1);
if ($upgrade_cost==0){
$this->_rspErr(1, 'Error in calculation formula');
return;
}
$cumulative_cost = 0;
$params = array();
foreach ($token_id_param as $val){
$chip_param = Chip::getChipByTokenId($val);
array_push($params,array(
'unique_id'=>$chip_param['idx'],
'token_id'=>$chip_param['token_id'],
));
$cumulative_cost += \services\FormulaService::getChipCumulativeCost($chip_param['chip_grade']);
Chip::deleteChip(['token_id'=>$val]);
}
{
//埋点
$event = [
'name' => LogService::CHIP_SYNTH_MATERIAL,
];
LogService::ConsumableMaterial($event,$params);
}
$num = round(round(($cumulative_cost/$upgrade_cost),5)*10000);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
if ($num<rand(1,10000)){
//合成失败
$this->_rspData([
'state' => 0,
'property_chg' => $propertyChgService->toDto(),
]);
}else{
//合成成功
$this->_upgraded($chip_main);
$chip_new = Chip::toDto(Chip::getChipByTokenId($token_id_main));
$chip_new['strength_temporary'] = \services\FormulaService::Hero_Chip_NFT_Maximum_Physical_Strength($chip_new['chip_grade'],$chip_new['lucky_temporary']);
$chip_new['mint'] = \services\FormulaService::Chip_Need_Mint_Cost($chip_new['chip_grade'])-$chip_new['upgrade_mint'];
$this->_rspData([
'data'=>$chip_new,
'state' => 1,
'property_chg' => $propertyChgService->toDto(),
]);
}
}
public function isNeedLucky(){
$token_id = trim(getReqVal('token_id', 0));
if (! $token_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$chip = Chip::getChipByTokenId($token_id);
$strength_max = \services\FormulaService::Hero_Chip_NFT_Maximum_Physical_Strength($chip['chip_grade'],$chip['lucky_temporary']);
$mint_cost = \services\FormulaService::Chip_Need_Mint_Cost($chip['chip_grade'])-$chip['upgrade_mint'];
$fieldsKv = [
'supper_state' => 0,
'lucky_final'=>$chip['lucky_temporary'],
'lucky_temporary'=>0,
'strength_max' => $strength_max,
'strength' => $strength_max,
'upgrade_mint' => $mint_cost,
'labour' => 0
];
$userObj = new User();
$user=$userObj->find(myself()->_getAccountId());
if ($mint_cost>$user['gold']){
$this->_rspErr(2, 'Be short of gold coins');
return;
}
User::update(['gold'=>$user['gold']-$mint_cost]);
Chip::update($chip['token_id'],$fieldsKv);
$old_chip = Chip::toDto($chip);
$new_chip = Chip::toDto(Chip::getChipByTokenId($token_id));
{
//埋点
$event = [
'name' => LogService::CHIP_LUCKY,
'val' => $mint_cost
];
$old_chip['level'] = $old_chip['chip_grade'];
$new_chip['level'] = $new_chip['chip_grade'];
LogService::consumeCEG($event,$old_chip,$new_chip);
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addUserChg();
$propertyChgService->addChip();
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function isNotNeedLucky(){
$token_id = trim(getReqVal('token_id', 0));
if (! $token_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$chip = Chip::getChipByTokenId($token_id);
$fieldsKv = [
'supper_state' => 0,
'lucky_temporary'=>0,
];
Chip::update($chip['token_id'],$fieldsKv);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function waitingLucky(){
$token_id = trim(getReqVal('token_id', 0));
if (! $token_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$chip = Chip::getChipByTokenId($token_id);
$fieldsKv = [
'supper_state'=>1,
];
Chip::update($chip['token_id'],$fieldsKv);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
$this->_rspData([
'property_chg' => $propertyChgService->toDto(),
]);
}
public function aKeySynthetic(){
$token_id = trim(getReqVal('token_id', 0));
if (! $token_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$chip = Chip::getChipByTokenId($token_id);
$upgrade_cost =\services\FormulaService::getChipUpgradeCost($chip['chip_grade']+1); //芯片升星价值
$chipList = Chip::all($chip['chip_type']);
$chipDtoList = array();
foreach ($chipList as $item) {
if ($item['supper_state']==0 && $item['inlay_state']==0 && $item['state']!=-1 && $item['token_id'] != $token_id && $item['chip_grade'] <= $chip['chip_grade']){
array_push($chipDtoList, $item);
}
};
$data = $this->_orderByChip($chipDtoList,'asc');
$chip_param = [];
$chip_param_out = [];
if (count($data) > 20) {
$cost = 0;
for ($i=0;$i<count($data);$i++){
$data[$i]['cost'] = \services\FormulaService::getChipCumulativeCost($data[$i]['chip_grade']);
if ($i<20){
$cost += $data[$i]['cost'];
array_push($chip_param,$data[$i]);
}else{
array_push($chip_param_out,$data[$i]);
}
}
if ($cost>$upgrade_cost){
$diff_cost = $cost - $upgrade_cost;
foreach ($chip_param as $k=>$value){
if ($diff_cost>$value['cost']){
$diff_cost -= $value['cost'];
unset($chip_param[$k]);
}
}
}else{
$chip_param = $this->_bestFun($upgrade_cost,$chip_param,$chip_param_out);
}
}else{
$cost = 0;
foreach ($data as $k=>$val){
$data[$k]['cost'] = \services\FormulaService::getChipCumulativeCost($data[$k]['chip_grade']);
$cost += $data[$k]['cost'];
}
if ($cost > $upgrade_cost){
$diff_cost = $cost - $upgrade_cost;
foreach ($data as $k=>$value){
if ($diff_cost>$value['cost']){
$diff_cost -= $value['cost'];
unset($data[$k]);
}
}
}
$chip_param = $data;
}
$cumulative_cost = [];
$token_id_arr = [];
foreach ($chip_param as $value){
array_push($cumulative_cost,$value['cost']);
array_push($token_id_arr,$value['token_id']);
}
$this->_rspData(['data'=>['cost'=>$cumulative_cost,'token_ids'=>$token_id_arr]]);
}
public function lockChip(){
$token_id = trim(getReqVal('token_id', 0));
$state = trim(getReqVal('state', 0));
if (! $token_id){
$this->_rspErr(1, 'Please enter instructions');
return;
}
if ($state == 1){
Chip::update($token_id,['state'=>-1]);
}else{
Chip::update($token_id,['state'=>0]);
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addChip();
$this->_rspData([
'property_chg' => $propertyChgService->toDto(),
]);
}
private function _doInlayHero($hero_id,$token_id){
$chip = Chip::getChipByTokenId($token_id);
$hero = Hero::find($hero_id);
if (! $chip || ! $hero){
$this->_rspErr(1, 'Not enough chip or hero');
return false;
}
if (! $hero['chip_ids']){
$fieldsKv = ['chip_ids'=>$chip['idx']];
}
if ($hero['chip_ids'] && !strpos($hero['chip_ids'], '|')){
$chipIdsArr = [$hero['chip_ids']];
array_push($chipIdsArr,$chip['idx']);
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
}
if ($hero['chip_ids'] && strpos($hero['chip_ids'], '|')){
$chipIdsArr = explode('|',$hero['chip_ids']);
array_push($chipIdsArr,$chip['idx']);
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
}
Hero::update($hero_id,$fieldsKv);
Chip::updateChipInlayState($token_id,1);
return true;
}
private function _doInlayGun($gun_id,$token_id){
$chip = Chip::getChipByTokenId($token_id);
$gun = Gun::find($gun_id);
if (! $chip || ! $gun){
$this->_rspErr(1, 'Not enough chip or gun');
return false;
}
if (! $gun['chip_ids']){
$fieldsKv = ['chip_ids'=>$chip['idx']];
}
if ($gun['chip_ids'] && !strpos($gun['chip_ids'], '|')){
$chipIdsArr = [$gun['chip_ids']];
array_push($chipIdsArr,$chip['idx']);
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
}
if ($gun['chip_ids'] && strpos($gun['chip_ids'], '|')){
$chipIdsArr = explode('|',$gun['chip_ids']);
array_push($chipIdsArr,$chip['idx']);
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
}
Gun::update($gun_id,$fieldsKv);
Chip::updateChipInlayState($token_id,1);
return true;
}
private function _doDemount($unique_id,$token_id){
$chip = Chip::getChipByTokenId($token_id);
$hero = Hero::find($unique_id);
if (! $chip || ! $hero){
$this->_rspErr(1, 'Not enough chip or hero');
return false;
}
$chipIdsArr = explode('|',$hero['chip_ids']);
$k = array_search($chip['idx'],$chipIdsArr);
unset($chipIdsArr[$k]);
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
$tili = $chip['strength_max']-$chip['strength'];
if ($tili){
$mint = \services\FormulaService::Chip_Demount_Mint($tili);
$userObj = new User();
$user=$userObj->find(myself()->_getAccountId());
if ($mint>$user['gold']){
$this->_rspErr(2, 'Be short of gold coins');
return false;
}
User::update(['gold'=>$user['gold']-$mint]);
$chipDto = Chip::toDto($chip);
{
//埋点
$event = [
'name' => LogService::CHIP_DEMOUNT,
'val' => $mint
];
$chipDto['level'] = $chipDto['chip_grade'];
LogService::consumeCEG($event,$chipDto,$chipDto);
}
}
$where = ['inlay_state'=>0,'strength'=>$chip['strength_max']];
Hero::update($unique_id,$fieldsKv);
Chip::update($token_id,$where);
return true;
}
private function _doDemountGun($unique_id,$token_id){
$chip = Chip::getChipByTokenId($token_id);
$gun = Gun::find($unique_id);
if (! $chip || ! $gun){
$this->_rspErr(1, 'Not enough chip or gun');
return false;
}
$chipIdsArr = explode('|',$gun['chip_ids']);
$k = array_search($chip['idx'],$chipIdsArr);
unset($chipIdsArr[$k]);
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
$tili = $chip['strength_max']-$chip['strength'];
if ($tili){
$mint = \services\FormulaService::Chip_Demount_Mint($tili);
$userObj = new User();
$user=$userObj->find(myself()->_getAccountId());
if ($mint>$user['gold']){
$this->_rspErr(2, 'Be short of gold coins');
return false;
}
User::update(['gold'=>$user['gold']-$mint]);
$chipDto = Chip::toDto($chip);
{
//埋点
$event = [
'name' => LogService::CHIP_DEMOUNT,
'val' => $mint
];
$chipDto['level'] = $chipDto['chip_grade'];
LogService::consumeCEG($event,$chipDto,$chipDto);
}
}
$where = ['inlay_state'=>0,'strength'=>$chip['strength_max']];
Gun::update($unique_id,$fieldsKv);
Chip::update($token_id,$where);
return true;
}
private function _inLayNewAttr($hero_id){
$heroDb = Hero::find($hero_id);
$hero = Hero::toDto($heroDb);
$chipAttr = [];
if (! $hero['chip_ids'] ){
return $hero;
}
if ($hero['chip_ids'] && !strpos($hero['chip_ids'], '|')){
$chip =Chip::toDto(Chip::getChipByIdx($hero['chip_ids']));
foreach ($chip['rand_attr'] as $val){
array_push($chipAttr,$val);
}
}
if ($hero['chip_ids'] && strpos($hero['chip_ids'], '|')){
$chipIdsArr = explode('|',$hero['chip_ids']);
foreach ($chipIdsArr as $val){
$chip = Chip::toDto(Chip::getChipByIdx($val));
foreach ($chip['rand_attr'] as $v){
array_push($chipAttr,$v);
}
}
}
$item = [];
foreach ($chipAttr as $k=>$v){
if (!isset($item[$v['attr_id']])){
$item[$v['attr_id']] = $v;
}else{
$item[$v['attr_id']]['val']+= $v['val'];
}
}
$hero['attr_chip'] = $item;//芯片属性
return $hero;
}
private function _inLayNewAttrGun($gun_id){
$gunDb = Gun::find($gun_id);
$gun = Gun::toDto($gunDb);
$chipAttr = [];
if (! $gun['chip_ids'] ){
return $gun;
}
if ($gun['chip_ids'] && !strpos($gun['chip_ids'], '|')){
$chip =Chip::toDto(Chip::getChipByIdx($gun['chip_ids']));
foreach ($chip['rand_attr'] as $val){
array_push($chipAttr,$val);
}
}
if ($gun['chip_ids'] && strpos($gun['chip_ids'], '|')){
$chipIdsArr = explode('|',$gun['chip_ids']);
foreach ($chipIdsArr as $val){
$chip = Chip::toDto(Chip::getChipByIdx($val));
foreach ($chip['rand_attr'] as $v){
array_push($chipAttr,$v);
}
}
}
$item = [];
foreach ($chipAttr as $k=>$v){
if (!isset($item[$v['attr_id']])){
$item[$v['attr_id']] = $v;
}else{
$item[$v['attr_id']]['val']+= $v['val'];
}
}
$gun['attr_chip'] = $item;//芯片属性
return $gun;
}
private function _chipCore($unique_id,$type){
$chipCore = [];
if ($type==1){
$hero = Hero::find($unique_id);
}else{
$hero = Gun::find($unique_id);
}
if ($hero['chip_ids'] && strpos($hero['chip_ids'], '|')){
$chipIdsArr = explode('|',$hero['chip_ids']);
if (count($chipIdsArr) == 4){
$min = 15;
foreach ($chipIdsArr as $val){
$chip = Chip::getChipByIdx($val);
if ($chip['chip_grade']<$min){
$min = $chip['chip_grade'];
}
}
$chipCoreList = getMetaTable('chipCore@chipCore.php');
foreach ($chipCoreList as $val){
if ($val['chip_core_type']==$type && $val['chip_core_lv']<=$min){
array_push($chipCore,$val);
}
}
return $chipCore;
}else{
return [];
}
}else{
return [];
}
}
private function _getHignGradeLimit($data,$limit){
$len = count($data);
if ($len<=1 || $len<=$limit){
return $data;
}
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($data[$i]['chip_grade'] < $data[$j]['chip_grade']) {
$tmp = $data[$i];
$data[$i] = $data[$j];
$data[$j] = $tmp;
}
}
}
return array_slice($data,0,$limit);
}
private function _orderByChip($data,$order){
$len = count($data);
if ($len<=1){
return $data;
}
if ($order == 'desc'){
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($data[$i]['chip_grade'] < $data[$j]['chip_grade']) {
$tmp = $data[$i];
$data[$i] = $data[$j];
$data[$j] = $tmp;
}
}
}
}else{
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($data[$i]['chip_grade'] > $data[$j]['chip_grade']) {
$tmp = $data[$i];
$data[$i] = $data[$j];
$data[$j] = $tmp;
}
}
}
}
return $data;
}
private function _upgraded($chip){
$new_grade = $chip['chip_grade']+1;
if ($new_grade == 3 || $new_grade == 5){
Chip::updateRandAttr($chip);
}
$lucky = ltrim(\services\FormulaService::getChipLuckyValue($new_grade),'-') ;
if ($new_grade>=5){
$where = ['lucky_temporary'=>$lucky, 'chip_grade'=>$new_grade,'supper_state'=>1,'modifytime' => $this->_getNowTime()];
}else{
$where = ['lucky_temporary'=>$lucky, 'chip_grade'=>$new_grade,'modifytime' => $this->_getNowTime()];
}
Chip::update($chip['token_id'],$where);
}
private function _bestFun($upgrade_cost,$chip_param,$chip_param_out){
$cost = 0;
foreach ($chip_param as $v){
$cost += $v['cost'];
};
if ($cost>$upgrade_cost){
return $chip_param;
}else{
array_shift($chip_param);
array_push($chip_param,array_shift($chip_param_out));
return $this->_bestFun($upgrade_cost,$chip_param,$chip_param_out);
}
}
}