game2005api/webapp/controller/BaseAuthedController.class.php
aozhiwei 61c578bf8f 1
2021-11-26 16:14:12 +08:00

262 lines
7.2 KiB
PHP

<?php
use phpcommon\SqlHelper;
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 ($_REQUEST['c'] == 'Role' && $_REQUEST['a'] == 'battleReport') {
return;
}
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(
'accountid' => $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(
'accountid' => $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(
'accountid' => $this->_getAccountId()
)
);
return $row ? $row : null;
}
public function _updateUserInfo($fieldsKv)
{
SqlHelper::update
($this->_getSelfMysql(),
'user',
array(
'accountid' => $this->_getAccountId()
),
$fieldsKv
);
}
public function _getItem($itemId)
{
$row = SqlHelper::selectOne
($this->_getSelfMysql(),
'bag',
array(
'id',
'num'
),
array(
'id' => $itemId,
'accountid' => $this->_getAccountId()
));
if (empty($row)) {
return null;
}
return array(
'item_id' => $row['id'],
'item_num' => $row['num']
);
}
public function _getItemCount($itemId, $userInfo)
{
switch ($itemId) {
case V_ITEM_GOLD:
{
return $userInfo['coin_num'];
}
break;
case V_ITEM_DIAMOND:
{
return $userInfo['diamond_num'];
}
break;
case V_ITEM_LOTTERY:
{
return $userInfo['rmb_num'];
}
break;
default:
{
$row = SqlHelper::selectOne
($this->_getSelfMysql(),
'bag',
array(
'id',
'num'
),
array(
'id' => $itemId,
'accountid' => $this->_getAccountId()
));
return $row ? $row['num'] : 0;
}
break;
}
}
public function _isVirtualItem($itemId)
{
$isVirtualItem = false;
return $isVirtualItem;
}
public function _addItem($items)
{
foreach ($items as $item) {
if ($this->_isVirtualItem($item['item_id'])) {
if ($item['item_id'] == $this->goldID) {
$this->_addGold($item['item_num']);
}
else if ($item['item_id'] == $this->lotteryID){
$this->addLottery($item['item_num']);
}
} else {
SqlHelper::upsert
($this->_getSelfMysql(),
't_bag',
array(
'accountid' => $this->_getAccountId(),
'item_id' => $item['item_id']
),
array(
'item_num' => function () use($item) { return "item_num + {$item['item_num']}";},
'modifytime' => $this->_getNowTime()
),
array(
'accountid' => $this->_getAccountId(),
'item_id' => $item['item_id'],
'item_num' => 1,
'item_state' => 0,
'try_expire_at' => 0,
'createtime' => $this->_getNowTime(),
'modifytime' => $this->_getNowTime()
)
);
}
}
}
public function _decItems($items)
{
foreach ($items as $item) {
if ($this->_isVirtualItem($item['item_id'])) {
// if ($item['item_id'] == VIRTUAL_ITEM_GOLD_ID) {
// $this->_decGold($item['item_num']);
// }
} else {
SqlHelper::update
($this->_getSelfMysql(),
'bag',
array(
'accountid' => $this->_getAccountId(),
'id' => $item['item_id']
),
array(
'num' => function () use($item) {
return "CASE WHEN num < {$item['item_num']} THEN 0 ELSE num - {$item['item_num']} END";
},
'modify_time' => $this->_getNowTime()
)
);
}
}
}
public function _hasEnoughItems($items, &$lackItem)
{
$userInfo = $this->_getUserInfo(array(
'coin_num',
'diamond_num',
'rmb_num'
));
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;
}
}