This commit is contained in:
aozhiwei 2023-08-03 10:41:56 +08:00
parent 158332f4c7
commit 6b236e038b
4 changed files with 148 additions and 208 deletions

View File

@ -2,6 +2,7 @@
define('TEAMID_KEY', 'team_uuid:'); define('TEAMID_KEY', 'team_uuid:');
define('RANKING_KEY', 'game2006api:'); define('RANKING_KEY', 'game2006api:');
define('DAILY_SELECTION_KEY', 'game2006api:daily_selection:');
define('LAST_SESSION_KEY', 'last_session:'); define('LAST_SESSION_KEY', 'last_session:');

View File

@ -206,7 +206,7 @@ class BaseAuthedController extends BaseController {
public function _getSelfRedis() public function _getSelfRedis()
{ {
if (!$this->redisConn) { if (!$this->redisConn) {
$this->redisconn = $this->_getRedis($this->_getAccountId()); $this->redisConn = $this->_getRedis($this->_getAccountId());
} }
return $this->redisConn; return $this->redisConn;
} }

View File

@ -4,98 +4,130 @@ require_once('mt/Item.php');
require_once('mt/Parameter.php'); require_once('mt/Parameter.php');
require_once('mt/Dailyselection.php'); require_once('mt/Dailyselection.php');
require_once('models/DailySelection.php');
require_once('services/AwardService.php'); require_once('services/AwardService.php');
require_once('services/PropertyChgService.php'); require_once('services/PropertyChgService.php');
require_once('services/LogService.php'); require_once('services/LogService.php');
use models\DailySelection;
use services\LogService; use services\LogService;
class DailySelectionController extends BaseAuthedController { class DailySelectionController extends BaseAuthedController {
const MAX_GRID_ID = 6; 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() public function info()
{ {
$dbInfo = DailySelection::get(); $goodsList = array();
if (!$dbInfo) { $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'];
$dbInfo = DailySelection::get(); $costs = mt\Parameter::getByName('daily_selection_refresh_cost');
if (!$dbInfo) { $arrCosts = explode('|', $costs['param_value']);
myself()->_rspErr(500, 'server internal error');
return; $maxCount = count($arrCosts);
} $cost = $count < $maxCount ? $arrCosts[$count] : -1;
$data = json_decode($dbInfo);
for ($i = 1; $i <= self::MAX_GRID_ID; ++$i) { $this->_rspData(
$goodsList[$i] = mt\Dailyselection::get($dbInfo['grid_' . $i]); array(
} 'refresh_count' => min($maxCount, $this->data['refresh_count']),
} 'refresh_max_count' => $maxCount,
'cost' => $cost,
'goods_list' => $goodsList,
)
);
}
public function refresh() public function refresh()
{ {
$this->save();
} }
public function buy() public function buy()
{ {
$this->save();
} }
public function getDailySelectionListOld() private function internalRefresh($manual)
{ {
$address = $this->_getAccountId(); if ($manual) {
$this->data['refresh_time'] = myself()->_getNowTime();
$chk = $this->getTodayLastDailySelection($address); ++$this->data['refresh_count'];
if (!$chk) { } else {
$chk = $this->refreshDailySelectionWithMode($address, 0); $this->data = array(
$chk = $this->getTodayLastDailySelection($address); "refresh_time" => myself()->_getNowTime(),
"refresh_count" => 0,
"grid_list" => array()
);
} }
$this->data['grid_list'] = array();
// 预检查是否有表格更新,未必能检测准确,但是可以避免大部分情况下的错误 for ($i = 1; $i <= self::MAX_GRID_ID; $i++) {
$check = true; $rndMeta = mt\Dailyselection::randomBySlot($i);
$selection = $chk[0]; if ($rndMeta) {
$pre_goodsList = array(); array_push(
for ($i = 1; $i <= 6; $i++) { $this->data['grid_list'],
$pre_goodsList[$i] = mt\Dailyselection::get($selection['grid_' . $i]); array(
if (!$pre_goodsList[$i]) { 'id' => $rndMeta['id'],
$check = false; 'count' => 1
break; )
);
} }
} }
if (!$check) { }
$chk = $this->refreshDailySelectionWithMode($address, 0);
$chk = $this->getTodayLastDailySelection($address); private function load()
$selection = $chk[0]; {
$rawData = myself()->_getSelfRedis()->get(
DAILY_SELECTION_KEY . myself()->_getAccountId());
if (empty($rawData)) {
$this->internalRefresh(false);
$this->save();
return;
} }
$this->data = json_decode($rawData, true);
}
$selection = $chk[0]; private function save()
$goodsList = array(); {
for ($i = 1; $i <= 6; $i++) { $remainSec = 3600 * 24 - (myself()->_getNowTime() - myself()->_getNowDaySeconds());
$goodsList[$i] = mt\Dailyselection::get($selection['grid_' . $i]); $remainSec = max(0, $remainSec);
if ($goodsList[$i]) { myself()->_getSelfRedis()->setNxPx(
$goodsList[$i]['count'] = $selection['count_' . $i]; DAILY_SELECTION_KEY . myself()->_getAccountId(),
// $goodsList[$i]['pending'] = $this->checkPendingBuyGoodsDS($address, $goodsList[$i]['goods_id'], $selection['idx'], $i); json_encode($this->data),
$goodsList[$i]['pending'] = 0; myself()->_getNowDaySeconds(),
} $remainSec * 1000
}
$count = $this->countTodayRefreshTimes($address);
$costs = mt\Parameter::getByName('daily_selection_refresh_cost');
$arrCosts = explode('|', $costs['param_value']);
$max_count = count($arrCosts);
$cost = $count < $max_count ? $arrCosts[$count] : -1;
$this->_rspData(
array(
'refresh_info' => "{$count}/{$max_count}",
'cost' => $cost,
'goods_list' => $goodsList,
)
); );
} }
@ -288,114 +320,4 @@ class DailySelectionController extends BaseAuthedController {
); );
} }
private function decDailySelectionItem($idx, $grid, $count)
{
$self = myself();
if (!$self) return false;
$conn = $self->_getMysql('');
$sql = "SELECT count_$grid FROM t_shop_dailyselection WHERE idx = $idx";
$chk = $conn->execQuery($sql);
if (!$chk) return false;
if ($chk[0]['count_' . $grid] < $count) return false;
$sql = "UPDATE t_shop_dailyselection SET count_$grid = count_$grid - $count WHERE idx = $idx";
$chk = $conn->execScript($sql);
return $chk;
}
private function countTodayRefreshTimes($address)
{
$self = myself();
if (!$self) return;
$conn = $self->_getMysql('');
$nowTime = $this->_getNowTime();
$dayTime = $this->_getDaySeconds($nowTime);
$sql = "SELECT COUNT(idx) AS cnt FROM t_shop_dailyselection WHERE address = '$address' AND refresh_mode = 1 AND refresh_time >= $dayTime";
$row = $conn->execQuery($sql);
return $row[0]['cnt'];
}
private function getTodayLastDailySelection($address)
{
$self = myself();
if (!$self) return;
$conn = $self->_getMysql('');
$nowTime = $this->_getNowTime();
$dayTime = $this->_getDaySeconds($nowTime);
$sql = "SELECT * FROM t_shop_dailyselection WHERE address = '$address' AND refresh_time >= $dayTime ORDER BY idx DESC LIMIT 1";
$row = $conn->execQuery($sql);
return $row;
}
private function refreshDailySelectionWithMode($address, $mode)
{
$selection = $this->randomNewDailySelection();
$self = myself();
if (!$self) return;
$conn = $self->_getMysql('');
$nowTime = $this->_getNowTime();
$chk = SqlHelper::insert(
$conn,
't_shop_dailyselection',
array(
'address' => $address,
'refresh_mode' => $mode,
'refresh_time' => $nowTime,
'grid_1' => $selection[1]['id'],
'grid_2' => $selection[2]['id'],
'grid_3' => $selection[3]['id'],
'grid_4' => $selection[4]['id'],
'grid_5' => $selection[5]['id'],
'grid_6' => $selection[6]['id'],
'count_1' => 1,
'count_2' => 1,
'count_3' => 1,
'count_4' => 1,
'count_5' => 1,
'count_6' => 1,
)
);
return $chk;
}
private function randomNewDailySelection()
{
$newDailySelection = array();
for ($i = 1; $i <= 6; $i++) {
$store = mt\Dailyselection::getBySlot($i);
$newDailySelection[$i] = $this->weighted_random($store);
}
return $newDailySelection;
}
private function weighted_random($array)
{
// 计算数组元素的总权重
$total_weight = array_sum(array_column($array, "weight"));
// 生成一个随机数
$rand = mt_rand(1, $total_weight);
// 遍历数组,找到随机数对应的元素
foreach ($array as $item) {
// 如果随机数小于或等于当前元素的权重,返回该元素
if ($rand <= $item["weight"]) {
return $item;
}
// 否则,减去当前元素的权重,继续循环
$rand -= $item["weight"];
}
}
} }

