game2006api/webapp/controller/BaseAuthedController.class.php
aozhiwei a0a3e437a5 1
2021-11-26 13:50:14 +08:00

427 lines
12 KiB
PHP

<?php
require_once('mt/Player.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 _getMysql($data)
{
$mysql_conf = getMysqlConfig(crc32($data));
$conn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => DBNAME_PREFIX . $mysql_conf['instance_id']
));
return $conn;
}
public function _getSelfMysql()
{
if (!$this->mysqlConn) {
$this->mysqlConn = $this->getMysql($this->getAccountId());
}
return $this->mysqlConn;
}
public function _getRedis($data)
{
$redis_conf = getRedisConfig(crc32($data));
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
public function _isValidSex($sex)
{
return in_array($sex, array(0, 1, 2));
}
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 _sendDataToClient($_code,$_msg,$_data)
{
echo json_encode(array(
'errcode' => $_code,
'errmsg' => $_msg,
'data' => $_data,
));
}
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;
if($itemId == $this->goldID)
{
$isVirtualItem = true;
}
else if($itemId == $this->lotteryID)
{
$isVirtualItem = true;
}
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'],
'color_id' => 0,
'status' => 1,
'active_time' => 0,
'item_num' => $item['item_num'],
'createtime' => $this->getNowTime(),
'modifytime' => $this->getNowTime()
)
);
}
}
}
public function _decItems($items)
{
$this->decItem($items);
}
public function _decItem($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 _deleteItem($item)
{
$tmpItemID = $item["item_id"];
$beDeleteNum = $item['item_num'];
$accountID = $this->getAccountId();
$conn = $this->getMysql($accountID);
$sqlStr = "SELECT id,num FROM bag WHERE accountid=:accountid AND id=:itemId";
$row = $conn->execQuery($sqlStr,array(':accountid' => $accountID,':itemId' =>$tmpItemID));
$code = 100;
if($row)
{
$num1 = $row[0]['num'];
if($num1 < $beDeleteNum)
{
$code = 98;//道具不足
}
}
else
{
$code = 99;//道具不存在
}
return $code;
}
public function _decGold($decGold)
{
SqlHelper::update
(
$this->getSelfMysql(),
'user',
array(
'accountid' => $this->getAccountId()
),
array(
'coin_num' => function () use($decGold) { return "CASE coin_num WHEN coin_num < {$decGold} THEN 0 ELSE coin_num - {$decGold} END"; }
)
);
// $this->incV(TN_CONSUME_GOLD, 0, $decGold);
// $this->incV(TN_CONSUME_GOLD, $this->getNowDaySeconds(), $decGold);
}
public function _addGold($addGold)
{
SqlHelper::update
(
$this->getSelfMysql(),
'user',
array(
'accountid' => $this->getAccountId()
),
array(
'coin_num' => function () use($addGold) { return "coin_num + {$addGold}"; }
)
);
}
public function _getTalentLv($skillId, $skillTypeId)
{
$row = SqlHelper::selectOne
(
$this->getSelfMysql(),
'gun_intensify',
array(
'gun_type_id',
'skill_id',
'skill_lv'
),
array(
'accountid' => $this->getAccountId(),
'skill_id' => $skillId,
'gun_type_id' => $skillTypeId
)
);
return $row ? $row['skill_iv'] : 0;
}
public function _hasEnoughItemsEx($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;
}
public function _getUsingEquipId()
{
$row = SqlHelper::selectOne
($this->getSelfMysql(),
'equip',
array(
'using_id',
),
array(
'accountid' => $this->getAccountId(),
)
);
return $row ? $row['using_id'] : 0;
}
public function _redisSetAndExpire($pk, $key, $val, $time)
{
$r = $this->getRedis($pk);
$r->set($key, $val);
$r->pexpire($key, $time);
}
public function _redisGetJson($pk, $key)
{
$r = $this->getRedis($pk);
$dataStr = $r->get($key);
$result = null;
if (!empty($dataStr)) {
$result = json_decode($dataStr, true);
}
return $result;
}
}