game2006api/webapp/services/ShopService.php
aozhiwei a431c927a1 1
2024-10-14 11:35:25 +08:00

209 lines
7.7 KiB
PHP

<?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 {
private static $dailHeroHash = array();
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;
}
if ($goodsMeta['shop_id'] == mt\Shop::DAILY_HERO_SHOP &&
!self::inDailyHeroList($goodsMeta)) {
continue;
}
$goodsDto = array(
'goods_id' => $goodsMeta['goods_id'],
'goods_meta' => self::goodsMetaToInfo($goodsMeta),
'bought_times' => 0,
'free_num' => 0,
);
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;
}
array_push($goodsList, $goodsDto);
$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'],
'function' => $goodsMeta['function'],
);
}
private static function getDailyHeroList()
{
if (isset(self::$dailyHeroHash[myself()->_getAccountId()])) {
return self::$dailyHeroHash[myself()->_getAccountId()];
}
$r = myself()->_getSelfRedis();
$key = 'shop.daily.shop:' . myself()->_getAccountId();
$data = $r->get($key);
if (empty($data)) {
$num = myself()->_callMtStatic('Parameter', 'getByName', 'SHOP_TYPE2_SLOT')['param_value'];
$data = mt\ShopGoods::getRandDailyHeroGoodsIds(mt\Shop::DAILY_HERO_SHOP, min(50, $num));
$r->setPx($key, json_encode($data), 1000 * 3600 * 24);
} else {
$data = json_decode($data, true);
}
return $data;
}
private static function inDailyHeroList($goodsMeta)
{
return in_array($goodsMeta['goods_id'], self::getDailyHeroList());
}
// 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;
// }
}