337 lines
10 KiB
PHP
337 lines
10 KiB
PHP
<?php
|
|
|
|
class BaseAuthedController extends BaseController {
|
|
|
|
private $accountId = '';
|
|
private $sessionId = '';
|
|
private $mysqlConn = null;
|
|
private $goldID = 10001;
|
|
private $lotteryID = 10003;
|
|
private $diamondID = 10003;
|
|
protected $itemArr = array();
|
|
|
|
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();
|
|
}
|
|
}
|
|
protected function getAccountId()
|
|
{
|
|
return $this->accountId;
|
|
}
|
|
|
|
protected function getChannel()
|
|
{
|
|
return phpcommon\extractChannel($this->getAccountId());
|
|
}
|
|
|
|
protected function getSessionId()
|
|
{
|
|
return $this->sessionId;
|
|
}
|
|
|
|
protected function getRegisterTime()
|
|
{
|
|
$registertime = phpcommon\extractRegisterTimeFromSessionId($this->sessionId);
|
|
return $registertime;
|
|
}
|
|
|
|
|
|
protected function getMysql($data)
|
|
{
|
|
$mysql_conf = getMysqlConfig(crc32($data));
|
|
//error_log("Sql01=====".json_encode($mysql_conf)."==".DBNAME_PREFIX . $mysql_conf['instance_id']);
|
|
$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;
|
|
}
|
|
|
|
protected function getSelfMysql()
|
|
{
|
|
if (!$this->mysqlConn) {
|
|
$this->mysqlConn = $this->getMysql($this->getAccountId());
|
|
}
|
|
return $this->mysqlConn;
|
|
}
|
|
|
|
protected 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;
|
|
}
|
|
|
|
protected function isValidSex($sex)
|
|
{
|
|
return in_array($sex, array(0, 1, 2));
|
|
}
|
|
|
|
protected function getUserInfo($fields)
|
|
{
|
|
$row = phpcommon\SqlHelper::selectOne
|
|
($this->getSelfMysql(),
|
|
'user',
|
|
$fields,
|
|
array(
|
|
'accountid' => $this->getAccountId()
|
|
)
|
|
);
|
|
if (empty($row)) {
|
|
phpcommon\sendError(500, '服务器内部错误');
|
|
error_log('getUserInfo error '. $this->getAccountId());
|
|
die();
|
|
}
|
|
return $row;
|
|
}
|
|
protected function sendDataToClient($_code,$_msg,$_data)
|
|
{
|
|
echo json_encode(array(
|
|
'errcode' => $_code,
|
|
'errmsg' => $_msg,
|
|
'data' => $_data,
|
|
));
|
|
}
|
|
protected function getItem($itemId)
|
|
{
|
|
$row = phpcommon\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']
|
|
);
|
|
}
|
|
protected function isVirtualItem($itemId)
|
|
{
|
|
$isVirtualItem = false;
|
|
if($itemId == $this->goldID)
|
|
{
|
|
$isVirtualItem = true;
|
|
}
|
|
else if($itemId == $this->lotteryID)
|
|
{
|
|
$isVirtualItem = true;
|
|
}
|
|
return $isVirtualItem;
|
|
}
|
|
protected function addItem($items)
|
|
{
|
|
foreach ($items as $item) {
|
|
error_log("准备添加道具======".json_encode($items));
|
|
if ($this->isVirtualItem($item['item_id'])) {
|
|
if ($item['item_id'] == $this->goldID) {
|
|
$this->addGold($item['item_num']);
|
|
error_log("添加金币=====".$item['item_num']);
|
|
}
|
|
else if ($item['item_id'] == $this->lotteryID){
|
|
$this->addLottery($item['item_num']);
|
|
error_log("添加奖券=====".$item['item_num']);
|
|
}
|
|
} else {
|
|
|
|
phpcommon\SqlHelper::insertOrUpdate
|
|
($this->getSelfMysql(),
|
|
'bag',
|
|
array(
|
|
'accountid' => $this->getAccountId(),
|
|
'id' => $item['item_id']
|
|
),
|
|
array(
|
|
'accountid' => $this->getAccountId(),
|
|
'id' => $item['item_id'],
|
|
'color_id' => 0,
|
|
'status' =>1,
|
|
'active_time' =>'',
|
|
'num' => $item['item_num'],
|
|
'create_time' => $this->getNowTime(),
|
|
'modify_time' => $this->getNowTime()
|
|
),
|
|
array(
|
|
'num' => function () use($item) { return "num + {$item['item_num']}";},
|
|
'modify_time' => $this->getNowTime()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
protected 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 {
|
|
phpcommon\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()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
protected 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;
|
|
}
|
|
protected function decGold($decGold)
|
|
{
|
|
phpcommon\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);
|
|
}
|
|
|
|
protected function addGold($addGold)
|
|
{
|
|
phpcommon\SqlHelper::update
|
|
(
|
|
$this->getSelfMysql(),
|
|
'user',
|
|
array(
|
|
'accountid' => $this->getAccountId()
|
|
),
|
|
array(
|
|
'coin_num' => function () use($addGold) { return "coin_num + {$addGold}"; }
|
|
)
|
|
);
|
|
}
|
|
protected function addLottery($addLottery)
|
|
{
|
|
phpcommon\SqlHelper::update
|
|
(
|
|
$this->getSelfMysql(),
|
|
'user',
|
|
array(
|
|
'accountid' => $this->getAccountId()
|
|
),
|
|
array(
|
|
'rmb_num' => function () use($addLottery) { return "rmb_num + {$addLottery}"; }
|
|
)
|
|
);
|
|
}
|
|
protected function checkPriceEnough($_priceID,$_priceNum)
|
|
{
|
|
//$priceStrArr = explode(":",$price);
|
|
$tmpID = $_priceID;
|
|
$priceNum = $_priceNum;
|
|
$accountID = $this->getAccountId();
|
|
$conn = $this->getMysql($accountID);
|
|
$sqlStr = null;
|
|
$row = null;
|
|
$tmpNum = 0;
|
|
if($tmpID == $this->goldID)
|
|
{
|
|
|
|
$sqlStr = "SELECT coin_num FROM user WHERE accountid=:accountid;";
|
|
|
|
}
|
|
else if($tmpID == $this->diamondID)
|
|
{
|
|
$sqlStr = "SELECT diamond_num FROM user WHERE accountid=:accountid;";
|
|
}
|
|
|
|
$code = 100;
|
|
if($sqlStr)
|
|
{
|
|
$row = $conn->execQuery($sqlStr,array(':accountid' => $accountID));
|
|
if($row)
|
|
{
|
|
if($tmpID == $this->goldID)
|
|
{
|
|
|
|
$tmpNum = $row[0]["coin_num"];
|
|
if($tmpNum < $priceNum)
|
|
{
|
|
$code = 88;//金币不足
|
|
}
|
|
|
|
}
|
|
else if($tmpID == $this->diamondID)
|
|
{
|
|
$tmpNum = $row[0]["diamond_num"];
|
|
if($tmpNum < $priceNum)
|
|
{
|
|
$code = 87;//钻石不足
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
$code = 86;//道具不足
|
|
}
|
|
|
|
}
|
|
return $code;
|
|
}
|
|
|
|
}
|