From 98c38f44e3bc2bd162724709f4f2e7ef5f9d1396 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 29 Jul 2023 09:05:23 +0800 Subject: [PATCH] 1 --- webapp/controller/ShopController.class.php | 293 +++++++++++++++++++-- webapp/services/ShopService.php | 11 + 2 files changed, 288 insertions(+), 16 deletions(-) diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 2316fae8..2bebcb29 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -336,6 +336,283 @@ class ShopController extends BaseAuthedController { } } + private function buyGoodsOld() + { + $id = getReqVal('id', 0); + $tokenType = getReqVal('token_type', ''); + $goodsNum = getReqVal('goods_num', 0); + + if ($goodsNum < 1) { + $this->_rspErr(1, "goods_num parameter error, goods_num: {$goodsNum}"); + return; + } + + $goodsMeta = mt\ShopGoods::get($id); + + if (!$goodsMeta) { + $this->_rspErr(1, 'goods not found'); + return; + } + + $goodsId = $goodsMeta['goods_id']; + + $desired_token_type = $goodsMeta['token_type']; + $check_token_type = splitStr1($desired_token_type); + $token_pos = array_search($tokenType, $check_token_type, true); + $isFreeBuy = false; + if (!empty($goodsMeta['free_type'])) { + $count = $this->countFreeBuyTimes($goodsMeta['free_type'], $goodsMeta['id'], $goodsMeta['goods_id']); + if ($count < $goodsMeta['free_num']) { + $isFreeBuy = true; + } + } + if (!$isFreeBuy) { + if (!in_array($tokenType, $check_token_type)) { + $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); + return; + } + } + + if ($goodsNum > $goodsMeta['max_amount']) { + $this->_rspErr(1, "goods_num parameter error, max_amount: {$goodsMeta['max_amount']}"); + return; + } + + // 这里命名混乱了, 购买个数,一捆个数命名冲突 + $goods_count = $goodsMeta['goods_num']; + + $buyRecordHash = ShopBuyRecord::allToHash(); + $boughtTimes = 1; + + $itemMeta = mt\Item::get($goodsMeta['goods_id']); + if (!$itemMeta) { + $this->_rspErr(1, 'goods not found, goods_id: ' . $goodsMeta['goods_id']); + return; + } + + if ($itemMeta['type'] == mt\Item::HERO_SKIN_TYPE) { + $errCode = 0; + $errMsg = ''; + if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { + $this->_rspErr($errCode, $errMsg); + return; + } + } else { + + switch ($goodsMeta['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) >= $goodsMeta['limit_num']) { + $this->_rspErr(2, 'Daily purchase limit'); + 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) >= $goodsMeta['limit_num']) { + $this->_rspErr(2, 'Weekly purchase limit reached'); + return; + } + } + break; + case ShopController::TOTAL_BUY_LIMIT: { + // error_log("total buy limit " . $address . " " . $id . " " . $goodsMeta['limit_num']); + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $goodsMeta['limit_num']) { + $this->_rspErr(2, 'Purchase limit reached'); + return; + } + } + break; + default: { + } + break; + } + } + + $price_array = splitStr1($goodsMeta['price']); + $discount_array = splitStr1($goodsMeta['discount']); + + $need_price = $price_array[$token_pos]; + $discount = $discount_array[$token_pos]; + + $discount_begin = strtotime($goodsMeta['discount_begin']); + $discount_end = strtotime($goodsMeta['discount_end']); + $nowTime = $this->_getNowTime(); + + if ($nowTime >= $discount_begin && $nowTime < $discount_end) { + + $need_price = ceil($need_price * ($discount / 100.0)); + } + + $costItemId = $this->getCostItemIdByTokenType($tokenType); + + switch ($tokenType) { + case ShopController::TOKEN_TYPE_GOLD: + $costItems = $this->makeCostItems($costItemId, $goodsNum * $need_price); + $lackItem = null; + if (!$this->_hasEnoughItems($costItems, $lackItem)) { + $this->_rspErr(2, $this->_getLackItemErrMsg($lackItem)); + return; + } + + $itemMeta = mt\Item::get($goodsMeta['goods_id']); + $propertyChgService = new services\PropertyChgService(); + for ($i = 0; $i < $goodsNum; $i++) { + $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0); + } + $awardService = new services\AwardService(); + $awardService->addItem($goodsMeta['goods_id'], $goodsNum); + ShopBuyRecord::add($id, $goodsNum); + $this->_decItems($costItems); + $goodsDto = array( + 'goods_id' => $id, + 'item_id' => $goodsMeta['goods_id'], + 'price_info' => array( + 'item_id' => $goodsMeta['goods_id'], + 'cost_list' => array(), + 'discount_begin_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_begin']), + 'discount_end_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_end']) + ), + 'flag_icon' => $goodsMeta['tag'], + 'limit_type' => $goodsMeta['limit_type'], + 'bought_times' => $boughtTimes, + 'total_buy_times' => $goodsMeta['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_DIAMOND: + if ($isFreeBuy) { + $need_price = 0; + } + $costItems = $this->makeCostItems($costItemId, $goodsNum * $need_price); + $lackItem = null; + if (!$this->_hasEnoughItems($costItems, $lackItem)) { + $this->_rspErr(2, $this->_getLackItemErrMsg($lackItem)); + return; + } + + $itemMeta = mt\Item::get($goodsMeta['goods_id']); + $propertyChgService = new services\PropertyChgService(); + for ($i = 0; $i < $goodsNum; $i++) { + $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0); + } + $awardService = new services\AwardService(); + $awardService->addItem($goodsMeta['goods_id'], $goodsNum); + ShopBuyRecord::add($id, $goodsNum); + if ($isFreeBuy) { + $this->addFreeBuyRecord($goodsMeta); + } + $this->_decItems($costItems); + $event = [ + 'name' => LogService::SHOP_BUY_ITEM, + 'val' => $costItems[0]['item_num'] + ]; + LogService::consumeDiamond($event); + + $goodsDto = array( + 'goods_id' => $id, + 'item_id' => $goodsMeta['goods_id'], + 'price_info' => array( + 'item_id' => $goodsMeta['goods_id'], + 'cost_list' => array(), + 'discount_begin_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_begin']), + 'discount_end_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_end']) + ), + 'flag_icon' => $goodsMeta['tag'], + 'limit_type' => $goodsMeta['limit_type'], + 'bought_times' => $boughtTimes, + 'total_buy_times' => $goodsMeta['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_CEG: + case ShopController::TOKEN_TYPE_CEC: + if ($isFreeBuy) { + $propertyChgService = new services\PropertyChgService(); + $this->addFreeBuyRecord($goodsMeta); + $itemMeta = mt\Item::get($goodsMeta['goods_id']); + $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 1); + $this->_rspOk(); + } else { + $price = $this->normalizeWeb3Price($goodsNum * $need_price); + $item_id = $goodsMeta['goods_id']; + $item_count = $goodsNum; + + $response = services\BlockChainService::gameItemMallBuy( + Transaction::BUY_GOODS_ACTION_TYPE, + $price, + $item_id, + $item_count + ); + + BcOrder::upsert($response['trans_id'], array( + 'item_id' => $item_id, + 'item_num' => $item_count, + 'order_type' => 1, + 'price' => $goodsNum * $need_price, + 'ext_data' => json_encode(array( + 'mode' => SHOP_BUY_MODE_NORMAL, + 'shop_id' => $goodsMeta['shop_id'], + 'id' => $id, + )), + )); + + $response['item_id'] = $item_id; + $response['item_num'] = $item_count; + + error_log("buy normal, item_id = " . $item_id . " item_count = " . $item_count . " need_price = " . $need_price . " price = " . $price . " response = " . json_encode($response)); + + $this->_rspData( + array( + "block_chain" => $response + ) + ); + } + 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, {$tokenType}"); + } + } + } + public function outappPurchase() { $id = getReqVal('id', 0); @@ -674,22 +951,6 @@ class ShopController extends BaseAuthedController { return phpcommon\bnToStr($ret_price); } - private function countFreeBuyTimes($free_type, $id, $goodsId) - { - return 0; - switch ($free_type) { - case 1: { - $dayTime = myself()->_getNowDaySeconds(); - $sql = 'SELECT COUNT(idx) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?'; - $row = $conn->execQueryOne($sql, array($account, $id, $goodsId, $dayTime)); - return $row['cnt']; - } - break; - } - - return 0; - } - private function addFreeBuyRecord($goods) { $conn = myself()->_getMysql(''); diff --git a/webapp/services/ShopService.php b/webapp/services/ShopService.php index 7aff8791..baffc503 100644 --- a/webapp/services/ShopService.php +++ b/webapp/services/ShopService.php @@ -86,7 +86,18 @@ class ShopService { } public static function getFreeBuyTimes() { + return 0; + switch ($free_type) { + case 1: { + $dayTime = myself()->_getNowDaySeconds(); + $sql = 'SELECT COUNT(idx) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?'; + $row = $conn->execQueryOne($sql, array($account, $id, $goodsId, $dayTime)); + return $row['cnt']; + } + break; + } + return 0; } private static function goodsMetaToInfo($goodsMeta) {