From 8aef9f16c0923f468982918aaef594c8a892ae8f Mon Sep 17 00:00:00 2001 From: songliang Date: Mon, 3 Jul 2023 15:05:35 +0800 Subject: [PATCH] ... --- webapp/controller/ShopController.class.php | 1149 ++++++++++---------- 1 file changed, 575 insertions(+), 574 deletions(-) diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 206fe569..fdcf813f 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -172,155 +172,70 @@ class ShopController extends BaseAuthedController ); } - public function buyGoodsNew() + public function startGoodsDirect() { $id = getReqVal('id', 0); $token_type = getReqVal('token_type', ''); $goods_num = getReqVal('goods_num', 0); - $row = mt\ShopGoods::get($id); + $conn = myself()->_getMysql(''); - $desired_token_type = $row['token_type']; - $check_token_type = splitStr1($desired_token_type); - $token_pos = array_search($token_type, $check_token_type, true); - if (!in_array($token_type, $check_token_type)) { - $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); - return; + $address = myself()->_getAddress(); + + + if (empty($address)) { + $address = myself()->_getAccountId(); } - if ($goods_num > $row['max_amount']) { - $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); - return; - } - - // 这里命名混乱了, 购买个数,一捆个数命名冲突 - $goods_count = $row['goods_num']; - - $buyRecordHash = ShopBuyRecord::allToHash(); - $boughtTimes = 1; - switch ($row['limit_type']) { - case ShopController::DAILY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $id); - $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; - if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $row['limit_num']) { - $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); - return; - } - if ($row['limit_num'] <= 0) { - $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); - return; - } - } - break; - case ShopController::WEEKLY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $id); - $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; - if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $row['limit_num']) { - $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); - return; - } - if ($row['limit_num'] <= 0) { - $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); - return; - } - } - break; - case ShopController::TOTAL_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $id); - $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; - if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $row['limit_num']) { - $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); - return; - } - if ($row['limit_num'] <= 0) { - $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); - return; - } - } - break; - default: { - } - break; - } - - $price_array = splitStr1($row['price']); - $discount_array = splitStr1($row['discount']); - - $need_price = $price_array[$token_pos]; - $discount = $discount_array[$token_pos]; - - $discount_begin = strtotime($row['discount_begin'] . ' UTC'); - $discount_end = strtotime($row['discount_end'] . ' UTC'); - $nowTime = $this->_getNowTime(); - - if ($nowTime >= $discount_begin && $nowTime < $discount_end) { - - $need_price = ceil($need_price * ($discount / 100.0)); - } - - $costItemId = $this->getCostItemIdByTokenType($token_type); - - switch ($token_type) { - case ShopController::TOKEN_TYPE_CEG: - case ShopController::TOKEN_TYPE_CEC: - $costItems = $this->makeCostItems($costItemId, $goods_num * $need_price); - $lackItem = null; - if (!$this->_hasEnoughItems($costItems, $lackItem)) { - $this->_rspErr(2, $this->_getLackItemErrMsg($lackItem)); - return; - } - - $itemMeta = mt\Item::get($row['goods_id']); - $propertyChgService = new services\PropertyChgService(); - for ($i = 0; $i < $goods_num; $i++) { - $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0); - } - $awardService = new services\AwardService(); - $awardService->addItem($row['goods_id'], $goods_num); - ShopBuyRecord::add($id, $goods_num); - $this->_decItems($costItems); - $goodsDto = array( - 'goods_id' => $id, - 'item_id' => $row['goods_id'], - 'price_info' => array( - 'item_id' => $row['goods_id'], - 'cost_list' => array(), - 'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), - 'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) - ), - 'flag_icon' => $row['tag'], - 'limit_type' => $row['limit_type'], - 'bought_times' => $boughtTimes, - 'total_buy_times' => $row['limit_num'], - ); { - $priceInfo = mt\Item::getPriceInfo($itemMeta); - if (!empty($priceInfo)) { - $goodsDto['price_info'] = $priceInfo['price_info']; - } - } - $propertyChgService->addUserChg(); - $this->_rspData( - array( - 'award' => $awardService->toDto(), - 'property_chg' => $propertyChgService->toDto(), - 'goods_chg' => $goodsDto - ) - ); - break; - - case ShopController::TOKEN_TYPE_BCEG: - break; - - case ShopController::TOKEN_TYPE_USDT: - case ShopController::TOKEN_TYPE_USDC: - case ShopController::TOKEN_TYPE_BUSD: - case ShopController::TOKEN_TYPE_MATIC: - case ShopController::TOKEN_TYPE_BNB: - default: - $this->_rspErr(1, "token_type is unsupport, {$token_type}"); + $chk = SqlHelper::insert( + $conn, + 't_shop_buy_order', + array( + 'address' => $address, + 'createtime' => myself()->_getNowTime(), + 'item_id' => $id, + 'goods_num' => $goods_num, + 'status' => 0, // 0-客户端申请了订单 1-订单完成 2-订单失败 + ) + ); + if ($chk) { + $lastId = $this->lastInsertId($conn); + $this->_rspData( + array( + 'order_id' => $lastId, + ) + ); + } else { + $this->_rspErr(1, "insert error, id: {$id}, token_type: {$token_type}, goods_num: {$goods_num}"); } } + public function statusGoodsDirect() + { + $order_id = getReqVal('order_id', ''); + + $conn = myself()->_getMysql(''); + + $row = SqlHelper::selectOne( + $conn, + 't_shop_buy_order', + array('status'), + array( + 'idx' => $order_id, + ) + ); + if ($row) { + $this->_rspData( + array( + 'status' => $row['status'], + ) + ); + } else { + $this->_rspErr(1, "order_id not found, order_id: {$order_id}"); + } + } + + // callback from (hongliang) server public function buyGoodsDirect() { // let repdata = { @@ -425,356 +340,6 @@ class ShopController extends BaseAuthedController $this->_rspOk(); } - public function startGoodsDirect() - { - $id = getReqVal('id', 0); - $token_type = getReqVal('token_type', ''); - $goods_num = getReqVal('goods_num', 0); - - $conn = myself()->_getMysql(''); - - $address = myself()->_getAddress(); - - - if (empty($address)) { - $address = myself()->_getAccountId(); - } - - $chk = SqlHelper::insert( - $conn, - 't_shop_buy_order', - array( - 'address' => $address, - 'createtime' => myself()->_getNowTime(), - 'item_id' => $id, - 'goods_num' => $goods_num, - 'status' => 0, // 0-客户端申请了订单 1-订单完成 2-订单失败 - ) - ); - if ($chk) { - $lastId = $this->lastInsertId($conn); - $this->_rspData( - array( - 'order_id' => $lastId, - ) - ); - } else { - $this->_rspErr(1, "insert error, id: {$id}, token_type: {$token_type}, goods_num: {$goods_num}"); - } - } - - public function statusGoodsDirect() - { - $order_id = getReqVal('order_id', ''); - - $conn = myself()->_getMysql(''); - - $row = SqlHelper::selectOne( - $conn, - 't_shop_buy_order', - array('status'), - array( - 'idx' => $order_id, - ) - ); - if ($row) { - $this->_rspData( - array( - 'status' => $row['status'], - ) - ); - } else { - $this->_rspErr(1, "order_id not found, order_id: {$order_id}"); - } - } - - private function getCostItemIdByTokenType($token_type) - { - switch ($token_type) { - case ShopController::TOKEN_TYPE_GOLD: - return V_ITEM_GOLD; - break; - case ShopController::TOKEN_TYPE_CEG: - return V_ITEM_GOLD; - break; - - case ShopController::TOKEN_TYPE_CEC: - return V_ITEM_DIAMOND; - break; - - case ShopController::TOKEN_TYPE_BCEG: - case ShopController::TOKEN_TYPE_USDT: - case ShopController::TOKEN_TYPE_USDC: - case ShopController::TOKEN_TYPE_BUSD: - case ShopController::TOKEN_TYPE_MATIC: - case ShopController::TOKEN_TYPE_BNB: - default: - return -1; - } - } - - private function makeCostItems($item_id, $num) - { - $costItems = array( - array( - 'item_id' => $item_id, - 'item_num' => $num - ) - ); - return $costItems; - } - - public function info() - { - $shopId = getReqVal('shop_id', 0); - if ($shopId == mt\Shop::OUTSIDE_SHOP) { - $this->getOutsideShopInfo(); - return; - } - $goodsList = mt\ShopGoods::getGoodsList($shopId); - if (!$goodsList) { - $this->_rspData( - array( - 'info' => array( - 'shop_id' => $shopId, - 'goods_list1' => array(), - 'goods_list2' => array(), - ) - ) - ); - return; - } - $buyRecordHash = ShopBuyRecord::allToHash(); - $goodsDtoList1 = array(); - $goodsDtoList2 = array(); - foreach ($goodsList as $goods) { - $itemMeta = mt\Item::get($goods['goods_id']); - if ($itemMeta) { - $goodsDto = array( - 'goods_id' => $goods['goods_id'], - 'item_id' => $itemMeta['id'], - 'price_info' => null, - 'flag_icon' => $goods['tag'], - 'limit_type' => $itemMeta['limit_type'], - 'bought_times' => 0, - 'total_buy_times' => $itemMeta['limit_num'], - ); - switch ($itemMeta['limit_type']) { - case mt\Item::DAILY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); - $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0; - } - break; - case mt\Item::WEEKLY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); - $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0; - } - break; - case mt\Item::TOTAL_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); - $goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0; - } - break; - default: { - } - break; - } - $priceInfo = mt\Item::getPriceInfo($itemMeta); - if (!empty($priceInfo)) { - $goodsDto['price_info'] = $priceInfo['price_info']; - array_push($goodsDtoList1, $goodsDto); - } - } - } - $this->_rspData( - array( - 'info' => array( - 'shop_id' => $shopId, - 'goods_list1' => $goodsDtoList1, - 'goods_list2' => $goodsDtoList2, - ) - ) - ); - } - - public function buyGoods() - { - $shopId = getReqVal('shop_id', 0); - $goodsId = getReqVal('goods_id', 0); - $itemNum = getReqVal('goods_num', 0); - $costItemId = getReqVal('cost_item_id', 0); - $itemId = $goodsId; - - if ($shopId == mt\Shop::OUTSIDE_SHOP) { - $this->outsideBuy($shopId, $itemId, $itemNum, $costItemId); - return; - } - - $propertyChgService = new services\PropertyChgService(); - $itemMeta = mt\Item::get($itemId); - if (!$itemMeta) { - $this->_rspErr(1, 'goods_id parameter error'); - return; - } - $goodsMeta = mt\ShopGoods::getGoodsInfo($shopId, $goodsId); - if (!$goodsMeta) { - $this->_rspErr(1, 'goods_id parameter error'); - return; - } - $buyRecordHash = ShopBuyRecord::allToHash(); - $boughtTimes = 1; - switch ($itemMeta['limit_type']) { - case mt\Item::DAILY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); - $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; - if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $itemMeta['limit_num']) { - $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); - return; - } - if ($itemMeta['limit_num'] <= 0) { - $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); - return; - } - } - break; - case mt\Item::WEEKLY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); - $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; - if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $itemMeta['limit_num']) { - $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); - return; - } - if ($itemMeta['limit_num'] <= 0) { - $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); - return; - } - } - break; - case mt\Item::TOTAL_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); - $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; - if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $itemMeta['limit_num']) { - $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); - return; - } - if ($itemMeta['limit_num'] <= 0) { - $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); - return; - } - } - break; - default: { - } - break; - } { - $errCode = 0; - $errMsg = ''; - if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { - $this->_rspErr($errCode, $errMsg); - return; - } - } - $priceInfo = mt\Item::getPriceInfo($itemMeta); - if (empty($priceInfo)) { - $this->_rspErr(2, 'config error'); - return; - } - $costItems = $this->getCostItems($priceInfo, $costItemId); - if (empty($costItems)) { - $this->_rspErr(2, 'config error2'); - return; - } - $lackItem = null; - if (!$this->_hasEnoughItems($costItems, $lackItem)) { - $this->_rspErr(3, $this->_getLackItemErrMsg($lackItem)); - return; - } - $this->_decItems($costItems); - $this->internalAddItem($propertyChgService, $itemMeta, 1, 0); - $awardService = new services\AwardService(); - $awardService->addItem($itemId, $itemNum); - ShopBuyRecord::add($itemId, $itemNum); - $goodsDto = array( - 'goods_id' => $itemMeta['id'], - 'item_id' => $itemMeta['id'], - 'price_info' => array( - 'item_id' => $itemMeta['id'], - 'cost_list' => array(), - 'discount_begin_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_begin']), - 'discount_end_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_end']) - ), - 'flag_icon' => $goodsMeta['tag'], - 'limit_type' => $itemMeta['limit_type'], - 'bought_times' => $boughtTimes, - 'total_buy_times' => $itemMeta['limit_num'], - ); { - $priceInfo = mt\Item::getPriceInfo($itemMeta); - if (!empty($priceInfo)) { - $goodsDto['price_info'] = $priceInfo['price_info']; - } - } - $propertyChgService->addUserChg(); - $this->_rspData( - array( - 'award' => $awardService->toDto(), - 'property_chg' => $propertyChgService->toDto(), - 'goods_chg' => $goodsDto - ) - ); - } - - public function getDiscountList() - { - $items = array(); { - $types = array( - mt\Item::HERO_TYPE, - mt\Item::HERO_SKIN_TYPE, - mt\Item::GUN_SKIN_TYPE - ); - mt\Item::filter(function ($meta) use (&$items, &$types) { - if (mt\Item::inTypes($meta, $types)) { - array_push($items, $meta); - } - return true; - }); - } - $goodsDtoList = array(); - array_walk($items, function ($val) use (&$priceList, &$goodsDtoList) { - $goodsDto = array( - 'item_id' => $val['id'], - 'gold_discount' => 0, - 'diamond_discount' => 0, - ); - $priceInfo = mt\Item::getPriceInfo($val); - if (!empty($priceInfo)) { - foreach ($priceInfo['price_info']['cost_list'] as $costGroup) { - foreach ($costGroup as $cost) { - if ($cost['discount'] > 0) { - switch ($cost['item_id']) { - case V_ITEM_GOLD: { - $goodsDto['gold_discount'] = $cost['discount']; - } - break; - case V_ITEM_DIAMOND: { - $goodsDto['diamond_discount'] = $cost['discount']; - } - break; - } - } - } - } - if ($goodsDto['gold_discount'] > 0 || $goodsDto['diamond_discount'] > 0) { - array_push($goodsDtoList, $goodsDto); - } - } - }); - $this->_rspData( - array( - 'goods_list' => $goodsDtoList, - ) - ); - } - public function getPayMethods() { $token_type = getReqVal('token_type', 99); @@ -788,7 +353,7 @@ class ShopController extends BaseAuthedController public function refreshDailySelection() { - $address = $this->_getAccountId(); + $address = $this->_getAddress(); if (empty($address)) { $this->_rspErr(2, 'address is empty'); return; @@ -828,7 +393,7 @@ class ShopController extends BaseAuthedController public function getDailySelectionList() { - $address = $this->_getAccountId(); + $address = $this->_getAddress(); if (empty($address)) { $this->_rspErr(2, 'address is empty'); return; @@ -1194,89 +759,7 @@ class ShopController extends BaseAuthedController ) ); } - - private function buyBlindBox() - { - $account = $this->_getAccountId(); - - $id = getReqVal('id', 0); - $num = getReqVal('num', 0); - - if (!($num == 1 || $num == 10)) { - $this->_rspErr(2, 'num is invalid'); - return; - } - - $shop = mt\ShopGoods::get($id); - if (!$shop) { - $this->_rspErr(2, 'id is invalid'); - return; - } - $meta = mt\Item::get($shop['goods_id']); - $cost = $shop['price'] * $num; - $isFreeBuy = false; - if (!empty($shop['free_type'])) { - $count = $this->countFreeBuyTimes($shop['free_type'], $shop['id'], $shop['goods_id']); - if ($count < $shop['free_num']) { - $isFreeBuy = true; - } - } - if ($isFreeBuy) { - if ($num != 1) { - $this->_rspErr(2, 'num is invalid'); - return; - } - } else { - $gold = 1500; - if ($gold < $cost) { - $this->_rspErr(2, 'gold is not enough'); - return; - } - $gold -= $cost; - } - - $recommend = $shop['recommend']; - $itemStore = mt\ShopChest::getRandomItemListByChestType($recommend); - $result = array(); - for ($i = 0; $i < $num; $i++) { - $record = array(); - foreach ($itemStore as $key => $value) { - $item = $this->weighted_random($value); - $itemMeta = mt\Item::get($item['item_id']); - $propertyChgService = new services\PropertyChgService(); - if ($item['item_type'] == 2) { - $this->internalAddItem($propertyChgService, $itemMeta, $item['num'], 1); - } else { - for ($j = 0; $j < $item['num']; $j++) { - $this->internalAddItem($propertyChgService, $itemMeta, 1, 1); - } - } - $record[$key] = array("item_id" => $item['item_id'], "item_num" => $item['num']); - array_push($result, $record[$key]); - } - if ($isFreeBuy) { - $this->addFreeBuyRecord($shop); - } - } - - error_log("buyBlindBox start " . json_encode( - array( - 'account' => $account, - 'id' => $id, - 'num' => $num, - 'cost' => $cost, - 'freeBuy' => $isFreeBuy, - 'itemListStore' => $result, - ) - )); - - $this->_rspData( - array( - 'reuslt' => $result, - ) - ); - } - + public function openBlindBox() { $account = $this->_getAccountId(); @@ -1417,6 +900,42 @@ class ShopController extends BaseAuthedController ); } + private function getCostItemIdByTokenType($token_type) + { + switch ($token_type) { + case ShopController::TOKEN_TYPE_GOLD: + return V_ITEM_GOLD; + break; + case ShopController::TOKEN_TYPE_CEG: + return V_ITEM_GOLD; + break; + + case ShopController::TOKEN_TYPE_CEC: + return V_ITEM_DIAMOND; + break; + + case ShopController::TOKEN_TYPE_BCEG: + case ShopController::TOKEN_TYPE_USDT: + case ShopController::TOKEN_TYPE_USDC: + case ShopController::TOKEN_TYPE_BUSD: + case ShopController::TOKEN_TYPE_MATIC: + case ShopController::TOKEN_TYPE_BNB: + default: + return -1; + } + } + + private function makeCostItems($item_id, $num) + { + $costItems = array( + array( + 'item_id' => $item_id, + 'item_num' => $num + ) + ); + return $costItems; + } + private function buyGoodsFree() { } @@ -1921,7 +1440,7 @@ class ShopController extends BaseAuthedController $bn2 = phpcommon\bnInit('1000000000000000000'); $ret_price = phpcommon\bnDiv(phpcommon\bnMul($bn1, $bn2), pow(10, 8)); - error_log('normalizeWeb3Price: ' . $ret_price . ' ' . $price * pow(10, 8)); + // error_log('normalizeWeb3Price: ' . $ret_price . ' ' . $price * pow(10, 8)); return phpcommon\bnToStr($ret_price); } @@ -1969,4 +1488,486 @@ class ShopController extends BaseAuthedController break; } } + + private function buyGoodsNew() + { + $id = getReqVal('id', 0); + $token_type = getReqVal('token_type', ''); + $goods_num = getReqVal('goods_num', 0); + + $row = mt\ShopGoods::get($id); + + $desired_token_type = $row['token_type']; + $check_token_type = splitStr1($desired_token_type); + $token_pos = array_search($token_type, $check_token_type, true); + if (!in_array($token_type, $check_token_type)) { + $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); + return; + } + + if ($goods_num > $row['max_amount']) { + $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); + return; + } + + // 这里命名混乱了, 购买个数,一捆个数命名冲突 + $goods_count = $row['goods_num']; + + $buyRecordHash = ShopBuyRecord::allToHash(); + $boughtTimes = 1; + switch ($row['limit_type']) { + case ShopController::DAILY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $row['limit_num']) { + $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); + return; + } + if ($row['limit_num'] <= 0) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + } + break; + case ShopController::WEEKLY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $row['limit_num']) { + $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); + return; + } + if ($row['limit_num'] <= 0) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + } + break; + case ShopController::TOTAL_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $row['limit_num']) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + if ($row['limit_num'] <= 0) { + $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); + return; + } + } + break; + default: { + } + break; + } + + $price_array = splitStr1($row['price']); + $discount_array = splitStr1($row['discount']); + + $need_price = $price_array[$token_pos]; + $discount = $discount_array[$token_pos]; + + $discount_begin = strtotime($row['discount_begin'] . ' UTC'); + $discount_end = strtotime($row['discount_end'] . ' UTC'); + $nowTime = $this->_getNowTime(); + + if ($nowTime >= $discount_begin && $nowTime < $discount_end) { + + $need_price = ceil($need_price * ($discount / 100.0)); + } + + $costItemId = $this->getCostItemIdByTokenType($token_type); + + switch ($token_type) { + case ShopController::TOKEN_TYPE_CEG: + case ShopController::TOKEN_TYPE_CEC: + $costItems = $this->makeCostItems($costItemId, $goods_num * $need_price); + $lackItem = null; + if (!$this->_hasEnoughItems($costItems, $lackItem)) { + $this->_rspErr(2, $this->_getLackItemErrMsg($lackItem)); + return; + } + + $itemMeta = mt\Item::get($row['goods_id']); + $propertyChgService = new services\PropertyChgService(); + for ($i = 0; $i < $goods_num; $i++) { + $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0); + } + $awardService = new services\AwardService(); + $awardService->addItem($row['goods_id'], $goods_num); + ShopBuyRecord::add($id, $goods_num); + $this->_decItems($costItems); + $goodsDto = array( + 'goods_id' => $id, + 'item_id' => $row['goods_id'], + 'price_info' => array( + 'item_id' => $row['goods_id'], + 'cost_list' => array(), + 'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), + 'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) + ), + 'flag_icon' => $row['tag'], + 'limit_type' => $row['limit_type'], + 'bought_times' => $boughtTimes, + 'total_buy_times' => $row['limit_num'], + ); { + $priceInfo = mt\Item::getPriceInfo($itemMeta); + if (!empty($priceInfo)) { + $goodsDto['price_info'] = $priceInfo['price_info']; + } + } + $propertyChgService->addUserChg(); + $this->_rspData( + array( + 'award' => $awardService->toDto(), + 'property_chg' => $propertyChgService->toDto(), + 'goods_chg' => $goodsDto + ) + ); + break; + + case ShopController::TOKEN_TYPE_BCEG: + break; + + case ShopController::TOKEN_TYPE_USDT: + case ShopController::TOKEN_TYPE_USDC: + case ShopController::TOKEN_TYPE_BUSD: + case ShopController::TOKEN_TYPE_MATIC: + case ShopController::TOKEN_TYPE_BNB: + default: + $this->_rspErr(1, "token_type is unsupport, {$token_type}"); + } + } + + private function buyBlindBox() + { + $account = $this->_getAccountId(); + + $id = getReqVal('id', 0); + $num = getReqVal('num', 0); + + if (!($num == 1 || $num == 10)) { + $this->_rspErr(2, 'num is invalid'); + return; + } + + $shop = mt\ShopGoods::get($id); + if (!$shop) { + $this->_rspErr(2, 'id is invalid'); + return; + } + $meta = mt\Item::get($shop['goods_id']); + $cost = $shop['price'] * $num; + $isFreeBuy = false; + if (!empty($shop['free_type'])) { + $count = $this->countFreeBuyTimes($shop['free_type'], $shop['id'], $shop['goods_id']); + if ($count < $shop['free_num']) { + $isFreeBuy = true; + } + } + if ($isFreeBuy) { + if ($num != 1) { + $this->_rspErr(2, 'num is invalid'); + return; + } + } else { + $gold = 1500; + if ($gold < $cost) { + $this->_rspErr(2, 'gold is not enough'); + return; + } + $gold -= $cost; + } + + $recommend = $shop['recommend']; + $itemStore = mt\ShopChest::getRandomItemListByChestType($recommend); + $result = array(); + for ($i = 0; $i < $num; $i++) { + $record = array(); + foreach ($itemStore as $key => $value) { + $item = $this->weighted_random($value); + $itemMeta = mt\Item::get($item['item_id']); + $propertyChgService = new services\PropertyChgService(); + if ($item['item_type'] == 2) { + $this->internalAddItem($propertyChgService, $itemMeta, $item['num'], 1); + } else { + for ($j = 0; $j < $item['num']; $j++) { + $this->internalAddItem($propertyChgService, $itemMeta, 1, 1); + } + } + $record[$key] = array("item_id" => $item['item_id'], "item_num" => $item['num']); + array_push($result, $record[$key]); + } + if ($isFreeBuy) { + $this->addFreeBuyRecord($shop); + } + } + + error_log("buyBlindBox start " . json_encode( + array( + 'account' => $account, + 'id' => $id, + 'num' => $num, + 'cost' => $cost, + 'freeBuy' => $isFreeBuy, + 'itemListStore' => $result, + ) + )); + + $this->_rspData( + array( + 'reuslt' => $result, + ) + ); + } + + private function buyGoods() + { + $shopId = getReqVal('shop_id', 0); + $goodsId = getReqVal('goods_id', 0); + $itemNum = getReqVal('goods_num', 0); + $costItemId = getReqVal('cost_item_id', 0); + $itemId = $goodsId; + + if ($shopId == mt\Shop::OUTSIDE_SHOP) { + $this->outsideBuy($shopId, $itemId, $itemNum, $costItemId); + return; + } + + $propertyChgService = new services\PropertyChgService(); + $itemMeta = mt\Item::get($itemId); + if (!$itemMeta) { + $this->_rspErr(1, 'goods_id parameter error'); + return; + } + $goodsMeta = mt\ShopGoods::getGoodsInfo($shopId, $goodsId); + if (!$goodsMeta) { + $this->_rspErr(1, 'goods_id parameter error'); + return; + } + $buyRecordHash = ShopBuyRecord::allToHash(); + $boughtTimes = 1; + switch ($itemMeta['limit_type']) { + case mt\Item::DAILY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $itemMeta['limit_num']) { + $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); + return; + } + if ($itemMeta['limit_num'] <= 0) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + } + break; + case mt\Item::WEEKLY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $itemMeta['limit_num']) { + $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); + return; + } + if ($itemMeta['limit_num'] <= 0) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + } + break; + case mt\Item::TOTAL_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $itemMeta['limit_num']) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + if ($itemMeta['limit_num'] <= 0) { + $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); + return; + } + } + break; + default: { + } + break; + } { + $errCode = 0; + $errMsg = ''; + if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { + $this->_rspErr($errCode, $errMsg); + return; + } + } + $priceInfo = mt\Item::getPriceInfo($itemMeta); + if (empty($priceInfo)) { + $this->_rspErr(2, 'config error'); + return; + } + $costItems = $this->getCostItems($priceInfo, $costItemId); + if (empty($costItems)) { + $this->_rspErr(2, 'config error2'); + return; + } + $lackItem = null; + if (!$this->_hasEnoughItems($costItems, $lackItem)) { + $this->_rspErr(3, $this->_getLackItemErrMsg($lackItem)); + return; + } + $this->_decItems($costItems); + $this->internalAddItem($propertyChgService, $itemMeta, 1, 0); + $awardService = new services\AwardService(); + $awardService->addItem($itemId, $itemNum); + ShopBuyRecord::add($itemId, $itemNum); + $goodsDto = array( + 'goods_id' => $itemMeta['id'], + 'item_id' => $itemMeta['id'], + 'price_info' => array( + 'item_id' => $itemMeta['id'], + 'cost_list' => array(), + 'discount_begin_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_begin']), + 'discount_end_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_end']) + ), + 'flag_icon' => $goodsMeta['tag'], + 'limit_type' => $itemMeta['limit_type'], + 'bought_times' => $boughtTimes, + 'total_buy_times' => $itemMeta['limit_num'], + ); { + $priceInfo = mt\Item::getPriceInfo($itemMeta); + if (!empty($priceInfo)) { + $goodsDto['price_info'] = $priceInfo['price_info']; + } + } + $propertyChgService->addUserChg(); + $this->_rspData( + array( + 'award' => $awardService->toDto(), + 'property_chg' => $propertyChgService->toDto(), + 'goods_chg' => $goodsDto + ) + ); + } + + private function getDiscountList() + { + $items = array(); { + $types = array( + mt\Item::HERO_TYPE, + mt\Item::HERO_SKIN_TYPE, + mt\Item::GUN_SKIN_TYPE + ); + mt\Item::filter(function ($meta) use (&$items, &$types) { + if (mt\Item::inTypes($meta, $types)) { + array_push($items, $meta); + } + return true; + }); + } + $goodsDtoList = array(); + array_walk($items, function ($val) use (&$priceList, &$goodsDtoList) { + $goodsDto = array( + 'item_id' => $val['id'], + 'gold_discount' => 0, + 'diamond_discount' => 0, + ); + $priceInfo = mt\Item::getPriceInfo($val); + if (!empty($priceInfo)) { + foreach ($priceInfo['price_info']['cost_list'] as $costGroup) { + foreach ($costGroup as $cost) { + if ($cost['discount'] > 0) { + switch ($cost['item_id']) { + case V_ITEM_GOLD: { + $goodsDto['gold_discount'] = $cost['discount']; + } + break; + case V_ITEM_DIAMOND: { + $goodsDto['diamond_discount'] = $cost['discount']; + } + break; + } + } + } + } + if ($goodsDto['gold_discount'] > 0 || $goodsDto['diamond_discount'] > 0) { + array_push($goodsDtoList, $goodsDto); + } + } + }); + $this->_rspData( + array( + 'goods_list' => $goodsDtoList, + ) + ); + } + + private function info() + { + $shopId = getReqVal('shop_id', 0); + if ($shopId == mt\Shop::OUTSIDE_SHOP) { + $this->getOutsideShopInfo(); + return; + } + $goodsList = mt\ShopGoods::getGoodsList($shopId); + if (!$goodsList) { + $this->_rspData( + array( + 'info' => array( + 'shop_id' => $shopId, + 'goods_list1' => array(), + 'goods_list2' => array(), + ) + ) + ); + return; + } + $buyRecordHash = ShopBuyRecord::allToHash(); + $goodsDtoList1 = array(); + $goodsDtoList2 = array(); + foreach ($goodsList as $goods) { + $itemMeta = mt\Item::get($goods['goods_id']); + if ($itemMeta) { + $goodsDto = array( + 'goods_id' => $goods['goods_id'], + 'item_id' => $itemMeta['id'], + 'price_info' => null, + 'flag_icon' => $goods['tag'], + 'limit_type' => $itemMeta['limit_type'], + 'bought_times' => 0, + 'total_buy_times' => $itemMeta['limit_num'], + ); + switch ($itemMeta['limit_type']) { + case mt\Item::DAILY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0; + } + break; + case mt\Item::WEEKLY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0; + } + break; + case mt\Item::TOTAL_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + $goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0; + } + break; + default: { + } + break; + } + $priceInfo = mt\Item::getPriceInfo($itemMeta); + if (!empty($priceInfo)) { + $goodsDto['price_info'] = $priceInfo['price_info']; + array_push($goodsDtoList1, $goodsDto); + } + } + } + $this->_rspData( + array( + 'info' => array( + 'shop_id' => $shopId, + 'goods_list1' => $goodsDtoList1, + 'goods_list2' => $goodsDtoList2, + ) + ) + ); + } }