game2006api/webapp/controller/HeroController.class.php
aozhiwei c5b41769ed q
2022-06-14 19:23:53 +08:00

576 lines
20 KiB
PHP

<?php
require_once('mt/Shop.php');
require_once('mt/Hero.php');
require_once('mt/Item.php');
require_once('mt/HeroLevel.php');
require_once('mt/HeroLevelAttr.php');
require_once('mt/HeroQuality.php');
require_once('mt/AttrHelper.php');
require_once('mt/Parameter.php');
require_once('models/Hero.php');
require_once('models/Bag.php');
require_once('models/HeroSkin.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
require_once('services/RankActivityService.php');
use phpcommon\SqlHelper;
use models\Hero;
use models\Bag;
use models\HeroSkin;
class HeroController extends BaseAuthedController {
public function heroList()
{
$heroList = array();
Hero::getHeroList(function ($row) use(&$heroList) {
array_push($heroList, Hero::toDto($row));
});
$this->_rspData(array(
'hero_list' => $heroList
));
}
public function skinList()
{
$skinList = array();
SqlHelper::ormSelect(
$this->_getSelfMysql(),
't_hero_skin',
array(
'account_id' => $this->_getAccountId()
),
function ($row) use(&$skinList) {
array_push($skinList, HeroSkin::toDto($row));
}
);
$this->_rspData(array(
'skin_list' => $skinList
));
}
public function takeonSkin()
{
$heroUniId = getReqVal('hero_uniid', 0);
$skinId = getReqVal('skin_id', 0);
$heroDb = Hero::find($heroUniId);
$heroSkinDb = HeroSkin::find($skinId);
if (!$heroDb) {
$this->_rspErr(1, "You don't have the hero yet");
return;
}
if (!$heroSkinDb) {
$this->_rspErr(2, "You don't have the skin yet");
return;
}
Hero::takeonSkin($heroUniId, $skinId);
$this->_rspOk();
}
public function upgradeSkill()
{
$heroUniId = getReqVal('hero_uniid', 0);
$skillIdx = getReqVal('skill_idx', 0);
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(1, "You don't have the hero yet");
return;
}
if (!in_array($skillIdx, array(0, 1))) {
$this->_rspErr(1, 'skill_idx must be 0-1');
return;
}
Hero::upgradeSkill($heroUniId, $skillIdx);
$this->_rspOk();
}
public function getUpgradeLevelList()
{
$infos = array();
for ($i = 0; $i < kMaxHeroUpLevelNum; ++$i) {
$heroUniId = $this->_getV(TN_HERO_LEVEL_UP, $i);
$info = null;
if ($heroUniId) {
$heroDb = Hero::find($heroUniId);
if ($heroDb) {
$heroDto = Hero::toDto($heroDb);
$info = array(
'info' => $heroDto,
'countdown' => $heroDto['unlock_lefttime']
);
}
}
array_push($infos, $info);
}
$this->_rspData(array(
'infos' => $infos
));
}
public function getUpgradeQualityList()
{
$infos = array();
for ($i = 0; $i < kMaxHeroUpQualityNum; ++$i) {
$heroUniId = $this->_getV(TN_HERO_QUALITY_UP, $i);
$info = null;
if ($heroUniId) {
$heroDb = Hero::find($heroUniId);
if ($heroDb) {
$heroDto = Hero::toDto($heroDb);
$info = array(
'info' => $heroDto,
'countdown' => $heroDto['unlock_lefttime']
);
}
}
array_push($infos, $info);
}
$this->_rspData(array(
'infos' => $infos
));
}
public function receive()
{
$type = getReqVal('type', 0);
$heroUniId = getReqVal('hero_uniid', 0);
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(1, 'hero does not exist1');
return;
}
$heroDto = Hero::toDto($heroDb);
if ($heroDto['unlock_lefttime'] > 0) {
$this->_rspErr(1, 'Countdown is not over');
return;
}
$oldHero = $heroDto;
$newHero = $heroDto;
switch ($type) {
case 1:
{
$idx = 0;
$found = false;
for ($i = 0; $i < kMaxHeroUpLevelNum; ++$i) {
$upHeroUniId = $this->_getV(TN_HERO_LEVEL_UP, $i);
if ($upHeroUniId == $heroUniId) {
$idx = $i;
$found = true;
break;
}
}
if (!$found) {
$this->_rspErr(1, 'hero does not exist2');
return;
}
$this->_setV(TN_HERO_LEVEL_UP, $idx, 0);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addHeroChg();
$propertyChgService->addBagChg();
$propertyChgService->addUserChg();
$currLevelMeta = mt\HeroLevel::getByLevel($heroDb['hero_lv']);
if ($currLevelMeta) {
$nextLevelMeta = mt\HeroLevel::getByLevel($heroDb['hero_lv'] + 1);
$attrs = $heroDto['attr'];
mt\HeroLevelAttr::addRandAttr($heroDb['hero_lv'] + 1, $attrs);
if ($nextLevelMeta) {
Hero::update($heroUniId,
array(
'hero_lv' => $heroDb['hero_lv'] + 1,
'rand_attr' => json_encode($attrs),
'lock_type' => 0,
'unlock_time' => 0,
)
);
$newHeroDb = Hero::find($heroUniId);
$newHero = Hero::toDto($newHeroDb);
$rankActivityService = new services\RankActivityService();
$rankActivityService->heroUpgradeLevel($heroDb['hero_lv'] + 1);
}
}
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
'old_hero' => $oldHero,
'new_hero' => $newHero,
));
}
break;
case 2:
{
$idx = 0;
$found = false;
for ($i = 0; $i < kMaxHeroUpQualityNum; ++$i) {
$upHeroUniId = $this->_getV(TN_HERO_QUALITY_UP, $i);
if ($upHeroUniId == $heroUniId) {
$idx = $i;
$found = true;
break;
}
}
if (!$found) {
$this->_rspErr(1, 'hero does not exist');
return;
}
$costHeroUniId = $this->_getV(TN_HERO_QUALITY_UP, (int)$idx + 1000);
$this->_setV(TN_HERO_QUALITY_UP, $idx, 0);
$this->_setV(TN_HERO_QUALITY_UP, (int)$idx + 1000, 0);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addHeroChg();
$propertyChgService->addBagChg();
$propertyChgService->addUserChg();
$currQualityMeta = mt\HeroQuality::getByQuality($heroDb['quality']);
$nextQualityMeta = null;
if ($currQualityMeta) {
$nextQualityMeta = mt\HeroQuality::getByQuality($heroDb['quality'] + 1);
if ($nextQualityMeta) {
$rnd = rand(1, 100);
if ($rnd > $nextQualityMeta['success_rate']) {
Hero::update($costHeroUniId,
array(
'lock_type' => 0,
'unlock_time' => 0,
)
);
Hero::update($heroUniId,
array(
'advanced_count' => $heroDb['advanced_count'] + 1,
'lock_type' => 0,
'unlock_time' => 0,
)
);
$this->_rspRawData(array(
'errcode' => 1,
'errmsg' => 'advance failed',
'property_chg' => $propertyChgService->toDto(),
));
return;
}
Hero::update($heroUniId,
array(
'quality' => $heroDb['quality'] + 1,
'advanced_count' => $heroDb['advanced_count'] + 1,
'lock_type' => 0,
'unlock_time' => 0,
)
);
Hero::update($costHeroUniId,
array(
'account_id' => myself()->_getAccountId() . '!!!',
'lock_type' => 0,
'unlock_time' => 0,
)
);
$newHeroDb = Hero::find($heroUniId);
$newHero = Hero::toDto($newHeroDb);
$rankActivityService = new services\RankActivityService();
$rankActivityService->heroUpgradeLevel($heroDb['quality'] + 1);
}
}
if (!$nextQualityMeta) {
$this->_rspErr(1, 'quality is full');
return;
}
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
'old_hero' => $oldHero,
'new_hero' => $newHero,
));
}
break;
default:
{
$this->_rspErr(1, 'type parameter error');
return;
}
break;
}
}
public function upgradeLevel()
{
$costItemId = getReqVal('cost_item_id', 0);
$heroUniId = getReqVal('hero_uniid', 0);
$slotId = getReqVal('slot_id', 0);
$heroDb = Hero::find($heroUniId);
if (!in_array($slotId, array(0, kMaxHeroUpLevelNum - 1))) {
$this->_rspErr(1, 'slot_id parameter error');
return;
}
{
$srcHeroDb = Hero::find($this->_getV(TN_HERO_LEVEL_UP, $slotId));
if ($srcHeroDb) {
$this->_rspErr(1, 'slot_id parameter error');
return;
}
}
if (!$heroDb) {
$this->_rspErr(1, 'hero does not exist');
return;
}
if ($heroDb['state'] != Hero::GETED_STATE) {
$this->_rspErr(3, 'Trial hero cannot operate');
return;
}
if ($heroDb['unlock_time'] > $this->_getNowTime()) {
$this->_rspErr(2, 'Cannot operate during locking');
return;
}
if ($heroDb['unlock_trade_time'] > $this->_getNowTime()) {
$this->_rspErr(2, 'Cannot operate during locking');
return;
}
$heroMeta = mt\Hero::get($heroDb['hero_id']);
if (!$heroMeta) {
$this->_rspErr(100, 'server internal error');
return;
}
$currLevelMeta = mt\HeroLevel::getByLevel($heroDb['hero_lv']);
if (!$currLevelMeta) {
$this->_rspErr(100, 'server internal error');
return;
}
$nextLevelMeta = mt\HeroLevel::getByLevel($heroDb['hero_lv'] + 1);
if (!$nextLevelMeta) {
$this->_rspErr(5, "It's already the highest level");
return;
}
$costItems = array();
switch ($costItemId) {
case V_ITEM_GOLD:
{
$costItems = array(
array(
'item_id' => $costItemId,
'item_num' => $currLevelMeta['gold']
)
);
}
break;
case V_ITEM_DIAMOND:
{
$costItems = array(
array(
'item_id' => $costItemId,
'item_num' => $currLevelMeta['diamond']
)
);
}
break;
default:
{
$this->_rspErr(2, 'Payment method not supported');
return;
}
break;
}
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
{
Hero::update(
$heroUniId,
array(
'lock_type' => Hero::LEVEL_LOCK,
'unlock_time' => $this->_getNowTime() + $nextLevelMeta['time'],
)
);
}
$this->_setV(TN_HERO_LEVEL_UP, (int)$slotId, (int)$heroDb['idx']);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addHeroChg();
$propertyChgService->addBagChg();
$propertyChgService->addUserChg();
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function upgradeQuality()
{
$costItemId = getReqVal('cost_item_id', 0);
$costHeroUniId = getReqVal('cost_hero_uniid', 0);
$heroUniId = getReqVal('hero_uniid', 0);
$slotId = getReqVal('slot_id', 0);
$heroDb = Hero::find($heroUniId);
if (!in_array($slotId, array(0, kMaxHeroUpQualityNum - 1))) {
$this->_rspErr(1, 'slot_id parameter error');
return;
}
$costHeroDb = Hero::find($costHeroUniId);
if (!$costHeroDb) {
$this->_rspErr(1, 'cost hero parameter error');
return;
}
if ($costHeroDb['lock_type']) {
$this->_rspErr(2, 'Cannot operate during locking');
return;
}
{
$srcHeroDb = Hero::find($this->_getV(TN_HERO_QUALITY_UP, $slotId));
if ($srcHeroDb) {
$this->_rspErr(1, 'slot_id parameter error');
return;
}
}
{
$idx = 0;
$found = false;
for ($i = 0; $i < kMaxHeroUpQualityNum; ++$i) {
$upHeroUniId = $this->_getV(TN_HERO_QUALITY_UP, $i);
if ($upHeroUniId == $heroUniId) {
$idx = $i;
$found = true;
break;
}
}
if ($found) {
$this->_rspErr(1, 'hero already upgrading');
return;
}
}
if (!$heroDb) {
$this->_rspErr(1, 'hero does not exist');
return;
}
if ($heroDb['advanced_count'] >= mt\Parameter::getVal('advence_limit', 0)) {
$this->_rspErr(1, 'hero does not exist');
return;
}
if ($heroDb['state'] != Hero::GETED_STATE) {
$this->_rspErr(3, 'Trial hero cannot operate');
return;
}
if ($heroDb['lock_type']) {
$this->_rspErr(2, 'Cannot operate during locking');
return;
}
if ($heroDb['unlock_trade_time'] > $this->_getNowTime()) {
$this->_rspErr(2, 'Cannot operate during locking');
return;
}
$heroMeta = mt\Hero::get($heroDb['hero_id']);
if (!$heroMeta) {
$this->_rspErr(100, 'server internal error');
return;
}
$currQualityMeta = mt\HeroQuality::getByQuality($heroDb['quality']);
if (!$currQualityMeta) {
$this->_rspErr(100, 'server internal error');
return;
}
$nextQualityMeta = mt\HeroQuality::getByQuality($heroDb['quality'] + 1);
if (!$nextQualityMeta) {
$this->_rspErr(5, "It's already the highest level1");
return;
}
$nextLevelMeta = mt\HeroLevel::getByLevel($heroDb['hero_lv']);
if (!$nextLevelMeta) {
$this->_rspErr(5, "It's already the highest level2");
return;
}
$costItems = array();
switch ($costItemId) {
case V_ITEM_GOLD:
{
$costItems = array(
array(
'item_id' => $costItemId,
'item_num' => $currQualityMeta['gold']
)
);
}
break;
case V_ITEM_DIAMOND:
{
$costItems = array(
array(
'item_id' => $costItemId,
'item_num' => $currQualityMeta['diamond']
)
);
}
break;
default:
{
$this->_rspErr(2, 'Payment method not supported');
return;
}
break;
}
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
Hero::update($heroUniId,
array(
'lock_type' => Hero::QUALITY_LOCK,
'unlock_time' => $this->_getNowTime() + $nextQualityMeta['time'],
)
);
Hero::update($costHeroUniId,
array(
'lock_type' => Hero::COST_LOCK,
'unlock_time' => $this->_getNowTime() + $nextQualityMeta['time'],
)
);
$this->_setV(TN_HERO_QUALITY_UP, (int)$slotId, (int)$heroDb['idx']);
$this->_setV(TN_HERO_QUALITY_UP, (int)$slotId + 1000, (int)$costHeroDb['idx']);
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addHeroChg();
$propertyChgService->addBagChg();
$propertyChgService->addUserChg();
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function upgradeQualityPreview()
{
$heroUniId = getReqVal('hero_uniid', 0);
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(100, 'server internal error');
return;
}
$heroMeta = mt\Hero::get($heroDb['hero_id']);
if (!$heroMeta) {
$this->_rspErr(100, 'server internal error');
return;
}
$currQualityMeta = mt\HeroQuality::getByQuality($heroDb['quality']);
if (!$currQualityMeta) {
$this->_rspErr(100, 'server internal error');
return;
}
$nextQualityMeta = mt\HeroQuality::getByQuality($heroDb['quality'] + 1);
if (!$nextQualityMeta) {
$this->_rspErr(5, "It's already the highest level1");
return;
}
$nextLevelMeta = mt\HeroLevel::getByLevel($heroDb['hero_lv']);
if (!$nextLevelMeta) {
$this->_rspErr(5, "It's already the highest level2");
return;
}
$newHeroDb = $heroDb;
$newHeroDb['quality'] += 1;
$heroDto = Hero::toDto($heroDb);
$newHeroDto = Hero::toDto($newHeroDb);
$this->_rspData(array(
'old_hero' => $heroDto,
'new_hero' => $newHeroDto
));
}
}