464 lines
16 KiB
PHP
464 lines
16 KiB
PHP
<?php
|
|
|
|
require_once('models/Chip.php');
|
|
require_once('models/Hero.php');
|
|
require_once('mt/ChipAttr.php');
|
|
require_once('mt/AttrHelper.php');
|
|
require_once('services/FormulaService.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 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 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->_getHignGradeLimit($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 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 level 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;
|
|
}
|
|
if ($chip_param['state']){
|
|
Chip::update($token_ids[1],['state'=>0]);
|
|
$cost = 0;
|
|
}else{
|
|
Chip::update($token_ids[1],['state'=>1]);
|
|
$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 = explode(' ',getReqVal('token_id_param', 0));
|
|
if (! $token_id_main || count($token_id_param)<=0 ) {
|
|
$this->_rspErr(1, 'Please enter instructions');
|
|
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;
|
|
foreach ($token_id_param as $val){
|
|
$chip_param = Chip::getChipByTokenId($val);
|
|
$cumulative_cost += \services\FormulaService::getChipCumulativeCost($chip_param['chip_grade']);
|
|
Chip::deleteChip(['token_id'=>$val]);
|
|
}
|
|
$num = round(round(($cumulative_cost/$upgrade_cost),5)*10000);
|
|
if ($num<rand(1,10000)){
|
|
//合成失败
|
|
$this->_rspErr(1, 'The chip Upgrade failed');
|
|
return;
|
|
}else{
|
|
//合成成功
|
|
$this->_upgraded($chip_main);
|
|
$chip_new = Chip::toDto(Chip::getChipByTokenId($token_id_main));
|
|
$chip_new['strength_temporary'] = \services\FormulaService::getChipPhysicalStrengthValue($chip_new['chip_grade'],$chip_new['lucky_final']+$chip_new['lucky_temporary']);
|
|
$this->_rspData(['data'=>$chip_new]);
|
|
}
|
|
}
|
|
|
|
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);
|
|
$fieldsKv = [
|
|
'lucky_final'=>$chip['lucky_final']+$chip['lucky_temporary'],
|
|
'lucky_temporary'=>0,
|
|
'strength' => \services\FormulaService::getChipPhysicalStrengthValue($chip['chip_grade'],$chip['lucky_final']+$chip['lucky_temporary'])
|
|
];
|
|
Chip::update($chip['token_id'],$fieldsKv);
|
|
$this->_rspOk();
|
|
}
|
|
|
|
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 = [
|
|
'lucky_temporary'=>0,
|
|
];
|
|
Chip::update($chip['token_id'],$fieldsKv);
|
|
$this->_rspOk();
|
|
}
|
|
|
|
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);
|
|
$this->_rspOk();
|
|
}
|
|
|
|
public function aKeySynthetic(){
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test(){
|
|
$chip = Chip::getChipByTokenId(1660629357);
|
|
print_r($chip);
|
|
}
|
|
|
|
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 _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 = \services\FormulaService::getChipLuckyValue($chip['chip_grade']);
|
|
$lucky_pro = \services\FormulaService::getChipLuckyValue($new_grade);
|
|
Chip::update($chip['token_id'],['lucky_temporary'=>$lucky_pro-$lucky, 'chip_grade'=>$new_grade]);
|
|
}
|
|
|
|
} |