View File

@ -6,44 +6,61 @@ use phpcommon;
class Dailyselection class Dailyselection
{ {
public static function get($id)
{
return getXVal(self::getMetaList(), $id);
}
public static function getBySlot($slot) public static function get($id)
{ {
self::cacheDailyselectionInSlot(); return getXVal(self::getMetaList(), $id);
$dailyselection = getXVal(self::$cacheDailyselectionInSlotList, $slot, null); }
return $dailyselection;
}
protected static function cacheDailyselectionInSlot() public static function randomBySlot($slot)
{ {
if (!self::$cacheDailyselectionInSlotList) { self::mustBeSlotGroupHash();
self::$cacheDailyselectionInSlotList = array(); $metas = getXVal(self::$slotGroupHash, $slot);
foreach (self::getMetaList() as $meta) { if (empty($metas)) {
$keys = explode("|", $meta['slot']); return null;
foreach ($keys as $key) {
if (!getXVal(self::$cacheDailyselectionInSlotList, $key, null)) {
self::$cacheDailyselectionInSlotList[$key] = array();
}
array_push(self::$cacheDailyselectionInSlotList[$key], $meta);
} }
} $totalWeight = 0;
foreach ($metas as $meta) {
$totalWeight += $meta['weight'];
}
if ($totalWeight > 0) {
$currWeight = 0;
$rnd = rand() % $totalWeight;
foreach ($metas as $meta) {
$currWeight += $meta['weight'];
if ($currWeight > $rnd) {
return $meta;
}
}
}
return null;
} }
return self::$cacheDailyselectionInSlotList; protected static function getMetaList()
} {
if (!self::$metaList) {
protected static function getMetaList() self::$metaList = getMetaTable('Dailyselection@Dailyselection.php');
{ }
if (!self::$metaList) { return self::$metaList;
self::$metaList = getMetaTable('Dailyselection@Dailyselection.php');
} }
return self::$metaList;
}
protected static $cacheDailyselectionInSlotList; protected static function mustBeSlotGroupHash()
protected static $metaList; {
if (!self::$slotGroupHash) {
self::$slotGroupHash = array();
foreach (self::getMetaList() as $meta) {
$slots = explode("|", $meta['slot']);
foreach ($slots as $slot) {
if (!getXVal(self::$slotGroupHash, $slot)) {
self::$slotGroupHash[$slot] = array();
}
array_push(self::$slotGroupHash[$slot], $meta);
}
}
}
}
protected static $slotGroupHash;
protected static $metaList;
} }