1
This commit is contained in:
parent
158332f4c7
commit
6b236e038b
@ -2,6 +2,7 @@
|
||||
|
||||
define('TEAMID_KEY', 'team_uuid:');
|
||||
define('RANKING_KEY', 'game2006api:');
|
||||
define('DAILY_SELECTION_KEY', 'game2006api:daily_selection:');
|
||||
|
||||
define('LAST_SESSION_KEY', 'last_session:');
|
||||
|
||||
|
@ -206,7 +206,7 @@ class BaseAuthedController extends BaseController {
|
||||
public function _getSelfRedis()
|
||||
{
|
||||
if (!$this->redisConn) {
|
||||
$this->redisconn = $this->_getRedis($this->_getAccountId());
|
||||
$this->redisConn = $this->_getRedis($this->_getAccountId());
|
||||
}
|
||||
return $this->redisConn;
|
||||
}
|
||||
|
@ -4,100 +4,132 @@ require_once('mt/Item.php');
|
||||
require_once('mt/Parameter.php');
|
||||
require_once('mt/Dailyselection.php');
|
||||
|
||||
require_once('models/DailySelection.php');
|
||||
|
||||
require_once('services/AwardService.php');
|
||||
require_once('services/PropertyChgService.php');
|
||||
require_once('services/LogService.php');
|
||||
|
||||
use models\DailySelection;
|
||||
|
||||
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()
|
||||
{
|
||||
$dbInfo = DailySelection::get();
|
||||
if (!$dbInfo) {
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
||||
$dbInfo = DailySelection::get();
|
||||
if (!$dbInfo) {
|
||||
myself()->_rspErr(500, 'server internal error');
|
||||
return;
|
||||
}
|
||||
$data = json_decode($dbInfo);
|
||||
for ($i = 1; $i <= self::MAX_GRID_ID; ++$i) {
|
||||
$goodsList[$i] = mt\Dailyselection::get($dbInfo['grid_' . $i]);
|
||||
}
|
||||
$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();
|
||||
}
|
||||
|
||||
public function getDailySelectionListOld()
|
||||
private function internalRefresh($manual)
|
||||
{
|
||||
$address = $this->_getAccountId();
|
||||
|
||||
$chk = $this->getTodayLastDailySelection($address);
|
||||
if (!$chk) {
|
||||
$chk = $this->refreshDailySelectionWithMode($address, 0);
|
||||
$chk = $this->getTodayLastDailySelection($address);
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
// 预检查是否有表格更新,未必能检测准确,但是可以避免大部分情况下的错误
|
||||
$check = true;
|
||||
$selection = $chk[0];
|
||||
$pre_goodsList = array();
|
||||
for ($i = 1; $i <= 6; $i++) {
|
||||
$pre_goodsList[$i] = mt\Dailyselection::get($selection['grid_' . $i]);
|
||||
if (!$pre_goodsList[$i]) {
|
||||
$check = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$check) {
|
||||
$chk = $this->refreshDailySelectionWithMode($address, 0);
|
||||
$chk = $this->getTodayLastDailySelection($address);
|
||||
$selection = $chk[0];
|
||||
}
|
||||
|
||||
$selection = $chk[0];
|
||||
$goodsList = array();
|
||||
for ($i = 1; $i <= 6; $i++) {
|
||||
$goodsList[$i] = mt\Dailyselection::get($selection['grid_' . $i]);
|
||||
if ($goodsList[$i]) {
|
||||
$goodsList[$i]['count'] = $selection['count_' . $i];
|
||||
// $goodsList[$i]['pending'] = $this->checkPendingBuyGoodsDS($address, $goodsList[$i]['goods_id'], $selection['idx'], $i);
|
||||
$goodsList[$i]['pending'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$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(
|
||||
$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(
|
||||
'refresh_info' => "{$count}/{$max_count}",
|
||||
'cost' => $cost,
|
||||
'goods_list' => $goodsList,
|
||||
'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()
|
||||
{
|
||||
@ -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"];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,34 +6,34 @@ use phpcommon;
|
||||
|
||||
class Dailyselection
|
||||
{
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return getXVal(self::getMetaList(), $id);
|
||||
}
|
||||
|
||||
public static function getBySlot($slot)
|
||||
public static function randomBySlot($slot)
|
||||
{
|
||||
self::cacheDailyselectionInSlot();
|
||||
$dailyselection = getXVal(self::$cacheDailyselectionInSlotList, $slot, null);
|
||||
return $dailyselection;
|
||||
self::mustBeSlotGroupHash();
|
||||
$metas = getXVal(self::$slotGroupHash, $slot);
|
||||
if (empty($metas)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static function cacheDailyselectionInSlot()
|
||||
{
|
||||
if (!self::$cacheDailyselectionInSlotList) {
|
||||
self::$cacheDailyselectionInSlotList = array();
|
||||
foreach (self::getMetaList() as $meta) {
|
||||
$keys = explode("|", $meta['slot']);
|
||||
foreach ($keys as $key) {
|
||||
if (!getXVal(self::$cacheDailyselectionInSlotList, $key, null)) {
|
||||
self::$cacheDailyselectionInSlotList[$key] = array();
|
||||
$totalWeight = 0;
|
||||
foreach ($metas as $meta) {
|
||||
$totalWeight += $meta['weight'];
|
||||
}
|
||||
array_push(self::$cacheDailyselectionInSlotList[$key], $meta);
|
||||
if ($totalWeight > 0) {
|
||||
$currWeight = 0;
|
||||
$rnd = rand() % $totalWeight;
|
||||
foreach ($metas as $meta) {
|
||||
$currWeight += $meta['weight'];
|
||||
if ($currWeight > $rnd) {
|
||||
return $meta;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::$cacheDailyselectionInSlotList;
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static function getMetaList()
|
||||
@ -44,6 +44,23 @@ class Dailyselection
|
||||
return self::$metaList;
|
||||
}
|
||||
|
||||
protected static $cacheDailyselectionInSlotList;
|
||||
protected static $metaList;
|
||||
protected static function mustBeSlotGroupHash()
|
||||
{
|
||||
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;
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user