game2006api/webapp/controller/BaseAuthedController.class.php
2023-07-10 20:01:15 +08:00

761 lines
25 KiB
PHP

<?php
require_once('mt/Item.php');
require_once('models/Bag.php');
require_once('models/DynData.php');
require_once('models/Gun.php');
require_once('models/Hero.php');
require_once('models/HeroSkin.php');
require_once('models/GunSkin.php');
require_once('models/User.php');
require_once('models/Nft.php');
require_once('models/UserSeasonRing.php');
require_once('models/Parachute.php');
require_once('models/Chip.php');
require_once('models/Pass.php');
require_once('mt/Parameter.php');
require_once('mt/RankSeason.php');
require_once('mt/LevelUp.php');
require_once('mt/Task.php');
require_once('mt/Drop.php');
require_once('mt/StarLevel.php');
require_once('mt/BattlePass.php');
use phpcommon\SqlHelper;
use models\Bag;
use models\DynData;
use models\Hero;
use models\Gun;
use models\HeroSkin;
use models\GunSkin;
use models\User;
use models\Nft;
use models\Parachute;
use models\UserSeasonRing;
use models\Chip;
use models\Pass;
class BaseAuthedController extends BaseController {
protected $accountId = '';
private $sessionId = '';
private $mysqlConn = null;
private $address = null;
private $addressActived = false;
private function isWhiteList()
{
$whiteList = array(
'0x875835829e95fe87e53a2dfcfd9860a735b70754',
'0x11299400d87f24e79af8cb8cc5661792bea45300',
'0x898a887fa574dd0297f202d66d5a65787acc1316',
'0x9b703a5a094df5b1ba4f3dec15810f7c708e31fa',
'0xad2dea1977055db01c66e6e53309c4604ab869b8',
'0xef59f6cc4d190a0ae576c46d4583e92b61174340'
);
return in_array(myself()->_getOpenId(), $whiteList);
}
public function switchAccount($accountId){
$this->accountId = $accountId;
$this->mysqlConn = null;
}
public function _handlePre()
{
// if (SERVER_ENV == _ONLINE) {
// if (getReqVal('client_uuid', '') != '499af8a0-a1bc-0b0e-dc79-a42cb3f103dc') {
// if ((getReqVal('c', '') != 'Battle')) {
// phpcommon\sendError(1001, 'session expiration');
// die();
// }
// }
// }
$this->accountId = getReqVal('account_id', '');
$this->sessionId = getReqVal('session_id', '');
if (SERVER_ENV != _DEBUG) {
if (!phpcommon\isValidSessionId($this->accountId,
$this->sessionId)) {
phpcommon\sendError(500, 'invalid session_id');
die();
}
}
if (!(getReqVal('c', '') == 'User' && getReqVal('a', '') == 'login')) {
if ((getReqVal('c', '') == 'Battle')) {
return;
}
$r = $this->_getRedis($this->_getAccountId());
$sessionId = $r->get(LAST_SESSION_KEY . $this->_getAccountId());
// error_log('SERVER_ENV:' . SERVER_ENV . '_DEBUG:' . _DEBUG);
if (SERVER_ENV != _DEBUG)
{
if (empty($sessionId)) {
$this->updateSession(myself()->_getAccountId(),
myself()->_getSessionId());
} else if ($sessionId != $this->_getSessionId()) {
error_log('session expiration' . json_encode(
$_REQUEST
));
phpcommon\sendError(1001, 'session expiration');
die();
}
}
$this->_userLvRestriction();
}
/*if (SERVER_ENV == _ONLINE) {
if (phpcommon\cmpVersion(getReqVal('_version', ''), '0.2.0') > 0) {
if (!$this->isWhiteList() || myself()->_getChannel() != BC_CHANNEL) {
phpcommon\sendError(1002, '');
die();
}
}
}*/
}
protected function _userLvRestriction(){
$userDb = $this->_getOrmUserInfo();
$controller = getReqVal('c', '');
switch ($controller){
case "Chip" : {
if (getReqVal('a', '') != 'chipList' && $userDb['star_num'] < \mt\StarLevel::STAR_NUM_CHIP_LIMIT){
$this->_rspErr(1,'Not agreed terms. star number of need ' .\mt\StarLevel::STAR_NUM_CHIP_LIMIT);
die();
}
}
break;
case "Emoji" : {
if ($userDb['star_num'] < \mt\StarLevel::STAR_NUM_EMOJI_LIMIT){
$this->_rspErr(1,'Not agreed terms star number of need ' .\mt\StarLevel::STAR_NUM_EMOJI_LIMIT);
die();
}
}
break;
case "Fragment" : {
if (getReqVal('a', '') == 'syntheticFragment' && $userDb['star_num'] < \mt\StarLevel::STAR_NUM_PIECE_LIMIT){
$this->_rspErr(1,'Not agreed terms star number of need ' .\mt\StarLevel::STAR_NUM_PIECE_LIMIT);
die();
}
}
}
}
protected function updateSession($accountId, $sessionId)
{
$r = $this->_getRedis($this->_getAccountId());
$r->set(LAST_SESSION_KEY . $this->_getAccountId(), $sessionId);
$r->pexpire(LAST_SESSION_KEY . $this->_getAccountId(), 1000 * 3600 * 24);
}
public function _getAccountId()
{
return $this->accountId;
}
public function _getOpenId()
{
return phpcommon\extractOpenId($this->_getAccountId());
}
public function _getChannel()
{
return phpcommon\extractChannel($this->_getAccountId());
}
public function _getAddress()
{
if (!$this->addressActived) {
$userDb = $this->_getOrmUserInfo();
$this->address = $userDb['address'];
$this->addressActived = true;
}
return $this->address;
}
public function _isValidAddress()
{
$address = $this->_getAddress();
return !empty($address);
}
public function _getSessionId()
{
return $this->sessionId;
}
public function _getRegisterTime()
{
$registertime = phpcommon\extractRegisterTimeFromSessionId($this->sessionId);
return $registertime;
}
public function _getSelfMysql()
{
if (!$this->mysqlConn) {
$this->mysqlConn = $this->_getMysql($this->_getAccountId());
}
return $this->mysqlConn;
}
public function _getUserInfo($fields)
{
$row = SqlHelper::selectOne
($this->_getSelfMysql(),
't_user',
$fields,
array(
'account_id' => $this->_getAccountId()
)
);
if (empty($row)) {
phpcommon\sendError(500, 'server internal error');
error_log('getUserInfo error '. $this->_getAccountId());
die();
}
return $row;
}
public function _getOrmUserInfo()
{
$row = SqlHelper::ormSelectOne
($this->_getSelfMysql(),
't_user',
array(
'account_id' => $this->_getAccountId()
)
);
if (empty($row)) {
phpcommon\sendError(500, 'server internal error');
error_log('getUserInfo error '. $this->_getAccountId());
die();
}
return $row;
}
public function _safeGetOrmUserInfo()
{
$row = SqlHelper::ormSelectOne
($this->_getSelfMysql(),
't_user',
array(
'account_id' => $this->_getAccountId()
)
);
return $row ? $row : null;
}
public function _updateUserInfo($fieldsKv)
{
SqlHelper::update
($this->_getSelfMysql(),
't_user',
array(
'account_id' => $this->_getAccountId()
),
$fieldsKv
);
}
public function _getItemCount($itemId, $userInfo)
{
switch ($itemId) {
case V_ITEM_GOLD:
{
return $userInfo['gold'];
}
break;
case V_ITEM_DIAMOND:
{
return $userInfo['diamond'];
}
break;
case V_ITEM_EXP:
{
return $userInfo['exp'];
}
break;
case V_ITEM_ACTIVE:
{
return $this->_getV(TN_ACTIVE, 0);
}
break;
default:
{
return Bag::getItemCount($itemId);
}
break;
}
return 0;
}
public function _isVirtualItem($itemId)
{
return in_array($itemId, array(V_ITEM_GOLD, V_ITEM_DIAMOND, V_ITEM_EXP, V_ITEM_ACTIVE,V_ITEM_BCEG,V_ITEM_STAR));
}
public function _addVirtualItem($itemId, $itemNum,$awardService,$propertyChgService)
{
if ($itemNum <= 0){
return;
}
switch ($itemId) {
case V_ITEM_GOLD:
{
$this->_updateUserInfo(array(
'gold' => function () use($itemNum) {
return "gold + ${itemNum}";
}
));
}
break;
case V_ITEM_DIAMOND:
{
$this->_updateUserInfo(array(
'diamond' => function () use($itemNum) {
return "diamond + ${itemNum}";
}
));
}
break;
case V_ITEM_BCEG:
{
$this->_updateUserInfo(array(
'bceg' => function () use($itemNum) {
return "bceg + ${itemNum}";
}
));
}
break;
case V_ITEM_EXP:
{
// $this->_updateUserInfo(array(
// 'exp' => function () use($itemNum) {
// return "exp + ${itemNum}";
// }
// ));
$userDb = $this->_safeGetOrmUserInfo();
$newlV = $userDb['level'];
$newExp = $userDb['exp']+$itemNum;
\mt\BattlePass::getExpByLv($newlV,$newExp);
$this->_updateUserInfo(array(
'level' => $newlV,
'exp' => $newExp
));
if ($newlV != $userDb['level']){
$currSeasonMeta = \mt\BattlePass::getCurrentSeason();
$passDb = Pass::find($currSeasonMeta['id']);
$rewards = emptyReplace(json_decode($passDb['data'], true), array());
foreach ($rewards['basic'] as &$reward){
if ($newlV >= $reward['level'] && $reward['state']==-1){
$reward['state'] = 0;
}
}
if ($userDb['activated']){
foreach ($rewards['platinum'] as &$reward){
if ($newlV >= $reward['level'] && $reward['state']==-1){
$reward['state'] = 0;
}
}
}
Pass::upsert($currSeasonMeta['id'],json_encode($rewards));
}
}
break;
case V_ITEM_ACTIVE:
{
$addItem = max(0, mt\Parameter::getVal('activeness_limit', 0) - $this->_getDailyV(TN_DAILY_ACTIVE, 0));
$addItem = min($addItem, $itemNum);
if ($addItem > 0) {
$this->_incV(TN_ACTIVE, 0, $itemNum);
$this->_incDailyV(TN_DAILY_ACTIVE, 0, $itemNum);
$this->_incWeeklyV(TN_WEEKLY_ACTIVE, 0, $itemNum);
}
}
break;
case V_ITEM_STAR:
{
$this->_updateUserInfo(array(
'star_num' => function () use($itemNum) {
return "star_num + ${itemNum}";
}
));
$this->_incDailyV(TN_DAILY_GET_STAR_NUM, 0,$itemNum);
}
break;
default:
{
}
break;
}
}
public function _decVirtualItem($itemId, $itemNum)
{
if ($itemNum <= 0){
return;
}
switch ($itemId) {
case V_ITEM_GOLD:
{
$this->_updateUserInfo(array(
'gold' => function () use($itemNum) {
return "GREATEST(0, gold - ${itemNum})";
},
'consume_gold' => function () use ($itemNum){
return "consume_gold + ${itemNum}";
}
));
$this->_incDailyV(TN_DAILY_USED_GOLD_NUM, 0, $itemNum);
}
break;
case V_ITEM_DIAMOND:
{
$this->_updateUserInfo(array(
'diamond' => function () use($itemNum) {
return "GREATEST(0, diamond - ${itemNum})";
}
));
}
break;
default:
{
}
break;
}
}
public function _addItems($items, $awardService, $propertyService)
{
$heads = array();
$headFrames = array();
foreach ($items as $item) {
if ($awardService){
$awardService->addItem($item['item_id'], $item['item_num']);
}
if ($this->_isVirtualItem($item['item_id'])) {
$this->_addVirtualItem($item['item_id'], $item['item_num'],$awardService,$propertyService);
$propertyService->addUserChg();
} else {
$itemMeta = mt\Item::get($item['item_id']);
if ($itemMeta) {
if (mt\Item::isBagItem($itemMeta['type'], $itemMeta['sub_type'])) {
Bag::addItem($item['item_id'], $item['item_num']);
$propertyService->addBagChg();
} else {
for ($i=0; $i<$item['item_num']; $i++){
switch ($itemMeta['type']) {
case mt\Item::HERO_TYPE:
{
$res = Hero::addHero($itemMeta);
if ($res){
$lastIdx = SqlHelper::getLastInsertId( myself()->_getSelfMysql());
$awardService->addHero($item['item_id'],$lastIdx);
}
$propertyService->addHeroChg();
$propertyService->addUserChg();
}
break;
case mt\Item::HERO_SKIN_TYPE:
{
HeroSkin::addSkin($itemMeta);
$propertyService->addHeroSkinChg();
}
break;
case mt\Item::HEAD_TYPE:
{
array_push($heads, $itemMeta['id']);
$propertyService->addUserChg();
}
break;
case mt\Item::HEAD_FRAME_TYPE:
{
array_push($headFrames, $itemMeta['id']);
$propertyService->addUserChg();
}
break;
case mt\Item::GUN_TYPE:
{
Gun::addGun($itemMeta);
$propertyService->addGunChg();
}
break;
case mt\Item::GUN_SKIN_TYPE:
{
GunSkin::addSkin($itemMeta['id']);
$propertyService->addGunSkinChg();
}
break;
case mt\Item::FRAGMENT_BOX_TYPE:
{
Bag::addItem($item['item_id'], $item['item_num']);
$propertyService->addBagChg();
}
break;
case mt\Item::RING_TYPE:
{
UserSeasonRing::addRing($itemMeta);
$propertyService->addUserChg();
}
break;
case mt\Item::PARACHUTE_TYPE:
{
Parachute::addParachute($itemMeta);
$propertyService->addUserChg();
}
break;
case mt\Item::CHIP_TYPE:
{
Chip::addChip($itemMeta);
$propertyService->addChip();
}
break;
case mt\Item::RANDOM_BOX_TYPE:
{
$this->_openRandomBox($itemMeta,$awardService,$propertyService);
}
break;
default:
{
$this->_addLog('additems', 'invalid_item', array(
'param1' => $item['item_id'],
'param2' => $item['item_num'],
));
}
break;
}
}
}
}
}
}//end foreach
if (!empty($heads) || !empty($headFrames)) {
$userInfo = $this->_getOrmUserInfo();
$headsDb = emptyReplace(json_decode($userInfo['head_list'], true), array());
$headFramesDb = emptyReplace(json_decode($userInfo['head_frame_list'], true), array());
$heads = array_unique(array_merge($heads, $headsDb));
$headFrames = array_values(array_unique(array_merge($headFrames, $headFramesDb)));
$this->_updateUserInfo(array(
'head_list' => json_encode($heads),
'head_frame_list' => json_encode($headFrames),
));
}
}
public function _openRandomBox($itemMeta,$awardService,$propertyService){
if ($itemMeta['include_item_id']) {
$includeItemIds = explode('|', $itemMeta['include_item_id']);
$key = array_rand($includeItemIds,1);
$items = array(
array(
'item_id' => $includeItemIds[$key] ,
'item_num' => 1
)
);
$this->_addItems($items,$awardService,$propertyService);
}
}
public function _addTryUsingItems($items)
{
foreach ($items as $item) {
Bag::addTryUsingItem($item['item_id'], $item['item_num'], $item['try_using_time']);
}
}
public function _decItems($items)
{
foreach ($items as $item) {
if ($this->_isVirtualItem($item['item_id'])) {
$this->_decVirtualItem($item['item_id'], $item['item_num']);
} else {
Bag::decItem($item['item_id'], $item['item_num']);
}
}
}
public function _hasEnoughItems($items, &$lackItem)
{
$userInfo = $this->_getUserInfo(array(
'gold',
'diamond',
));
foreach ($items as $item) {
$inventory = $this->_getItemCount($item['item_id'], $userInfo);
if ($inventory < $item['item_num']) {
$lackItem = array(
'item_id' => $item['item_id'],
'item_num' => $item['item_num'],
'inventory' => $inventory
);
return false;
}
}
return true;
}
public function _getLackItemErrMsg($lackItem)
{
if (!$lackItem) {
return 'Not enough item';
}
$itemMeta = mt\Item::get($lackItem['item_id']);
if (!$itemMeta) {
return 'Not enough item';
}
if (SERVER_ENV != _ONLINE) {
return "${itemMeta['name']} Not enough stock:${lackItem['inventory']} need:${lackItem['item_num']}";
} else {
return "${itemMeta['name']} Not enough";
}
}
public function _scatterDrop($dropSource, $dropMeta, $awardService, $propertyService)
{
$itemIds = explode('|', $dropMeta['item_id']);
$nums = explode('|', $dropMeta['num']);
$weights = explode('|', $dropMeta['weight']);
if (count($itemIds) != count($nums) ||
count($itemIds) != count($weights)) {
return;
}
$totalWeight = 0;
foreach ($weights as $weight) {
$totalWeight += $weight;
}
$addItems = array();
if ($dropMeta['type'] == 1) {
//N out of n
for ($i = 0; $i < count($itemIds); ++$i) {
$itemId = $itemIds[$i];
$num = $nums[$i];
$weight = $weights[$i];
if ((rand() % 10000) < $weight) {
array_push($addItems, array(
'item_id' => $itemId,
'item_num' => $num
));
}
}
}else if ($dropMeta['type'] == 2 && $totalWeight > 0) {
//1 out of n
$currWeight = 0;
$rnd = rand() % $totalWeight;
for ($i = 0; $i < count($itemIds); ++$i) {
$itemId = $itemIds[$i];
$num = $nums[$i];
$currWeight += $weights[$i];
if ($currWeight > $rnd) {
array_push($addItems, array(
'item_id' => $itemId,
'item_num' => $num
));
break;
}
}
}
if (count($addItems) > 0) {
$this->_addItems($addItems, $awardService, $propertyService);
}
SqlHelper::insert(
myself()->_getSelfMysql(),
't_drop_log',
array(
'account_id' => myself()->_getAccountId(),
'drop_source' => $dropSource,
'drop_id' => $dropMeta['drop_id'],
'drop_items' => json_encode($addItems),
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
)
);
}
public function _addLog($type, $subtype, $params)
{
$fieldsKv = array(
'account_id' => $this->_getAccountId(),
'type' => $type,
'subtype' => $subtype,
'param1' => getXVal($params, 'param1', ''),
'param2' => getXVal($params, 'param2', ''),
'param3' => getXVal($params, 'param3', ''),
'param4' => getXVal($params, 'param4', ''),
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
);
SqlHelper::insert(
myself()->_getSelfMysql(),
't_game_log',
$fieldsKv
);
}
public function _getV($x, $y, $defVal = 0)
{
return DynData::getV($x, $y, $defVal);
}
public function _setV($x, $y, $val)
{
DynData::setV($x, $y, $val);
}
public function _incV($x, $y, $incVal)
{
DynData::incV($x, $y, $incVal);
}
public function _decV($x, $y, $decVal)
{
DynData::decV($x, $y, $decVal);
}
public function _getDailyV($x, $y, $defVal = 0)
{
return DynData::getDailyV($x, $y, $defVal);
}
public function _setDailyV($x, $y, $val)
{
DynData::setDailyV($x, $y, $val);
}
public function _incDailyV($x, $y, $incVal)
{
DynData::incDailyV($x, $y, $incVal);
}
public function _decDailyV($x, $y, $decVal)
{
DynData::decDailyV($x, $y, $decVal);
}
public function _getWeeklyV($x, $y, $defVal = 0)
{
return DynData::getWeeklyV($x, $y, $defVal);
}
public function _setWeeklyV($x, $y, $val)
{
DynData::setWeeklyV($x, $y, $val);
}
public function _incWeeklyV($x, $y, $incVal)
{
DynData::incWeeklyV($x, $y, $incVal);
}
public function _decWeeklyV($x, $y, $decVal)
{
DynData::decWeeklyV($x, $y, $decVal);
}
}