Merge branch 'star' of git.kingsome.cn:server/game2006api into star

This commit is contained in:
hujiabin 2023-06-28 20:01:14 +08:00
commit 6b0187869b
4 changed files with 207 additions and 64 deletions

View File

@ -217,5 +217,20 @@ class Shop(object):
_common.RspHead(),
['block_chain', _common.ShopTrans(), '链上购买订单信息']
]
},
{
'name': 'buyBlindBox',
'desc': '购买盲盒(宝箱)',
'group': 'Shop',
'url': 'webapp/index.php?c=Shop&a=buyBlindBox',
'params': [
_common.ReqHead(),
['id', 0, '商品唯一id参见shopGoods表'],
['num', 0, '购买数量 1:单个 10:十连抽'],
],
'response': [
_common.RspHead(),
['!result', [_common.BlindBoxResult()], '盲盒结果列表'],
]
}
]

View File

@ -1218,4 +1218,12 @@ class ShopTrans(object):
['!params', [''], 'params'],
['item_id', 0, 'item_id'],
['item_num', 0, 'item_num'],
]
class BlindBoxResult(object):
def __init__(self):
self.fields = [
['item_id', 0, 'item_id'],
['item_num', 0, 'item_num'],
]

View File

@ -8,6 +8,7 @@ require_once('mt/Parameter.php');
require_once('mt/Drop.php');
require_once('mt/PayMethod.php');
require_once('mt/Dailyselection.php');
require_once('mt/ShopChest.php');
require_once('models/User.php');
require_once('models/Hero.php');
@ -40,6 +41,7 @@ use models\Chip;
use mt\Shop;
use mt\PayMethod;
use mt\Dailyselection;
use mt\ShopChest;
use models\Transaction;
use models\BcOrder;
@ -265,7 +267,7 @@ class ShopController extends BaseAuthedController
$itemMeta = mt\Item::get($row['goods_id']);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goods_num; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count);
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0);
}
$awardService = new services\AwardService();
$awardService->addItem($row['goods_id'], $goods_num);
@ -406,7 +408,7 @@ class ShopController extends BaseAuthedController
$itemMeta = mt\Item::get($row['goods_id']);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goods_num; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count);
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0);
}
$awardService = new services\AwardService();
// $awardService->addItem($row['goods_id'], $goods_num);
@ -671,7 +673,7 @@ class ShopController extends BaseAuthedController
return;
}
$this->_decItems($costItems);
$this->internalAddItem($propertyChgService, $itemMeta, 1);
$this->internalAddItem($propertyChgService, $itemMeta, 1, 0);
$awardService = new services\AwardService();
$awardService->addItem($itemId, $itemNum);
ShopBuyRecord::add($itemId, $itemNum);
@ -952,7 +954,7 @@ class ShopController extends BaseAuthedController
$itemMeta = mt\Item::get($row['goods_id']);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goods_num; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count);
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0);
}
$awardService = new services\AwardService();
$awardService->addItem($row['goods_id'], $goods_num);
@ -992,22 +994,22 @@ class ShopController extends BaseAuthedController
$propertyChgService = new services\PropertyChgService();
$this->addFreeBuyRecord($row);
$itemMeta = mt\Item::get($row['goods_id']);
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count);
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 1);
$this->_rspOk();
} else {
error_log("buy normal 1, need_price = ". $need_price . " goods_num = " . $goods_num);
error_log("buy normal 1, need_price = " . $need_price . " goods_num = " . $goods_num);
$price = $this->normalizeWeb3Price($goods_num * $need_price);
error_log("buy normal 2, price = " . $price);
$item_id = $row['goods_id'];
$item_count = $goods_num;
$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,
@ -1018,11 +1020,11 @@ class ShopController extends BaseAuthedController
'id' => $id,
)),
));
$response['item_id'] = $item_id;
$response['item_num'] = $item_count;
$this->_rspData(
array(
array(
"block_chain" => $response
)
);
@ -1096,15 +1098,16 @@ class ShopController extends BaseAuthedController
$price = $this->normalizeWeb3Price($goods['price'] * $count);
$item_id = $goods['goods_id'];
$item_count = $goods['goods_num'] * $count;
error_log("buyGoodsDS start " . json_encode(array(
'idx' => $idx,
'grid' => $grid,
'count' => $count,
'goodPrice' => $goods['price'],
'price' => $price,
'item_id' => $item_id,
'item_count' => $item_count,
)
error_log("buyGoodsDS start " . json_encode(
array(
'idx' => $idx,
'grid' => $grid,
'count' => $count,
'goodPrice' => $goods['price'],
'price' => $price,
'item_id' => $item_id,
'item_count' => $item_count,
)
));
$response = services\BlockChainService::gameItemMallBuy(
Transaction::BUY_GOODS_ACTION_TYPE,
@ -1125,12 +1128,13 @@ class ShopController extends BaseAuthedController
)),
));
error_log("buyGoodsDS start " . json_encode(array(
'idx' => $idx,
'grid' => $grid,
'count' => $count,
'block_chain' => $response,
)
error_log("buyGoodsDS start " . json_encode(
array(
'idx' => $idx,
'grid' => $grid,
'count' => $count,
'block_chain' => $response,
)
));
$this->_rspData(
@ -1143,6 +1147,80 @@ class ShopController extends BaseAuthedController
);
}
public 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;
}
$isFreeBuy = false;
if (!empty($shop['free_type'])) {
$count = $this->countFreeBuyTimes($shop);
if ($count < $shop['free_num']) {
$isFreeBuy = true;
}
}
$cost = $shop['price'] * $num;
if ($isFreeBuy) {
$cost = 0;
}
$recommend = $shop['recommend'];
$itemStore = mt\ShopChest::getRandomItemListByChestType($recommend);
if ($isFreeBuy) {
if ($num != 1) {
$this->_rspErr(2, 'num is invalid');
return;
}
}
$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 && $num == 1) {
$this->addFreeBuyRecord($shop);
}
}
error_log("buyBlindBox start " . json_encode(
array(
'account' => $account,
'itemListStore' => $result,
)
));
$this->_rspData(
array(
'reuslt' => $result,
)
);
}
private function buyGoodsFree()
{
@ -1335,7 +1413,7 @@ class ShopController extends BaseAuthedController
return;
}
$this->_decItems($costItems);
$this->internalAddItem($propertyChgService, $itemMeta, 1);
$this->internalAddItem($propertyChgService, $itemMeta, 1, 0);
$awardService = new services\AwardService();
$awardService->addItem($itemId, $itemNum);
ShopBuyRecord::add($itemId, $itemNum);
@ -1454,7 +1532,7 @@ class ShopController extends BaseAuthedController
return $costItems;
}
private function internalAddItem($propertyChgService, $itemMeta, $count)
private function internalAddItem($propertyChgService, $itemMeta, $count, $sysAdd)
{
switch ($itemMeta['type']) {
case mt\Item::HERO_TYPE: {
@ -1488,8 +1566,10 @@ class ShopController extends BaseAuthedController
$this->_addVirtualItem($itemMeta['id'], $count, null, $propertyChgService);
$propertyChgService->addUserChg();
// 充值就尝试开启首充活动
if ($itemMeta['id'] == 10001) {
$this->beginFirstTupop();
if ($sysAdd != 1) {
if ($itemMeta['id'] == 10001) {
$this->beginFirstTupop();
}
}
} else {
Bag::addItem($itemMeta['id'], $count);
@ -1587,50 +1667,48 @@ class ShopController extends BaseAuthedController
return phpcommon\bnToStr($ret_price);
}
private function countFreeBuyTimes($goods) {
private function countFreeBuyTimes($goods)
{
$conn = myself()->_getMysql('');
$account = myself()->_getAccountId();
switch($goods['free_type'])
{
case 1:
{
$dayTime = myself()->_getNowDaySeconds();
$sql = 'SELECT COUNT(*) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?';
$row = $conn->execQueryOne($sql, array($account, $goods['id'], $goods['goods_id'], $dayTime));
return $row['cnt'];
}
break;
switch ($goods['free_type']) {
case 1: {
$dayTime = myself()->_getNowDaySeconds();
$sql = 'SELECT COUNT(*) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?';
$row = $conn->execQueryOne($sql, array($account, $goods['id'], $goods['goods_id'], $dayTime));
return $row['cnt'];
}
break;
}
return 0;
}
private function addFreeBuyRecord($goods) {
private function addFreeBuyRecord($goods)
{
$conn = myself()->_getMysql('');
$account = myself()->_getAccountId();
switch($goods['free_type'])
{
case 1:
{
$dayTime = myself()->_getNowTime();
SqlHelper::insert(
$conn,
't_shop_free_record',
array(
'account_id' => $account,
'shop_id' => $goods['shop_id'],
'id' => $goods['id'],
'goods_id' => $goods['goods_id'],
'goods_num' => $goods['goods_num'],
'free_type' => $goods['free_type'],
'free_num' => $goods['free_num'],
'createtime' => $dayTime,
)
);
}
break;
switch ($goods['free_type']) {
case 1: {
$dayTime = myself()->_getNowTime();
SqlHelper::insert(
$conn,
't_shop_free_record',
array(
'account_id' => $account,
'shop_id' => $goods['shop_id'],
'id' => $goods['id'],
'goods_id' => $goods['goods_id'],
'goods_num' => $goods['goods_num'],
'free_type' => $goods['free_type'],
'free_num' => $goods['free_num'],
'createtime' => $dayTime,
)
);
}
break;
}
}
}

42
webapp/mt/ShopChest.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace mt;
use phpcommon;
class ShopChest
{
public static function getRandomItemListByChestType($chestType) {
self::cacheRandomItemListByChestTypes();
return getXVal(self::$itemListWithChestType, $chestType, null);
}
protected static function cacheRandomItemListByChestTypes() {
if (!self::$itemListWithChestType) {
self::$itemListWithChestType = array();
foreach (self::getMetaList() as $meta) {
if (!getXVal(self::$itemListWithChestType, $meta['chest_type'], null)) {
self::$itemListWithChestType[$meta['chest_type']] = array();
}
if(!getXVal(self::$itemListWithChestType[$meta['chest_type']], $meta['item_type'], null)) {
self::$itemListWithChestType[$meta['chest_type']][$meta['item_type']] = array();
}
array_push(self::$itemListWithChestType[$meta['chest_type']][$meta['item_type']], $meta);
}
}
}
protected static function getMetaList()
{
if (!self::$metaList) {
self::$metaList = getMetaTable('Shopchest@Shopchest.php');
}
return self::$metaList;
}
protected static $itemListWithChestType;
protected static $metaList;
}