This commit is contained in:
hujiabin 2024-08-14 11:13:43 +08:00
parent 04a32ba926
commit 425eacf9db
2 changed files with 394 additions and 0 deletions

View File

@ -0,0 +1,215 @@
<?php
require_once('mt/Shop.php');
require_once('mt/ShopGoods.php');
require_once('mt/Item.php');
require_once('mt/Hero.php');
require_once('mt/Parameter.php');
require_once('mt/ShopChest.php');
require_once('models/Hero.php');
require_once('models/ShopBuyRecord.php');
require_once('models/InAppOrder.php');
require_once('models/OutAppOrder.php');
require_once('models/InAppRecord.php');
require_once('models/OrderId.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
require_once('services/ShopService.php');
require_once('services/LogService.php');
require_once('services/CallBack.php');
use mt\Shop;
use mt\ShopChest;
use mt\ShopGoods;
use models\Hero;
use models\ShopBuyRecord;
use models\OrderId;
use services\LogService;
use services\ShopService;
class ShopController extends BaseAuthedController {
public function getGoodsList()
{
$shopId = getReqVal('shop_id', 0);
$goodsList = ShopService::getGoodsList($shopId);
myself()->_rspData(
array(
'goods_list' => $goodsList,
)
);
}
public function buyGoods()
{
$goodsId = getReqVal('goods_id', 0);
$goodsNum = getReqVal('goods_num', 0);
if ($goodsNum < 1) {
myself()->_rspErr(1, "goods_num parameter error, goods_num: {$goodsNum}");
return;
}
$goodsMeta = mt\ShopGoods::getByGoodsUuid($goodsId);
if (!$goodsMeta) {
myself()->_rspErr(1, 'goods not found');
return;
}
if ($goodsMeta['is_close']) {
myself()->_rspErr(1, 'no right to purchase');
return;
}
if ($goodsNum < 1) {
myself()->_rspErr(1, 'goods_num paramater error');
return;
}
if ($goodsNum > $goodsMeta['max_amount']) {
myself()->_rspErr(1, "goods_num parameter error, max_amount: {$goodsMeta['max_amount']}");
return;
}
$itemMeta = mt\Item::get($goodsMeta['item_id']);
if (!$itemMeta) {
myself()->_rspErr(1, 'goods not found, goods_id: ' . $goodsMeta['goods_id']);
return;
}
$errCode = 0;
$errMsg = '';
// if ($itemMeta['type'] == mt\Item::HERO_SKIN_TYPE) {
// if (!ShopService::canBuy($itemMeta, $errCode, $errMsg)) {
// myself()->_rspErr($errCode, $errMsg);
// return;
// }
// }
if (!ShopService::buyLimitCheck($goodsMeta, $errCode, $errMsg)) {
myself()->_rspErr($errCode, $errMsg);
return;
}
$propertyChgService = new services\PropertyChgService();
$awardService = new services\AwardService();
$tokenType = $goodsMeta['token_type'];
$price = $goodsMeta['price'];
if (!in_array(
$tokenType,
array(
mt\Shop::TOKEN_TYPE_GOLD,
mt\Shop::TOKEN_TYPE_DIAMOND
)
)) {
myself()->_rspErr(1, "token_type is unsupport, {$tokenType}");
return;
}
if ($price < 1) {
myself()->_rspErr(1, "config error");
return;
}
$costItemId = myself()->getCostItemIdByTokenType($tokenType);
$costItems = array(
array(
'item_id' => $costItemId,
'item_num' => $goodsNum * $price
)
);
$lackItem = null;
if (!myself()->_hasEnoughItems($costItems, $lackItem)) {
myself()->_rspErr(2, myself()->_getLackItemErrMsg($lackItem));
return;
}
ShopBuyRecord::add($goodsMeta['goods_id'], $goodsNum);
myself()->_decItems($costItems);
for ($i = 0; $i < $goodsNum; $i++) {
$this->internalAddItem($awardService,
$propertyChgService,
$itemMeta,
$goodsMeta['item_num']
);
}
$propertyChgService->addUserChg();
$this->_rspData(
array(
'award' => $awardService->toDto(),
'property_chg' => $propertyChgService->toDto(),
)
);
}
private function getCostItemIdByTokenType($tokenType)
{
switch ($tokenType) {
case mt\Shop::TOKEN_TYPE_GOLD:
{
return V_ITEM_GOLD;
}
break;
case mt\Shop::TOKEN_TYPE_DIAMOND:
{
return V_ITEM_DIAMOND;
}
break;
default:
return -1;
}
return -1;
}
public function boxPreview()
{
$goodsId = getReqVal('goods_id', '');
$goodsMeta = mt\ShopGoods::get($goodsId);
if (!$goodsMeta) {
myself()->_rspErr(1, 'goods not found');
return;
}
$itemMeta = mt\Item::get($goodsMeta['item_id']);
if ($itemMeta['type'] != mt\Item::CHEST_BOX_TYPE) {
$this->_rspErr(2, 'goods_id is invalid');
return;
}
$chestType = $itemMeta['sub_type'];
$itemStore = mt\ShopChest::getRandomItemListByChestType($chestType);
if (!$itemStore) {
$this->_rspErr(2, 'goods_id is invalid');
return;
}
$record = array();
foreach ($itemStore as $key => $value) {
foreach ($value as $k => $v) {
if (empty($record[$v['item_id']])) {
$record[$v['item_id']] = 0;
}
$record[$v['item_id']] += 1;
}
}
$this->_rspData(
array(
'items' => array_keys($record),
'free_num' => 0,
)
);
}
private function internalAddItem($awardService,
$propertyChgService,
$itemMeta,
$count
)
{
myself()->_addItems(
array(
array(
'item_id' => $itemMeta['id'],
'item_num' => $count
)
),
$awardService,
$propertyChgService);
}
}

View File

@ -0,0 +1,179 @@
<?php
namespace services;
require_once('mt/ShopGoods.php');
require_once('mt/Item.php');
require_once('mt/Shop.php');
require_once('models/ShopBuyRecord.php');
require_once('models/Hero.php');
require_once('models/HeroSkin.php');
require_once('models/GunSkin.php');
use mt;
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) {
if ($goodsMeta['is_close']) {
continue;
}
$goodsDto = array(
'goods_id' => $goodsMeta['goods_id'],
'goods_meta' => self::goodsMetaToInfo($goodsMeta),
'bought_times' => 0,
'free_num' => 0,
);
array_push($goodsList, $goodsDto);
switch ($goodsMeta['limit_type']) {
case mt\Shop::DAILY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $goodsMeta['goods_id']);
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0;
}
break;
case mt\Shop::WEEKLY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $goodsMeta['goods_id']);
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0;
}
break;
case mt\Shop::TOTAL_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $goodsMeta['goods_id']);
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0;
}
break;
default: {
}
break;
}
$itemMeta = mt\Item::get($goodsMeta['item_id']);
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 if ($goodsMeta['goods_id'] != 9999){
error_log('item not found:' . json_encode($goodsMeta));
}
}
return $goodsList;
}
public static function buyLimitCheck($goodsMeta, &$errCode, &$errMsg)
{
$errCode = 0;
$errMsg = '';
$buyRecordHash = ShopBuyRecord::allToHash();
$boughtTimes = 1;
$goodsId = $goodsMeta['goods_id'];
{
switch ($goodsMeta['limit_type']) {
case mt\Shop::DAILY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $goodsId);
$boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $goodsMeta['limit_num']) {
$errCode = 2;
$errMsg = 'Daily purchase limit';
return false;
}
}
break;
case mt\Shop::WEEKLY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $goodsId);
$boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $goodsMeta['limit_num']) {
$errCode = 2;
$errMsg = 'Weekly purchase limit reached';
return false;
}
}
break;
case mt\Shop::TOTAL_BUY_LIMIT: {
// error_log("total buy limit " . $address . " " . $goodsId . " " . $goodsMeta['limit_num']);
$buyRecord = getXVal($buyRecordHash, $goodsId);
$boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $goodsMeta['limit_num']) {
$errCode = 2;
$errMsg = 'Purchase limit reached';
return false;
}
}
break;
default: {
}
break;
}
}
return true;
}
public static function goodsMetaToInfo($goodsMeta)
{
return array(
'item_id' => $goodsMeta['item_id'],
'item_num' => $goodsMeta['item_num'],
'max_amount' => $goodsMeta['max_amount'],
'shop_id' => $goodsMeta['shop_id'],
'shopstage' => $goodsMeta['shopstage'],
'tag' => $goodsMeta['tag'],
'recommend' => $goodsMeta['recommend'],
'token_type' => $goodsMeta['token_type'],
'price' => $goodsMeta['price'],
'free_type' => $goodsMeta['free_type'],
'shop_icon' => $goodsMeta['shop_icon'],
'gg_product_id' => $goodsMeta['gg_product_id'],
'ios_product_id' => $goodsMeta['ios_product_id'],
'bonus' => $goodsMeta['bonus'],
'bonus_num' => $goodsMeta['bonus_num'],
);
}
// public static function canBuy($itemMeta, &$errCode, &$errMsg)
// {
// $errCode = 0;
// $errMsg = '';
// switch ($itemMeta['type']) {
// /*
// case mt\Item::HERO_SKIN_TYPE: {
// $heroSkinDb = HeroSkin::find($itemMeta['id']);
// if ($heroSkinDb) {
// $errCode = 10;
// $errMsg = 'You already have the skin';
// return false;
// }
// }
// break;*/
// default: {
// return true;
// }
// break;
// }
// return true;
// }
}