507 lines
14 KiB
PHP
507 lines
14 KiB
PHP
<?php
|
|
|
|
require_once('mt/Item.php');
|
|
|
|
require_once('models/Bag.php');
|
|
require_once('models/DynData.php');
|
|
require_once('models/Hero.php');
|
|
require_once('models/HeroSkin.php');
|
|
require_once('models/GunSkin.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\Bag;
|
|
use models\DynData;
|
|
use models\Hero;
|
|
use models\HeroSkin;
|
|
use models\GunSkin;
|
|
|
|
class BaseAuthedController extends BaseController {
|
|
|
|
private $accountId = '';
|
|
private $sessionId = '';
|
|
private $mysqlConn = null;
|
|
|
|
public function _handlePre()
|
|
{
|
|
$this->accountId = $_REQUEST['account_id'];
|
|
$this->sessionId = $_REQUEST['session_id'];
|
|
if (!phpcommon\isValidSessionId($this->accountId,
|
|
$this->sessionId)) {
|
|
phpcommon\sendError(500, '无效的session_id');
|
|
die();
|
|
}
|
|
}
|
|
|
|
public function _getAccountId()
|
|
{
|
|
return $this->accountId;
|
|
}
|
|
|
|
public function _getChannel()
|
|
{
|
|
return phpcommon\extractChannel($this->_getAccountId());
|
|
}
|
|
|
|
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, '服务器内部错误');
|
|
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, '服务器内部错误');
|
|
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));
|
|
}
|
|
|
|
public function _addVirtualItem($itemId, $itemNum)
|
|
{
|
|
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_EXP:
|
|
{
|
|
}
|
|
break;
|
|
case V_ITEM_ACTIVE:
|
|
{
|
|
$this->_incV(TN_ACTIVE, 0, $itemNum);
|
|
$this->_incV(TN_DAILY_ACTIVE, 0, $itemNum);
|
|
$this->_incV(TN_WEEKLY_ACTIVE, 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})";
|
|
}
|
|
));
|
|
}
|
|
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) {
|
|
$awardService->addItem($item['item_id'], $item['item_num']);
|
|
if ($this->_isVirtualItem($item['item_id'])) {
|
|
$this->_addVirtualItem($item['item_id'], $item['item_num']);
|
|
$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 {
|
|
switch ($itemMeta['type']) {
|
|
case mt\Item::HERO_TYPE:
|
|
{
|
|
Hero::addHero($itemMeta);
|
|
$propertyService->addHeroChg();
|
|
}
|
|
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:
|
|
{
|
|
|
|
}
|
|
break;
|
|
case mt\Item::GUN_SKIN_TYPE:
|
|
{
|
|
GunSkin::addSkin($itemMeta['id']);
|
|
$propertyService->addGunSkinChg();
|
|
}
|
|
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_unique(array_merge($headFrames, $headFramesDb));
|
|
$this->_updateUserInfo(array(
|
|
'head_list' => json_encode($heads),
|
|
'head_frame_list' => json_encode($headFrames),
|
|
));
|
|
}
|
|
}
|
|
|
|
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 '道具不足';
|
|
}
|
|
$itemMeta = mt\Item::get($lackItem['item_id']);
|
|
if (!$itemMeta) {
|
|
return '道具不足';
|
|
}
|
|
if (SERVER_ENV != _ONLINE) {
|
|
return "${itemMeta['name']}不足 库存:${lackItem['inventory']} 需求:${lackItem['item_num']}";
|
|
} else {
|
|
return "${itemMeta['name']}不足";
|
|
}
|
|
}
|
|
|
|
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选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) {
|
|
//N选1
|
|
$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);
|
|
}
|
|
|
|
}
|