diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 58e98191..2316fae8 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -6,7 +6,6 @@ require_once('mt/Hero.php'); require_once('mt/Item.php'); require_once('mt/Parameter.php'); require_once('mt/Drop.php'); -require_once('mt/Dailyselection.php'); require_once('mt/ShopChest.php'); require_once('models/User.php'); @@ -23,6 +22,7 @@ require_once('models/Transaction.php'); require_once('services/AwardService.php'); require_once('services/PropertyChgService.php'); require_once('services/BlockChainService.php'); +require_once('services/ShopService.php'); require_once('phpcommon/bignumber.php'); @@ -38,85 +38,20 @@ use models\GunSkin; use models\ShopBuyRecord; use models\Chip; use mt\Shop; -use mt\Dailyselection; use mt\ShopChest; use mt\ShopGoods; use models\Transaction; use models\BcOrder; use services\LogService; use services\ShopAddItemService; +use services\ShopService; -class ShopController extends BaseAuthedController -{ +class ShopController extends BaseAuthedController { public function getGoodsList() { $shopId = getReqVal('shop_id', 0); - if ($shopId == 0) { - $goodsMetaList = mt\ShopGoods::all(); - } else { - $goodsMetaList = mt\ShopGoods::getGoodsList($shopId); - } - - $goodsMetaList = $goodsMetaList ? $goodsMetaList : array(); - $buyRecordHash = ShopBuyRecord::allToHash(); - - $goodsList = array(); - foreach ($goodsMetaList as $goodsMeta) { - $goodsDto = array( - 'goods_meta' => $this->goodsMetaToInfo($goodsMeta), - 'bought_times' => 0, - 'free_num' => 0, - ); - array_push($goodsList, $goodsDto); - switch ($goodsMeta['limit_type']) { - case mt\Item::DAILY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $goodsMeta['id']); - $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0; - } - break; - case mt\Item::WEEKLY_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $goodsMeta['id']); - $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0; - } - break; - case mt\Item::TOTAL_BUY_LIMIT: { - $buyRecord = getXVal($buyRecordHash, $goodsMeta['id']); - $goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0; - } - break; - default: { - } - break; - } - - $goodsId = $goodsMeta['goods_id']; - $itemMeta = mt\Item::get($goodsId); - if ($itemMeta) { - // 如果是皮肤,判断是否已经拥有,如果已经拥有,不能购买 - if ($itemMeta['type'] == mt\Item::HERO_SKIN_TYPE) { - $errCode = 0; - $errMsg = ''; - if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { - $goods['bought_times'] = 1; - } else { - $goods['bought_times'] = 0; - } - } - } else { - // error !!!!!! - error_log('item not found:' . $goodsId); - } - - if (!empty($goods['free_type'])) { - $count = $this->countFreeBuyTimes($goodsMeta['free_type'], - $goodsMeta['id'], - $goodsMeta['goods_id']); - $goods['free_num'] = $goods['free_num'] - $count; - // error_log('free_num:' . $goods['free_num']); - } - } - + $goodsList = ShopService::getGoodsList($shopId); $this->_rspData( array( 'goods_list' => $goodsList, @@ -135,29 +70,22 @@ class ShopController extends BaseAuthedController return; } - $row = mt\ShopGoods::get($id); + $goodsMeta = mt\ShopGoods::get($id); - if (!$row) { + if (!$goodsMeta) { $this->_rspErr(1, 'goods not found'); return; } - $goodsId = $row['goods_id']; - if (!empty($address)) { - // $pending = $this->checkPendingBuyGoodsNormal($address, $goodsId, $row['shop_id'], $id); - // if ($pending) { - // $this->_rspErr(1, 'pending'); - // return; - // } - } + $goodsId = $goodsMeta['goods_id']; - $desired_token_type = $row['token_type']; + $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($row['free_type'])) { - $count = $this->countFreeBuyTimes($row['free_type'], $row['id'], $row['goods_id']); - if ($count < $row['free_num']) { + if (!empty($goodsMeta['free_type'])) { + $count = $this->countFreeBuyTimes($goodsMeta['free_type'], $goodsMeta['id'], $goodsMeta['goods_id']); + if ($count < $goodsMeta['free_num']) { $isFreeBuy = true; } } @@ -168,20 +96,20 @@ class ShopController extends BaseAuthedController } } - if ($goodsNum > $row['max_amount']) { - $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); + if ($goodsNum > $goodsMeta['max_amount']) { + $this->_rspErr(1, "goods_num parameter error, max_amount: {$goodsMeta['max_amount']}"); return; } // 这里命名混乱了, 购买个数,一捆个数命名冲突 - $goods_count = $row['goods_num']; + $goods_count = $goodsMeta['goods_num']; $buyRecordHash = ShopBuyRecord::allToHash(); $boughtTimes = 1; - $itemMeta = mt\Item::get($row['goods_id']); + $itemMeta = mt\Item::get($goodsMeta['goods_id']); if (!$itemMeta) { - $this->_rspErr(1, 'goods not found, goods_id: ' . $row['goods_id']); + $this->_rspErr(1, 'goods not found, goods_id: ' . $goodsMeta['goods_id']); return; } @@ -194,11 +122,11 @@ class ShopController extends BaseAuthedController } } else { - switch ($row['limit_type']) { + 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) >= $row['limit_num']) { + if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $goodsMeta['limit_num']) { $this->_rspErr(2, 'Daily purchase limit'); return; } @@ -207,17 +135,17 @@ class ShopController extends BaseAuthedController 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']) { + 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 . " " . $row['limit_num']); + // 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) >= $row['limit_num']) { + if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $goodsMeta['limit_num']) { $this->_rspErr(2, 'Purchase limit reached'); return; } @@ -229,14 +157,14 @@ class ShopController extends BaseAuthedController } } - $price_array = splitStr1($row['price']); - $discount_array = splitStr1($row['discount']); + $price_array = splitStr1($goodsMeta['price']); + $discount_array = splitStr1($goodsMeta['discount']); $need_price = $price_array[$token_pos]; $discount = $discount_array[$token_pos]; - $discount_begin = strtotime($row['discount_begin']); - $discount_end = strtotime($row['discount_end']); + $discount_begin = strtotime($goodsMeta['discount_begin']); + $discount_end = strtotime($goodsMeta['discount_end']); $nowTime = $this->_getNowTime(); if ($nowTime >= $discount_begin && $nowTime < $discount_end) { @@ -255,28 +183,28 @@ class ShopController extends BaseAuthedController return; } - $itemMeta = mt\Item::get($row['goods_id']); + $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($row['goods_id'], $goodsNum); + $awardService->addItem($goodsMeta['goods_id'], $goodsNum); ShopBuyRecord::add($id, $goodsNum); $this->_decItems($costItems); $goodsDto = array( 'goods_id' => $id, - 'item_id' => $row['goods_id'], + 'item_id' => $goodsMeta['goods_id'], 'price_info' => array( - 'item_id' => $row['goods_id'], + 'item_id' => $goodsMeta['goods_id'], 'cost_list' => array(), - 'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), - 'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) + 'discount_begin_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_begin']), + 'discount_end_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_end']) ), - 'flag_icon' => $row['tag'], - 'limit_type' => $row['limit_type'], + 'flag_icon' => $goodsMeta['tag'], + 'limit_type' => $goodsMeta['limit_type'], 'bought_times' => $boughtTimes, - 'total_buy_times' => $row['limit_num'], + 'total_buy_times' => $goodsMeta['limit_num'], ); { $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { @@ -303,16 +231,16 @@ class ShopController extends BaseAuthedController return; } - $itemMeta = mt\Item::get($row['goods_id']); + $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($row['goods_id'], $goodsNum); + $awardService->addItem($goodsMeta['goods_id'], $goodsNum); ShopBuyRecord::add($id, $goodsNum); if ($isFreeBuy) { - $this->addFreeBuyRecord($row); + $this->addFreeBuyRecord($goodsMeta); } $this->_decItems($costItems); $event = [ @@ -323,17 +251,17 @@ class ShopController extends BaseAuthedController $goodsDto = array( 'goods_id' => $id, - 'item_id' => $row['goods_id'], + 'item_id' => $goodsMeta['goods_id'], 'price_info' => array( - 'item_id' => $row['goods_id'], + 'item_id' => $goodsMeta['goods_id'], 'cost_list' => array(), - 'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), - 'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) + 'discount_begin_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_begin']), + 'discount_end_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_end']) ), - 'flag_icon' => $row['tag'], - 'limit_type' => $row['limit_type'], + 'flag_icon' => $goodsMeta['tag'], + 'limit_type' => $goodsMeta['limit_type'], 'bought_times' => $boughtTimes, - 'total_buy_times' => $row['limit_num'], + 'total_buy_times' => $goodsMeta['limit_num'], ); { $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { @@ -353,13 +281,13 @@ class ShopController extends BaseAuthedController case ShopController::TOKEN_TYPE_CEC: if ($isFreeBuy) { $propertyChgService = new services\PropertyChgService(); - $this->addFreeBuyRecord($row); - $itemMeta = mt\Item::get($row['goods_id']); + $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 = $row['goods_id']; + $item_id = $goodsMeta['goods_id']; $item_count = $goodsNum; $response = services\BlockChainService::gameItemMallBuy( @@ -376,7 +304,7 @@ class ShopController extends BaseAuthedController 'price' => $goodsNum * $need_price, 'ext_data' => json_encode(array( 'mode' => SHOP_BUY_MODE_NORMAL, - 'shop_id' => $row['shop_id'], + 'shop_id' => $goodsMeta['shop_id'], 'id' => $id, )), )); @@ -696,46 +624,6 @@ class ShopController extends BaseAuthedController } } - private function canBuy($itemMeta, &$errCode, &$errMsg) - { - $errCode = 0; - $errMsg = ''; - switch ($itemMeta['type']) { - case mt\Item::HERO_TYPE: { - $heroDb = Hero::find($itemMeta['id']); - if ($heroDb) { - $errCode = 10; - $errMsg = 'You already have the hero'; - return false; - } - } - break; - case mt\Item::HERO_SKIN_TYPE: { - $heroSkinDb = HeroSkin::find($itemMeta['id']); - if ($heroSkinDb) { - $errCode = 10; - $errMsg = 'You already have the skin'; - return false; - } - } - break; - case mt\Item::GUN_SKIN_TYPE: { - $gunSkinDb = GunSkin::find($itemMeta['id']); - if ($gunSkinDb) { - $errCode = 10; - $errMsg = 'You already have the skin'; - return false; - } - } - break; - default: { - return true; - } - break; - } - return true; - } - private function beginFirstTupop($address = null) { if (!$address) { @@ -829,10 +717,4 @@ class ShopController extends BaseAuthedController } } - private function goodsMetaToInfo($goodsMeta) { - return array( - - ); - } - } diff --git a/webapp/services/ShopService.php b/webapp/services/ShopService.php new file mode 100644 index 00000000..7aff8791 --- /dev/null +++ b/webapp/services/ShopService.php @@ -0,0 +1,138 @@ + self::goodsMetaToInfo($goodsMeta), + 'bought_times' => 0, + 'free_num' => 0, + ); + array_push($goodsList, $goodsDto); + switch ($goodsMeta['limit_type']) { + case mt\Item::DAILY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $goodsMeta['id']); + $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0; + } + break; + case mt\Item::WEEKLY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $goodsMeta['id']); + $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0; + } + break; + case mt\Item::TOTAL_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $goodsMeta['id']); + $goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0; + } + break; + default: { + } + break; + } + + $goodsId = $goodsMeta['goods_id']; + $itemMeta = mt\Item::get($goodsId); + if ($itemMeta) { + // 如果是皮肤,判断是否已经拥有,如果已经拥有,不能购买 + if ($itemMeta['type'] == mt\Item::HERO_SKIN_TYPE) { + $errCode = 0; + $errMsg = ''; + if (!self::canBuy($itemMeta, $errCode, $errMsg)) { + $goods['bought_times'] = 1; + } else { + $goods['bought_times'] = 0; + } + } + } else { + // error !!!!!! + error_log('item not found:' . $goodsId); + } + + if (!empty($goods['free_type'])) { + $count = self::getFreeBuyTimes($goodsMeta['free_type'], + $goodsMeta['id'], + $goodsMeta['goods_id']); + $goodsDto['free_num'] = $goodsDto['free_num'] - $count; + } + } + return $goodsList; + } + + public static function getFreeBuyTimes() { + + } + + private static function goodsMetaToInfo($goodsMeta) { + return array( + + ); + } + + private static function canBuy($itemMeta, &$errCode, &$errMsg) + { + $errCode = 0; + $errMsg = ''; + switch ($itemMeta['type']) { + case mt\Item::HERO_TYPE: { + $heroDb = Hero::find($itemMeta['id']); + if ($heroDb) { + $errCode = 10; + $errMsg = 'You already have the hero'; + return false; + } + } + break; + case mt\Item::HERO_SKIN_TYPE: { + $heroSkinDb = HeroSkin::find($itemMeta['id']); + if ($heroSkinDb) { + $errCode = 10; + $errMsg = 'You already have the skin'; + return false; + } + } + break; + case mt\Item::GUN_SKIN_TYPE: { + $gunSkinDb = GunSkin::find($itemMeta['id']); + if ($gunSkinDb) { + $errCode = 10; + $errMsg = 'You already have the skin'; + return false; + } + } + break; + default: { + return true; + } + break; + } + return true; + } + +}