game2006api/webapp/controller/BagController.class.php
aozhiwei d7451619b6 1
2024-04-03 21:56:12 +08:00

587 lines
19 KiB
PHP

<?php
require_once('mt/Parameter.php');
require_once('mt/Item.php');
require_once('mt/Drop.php');
require_once('mt/Hero.php');
require_once('models/Bag.php');
require_once('models/Hero.php');
require_once('models/Gun.php');
require_once('models/Nft.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
require_once('services/LogService.php');
require_once('services/NameService.php');
require_once('services/LootService.php');
use phpcommon\SqlHelper;
use models\Bag;
use models\Hero;
use models\Gun;
use models\Nft;
use services\LogService;
class BagController extends BaseAuthedController {
private $propertyChgService = null;
private $awardService = null;
public function _handlePre()
{
parent::_handlePre();
$this->propertyChgService = new services\PropertyChgService();
$this->awardService = new services\AwardService();
}
public function itemList()
{
$itemList = Bag::all();
$itemDtoList = array();
foreach ($itemList as $item) {
array_push($itemDtoList, Bag::toDto($item));
}
$this->_rspData(array(
'item_list' => $itemDtoList,
));
}
public function createGuildConsume()
{
$consume = explode("|",mt\Parameter::getVal('create_club_cost'));
$costItems = array(
array(
'item_id' => $consume[0],
'item_num' => $consume[1]
),
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
$this->_rspOk();
}
// 兑换水晶
public function exchangeCrystal()
{
// step1. consume items
// step2. send rewards
$this->_rspOk();
}
public function useItem()
{
$itemId = getReqVal('item_id', 0);
$itemNum = getReqVal('item_num', 0);
$param1 = getReqVal('param1', '');
$param2 = getReqVal('param2', '');
$param3 = getReqVal('param3', '');
if ($itemNum < 0) {
$this->_rspErr(1, 'item_num must be greater than 0');
return;
}
$itemDb = Bag::find($itemId);
if (!isset($itemDb) || $itemDb['item_num'] <= 0) {
$this->_rspErr(1, 'Not enough item');
return;
}
$itemMeta = mt\Item::get($itemId);
if (!$itemMeta) {
$this->_rspErr(2, 'config error');
return;
}
if ($itemMeta['type'] == mt\Item::FUNC_TYPE) {
switch ($itemMeta['sub_type']) {
case mt\Item::FUNC_RENAME_CARD_SUBTYPE:
{
$this->renameCard($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3);
}
break;
case mt\Item::FUNC_GUILD_CARD_SUBTYPE:
{
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => 1
)
));
$this->_rspOk();
return;
}
break;
case mt\Item::RECOVER_DRUG_SUBTYPE:
{
$this->useRecoverDrug($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3);
}
break;
case mt\Item::PACKAGING_SUBTYPE:
{
$this->useSealDrug($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3);
}
break;
default:
{
$this->_rspErr(4, 'The prop function has not been realized yet');
return;
}
break;
}
} else if ($itemMeta['type'] == mt\Item::GIFT_PACKAGE_TYPE) {
$this->openGiftPackage($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3);
} else if ($itemMeta['type'] == mt\Item::TREASURE_BOX) {
$this->openTreasureBox($itemDb, $itemMeta, $itemNum);
} else if ($itemMeta['type'] == mt\Item::BATTLE_REWARD_BOX) {
$this->openBattleBox($itemDb, $itemMeta, $itemNum);
} else {
$this->_rspErr(4, 'The prop function has not been realized yet');
}
}
public function rename()
{
$userInfo = $this->_getOrmUserInfo();
if ($userInfo['rename_count'] == 0){
$errCode = 0;
$errMsg = '';
$this->internalRename(getReqVal('name', ''), getReqVal('name_sign', ''), $errCode, $errMsg);
if ($errCode) {
$this->_rspErr($errCode, $errMsg);
return;
}
}
$itemDto = Bag::findByType(mt\Item::FUNC_TYPE, mt\Item::FUNC_RENAME_CARD_SUBTYPE);
error_log(json_encode($itemDto));
if (!$itemDto || $itemDto['item_num'] < 0) {
$this->_rspErr(1, 'Not enough item');
return;
}
$errCode = 0;
$errMsg = '';
$this->internalRename(getReqVal('name', ''), getReqVal('name_sign', ''), $errCode, $errMsg);
if ($errCode) {
$this->_rspErr($errCode, $errMsg);
return;
}
$this->_decItems(array(
array(
'item_id' => $itemDto['item_id'],
'item_num' => 1
)
));
$this->propertyChgService->addBagChg();
$this->propertyChgService->addUserChg();
$this->_rspData(array(
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function oldRename()
{
$itemDto = Bag::findByType(mt\Item::FUNC_TYPE, mt\Item::FUNC_RENAME_CARD_SUBTYPE);
$userInfo = $this->_getOrmUserInfo();
if ($userInfo['rename_count'] == 0){
$errCode = 0;
$errMsg = '';
$this->internalRename(getReqVal('name', ''), getReqVal('name_sign', ''), $errCode, $errMsg);
if ($errCode) {
$this->_rspErr($errCode, $errMsg);
return;
}
}else{
if (!$itemDto || $itemDto['item_num'] < 0) {
if (mt\Parameter::getVal('rename_diamond_cost', 0) <= 0) {
$this->_rspErr(1, 'config error');
return;
}
if ($userInfo['diamond'] < mt\Parameter::getVal('rename_diamond_cost', 0)) {
$this->_rspErr(1, 'Not enough item');
return;
}
}
$errCode = 0;
$errMsg = '';
$this->internalRename(getReqVal('name', ''), getReqVal('name_sign', ''), $errCode, $errMsg);
if ($errCode) {
$this->_rspErr($errCode, $errMsg);
return;
}
if (!$itemDto || $itemDto['item_num'] <= 0) {
$this->_decItems(array(
array(
'item_id' => V_ITEM_DIAMOND,
'item_num' => mt\Parameter::getVal('rename_diamond_cost', 0)
)
));
{
//埋点
$event = [
'name' => LogService::USER_RENAME,
'val' => mt\Parameter::getVal('rename_diamond_cost', 0)
];
LogService::consumeDiamond($event);
myself()->_incV(TN_TOTAL_DIAMOND_CONSUME, 0, mt\Parameter::getVal('rename_diamond_cost', 0));
}
$this->propertyChgService->addUserChg();
} else {
$this->_decItems(array(
array(
'item_id' => $itemDto['item_id'],
'item_num' => 1
)
));
$this->propertyChgService->addBagChg();
}
}
$this->propertyChgService->addUserChg();
$this->_rspData(array(
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function renameCard($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3)
{
$errCode = 0;
$errMsg = '';
$this->internalRename($param1, $param2, $errCode, $errMsg);
if ($errCode) {
$this->_rspErr($errCode, $errMsg);
return;
}
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => 1
)
));
$this->propertyChgService->addUserChg();
$this->propertyChgService->addBagChg();
$this->_rspData(array(
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function internalRename($name, $nameSign, &$errCode, &$errMsg)
{
$errCode = 0;
$errMsg = '';
if (mb_strlen($name) < 3) {
$errCode = 5;
$errMsg = 'Parameter error name length must not be less than 3';
return;
}
if (mb_strlen($name, 'utf8') > 12) {
$errCode = 5;
$errMsg = 'Parameter error name length must not be greater than 12';
return;
}
/*if(!preg_match("/^[a-z\d]*$/i",$name))
{
$errCode = 5;
$errMsg = 'Excuse me , the name you entered is in the wrong format.';
return;
}*/
$nameService = new services\NameService();
if (!$nameService->verifyNameSign($name, $nameSign)){
$errCode = 5;
$errMsg = 'Parameter name error, signature verification failed';
return;
}
if ($nameService->nameUsed($name)){
$errCode = 5;
$errMsg = 'This name has been taken';
return;
}
$ret = $nameService->useName($name);
if (!$ret) {
$errCode = 10;
$errMsg = 'server internal error';
return;
}
$this->_updateUserInfo(array(
'name' => $name,
'rename_count' => function () {
return 'rename_count + 1';
}
));
}
private function openGiftPackage($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3)
{
$dropMeta = mt\Drop::get($itemMeta['drop']);
if (!$dropMeta) {
$this->_rspErr(1, 'config error');
return;
}
$costItems = mt\Item::getUseCostItems($itemMeta);
error_log(json_encode($costItems));
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => 1
)
));
$this->_scatterDrop('gift_package:' . $itemMeta['id'],
$dropMeta,
$this->awardService,
$this->propertyChgService);
$this->propertyChgService->addBagChg();
$this->_rspData(array(
'award' => $this->awardService->toDto(),
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function openTreasureBox($itemDb,$itemMeta,$itemNum){
$dropMeta = mt\Drop::get($itemMeta['drop']);
if (!$dropMeta) {
$this->_rspErr(1, 'config error');
return;
}
$costItems = mt\Item::getUseCostItems($itemMeta);
foreach ($costItems as &$costItem){
$costItem['item_num'] *= $itemNum;
}
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
if ($itemDb['item_num'] < $itemNum){
$this->_rspErr(3, "item inadequate in number");
return;
}
$this->_decItems($costItems);
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => $itemNum
)
));
for ($i=1;$i<=$itemNum;$i++){
$this->_scatterDrop('serverBox:' . $itemMeta['id'], $dropMeta, $this->awardService, $this->propertyChgService);
}
$this->propertyChgService->addBagChg();
$this->_rspData(array(
'award' => $this->awardService->toDto(),
'property_chg' => $this->propertyChgService->toDto(),
));
}
// public function test(){
// $itemMeta = mt\Item::get(400001);
// $itemDb= 0;
// $itemNum= 0;
// $this->openBattleBox($itemDb,$itemMeta,$itemNum);
//// print_r($itemMeta);
// }
private function openBattleBox($itemDb,$itemMeta,$itemNum){
$lucky = Hero::getAccountLucky(myself()->_getAddress());
$luckyParam = \mt\Parameter::getVal('economy_account_luck_K',0);
$luckyMaxPro = \mt\Parameter::getVal('economy_account_luck_E',0);
$prob = $luckyMaxPro * ($lucky / ($lucky+$luckyParam));
$times = $this->proEffect($prob,1);
$items = \services\LootService::dropOutItem($itemMeta['loot']);
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => 1
)
));
if ($times > 0){
foreach ($items as &$item){
$item['item_num'] += $item['item_num']*$times;
}
}
$this->_addItems($items,$this->awardService,$this->propertyChgService);
$this->propertyChgService->addBagChg();
$this->_rspData(array(
'award' => $this->awardService->toDto(),
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function proEffect($p,$b){
if ($p * $b > -1 && $b != 0) {
$x = floor($p) * $b;
$y = max($x+$b,-1);
$q = ($p * $b - $y) / ($x - $y);
$rnd = rand(1,100);
return $rnd < $q*100 ? $x : $y;
}else{
return 0;
}
}
public function buyItem()
{
$itemId = getReqVal('item_id', 0);
$itemMeta = mt\Item::get($itemId);
if (!$itemMeta) {
$this->_rspErr(1, "item_id error");
return;
}
if (!(
$itemMeta['type'] == mt\Item::FUNC_TYPE &&
$itemMeta['sub_type'] == mt\Item::FUNC_RENAME_CARD_SUBTYPE)) {
$this->_rspErr(1, "only supported rename card");
return;
}
$costItems= array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => $itemMeta['gold']
)
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$items = array(
array(
'item_id' => $itemId,
'item_num' => 1
)
);
$this->_decItems($costItems);
$this->_addItems($items, $this->awardService, $this->propertyChgService);
$this->propertyChgService->addUserChg();
$this->propertyChgService->addBagChg();
$this->_rspData(array(
'award' => $this->awardService->toDto(),
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function useRecoverDrug($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3)
{
$errCode = 0;
$errMsg = '';
$heroUniId = $param1;
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(1, "You don't have the hero yet");
return;
}
if (!$heroDb['token_id']){
$this->_rspErr(1, "this hero is not NFT");
return;
}
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => 1
)
));
Hero::recoverUsedTime($heroUniId);
$this->propertyChgService->addBagChg();
$this->propertyChgService->addHeroChg();
$this->_rspData(array(
'property_chg' => $this->propertyChgService->toDto(),
));
}
private function useSealDrug($itemDb, $itemMeta, $itemNum, $param1, $param2, $param3)
{
$errCode = 0;
$errMsg = '';
$heroUniId = $param1;
$heroDb = Hero::find($heroUniId);
if (!$heroDb) {
$this->_rspErr(1, "You don't have the hero yet");
return;
}
if (!$heroDb['token_id']){
$this->_rspErr(1, "this hero is not NFT");
return;
}
$this->_decItems(array(
array(
'item_id' => $itemMeta['id'],
'item_num' => 1
)
));
Hero::update($heroUniId,array(
'seal_type' => 1
));
$this->propertyChgService->addBagChg();
$this->propertyChgService->addHeroChg();
$this->_rspData(array(
'property_chg' => $this->propertyChgService->toDto(),
));
}
public function syntheticGold(){
$itemId = getReqVal('item_id', 0);
$paramMeta = mt\Parameter::getVal('gold_synthesis_rank',0);
if (!$paramMeta){
$this->_rspErr(1,"config error");
return ;
}
$consume = explode("|",$paramMeta);
switch ($itemId){
case V_ITEM_GOLDS :{
$costItems = array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => $consume[0]
)
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
Bag::addItem(V_ITEM_GOLDS,1);
}
break;
case V_ITEM_GOLDBRICK :{
$costItems = array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => $consume[1]
)
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
Bag::addItem(V_ITEM_GOLDBRICK,1);
}
break;
default:{
$this->_rspErr(1,"param error");
return ;
}
}
$propertyChgService = new services\PropertyChgService();
$propertyChgService->addUserChg();
$propertyChgService->addBagChg();
$this->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
}