515 lines
17 KiB
PHP
515 lines
17 KiB
PHP
<?php
|
|
|
|
require_once('mt/Shop.php');
|
|
require_once('mt/ShopGoods.php');
|
|
require_once('mt/Hero.php');
|
|
require_once('mt/Item.php');
|
|
require_once('mt/Parameter.php');
|
|
require_once('mt/Drop.php');
|
|
|
|
require_once('models/User.php');
|
|
require_once('models/Hero.php');
|
|
require_once('models/Bag.php');
|
|
require_once('models/HeroSkin.php');
|
|
require_once('models/GunSkin.php');
|
|
require_once('models/ShopBuyRecord.php');
|
|
|
|
require_once('services/AwardService.php');
|
|
require_once('services/PropertyChgService.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\User;
|
|
use models\Bag;
|
|
use models\Hero;
|
|
use models\HeroSkin;
|
|
use models\GunSkin;
|
|
use models\ShopBuyRecord;
|
|
|
|
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(),
|
|
)
|
|
));
|
|
return;
|
|
}
|
|
$buyRecordHash = ShopBuyRecord::allToHash();
|
|
$goodsDtoList1 = array();
|
|
$goodsDtoList2 = array();
|
|
foreach ($goodsList as $goods) {
|
|
$itemMeta = mt\Item::get($goods['goods_id']);
|
|
if ($itemMeta) {
|
|
$goodsDto = array(
|
|
'goods_id' => $goods['goods_id'],
|
|
'item_id' => $itemMeta['id'],
|
|
'price_info' => null,
|
|
'flag_icon' => $goods['tag'],
|
|
'limit_type' => $itemMeta['limit_type'],
|
|
'bought_times' => 0,
|
|
'total_buy_times' => $itemMeta['limit_num'],
|
|
);
|
|
switch ($itemMeta['limit_type']) {
|
|
case mt\Item::DAILY_BUY_LIMIT:
|
|
{
|
|
$buyRecord = getXVal($buyRecordHash, $itemMeta['id']);
|
|
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0;
|
|
}
|
|
break;
|
|
case mt\Item::WEEKLY_BUY_LIMIT:
|
|
{
|
|
$buyRecord = getXVal($buyRecordHash, $itemMeta['id']);
|
|
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0;
|
|
}
|
|
break;
|
|
case mt\Item::TOTAL_BUY_LIMIT:
|
|
{
|
|
$buyRecord = getXVal($buyRecordHash, $itemMeta['id']);
|
|
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
}
|
|
break;
|
|
}
|
|
$priceInfo = mt\Item::getPriceInfo($itemMeta);
|
|
if (!empty($priceInfo)) {
|
|
$goodsDto['price_info'] = $priceInfo['price_info'];
|
|
array_push($goodsDtoList1, $goodsDto);
|
|
}
|
|
}
|
|
}
|
|
$this->_rspData(array(
|
|
'info' => array(
|
|
'shop_id' => $shopId,
|
|
'goods_list1' => $goodsDtoList1,
|
|
'goods_list2' => $goodsDtoList2,
|
|
)
|
|
));
|
|
}
|
|
|
|
public function buyGoods()
|
|
{
|
|
$shopId = getReqVal('shop_id', 0);
|
|
$goodsId = getReqVal('goods_id', 0);
|
|
$itemNum = getReqVal('goods_num', 0);
|
|
$costItemId = getReqVal('cost_item_id', 0);
|
|
$itemId = $goodsId;
|
|
|
|
if ($shopId == mt\Shop::OUTSIDE_SHOP) {
|
|
$this->outsideBuy($shopId, $itemId, $itemNum, $costItemId);
|
|
return;
|
|
}
|
|
|
|
$propertyChgService = new services\PropertyChgService();
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if (!$itemMeta) {
|
|
$this->_rspErr(1, 'goods_id参数错误');
|
|
return;
|
|
}
|
|
$goodsMeta = mt\ShopGoods::getGoodsInfo($shopId, $goodsId);
|
|
if (!$goodsMeta) {
|
|
$this->_rspErr(1, 'goods_id参数错误');
|
|
return;
|
|
}
|
|
$buyRecordHash = ShopBuyRecord::allToHash();
|
|
$boughtTimes = 1;
|
|
switch ($itemMeta['limit_type']) {
|
|
case mt\Item::DAILY_BUY_LIMIT:
|
|
{
|
|
$buyRecord = getXVal($buyRecordHash, $itemMeta['id']);
|
|
$boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1: 1;
|
|
if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $itemMeta['limit_num']) {
|
|
$this->_rspErr(2, '已达今日限购上限次数');
|
|
return;
|
|
}
|
|
if ($itemMeta['limit_num'] <= 0) {
|
|
$this->_rspErr(2, '已达限购上限次数');
|
|
return;
|
|
}
|
|
}
|
|
break;
|
|
case mt\Item::WEEKLY_BUY_LIMIT:
|
|
{
|
|
$buyRecord = getXVal($buyRecordHash, $itemMeta['id']);
|
|
$boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1: 1;
|
|
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']);
|
|
$boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1: 1;
|
|
if ($buyRecord && getXVal($buyRecord, 'total_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);
|
|
if (empty($priceInfo)) {
|
|
$this->_rspErr(2, '配置表错误');
|
|
return;
|
|
}
|
|
$costItems = $this->getCostItems($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($propertyChgService, $itemMeta);
|
|
$awardService = new services\AwardService();
|
|
$awardService->addItem($itemId, $itemNum);
|
|
ShopBuyRecord::add($itemId, $itemNum);
|
|
$goodsDto = array(
|
|
'goods_id' => $itemMeta['id'],
|
|
'item_id' => $itemMeta['id'],
|
|
'price_info' => array(
|
|
'item_id' => $itemMeta['id'],
|
|
'cost_list' => array(),
|
|
'discount_begin_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_begin']),
|
|
'discount_end_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_end'])
|
|
),
|
|
'flag_icon' => $goodsMeta['tag'],
|
|
'limit_type' => $itemMeta['limit_type'],
|
|
'bought_times' => $boughtTimes,
|
|
'total_buy_times' => $itemMeta['limit_num'],
|
|
);
|
|
{
|
|
$priceInfo = mt\Item::getPriceInfo($itemMeta);
|
|
if (!empty($priceInfo)) {
|
|
$goodsDto['price_info'] = $priceInfo['price_info'];
|
|
}
|
|
}
|
|
$propertyChgService->addUserChg();
|
|
$this->_rspData(array(
|
|
'award' => $awardService->toDto(),
|
|
'property_chg' => $propertyChgService->toDto(),
|
|
'goods_chg' => $goodsDto
|
|
));
|
|
}
|
|
|
|
public function getDiscountList()
|
|
{
|
|
$items = array();
|
|
{
|
|
$types = array(mt\Item::HERO_TYPE,
|
|
mt\Item::HERO_SKIN_TYPE,
|
|
mt\Item::GUN_SKIN_TYPE);
|
|
mt\Item::filter(function ($meta) use(&$items, &$types) {
|
|
if (mt\Item::inTypes($meta, $types)) {
|
|
array_push($items, $meta);
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
$goodsDtoList = array();
|
|
array_walk($items, function ($val) use(&$priceList, &$goodsDtoList) {
|
|
$goodsDto = array(
|
|
'item_id' => $val['id'],
|
|
'gold_discount' => 0,
|
|
'diamond_discount' => 0,
|
|
);
|
|
$priceInfo = mt\Item::getPriceInfo($val);
|
|
if (!empty($priceInfo)) {
|
|
foreach ($priceInfo['price_info']['cost_list'] as $costGroup) {
|
|
foreach ($costGroup as $cost) {
|
|
if ($cost['discount'] > 0) {
|
|
switch ($cost['item_id']) {
|
|
case V_ITEM_GOLD:
|
|
{
|
|
$goodsDto['gold_discount'] = $cost['discount'];
|
|
}
|
|
break;
|
|
case V_ITEM_DIAMOND:
|
|
{
|
|
$goodsDto['diamond_discount'] = $cost['discount'];
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if ($goodsDto['gold_discount'] > 0 || $goodsDto['diamond_discount'] > 0) {
|
|
array_push($goodsDtoList, $goodsDto);
|
|
}
|
|
}
|
|
});
|
|
$this->_rspData(array(
|
|
'goods_list' => $goodsDtoList,
|
|
));
|
|
}
|
|
|
|
private function outsideBuy($shopId, $itemId, $itemNum, $costItemId)
|
|
{
|
|
$propertyChgService = new services\PropertyChgService();
|
|
$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, 'cost_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->getCostItems($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($propertyChgService, $itemMeta);
|
|
$awardService = new services\AwardService();
|
|
$awardService->addItem($itemId, $itemNum);
|
|
ShopBuyRecord::add($itemId, $itemNum);
|
|
$goodsDto = array(
|
|
'goods_id' => $itemMeta['id'],
|
|
'item_id' => $itemMeta['id'],
|
|
'price_info' => array(
|
|
'item_id' => $itemMeta['id'],
|
|
'cost_list' => array(),
|
|
'discount_begin_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_begin']),
|
|
'discount_end_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_end'])
|
|
),
|
|
'flag_icon' => '',
|
|
'limit_type' => $itemMeta['limit_type'],
|
|
'bought_times' => 0,
|
|
'total_buy_times' => $itemMeta['limit_num'],
|
|
);
|
|
{
|
|
$priceInfo = mt\Item::getPriceInfo($itemMeta);
|
|
if (!empty($priceInfo)) {
|
|
$goodsDto['price_info'] = $priceInfo;
|
|
}
|
|
}
|
|
$propertyChgService->addUserChg();
|
|
$this->_rspData(array(
|
|
'award' => $awardService->toDto(),
|
|
'property_chg' => $propertyChgService->toDto(),
|
|
'goods_chg' => $goodsDto
|
|
));
|
|
}
|
|
|
|
private function getOutsideShopInfo()
|
|
{
|
|
$items = array();
|
|
{
|
|
$types = array(mt\Item::HERO_TYPE,
|
|
mt\Item::HERO_SKIN_TYPE,
|
|
mt\Item::GUN_SKIN_TYPE);
|
|
mt\Item::filter(function ($meta) use(&$items, &$types) {
|
|
if (mt\Item::inTypes($meta, $types)) {
|
|
array_push($items, $meta);
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
$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['price_info'];
|
|
array_push($goodsDtoList1, $goodsDto);
|
|
}
|
|
});
|
|
$this->_rspData(array(
|
|
'info' => array(
|
|
'shop_id' => mt\Shop::OUTSIDE_SHOP,
|
|
'goods_list1' => $goodsDtoList1,
|
|
'goods_list2' => $goodsDtoList2,
|
|
)
|
|
));
|
|
}
|
|
|
|
private function getCostItems($priceInfo, $costItemId)
|
|
{
|
|
$costGroup = null;
|
|
array_walk($priceInfo['price_info']['cost_list'], function ($val) use(&$costGroup, $costItemId) {
|
|
if ($costGroup) {
|
|
return;
|
|
}
|
|
if (count($val) > 0 && $val[0]['item_id'] == $costItemId) {
|
|
$costGroup = $val;
|
|
return;
|
|
}
|
|
});
|
|
if (!$costGroup) {
|
|
return null;
|
|
}
|
|
$costItems = array();
|
|
array_walk($costGroup, function ($val) use (&$costItems, $priceInfo) {
|
|
if ($val['discount'] > 0 &&
|
|
$this->_getNowTime() >= $priceInfo['discount_begin_time'] &&
|
|
$this->_getNowTime() <= $priceInfo['discount_end_time']
|
|
) {
|
|
array_push($costItems, array(
|
|
'item_id' => $val['item_id'],
|
|
'item_num' => (int)($val['item_num'] * ($priceInfo['discount'] / 100)),
|
|
));
|
|
} else {
|
|
array_push($costItems, array(
|
|
'item_id' => $val['item_id'],
|
|
'item_num' => $val['item_num'],
|
|
));
|
|
}
|
|
});
|
|
return $costItems;
|
|
}
|
|
|
|
private function internalAddItem($propertyChgService, $itemMeta)
|
|
{
|
|
switch ($itemMeta['type']) {
|
|
case mt\Item::HERO_TYPE:
|
|
{
|
|
Hero::addHero($itemMeta);
|
|
$propertyChgService->addHeroChg();
|
|
}
|
|
break;
|
|
case mt\Item::HERO_SKIN_TYPE:
|
|
{
|
|
HeroSkin::addSkin($itemMeta);
|
|
$propertyChgService->addHeroSkinChg();
|
|
}
|
|
break;
|
|
case mt\Item::GUN_SKIN_TYPE:
|
|
{
|
|
GunSkin::addSkin($itemMeta);
|
|
$propertyChgService->addGunSkinChg();
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
Bag::addItem($itemMeta['id'], 1);
|
|
$propertyChgService->addBagChg();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|