265 lines
8.4 KiB
PHP
265 lines
8.4 KiB
PHP
<?php
|
|
|
|
require_once('models/Chip.php');
|
|
require_once('models/Hero.php');
|
|
require_once('mt/ChipAttr.php');
|
|
require_once('mt/AttrHelper.php');
|
|
|
|
use models\Chip;
|
|
use models\Hero;
|
|
use services\NftService;
|
|
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 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(){
|
|
$hero_id = trim(getReqVal('hero_id', 0));
|
|
$token_id = trim(getReqVal('token_id', 0));
|
|
if (! $token_id || ! $hero_id) {
|
|
$this->_rspErr(1, 'Please enter instructions');
|
|
return;
|
|
}
|
|
$this->_doInlay($hero_id,$token_id);
|
|
$hero = $this->_inLayNewAttr($hero_id);
|
|
$chip_core = $this->_chipCore($hero_id);
|
|
$hero['chip_core'] = $chip_core;
|
|
$this->_rspData(['data'=>$hero]);
|
|
}
|
|
|
|
public function demountChip(){
|
|
$hero_id = trim(getReqVal('hero_id', 0));
|
|
$token_id = trim(getReqVal('token_id', 0));
|
|
if (! $token_id || ! $hero_id) {
|
|
$this->_rspErr(1, 'Please enter instructions');
|
|
return;
|
|
}
|
|
$this->_doDemount($hero_id,$token_id);
|
|
$hero = $this->_inLayNewAttr($hero_id);
|
|
$chip_core = $this->_chipCore($hero_id);
|
|
$hero['chip_core'] = $chip_core;
|
|
$this->_rspData(['data'=>$hero]);
|
|
}
|
|
|
|
public function replaceChip(){
|
|
$hero_id = trim(getReqVal('hero_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 || ! $hero_id) {
|
|
$this->_rspErr(1, 'Please enter instructions');
|
|
return;
|
|
}
|
|
$this->_doDemount($hero_id,$token_id_old);
|
|
$this->_doInlay($hero_id,$token_id_new);
|
|
$hero = $this->_inLayNewAttr($hero_id);
|
|
$chip_core = $this->_chipCore($hero_id);
|
|
$hero['chip_core'] = $chip_core;
|
|
$this->_rspData(['data'=>$hero]);
|
|
}
|
|
|
|
public function aKeyInlayChip(){
|
|
$hero_id = trim(getReqVal('hero_id', 0));
|
|
if ( ! $hero_id) {
|
|
$this->_rspErr(1, 'Please enter instructions');
|
|
return;
|
|
}
|
|
$hero = Hero::find($hero_id);
|
|
if (! $hero){
|
|
$this->_rspErr(1, 'Not enough hero');
|
|
return;
|
|
}
|
|
$limit = 0;
|
|
if (! $hero['chip_ids'] ){
|
|
$limit = 4;
|
|
}
|
|
if ($hero['chip_ids'] && !strpos($hero['chip_ids'], '|')){
|
|
$limit = 3;
|
|
}
|
|
if ($hero['chip_ids'] && strpos($hero['chip_ids'], '|')){
|
|
$chipIdsArr = explode('|',$hero['chip_ids']);
|
|
$limit = 4-count($chipIdsArr);
|
|
}
|
|
|
|
$chipList = Chip::all(1);
|
|
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->_orderByChipDesc($usable,$limit);
|
|
if (! $res){
|
|
$this->_rspErr(1, 'The chip slot is full');
|
|
return;
|
|
}
|
|
foreach ($res as $val){
|
|
$this->_doInlay($hero_id,$val['token_id']);
|
|
}
|
|
$hero_new = $this->_inLayNewAttr($hero_id);
|
|
$chip_core = $this->_chipCore($hero_id);
|
|
$hero_new['chip_core'] = $chip_core;
|
|
$this->_rspData(['data'=>$hero_new]);
|
|
|
|
}
|
|
|
|
public function test(){
|
|
|
|
}
|
|
|
|
private function _doInlay($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;
|
|
}
|
|
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);
|
|
}
|
|
|
|
private function _doDemount($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;
|
|
}
|
|
$chipIdsArr = explode('|',$hero['chip_ids']);
|
|
$k = array_search($chip['idx'],$chipIdsArr);
|
|
unset($chipIdsArr[$k]);
|
|
$fieldsKv = ['chip_ids'=>implode('|',$chipIdsArr)];
|
|
Hero::update($hero_id,$fieldsKv);
|
|
Chip::updateChipInlayState($token_id,0);
|
|
|
|
}
|
|
|
|
private function _inLayNewAttr($hero_id){
|
|
$hero = Hero::find($hero_id);
|
|
if (! $hero){
|
|
$this->_rspErr(1, 'Not enough hero');
|
|
return;
|
|
}
|
|
$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);
|
|
}
|
|
}
|
|
}
|
|
$hero_attr = emptyReplace(json_decode($hero['rand_attr'], true), array());
|
|
\mt\AttrHelper::mergeAttr($hero_attr,$chipAttr);
|
|
$hero['rand_attr'] = $hero_attr;
|
|
return $hero;
|
|
}
|
|
|
|
private function _chipCore($hero_id){
|
|
$chipCore = [];
|
|
$hero = Hero::find($hero_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']==1 && $val['chip_core_lv']<=$min){
|
|
array_push($chipCore,$val);
|
|
}
|
|
}
|
|
return $chipCore;
|
|
}else{
|
|
return [];
|
|
}
|
|
}else{
|
|
return [];
|
|
}
|
|
}
|
|
|
|
private function _orderByChipDesc($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);
|
|
|
|
}
|
|
|
|
} |