diff --git a/doc/Shop.py b/doc/Shop.py index 9f863291..1a673b32 100644 --- a/doc/Shop.py +++ b/doc/Shop.py @@ -156,4 +156,42 @@ class Shop(object): ['!pay_methods', [_common.PayMethod()], '支付方式列表'], ] }, + { + 'name': 'getDailySelectionList', + 'desc': '获取每日精选列表', + 'group': 'Shop', + 'url': 'webapp/index.php?c=Shop&a=getDailySelectionList', + 'params': [ + _common.ReqHead(), + ], + 'response': [ + _common.RspHead(), + ['idx', 0, '每日精选的索引'], + ['!goods_list', [_common.DailySelectionGoods()], '商品列表'], + ] + }, + { + 'name': 'refreshDailySelection', + 'desc': '刷新每日精选', + 'group': 'Shop', + 'url': 'webapp/index.php?c=Shop&a=refreshDailySelection', + 'params': [ + _common.ReqHead(), + ], + 'response': [ + _common.RspHead(), + ] + }, + { + 'name': 'buyGoodsDS', + 'desc': '购买每日精选商品', + 'group': 'Shop', + 'url': 'webapp/index.php?c=Shop&a=buyGoodsDS', + 'params': [ + _common.ReqHead(), + ['idx', 0, '每日精选的索引'], + ['grid', 0, '商品在每日精选中的位置索引'], + ['count', 0, '购买数量,始终是1'], + ] + } ] diff --git a/doc/_common.py b/doc/_common.py index ae3c2ee0..ef261085 100644 --- a/doc/_common.py +++ b/doc/_common.py @@ -429,6 +429,20 @@ class PayMethod(object): ['pay_way_code', 0, '支付方式code'], ] +class DailySelectionGoods(object): + + def __init__(self): + self.fields = [ + ['id', 0, '商品表唯一id'], + ['slot', "", '商品槽位'], + ['goods_id', 0, '商品id'], + ['goods_num', 0, '商品堆叠数量'], + ['shop_icon', "", '商品图标'], + ['weight', 0, '权重'], + ['discount', 0, '折扣'], + ['price', 0, '价格'], + ] + class Mission(object): def __init__(self): diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 3540049f..f3814176 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -7,6 +7,7 @@ require_once('mt/Item.php'); require_once('mt/Parameter.php'); require_once('mt/Drop.php'); require_once('mt/PayMethod.php'); +require_once('mt/Dailyselection.php'); require_once('models/User.php'); require_once('models/Hero.php'); @@ -31,6 +32,7 @@ use models\ShopBuyRecord; use models\Chip; use mt\Shop; use mt\PayMethod; +use mt\Dailyselection; class ShopController extends BaseAuthedController { @@ -731,7 +733,8 @@ class ShopController extends BaseAuthedController ); } - public function getPayMethods() { + public function getPayMethods() + { $token_type = getReqVal('token_type', 99); $payMethods = mt\PayMethod::getPayMethods($token_type); $this->_rspData( @@ -741,6 +744,187 @@ class ShopController extends BaseAuthedController ); } + public function refreshDailySelection() + { + $account = $this->_getAccountId(); + $count = $this->countTodayRefreshTimes($account); + $chk = $this->refreshDailySelectionWithMode($account, 1); + if ($chk) { + $this->_rspOk(); + } + } + + public function getDailySelectionList() + { + $account = $this->_getAccountId(); + + // 不清除过期的每日精选可以避免跨日操作错误 + // $chk = $this->clearBeforeTodayDailySelections(); + + $chk = $this->getTodayLastDailySelection($account); + if (!$chk) { + $chk = $this->refreshDailySelectionWithMode($account, 0); + $chk = $this->getTodayLastDailySelection($account); + } + + $selection = $chk[0]; + $goodsList = array(); + for ($i = 1; $i <= 6; $i++) { + $goodsList[$i] = mt\Dailyselection::get($selection['grid_' . $i]); + $goodsList[$i]['count'] = $selection['count_' . $i]; + } + $this->_rspData( + array( + 'idx' => $selection['idx'], + 'goods_list' => $goodsList, + ) + ); + } + + public function buyGoodsDS() { + $account = $this->_getAccountId(); + $idx = getReqVal('idx', 0); + $grid = getReqVal('grid', 0); + $count = getReqVal('count', 0); + + $chk = $this->decDailySelectionItem($idx, $grid, $count); + if (!$chk) { + $this->_rspErr(1, 'goods not enough'); + return false; + } + + $this->_rspData( + array( + 'idx' => $idx, + 'grid' => $grid, + 'count' => $count, + ) + ); + } + + 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 clearBeforeTodayDailySelections() { + $self = myself(); + if (!$self) return; + + $conn = $self->_getMysql(''); + $nowTime = $this->_getNowTime(); + $dayTime = $this->_getDaySeconds($nowTime); + + $sql = "DELETE FROM t_shop_dailyselection WHERE refresh_time < $dayTime"; + $chk = $conn->execScript($sql); + return $chk; + } + + private function countTodayRefreshTimes($account) + { + $self = myself(); + if (!$self) return; + + $conn = $self->_getMysql(''); + $nowTime = $this->_getNowTime(); + $dayTime = $this->_getDaySeconds($nowTime); + + $sql = "SELECT COUNT(*) AS cnt FROM t_shop_dailyselection WHERE account_id = '$account' AND refresh_time >= $dayTime"; + + $row = $conn->execQuery($sql); + + return $row[0]['cnt']; + } + + private function getTodayLastDailySelection($account) + { + $self = myself(); + if (!$self) return; + + $conn = $self->_getMysql(''); + $nowTime = $this->_getNowTime(); + $dayTime = $this->_getDaySeconds($nowTime); + + $sql = "SELECT * FROM t_shop_dailyselection WHERE account_id = '$account' AND refresh_time >= $dayTime ORDER BY idx DESC LIMIT 1"; + + $row = $conn->execQuery($sql); + + return $row; + } + + private function refreshDailySelectionWithMode($account, $mode) + { + $selection = $this->randomNewDailySelection(); + + $self = myself(); + if (!$self) return; + + $conn = $self->_getMysql(''); + $nowTime = $this->_getNowTime(); + + $chk = SqlHelper::insert( + $conn, + 't_shop_dailyselection', + array( + 'account_id' => $account, + '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"]; + } + } + private function outsideBuy($shopId, $itemId, $itemNum, $costItemId) { $propertyChgService = new services\PropertyChgService(); diff --git a/webapp/mt/Dailyselection.php b/webapp/mt/Dailyselection.php new file mode 100644 index 00000000..9d6df1c3 --- /dev/null +++ b/webapp/mt/Dailyselection.php @@ -0,0 +1,49 @@ +