This commit is contained in:
azw 2023-07-29 08:36:56 +08:00
parent d65f1f29ad
commit 258053c297
2 changed files with 186 additions and 166 deletions

View File

@ -6,7 +6,6 @@ require_once('mt/Hero.php');
require_once('mt/Item.php'); require_once('mt/Item.php');
require_once('mt/Parameter.php'); require_once('mt/Parameter.php');
require_once('mt/Drop.php'); require_once('mt/Drop.php');
require_once('mt/Dailyselection.php');
require_once('mt/ShopChest.php'); require_once('mt/ShopChest.php');
require_once('models/User.php'); require_once('models/User.php');
@ -23,6 +22,7 @@ require_once('models/Transaction.php');
require_once('services/AwardService.php'); require_once('services/AwardService.php');
require_once('services/PropertyChgService.php'); require_once('services/PropertyChgService.php');
require_once('services/BlockChainService.php'); require_once('services/BlockChainService.php');
require_once('services/ShopService.php');
require_once('phpcommon/bignumber.php'); require_once('phpcommon/bignumber.php');
@ -38,85 +38,20 @@ use models\GunSkin;
use models\ShopBuyRecord; use models\ShopBuyRecord;
use models\Chip; use models\Chip;
use mt\Shop; use mt\Shop;
use mt\Dailyselection;
use mt\ShopChest; use mt\ShopChest;
use mt\ShopGoods; use mt\ShopGoods;
use models\Transaction; use models\Transaction;
use models\BcOrder; use models\BcOrder;
use services\LogService; use services\LogService;
use services\ShopAddItemService; use services\ShopAddItemService;
use services\ShopService;
class ShopController extends BaseAuthedController class ShopController extends BaseAuthedController {
{
public function getGoodsList() public function getGoodsList()
{ {
$shopId = getReqVal('shop_id', 0); $shopId = getReqVal('shop_id', 0);
if ($shopId == 0) { $goodsList = ShopService::getGoodsList($shopId);
$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']);
}
}
$this->_rspData( $this->_rspData(
array( array(
'goods_list' => $goodsList, 'goods_list' => $goodsList,
@ -135,29 +70,22 @@ class ShopController extends BaseAuthedController
return; return;
} }
$row = mt\ShopGoods::get($id); $goodsMeta = mt\ShopGoods::get($id);
if (!$row) { if (!$goodsMeta) {
$this->_rspErr(1, 'goods not found'); $this->_rspErr(1, 'goods not found');
return; return;
} }
$goodsId = $row['goods_id']; $goodsId = $goodsMeta['goods_id'];
if (!empty($address)) {
// $pending = $this->checkPendingBuyGoodsNormal($address, $goodsId, $row['shop_id'], $id);
// if ($pending) {
// $this->_rspErr(1, 'pending');
// return;
// }
}
$desired_token_type = $row['token_type']; $desired_token_type = $goodsMeta['token_type'];
$check_token_type = splitStr1($desired_token_type); $check_token_type = splitStr1($desired_token_type);
$token_pos = array_search($tokenType, $check_token_type, true); $token_pos = array_search($tokenType, $check_token_type, true);
$isFreeBuy = false; $isFreeBuy = false;
if (!empty($row['free_type'])) { if (!empty($goodsMeta['free_type'])) {
$count = $this->countFreeBuyTimes($row['free_type'], $row['id'], $row['goods_id']); $count = $this->countFreeBuyTimes($goodsMeta['free_type'], $goodsMeta['id'], $goodsMeta['goods_id']);
if ($count < $row['free_num']) { if ($count < $goodsMeta['free_num']) {
$isFreeBuy = true; $isFreeBuy = true;
} }
} }
@ -168,20 +96,20 @@ class ShopController extends BaseAuthedController
} }
} }
if ($goodsNum > $row['max_amount']) { if ($goodsNum > $goodsMeta['max_amount']) {
$this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); $this->_rspErr(1, "goods_num parameter error, max_amount: {$goodsMeta['max_amount']}");
return; return;
} }
// 这里命名混乱了, 购买个数,一捆个数命名冲突 // 这里命名混乱了, 购买个数,一捆个数命名冲突
$goods_count = $row['goods_num']; $goods_count = $goodsMeta['goods_num'];
$buyRecordHash = ShopBuyRecord::allToHash(); $buyRecordHash = ShopBuyRecord::allToHash();
$boughtTimes = 1; $boughtTimes = 1;
$itemMeta = mt\Item::get($row['goods_id']); $itemMeta = mt\Item::get($goodsMeta['goods_id']);
if (!$itemMeta) { 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; return;
} }
@ -194,11 +122,11 @@ class ShopController extends BaseAuthedController
} }
} else { } else {
switch ($row['limit_type']) { switch ($goodsMeta['limit_type']) {
case ShopController::DAILY_BUY_LIMIT: { case ShopController::DAILY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id); $buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; $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'); $this->_rspErr(2, 'Daily purchase limit');
return; return;
} }
@ -207,17 +135,17 @@ class ShopController extends BaseAuthedController
case ShopController::WEEKLY_BUY_LIMIT: { case ShopController::WEEKLY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id); $buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; $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'); $this->_rspErr(2, 'Weekly purchase limit reached');
return; return;
} }
} }
break; break;
case ShopController::TOTAL_BUY_LIMIT: { 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); $buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; $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'); $this->_rspErr(2, 'Purchase limit reached');
return; return;
} }
@ -229,14 +157,14 @@ class ShopController extends BaseAuthedController
} }
} }
$price_array = splitStr1($row['price']); $price_array = splitStr1($goodsMeta['price']);
$discount_array = splitStr1($row['discount']); $discount_array = splitStr1($goodsMeta['discount']);
$need_price = $price_array[$token_pos]; $need_price = $price_array[$token_pos];
$discount = $discount_array[$token_pos]; $discount = $discount_array[$token_pos];
$discount_begin = strtotime($row['discount_begin']); $discount_begin = strtotime($goodsMeta['discount_begin']);
$discount_end = strtotime($row['discount_end']); $discount_end = strtotime($goodsMeta['discount_end']);
$nowTime = $this->_getNowTime(); $nowTime = $this->_getNowTime();
if ($nowTime >= $discount_begin && $nowTime < $discount_end) { if ($nowTime >= $discount_begin && $nowTime < $discount_end) {
@ -255,28 +183,28 @@ class ShopController extends BaseAuthedController
return; return;
} }
$itemMeta = mt\Item::get($row['goods_id']); $itemMeta = mt\Item::get($goodsMeta['goods_id']);
$propertyChgService = new services\PropertyChgService(); $propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goodsNum; $i++) { for ($i = 0; $i < $goodsNum; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0); $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0);
} }
$awardService = new services\AwardService(); $awardService = new services\AwardService();
$awardService->addItem($row['goods_id'], $goodsNum); $awardService->addItem($goodsMeta['goods_id'], $goodsNum);
ShopBuyRecord::add($id, $goodsNum); ShopBuyRecord::add($id, $goodsNum);
$this->_decItems($costItems); $this->_decItems($costItems);
$goodsDto = array( $goodsDto = array(
'goods_id' => $id, 'goods_id' => $id,
'item_id' => $row['goods_id'], 'item_id' => $goodsMeta['goods_id'],
'price_info' => array( 'price_info' => array(
'item_id' => $row['goods_id'], 'item_id' => $goodsMeta['goods_id'],
'cost_list' => array(), 'cost_list' => array(),
'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), 'discount_begin_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_begin']),
'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) 'discount_end_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_end'])
), ),
'flag_icon' => $row['tag'], 'flag_icon' => $goodsMeta['tag'],
'limit_type' => $row['limit_type'], 'limit_type' => $goodsMeta['limit_type'],
'bought_times' => $boughtTimes, 'bought_times' => $boughtTimes,
'total_buy_times' => $row['limit_num'], 'total_buy_times' => $goodsMeta['limit_num'],
); { ); {
$priceInfo = mt\Item::getPriceInfo($itemMeta); $priceInfo = mt\Item::getPriceInfo($itemMeta);
if (!empty($priceInfo)) { if (!empty($priceInfo)) {
@ -303,16 +231,16 @@ class ShopController extends BaseAuthedController
return; return;
} }
$itemMeta = mt\Item::get($row['goods_id']); $itemMeta = mt\Item::get($goodsMeta['goods_id']);
$propertyChgService = new services\PropertyChgService(); $propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goodsNum; $i++) { for ($i = 0; $i < $goodsNum; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0); $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 0);
} }
$awardService = new services\AwardService(); $awardService = new services\AwardService();
$awardService->addItem($row['goods_id'], $goodsNum); $awardService->addItem($goodsMeta['goods_id'], $goodsNum);
ShopBuyRecord::add($id, $goodsNum); ShopBuyRecord::add($id, $goodsNum);
if ($isFreeBuy) { if ($isFreeBuy) {
$this->addFreeBuyRecord($row); $this->addFreeBuyRecord($goodsMeta);
} }
$this->_decItems($costItems); $this->_decItems($costItems);
$event = [ $event = [
@ -323,17 +251,17 @@ class ShopController extends BaseAuthedController
$goodsDto = array( $goodsDto = array(
'goods_id' => $id, 'goods_id' => $id,
'item_id' => $row['goods_id'], 'item_id' => $goodsMeta['goods_id'],
'price_info' => array( 'price_info' => array(
'item_id' => $row['goods_id'], 'item_id' => $goodsMeta['goods_id'],
'cost_list' => array(), 'cost_list' => array(),
'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), 'discount_begin_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_begin']),
'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) 'discount_end_time' => phpcommon\datetimeToTimestamp($goodsMeta['discount_end'])
), ),
'flag_icon' => $row['tag'], 'flag_icon' => $goodsMeta['tag'],
'limit_type' => $row['limit_type'], 'limit_type' => $goodsMeta['limit_type'],
'bought_times' => $boughtTimes, 'bought_times' => $boughtTimes,
'total_buy_times' => $row['limit_num'], 'total_buy_times' => $goodsMeta['limit_num'],
); { ); {
$priceInfo = mt\Item::getPriceInfo($itemMeta); $priceInfo = mt\Item::getPriceInfo($itemMeta);
if (!empty($priceInfo)) { if (!empty($priceInfo)) {
@ -353,13 +281,13 @@ class ShopController extends BaseAuthedController
case ShopController::TOKEN_TYPE_CEC: case ShopController::TOKEN_TYPE_CEC:
if ($isFreeBuy) { if ($isFreeBuy) {
$propertyChgService = new services\PropertyChgService(); $propertyChgService = new services\PropertyChgService();
$this->addFreeBuyRecord($row); $this->addFreeBuyRecord($goodsMeta);
$itemMeta = mt\Item::get($row['goods_id']); $itemMeta = mt\Item::get($goodsMeta['goods_id']);
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 1); $this->internalAddItem($propertyChgService, $itemMeta, $goods_count, 1);
$this->_rspOk(); $this->_rspOk();
} else { } else {
$price = $this->normalizeWeb3Price($goodsNum * $need_price); $price = $this->normalizeWeb3Price($goodsNum * $need_price);
$item_id = $row['goods_id']; $item_id = $goodsMeta['goods_id'];
$item_count = $goodsNum; $item_count = $goodsNum;
$response = services\BlockChainService::gameItemMallBuy( $response = services\BlockChainService::gameItemMallBuy(
@ -376,7 +304,7 @@ class ShopController extends BaseAuthedController
'price' => $goodsNum * $need_price, 'price' => $goodsNum * $need_price,
'ext_data' => json_encode(array( 'ext_data' => json_encode(array(
'mode' => SHOP_BUY_MODE_NORMAL, 'mode' => SHOP_BUY_MODE_NORMAL,
'shop_id' => $row['shop_id'], 'shop_id' => $goodsMeta['shop_id'],
'id' => $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) private function beginFirstTupop($address = null)
{ {
if (!$address) { if (!$address) {
@ -829,10 +717,4 @@ class ShopController extends BaseAuthedController
} }
} }
private function goodsMetaToInfo($goodsMeta) {
return array(
);
}
} }

View File

@ -0,0 +1,138 @@
<?php
namespace services;
require_once('mt/ShopGoods.php');
require_once('mt/Item.php');
require_once('models/ShopBuyRecord.php');
require_once('models/Hero.php');
require_once('models/HeroSkin.php');
require_once('models/GunSkin.php');
use phpcommon;
use models\ShopBuyRecord;
use models\Hero;
use models\HeroSkin;
use models\GunSkin;
class ShopService {
public static function getGoodsList($shopId) {
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' => 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;
}
}