game2006api/webapp/services/ShopService.php
aozhiwei 24d154faff 1
2023-07-31 11:22:43 +08:00

211 lines
7.8 KiB
PHP

<?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 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) {
$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['goods_id']);
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0;
}
break;
case mt\Item::WEEKLY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $goodsMeta['goods_id']);
$goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0;
}
break;
case mt\Item::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));
}
if (!empty($goods['free_type'])) {
$count = self::getFreeBuyTimes($goodsMeta['free_type'],
$goodsMeta['goods_id'],
$goodsMeta['goods_id']);
$goodsDto['free_num'] = $goodsDto['free_num'] - $count;
}
}
return $goodsList;
}
public static function getFreeBuyTimes()
{
return 0;
switch ($free_type) {
case 1: {
$dayTime = myself()->_getNowDaySeconds();
$sql = 'SELECT COUNT(idx) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?';
$row = $conn->execQueryOne($sql, array($account, $id, $goodsId, $dayTime));
return $row['cnt'];
}
break;
}
return 0;
}
public static function buyLimitCheck($goodsMeta, &$errCode, &$errMsg)
{
$errCode = 0;
$errMsg = '';
$buyRecordHash = ShopBuyRecord::allToHash();
$boughtTimes = 1;
{
switch ($goodsMeta['limit_type']) {
case ShopController::DAILY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $goodsMeta['limit_num']) {
$this->_rspErr(2, 'Daily purchase limit');
return;
}
}
break;
case ShopController::WEEKLY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $goodsMeta['limit_num']) {
$this->_rspErr(2, 'Weekly purchase limit reached');
return;
}
}
break;
case ShopController::TOTAL_BUY_LIMIT: {
// error_log("total buy limit " . $address . " " . $id . " " . $goodsMeta['limit_num']);
$buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $goodsMeta['limit_num']) {
$this->_rspErr(2, 'Purchase limit reached');
return;
}
}
break;
default: {
}
break;
}
}
}
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'],
'discount' => $goodsMeta['discount'],
'limit_type' => $goodsMeta['limit_type'],
'limit_num' => $goodsMeta['limit_num'],
'buy_cond' => $goodsMeta['buy_cond'],
'buy_gift' => $goodsMeta['buy_gift'],
'normal_gift' => $goodsMeta['normal_gift'],
'free_type' => $goodsMeta['free_type'],
'shop_icon' => $goodsMeta['shop_icon'],
);
}
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;
}
}