255 lines
6.2 KiB
PHP
255 lines
6.2 KiB
PHP
<?php
|
|
|
|
require_once('models/Bag.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\Bag;
|
|
|
|
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(
|
|
'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(),
|
|
'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;
|
|
default:
|
|
{
|
|
return Bag::getItemCount($itemId);
|
|
}
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function _isVirtualItem($itemId)
|
|
{
|
|
return in_array($itemId, array(V_ITEM_GOLD, V_ITEM_DIAMOND));
|
|
}
|
|
|
|
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;
|
|
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)
|
|
{
|
|
foreach ($items as $item) {
|
|
if ($this->_isVirtualItem($item['item_id'])) {
|
|
$this->_addVirtualItem($item['item_id'], $item['item_num']);
|
|
} else {
|
|
Bag::addItem($item['item_id'], $item['item_num']);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|