diff --git a/doc/Shop.py b/doc/Shop.py index 2375cb0..0b72127 100644 --- a/doc/Shop.py +++ b/doc/Shop.py @@ -13,7 +13,7 @@ class Shop(object): 'url': 'webapp/index.php?c=Shop&a=info', 'params': [ _common.ReqHead(), - ['shop_id', '', '商店id'], + ['shop_id', '', '商店id shop_id=100:商店外购买'], ], 'response': [ _common.RspHead(), @@ -27,7 +27,7 @@ class Shop(object): 'url': 'webapp/index.php?c=Shop&a=buyGoods', 'params': [ _common.ReqHead(), - ['shop_id', 0, '商店id'], + ['shop_id', 0, '商店id shop_id=100特殊商店id'], ['goods_id', 0, '商品id'], ['cost_item_id', 0, '支付方式'], ], @@ -37,34 +37,4 @@ class Shop(object): ['property_chg', _common.PropertyChg(), '属性变更'], ] }, - { - 'name': 'outsideBuy', - 'desc': '购买道具(商店外购买)', - 'group': 'Shop', - 'url': 'webapp/index.php?c=Shop&a=outsideBuy', - 'params': [ - _common.ReqHead(), - ['item_id', 0, '商品id'], - ['item_num', 0, '购买数量'], - ['cost_item_id', 0, '支付方式'], - ], - 'response': [ - _common.RspHead(), - ['award', _common.Award(), '奖励信息'], - ['property_chg', _common.PropertyChg(), '属性变更'], - ] - }, - { - 'name': 'getOutsidePriceInfo', - 'desc': '获取价格信息(商店外购买目前只有:英雄、英雄皮肤、枪支皮肤)', - 'group': 'Shop', - 'url': 'webapp/index.php?c=Shop&a=getOutsidePriceInfo', - 'params': [ - _common.ReqHead(), - ], - 'response': [ - _common.RspHead(), - ['!price_list', [_common.ItemPriceInfo()], '价格信息'], - ] - }, ] diff --git a/doc/_common.py b/doc/_common.py index bbdc006..f066ccb 100644 --- a/doc/_common.py +++ b/doc/_common.py @@ -155,6 +155,7 @@ class Shop(object): def __init__(self): self.fields = [ + ['shop_id', 0, '商店id'], ['!goods_list1', [Goods()], '商品列表1'], ['!goods_list2', [Goods()], '商品列表2'], ] diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 1591987..8103f7f 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -21,10 +21,15 @@ class ShopController extends BaseAuthedController { 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(), ) @@ -79,6 +84,7 @@ class ShopController extends BaseAuthedController { } $this->_rspData(array( 'info' => array( + 'shop_id' => $shopId, 'goods_list1' => $goodsDtoList1, 'goods_list2' => $goodsDtoList2, ) @@ -87,70 +93,74 @@ class ShopController extends BaseAuthedController { public function buyGoods() { - } - - public function outsideBuy() - { - $itemId = getReqVal('item_id', 0); - $itemNum = getReqVal('item_num', 0); - $costItemId = getReqVal('item_id', 0); + $shopId = getReqVal('shop_id', 0); + $goodsId = getReqVal('goods_id', 0); + $itemNum = getReqVal('goods_num', 0); + $costItemId = getReqVal('cost_item_id', 0); + $itemId = $goodsId; $itemMeta = mt\Item::get($itemId); if (!$itemMeta) { - $this->_rspErr(1, 'item_id参数错误'); + $this->_rspErr(1, 'goods_id参数错误'); return; } - if ($itemNum != 1) { - $this->_rspErr(1, 'item_num参数必须等于1'); - return; - } - $costItemMeta = mt\Item::get($costItemId); - if (!$costItemMeta) { - $this->_rspErr(1, 'item_id参数错误'); - return; - } - $types = array(mt\Item::HERO_TYPE, - mt\Item::HERO_SKIN_TYPE, - mt\Item::GUN_SKIN_TYPE); - if (!mt\Item::inTypes($itemMeta, $types)) { - $this->_rspErr(1, 'item_id参数错误'); + $goodsMeta = mt\ShopGoods::getGoodsInfo($shopId, $goodsId); + if (!$goodsMeta) { + $this->_rspErr(1, 'goods_id参数错误'); return; } + $buyRecordHash = ShopBuyRecord::allToHash(); + switch ($itemMeta['limit_type']) { + case mt\Item::DAILY_BUY_LIMIT: { - switch ($itemMeta['type']) { - case mt\Item::HERO_TYPE: - { - $heroDb = Hero::find($itemMeta['id']); - if (!$heroDb) { - $this->_rspErr(4, '你已经拥有该英雄'); - return; - } - } - break; - case mt\Item::HERO_SKIN_TYPE: - { - $heroSkinDb = HeroSkin::find($itemMeta['id']); - if (!$heroSkinDb) { - $this->_rspErr(4, '你已经拥有该皮肤'); - return; - } - } - break; - case mt\Item::GUN_SKIN_TYPE: - { - $gunSkinDb = GunSkin::find($itemMeta['id']); - if (!$gunSkinDb) { - $this->_rspErr(4, '你已经拥有该皮肤'); - return; - } - } - break; - default: - { - $this->_rspErr(1, 'item_id参数错误'); + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $itemMeta['limit_num']) { + $this->_rspErr(2, '已达今日限购上限次数'); return; } - break; + if ($itemMeta['limit_num'] <= 0) { + $this->_rspErr(2, '已达限购上限次数'); + return; + } + } + break; + case mt\Item::WEEKLY_BUY_LIMIT: + { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $itemMeta['limit_num']) { + $this->_rspErr(2, '已达本周限购上限次数'); + return; + } + if ($itemMeta['limit_num'] <= 0) { + $this->_rspErr(2, '已达限购上限次数'); + return; + } + } + break; + case mt\Item::TOTAL_BUY_LIMIT: + { + $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); + if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $itemMeta['limit_num']) { + $this->_rspErr(2, '已达限购上限次数'); + return; + } + if ($itemMeta['limit_num'] <= 0) { + $this->_rspErr(2, '已达限购上限次数'); + return; + } + } + break; + default: + { + } + break; + } + { + $errCode = 0; + $errMsg = ''; + if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { + $this->_rspErr($errCode, $errMsg); + return; } } $priceInfo = mt\Item::getPriceInfo($itemMeta); @@ -181,7 +191,70 @@ class ShopController extends BaseAuthedController { )); } - public function getOutsidePriceInfo() + private function outsideBuy() + { + $itemId = getReqVal('item_id', 0); + $itemNum = getReqVal('item_num', 0); + $costItemId = getReqVal('cost_item_id', 0); + + $itemMeta = mt\Item::get($itemId); + if (!$itemMeta) { + $this->_rspErr(1, 'item_id参数错误'); + return; + } + if ($itemNum != 1) { + $this->_rspErr(1, 'item_num参数必须等于1'); + return; + } + $costItemMeta = mt\Item::get($costItemId); + if (!$costItemMeta) { + $this->_rspErr(1, 'item_id参数错误'); + return; + } + $types = array(mt\Item::HERO_TYPE, + mt\Item::HERO_SKIN_TYPE, + mt\Item::GUN_SKIN_TYPE); + if (!mt\Item::inTypes($itemMeta, $types)) { + $this->_rspErr(1, 'item_id参数错误'); + return; + } + { + $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, '配置表错误'); + return; + } + $costItems = $this->getCostTimes($priceInfo, $costItemId); + if (empty($costItems)) { + $this->_rspErr(2, '配置表错误2'); + return; + } + $lackItem = null; + if (!$this->_hasEnoughItems($costItems, $lackItem)) { + $this->_rspErr(3, $this->_getLackItemErrMsg($lackItem)); + return; + } + $this->_decItems($costItems); + $this->internalAddItem($itemMeta); + $awardService = new services\AwardService(); + $awardService->addItem($itemId, $itemNum); + ShopBuyRecord::add($itemId, $itemNum); + $this->rspData(array( + 'award' => $awardService->toDto(), + 'property_chg' => array( + 'user_info' => User::info($this->_getOrmUserInfo()) + ) + )); + } + + private function getOutsideShopInfo() { $items = array(); { @@ -195,12 +268,30 @@ class ShopController extends BaseAuthedController { return true; }); } - $priceList = array(); - array_walk($items, function ($val) use(&$priceList) { - array_push($priceList, mt\Item::getPriceInfo($val)); + $goodsDtoList1 = array(); + $goodsDtoList2 = array(); + array_walk($items, function ($val) use(&$priceList, &$goodsDtoList1, &$goodsDtoList2) { + $goodsDto = array( + 'goods_id' => $val['id'], + 'item_id' => $val['id'], + 'price_info' => null, + 'flag_icon' => '', + 'limit_type' => $val['limit_type'], + 'bought_times' => 0, + 'total_buy_times' => $val['limit_num'], + ); + $priceInfo = mt\Item::getPriceInfo($val); + if (!empty($priceInfo)) { + $goodsDto['price_info'] = $priceInfo; + array_push($goodsDtoList1, $goodsDto); + } }); $this->_rspData(array( - 'price_list' => $priceList + 'info' => array( + 'shop_id' => mt\Shop::OUTSIDE_SHOP, + 'goods_list1' => $goodsDtoList1, + 'goods_list2' => $goodsDtoList2, + ) )); } @@ -265,4 +356,47 @@ 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 = '你已经拥有该英雄'; + return false; + } + } + break; + case mt\Item::HERO_SKIN_TYPE: + { + $heroSkinDb = HeroSkin::find($itemMeta['id']); + if (!$heroSkinDb) { + $errCode = 10; + $errMsg = '你已经拥有该皮肤'; + return false; + } + } + break; + case mt\Item::GUN_SKIN_TYPE: + { + $gunSkinDb = GunSkin::find($itemMeta['id']); + if (!$gunSkinDb) { + $errCode = 10; + $errMsg = '你已经拥有该皮肤'; + return false; + } + } + break; + default: + { + return true; + } + break; + } + return true; + } } diff --git a/webapp/mt/Shop.php b/webapp/mt/Shop.php index 8a3bf28..c621c0b 100644 --- a/webapp/mt/Shop.php +++ b/webapp/mt/Shop.php @@ -6,6 +6,8 @@ use phpcommon; class Shop { + const OUTSIDE_SHOP = 100; + public static function get($id) { return getXVal(self::getMetaList(), $id);