From 46c7a3b369dcbaa1fbf1dc57e165b2e7e42ad4af Mon Sep 17 00:00:00 2001 From: songliang Date: Thu, 29 Jun 2023 16:55:52 +0800 Subject: [PATCH] ... --- doc/Shop.py | 27 ++++++++ webapp/controller/ShopController.class.php | 72 +++++++++++++++++++++- webapp/models/Bag.php | 11 ++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/doc/Shop.py b/doc/Shop.py index 622d90a9..3da76a18 100644 --- a/doc/Shop.py +++ b/doc/Shop.py @@ -247,5 +247,32 @@ class Shop(object): ['!items', [0], '宝箱物品列表'], ] }, + { + 'name': 'openBlindBox', + 'desc': '打开盲盒', + 'group': 'Shop', + 'url': 'webapp/index.php?c=Shop&a=openBlindBox', + 'params': [ + _common.ReqHead(), + ['item_id', 0, '宝箱物品id item_id'], + ], + 'response': [ + _common.RspHead(), + ['!items', [_common.BlindBoxResult()], '宝箱物品列表'], + ] + }, + { + 'name': 'getMyBlindBoxs', + 'desc': '获取我的盲盒列表', + 'group': 'Shop', + 'url': 'webapp/index.php?c=Shop&a=getMyBlindBoxs', + 'params': [ + _common.ReqHead(), + ], + 'response': [ + _common.RspHead(), + ['!items', [id], '盲盒列表'], + ] + }, ] diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 96778349..88579841 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -1147,7 +1147,7 @@ class ShopController extends BaseAuthedController ); } - public function buyBlindBox() + private function buyBlindBox() { $account = $this->_getAccountId(); @@ -1229,6 +1229,61 @@ class ShopController extends BaseAuthedController ); } + public function openBlindBox() + { + $account = $this->_getAccountId(); + + $item_id = getReqVal('item_id', 0); + $meta = mt\Item::get($item_id); + $box = Bag::find($item_id); + if (!$box) { + $this->_rspErr(2, 'item_id is invalid'); + return; + } + + $num = $box['item_num']; + if ($num <= 1) { + $this->_rspErr(2, 'num is invalid'); + return; + } + + $itemStore = mt\ShopChest::getRandomItemListByChestType($meta['sub_type']); + $result = array(); + Bag::decItem($item_id, $num); + 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]); + } + } + + error_log("openBlindBox start " . json_encode( + array( + 'account' => $account, + 'item_id' => $item_id, + 'num' => $num, + 'items' => $result, + ) + )); + + $this->_rspData( + array( + 'items' => $result, + ) + ); + } + public function getChestItems() { $goods_id = getReqVal('goods_id', 0); @@ -1261,6 +1316,21 @@ class ShopController extends BaseAuthedController ); } + public function getMyBlindBoxs() { + + $itemDb = Bag::getAllByType(mt\Item::CHEST_BOX_TYPE); + $items = array(); + foreach ($itemDb as $key => $value) { + array_push($items, $value['item_id']); + } + + $this->_rspData( + array( + 'items' => $items, + ) + ); + } + private function buyGoodsFree() { } diff --git a/webapp/models/Bag.php b/webapp/models/Bag.php index 0424d9b3..320b01a4 100644 --- a/webapp/models/Bag.php +++ b/webapp/models/Bag.php @@ -103,6 +103,17 @@ class Bag extends BaseModel { ); } + public static function getAllByType($type) { + $itemList = array(); + self::getItemList(function ($row) use(&$itemList, $type) { + $itemMeta = mt\Item::get($row['item_id']); + if ($itemMeta['type'] == $type && $row['item_num'] > 0) { + array_push($itemList, $row); + } + }); + return $itemList; + } + public static function all() { $itemList = array();