game2006api/webapp/controller/DailySelectionController.class.php
aozhiwei 6b236e038b 1
2023-08-03 10:41:56 +08:00

324 lines
9.3 KiB
PHP

<?php
require_once('mt/Item.php');
require_once('mt/Parameter.php');
require_once('mt/Dailyselection.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
require_once('services/LogService.php');
use services\LogService;
class DailySelectionController extends BaseAuthedController {
const MAX_GRID_ID = 6;
/*
{
"refresh_time": 0,
"refresh_count": 0,
"grid_list": [
{
"id": 0,
"count": 0
}
]
}
*/
private $data = null;
public function _handlePre()
{
parent::_handlePre();
$this->load();
}
public function info()
{
$goodsList = array();
$slot = 1;
foreach ($this->data['grid_list'] as $grid) {
$meta = mt\Dailyselection::get($grid['id']);
if ($meta) {
array_push($goodsList,
array(
'slot' => $slot,
'count' => 1,
'goods_meta' => array(
'id' => $meta['id'],
)
));
}
++$slot;
}
$count = $this->data['refresh_count'];
$costs = mt\Parameter::getByName('daily_selection_refresh_cost');
$arrCosts = explode('|', $costs['param_value']);
$maxCount = count($arrCosts);
$cost = $count < $maxCount ? $arrCosts[$count] : -1;
$this->_rspData(
array(
'refresh_count' => min($maxCount, $this->data['refresh_count']),
'refresh_max_count' => $maxCount,
'cost' => $cost,
'goods_list' => $goodsList,
)
);
}
public function refresh()
{
$this->save();
}
public function buy()
{
$this->save();
}
private function internalRefresh($manual)
{
if ($manual) {
$this->data['refresh_time'] = myself()->_getNowTime();
++$this->data['refresh_count'];
} else {
$this->data = array(
"refresh_time" => myself()->_getNowTime(),
"refresh_count" => 0,
"grid_list" => array()
);
}
$this->data['grid_list'] = array();
for ($i = 1; $i <= self::MAX_GRID_ID; $i++) {
$rndMeta = mt\Dailyselection::randomBySlot($i);
if ($rndMeta) {
array_push(
$this->data['grid_list'],
array(
'id' => $rndMeta['id'],
'count' => 1
)
);
}
}
}
private function load()
{
$rawData = myself()->_getSelfRedis()->get(
DAILY_SELECTION_KEY . myself()->_getAccountId());
if (empty($rawData)) {
$this->internalRefresh(false);
$this->save();
return;
}
$this->data = json_decode($rawData, true);
}
private function save()
{
$remainSec = 3600 * 24 - (myself()->_getNowTime() - myself()->_getNowDaySeconds());
$remainSec = max(0, $remainSec);
myself()->_getSelfRedis()->setNxPx(
DAILY_SELECTION_KEY . myself()->_getAccountId(),
json_encode($this->data),
myself()->_getNowDaySeconds(),
$remainSec * 1000
);
}
public function refreshDailySelectionOld()
{
$address = $this->_getAccountId();
$costs = mt\Parameter::getByName('daily_selection_refresh_cost');
$arrCosts = explode('|', $costs['param_value']);
$maxCount = count($arrCosts);
$count = $this->countTodayRefreshTimes($address);
if ($count >= $maxCount) {
$this->_rspErr(2, 'The maximum number of refreshes has been reached');
return;
}
$cost = $arrCosts[$count];
$costItemId = $this->getCostItemIdByTokenType(ShopController::TOKEN_TYPE_GOLD);
$costItems = $this->makeCostItems($costItemId, $cost);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$chk = $this->refreshDailySelectionWithMode($address, 1);
if ($chk) {
$this->_decItems($costItems);
// error_log("refreshDailySelection-------" . $address . "---" . $cost);
$this->_rspData(
array(
'cost' => $cost,
)
);
} else {
$this->_rspErr(3, 'refresh failed');
}
}
public function buyGoodsDSOld()
{
$idx = getReqVal('idx', 0);
if ($idx <= 0) {
$this->_rspErr(2, 'idx is invalid');
return;
}
$grid = getReqVal('grid', 0);
if ($grid < 1 || $grid > 6) {
$this->_rspErr(2, 'grid is invalid');
return;
}
$count = getReqVal('count', 0);
if ($count<=0) {
$this->_rspErr(2, 'count is invalid');
return;
}
$token_type = getReqVal('token_type', '4');
switch ($token_type) {
case ShopController::TOKEN_TYPE_GOLD:
case ShopController::TOKEN_TYPE_DIAMOND:
break;
default:
$this->_rspErr(1, "token_type is unsupport, {$token_type}");
return;
}
$conn = $this->_getMysql('');
$row = SqlHelper::selectOne(
$conn,
't_shop_dailyselection',
array(
'idx',
'address',
'grid_' . $grid,
'count_' . $grid,
),
array('idx' => $idx)
);
if (!$row) {
$this->_rspErr(2, 'idx is invalid');
return;
}
if ($row['grid_' . $grid] == 0) {
$this->_rspErr(2, 'grid is invalid');
return;
}
if ($row['count_' . $grid] < $count) {
$this->_rspErr(2, 'count is invalid');
return;
}
$sel_id = $row['grid_' . $grid];
$goods = mt\Dailyselection::get($sel_id);
$desired_token_type = $goods['token_type'];
$check_token_type = splitStr1($desired_token_type);
if (!in_array($token_type, $check_token_type)) {
$this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}");
return;
}
$token_pos = array_search($token_type, $check_token_type, true);
$price_array = splitStr1($goods['price']);
$discount_array = splitStr1($goods['discount']);
$need_price = $price_array[$token_pos];
$discount = $discount_array[$token_pos];
$costItemId = $this->getCostItemIdByTokenType($token_type);
$costItems = $this->makeCostItems($costItemId, $count * $need_price);
error_log("buyGoodsDS costItems " . json_encode($costItems));
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$item_id = $goods['goods_id'];
$item_num = $goods['goods_num'];
$sql = "UPDATE t_shop_dailyselection SET count_$grid = count_$grid - $count WHERE idx = $idx";
$chk = $conn->execScript($sql);
$itemMeta = mt\Item::get($item_id);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $count; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $item_num, 0);
}
$awardService = new services\AwardService();
$awardService->addItem($goods['goods_id'], $count * $item_num);
$this->_decItems($costItems);
$event = [
'name' => LogService::SHOP_BUY_ITEM_DAILY,
'val' => $costItems[0]['item_num']
];
switch ($token_type) {
case ShopController::TOKEN_TYPE_GOLD:
LogService::consumeGold($event);
break;
case ShopController::TOKEN_TYPE_DIAMOND:
LogService::consumeDiamond($event);
break;
default:
// 这里不应该出现,出现了说明配置表新增了一种货币,但是这里没有处理
error_log("buyGoodsDS token_type is invalid, token_type: {$token_type}");
return;
}
$goodsDto = array(
'goods_id' => $sel_id,
'item_id' => $goods['goods_id'],
'price_info' => array(
'item_id' => $goods['goods_id'],
'cost_list' => array(),
),
'bought_times' => 0,
'total_buy_times' => 0,
);
{
$priceInfo = mt\Item::getPriceInfo($itemMeta);
if (!empty($priceInfo)) {
$goodsDto['price_info'] = $priceInfo['price_info'];
}
}
$propertyChgService->addUserChg();
$this->_rspData(
array(
'idx' => $idx,
'grid' => $grid,
'count' => $count,
'award' => $awardService->toDto(),
'property_chg' => $propertyChgService->toDto(),
)
);
}
}