game2006api/webapp/controller/HeroController.class.php
2024-10-09 16:52:06 +08:00

391 lines
14 KiB
PHP

<?php
require_once('mt/Hero.php');
require_once('mt/Item.php');
require_once('mt/Skill.php');
require_once('mt/EconomyAttribute.php');
require_once('mt/Manufacture.php');
require_once('mt/BattleBasicAttribute.php');
require_once('mt/BattleRandAttribute.php');
require_once('mt/StakingVip.php');
require_once('models/Hero.php');
require_once('models/HeroPreset.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
require_once('services/LogService.php');
use phpcommon\SqlHelper;
use models\Hero;
use models\Gun;
use models\HeroPreset;
use services\LogService;
class HeroController extends BaseAuthedController {
public function heroList()
{
$heroList = array();
Hero::getHeroList(function ($row) use(&$heroList) {
array_push($heroList, Hero::listDto($row));
});
$this->_rspData(array(
'hero_list' => $heroList
));
}
public function heroDetails()
{
$unique_id = trim(getReqVal('unique_id', 0));
if ( ! $unique_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$heroDb = Hero::find($unique_id);
if (! $heroDb){
$this->_rspErr(1, "You don't have the hero yet");
return;
}
$resetLv_state = $this->_getDailyV(TN_DAILY_RESET_HERO_LEVEL_STATE, $unique_id);
$hero = Hero::toDto($heroDb);
$hero['avatarInfo'] = Hero::avatarInfo($heroDb);
$hero['resetLv_state'] = $resetLv_state;
$this->_rspData(array(
'data' => $hero
));
}
public function mallHeroInfo(){
$unique_id = trim(getReqVal('unique_id', 0));
if ( ! $unique_id) {
$this->_rspErr(1, 'Please enter instructions');
return;
}
$heroDb = Hero::findEx($unique_id);
if (! $heroDb){
$this->_rspErr(1, "You don't have the hero yet");
return;
}
$hero = Hero::mallInfo($heroDb);
$this->_rspData(array(
'data' => $hero
));
}
public function synHeroS(){
myself()->_verifySwitch('heroPieceSyn');
$itemId = getReqVal('item_id',0);
$itemNum = getReqVal('item_num',0);
$itemMeta = \mt\Item::get($itemId);
if (!$itemMeta || $itemMeta['type'] != \mt\Item::FRAGMENT_TYPE){
$this->_rspErr(1, "param error");
return;
}
$heroMeta = \mt\Hero::get($itemMeta['relationship']);
if ( !$heroMeta ){
$this->_rspErr(1, "param error");
return;
}
if ($itemId != $heroMeta['heroPiece'] || $itemNum < $heroMeta['pieceNumber']){
$this->_rspErr(1, "item number enough ");
return;
}
$number = floor($itemNum / $heroMeta['pieceNumber']);
$costItems = array(
array(
'item_id' => $heroMeta['heroPiece'],
'item_num' => $heroMeta['pieceNumber'] * $number
),
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
//消耗材料
$this->_decItems($costItems);
$propertyChgService = new services\PropertyChgService();
$awardService = new services\AwardService();
$propertyChgService->addBagChg();
for ($i=0; $i<$number; $i++){
$res= Hero::addSyntheticHero($heroMeta,2);
if ($res){
$lastIdx = SqlHelper::getLastInsertId( myself()->_getSelfMysql());
$awardService->addHero($heroMeta['id'],$lastIdx);
}
}
$propertyChgService->addHeroChg();
// $this->_incPeriodV(TN_HASH_RATE_SYN_HERO_TIMES, 0, $number);
myself()->_fireEvent('Synthesis','onHroSyn',$number);
$event = array(
'ID' => 'hero',
'SUB_ID' => 'synthesis',
'SUB_KEY' => 'hero_synthesis',
'cost_item' => $costItems,
'outcome_hero' => array(
'item_id' => $heroMeta['id'],
'item_num' => $number
),
);
LogService::burialPointEvent($event);
$this->_rspData(array(
'award' => $awardService->toDto(),
'property_chg' => $propertyChgService->toDto(),
));
}
public function upgradeQualityS(){
myself()->_verifySwitch('heroUp');
$heroUniId = getReqVal('hero_uniid', 0);
$itemId = getReqVal('item_id',0);
$itemNum = max(0,getReqVal('item_num',0));
$extraItemId = getReqVal('extra_item',0);
$extraItemNum = max(0,getReqVal('extra_num',0));
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(100, 'param hero_uniid error ');
return;
}
// if ($heroDb['quality'] ==1 ){
// $this->_rspErr(5, "This hero cannot level up");
// return;
// }
if ($heroDb['quality'] == Hero::HERO_QUALITY_MAX){
$this->_rspErr(5, "It's already the highest level");
return;
}
$heroMeta = \mt\Hero::get($heroDb['hero_id']);
$manufactureMeta = \mt\Manufacture::getByInvoke($heroMeta['manufacture'],$heroDb['quality']+1);
if (!$manufactureMeta){
$this->_rspErr(1, "Error operation");
return;
}
if ($itemId != $heroMeta['heroPiece'] || $itemNum < $manufactureMeta['needItem']){
$this->_rspErr(100, 'Insufficient material');
return;
}
$extraItem = explode("|",$manufactureMeta['extraItem']);
$chanceKey = 0;
if ($extraItemId == $extraItem[0]){
$chanceKey = min($extraItemNum,$extraItem[1]);
}
//vip加成金币消耗
$rightVal = myself()->_getVipRightsVal(mt\StakingVip::HERO_UP);
$goldNum = $manufactureMeta['gold'];
if ($rightVal > 0){
$goldNum -= $goldNum*$rightVal;
}
$costItems = array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => ceil($goldNum)
),
array(
'item_id' => $itemId,
'item_num' => $manufactureMeta['needItem']
),
);
$lackItem = null;
if ($chanceKey > 0){
array_push($costItems, array(
'item_id' => $extraItemId,
'item_num' => $chanceKey
));
}
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
//消耗材料
$this->_decItems($costItems);
// $this->_incPeriodV(TN_HASH_RATE_UP_HERO_TIMES, 0, 1);
myself()->_fireEvent('Upgrade','onHeroUpgrade');
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addBagChg();
$propertyChgService->addUserChg();
$hashChance = \mt\Manufacture::hashChance($manufactureMeta['chance']);
$weight = $hashChance[$chanceKey];
$rnd = rand(1,100);
$status = 0;
if ($rnd <= $weight*100){
$status = 1;
$heroAtteMeta = \mt\EconomyAttribute::findByGrade($heroMeta['economyAttribute'],$heroDb['quality']);
$heroNextQualityAtteMeta = \mt\EconomyAttribute::findByGrade($heroMeta['economyAttribute'],$heroDb['quality'] + 1);
$unsealTime = $heroDb['unseal_time'] ? $heroDb['unseal_time'] : $heroDb['activate_time'];
$validTime = $unsealTime + 86400 * $heroAtteMeta['validTime'] ;
$valid_lefttime = max(0, $validTime - myself()->_getNowTime()) + ($heroNextQualityAtteMeta['validTime'] - $heroAtteMeta['validTime']) * 86400;
$attribute = \mt\EconomyAttribute::getAttribute($heroMeta['economyAttribute'], $heroDb['quality'] + 1);
$unseal_time_new = $valid_lefttime + myself()->_getNowTime() - 86400 * $heroNextQualityAtteMeta['validTime'];
$fieldsKv = array(
'quality' => $heroDb['quality'] + 1,
'unseal_time' => $unseal_time_new,
'wealth_attr' => json_encode($attribute),
);
if ($heroDb['quality'] == 1){
$this->_incDailyV(TN_DAILY_DRAW_BIND_GOLD_STATE, 0,1);
}
if ($heroDb['quality'] + 1 > 4){
$randAttr = emptyReplace(json_decode($heroDb['rand_attr'], true), array());
$basicMeta = mt\BattleBasicAttribute::get($heroDb['hero_id']);
$randMeta = mt\BattleRandAttribute::getByWeight($basicMeta['randomAttribute_Default'],$heroDb['quality'] + 1);
$attr = mt\BattleRandAttribute::getRandAttr($randMeta);
foreach ($attr as $key=>$value){
$attr[$key]['new_attr'] = 1;
}
$newAttr = array_merge($randAttr,$attr);
$fieldsKv['rand_attr'] = json_encode($newAttr);
}
Hero::update($heroUniId, $fieldsKv);
$heroNew = Hero::toDto(Hero::find($heroUniId));
$propertyChgService->addHeroChg();
$event = array(
'ID' => 'luck',
'SUB_ID' => 'change',
'SUB_KEY' => 'luck_change',
'change' => Hero::getAccountLuckyTemp(),
'reason' => 'hero_advanced',
);
LogService::burialPointEvent($event);
}
$event = array(
'ID' => 'hero',
'SUB_ID' => 'advanced',
'SUB_KEY' => 'hero_advanced',
'cost_hero' => $heroDb,
'cost_fragment' => array(
'item_id' => $itemId,
'item_num' => $manufactureMeta['needItem'],
),
'cost_gold' => array(
'item_id' => V_ITEM_GOLD,
'item_num' => $manufactureMeta['gold']
),
'cost_extraItem' => array(
'item_id' => $extraItemId,
'item_num' => $chanceKey
),
'chance' => $weight,
'result' => $status,
'outcome_hero' => isset($heroNew) ? $heroNew : array(),
);
LogService::burialPointEvent($event);
$this->_rspData(array(
'status' => $status,
'property_chg' => $propertyChgService->toDto(),
));
}
public function unSealHero(){
$heroUniId = getReqVal('hero_uniid', 0);
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(100, 'param error or null');
return;
}
// if (!$heroDb['token_id']){
// $this->_rspErr(1, "this hero is not NFT");
// return;
// }
Hero::update($heroUniId,array(
'seal_type' => Hero::UNSEAL_STATE,
"unseal_time" => myself()->_getNowTime()
));
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addHeroChg();
$event = array(
'ID' => 'hero',
'SUB_ID' => 'unlock',
'SUB_KEY' => 'hero_unlock',
'before' => $heroDb,
'result' => 1,
'after' => Hero::find($heroUniId),
);
LogService::burialPointEvent($event);
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
/*
英雄预设
*/
public function presetHero(){
$heroUid = getReqVal('hero_uid',0);
$heroDb = Hero::find($heroUid);
if (! $heroDb){
$this->_rspErr(1, "You don't have the hero yet");
return;
}
$data = HeroPreset::getHeroPreset($heroUid);
$this->_rspData(array(
'data' => $data,
));
}
/*
应用预设
*/
public function applyHero(){
$heroId = getReqVal('hero_uid',0);
$chipPageId = getReqVal('chip_page',0);
$weaponUid1 = getReqVal('weapon_uid1',0);
$weaponUid2 = getReqVal('weapon_uid2',0);
$skillId = getReqVal('skill_id',0);
$heroDb = Hero::find($heroId);
if (! $heroDb){
$this->_rspErr(1, "You don't have the hero yet");
return;
}
if ($weaponUid1){
$gunDb1 = Gun::find($weaponUid1);
if (!$gunDb1){
$this->_rspErr(1, "You don't have the gun1 yet");
return;
}
}
if ($weaponUid2){
$gunDb2 = Gun::find($weaponUid2);
if (!$gunDb2){
$this->_rspErr(1, "You don't have the gun2 yet");
return;
}
}
$skillMeta = \mt\Skill::get($skillId);
if (! $skillMeta){
$this->_rspErr(1,'skill_id parameter error');
return ;
}
HeroPreset::upsertPreset($heroId,$skillId,$chipPageId,$weaponUid1,$weaponUid2);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addGunChg();
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
/*
* 设置通用技能
*/
public function setHeroSkill(){
$skillId = getReqVal('skill_id',0);
$skillMeta = \mt\Skill::get($skillId);
if (! $skillMeta){
$this->_rspErr(1,'skill_id parameter error');
return ;
}
$userDb = $this->_getOrmUserInfo();
$hero_uid = $userDb['hero_id'];
$preset = HeroPreset::getHeroPreset($hero_uid);
HeroPreset::upsertPreset($hero_uid,$skillId,$preset['chip_page'],0,0);
$this->_rspOk();
}
}