hy-farm/common/modules/treasurebox/TreasureBoxUtil.php
2026-05-07 20:10:54 +08:00

167 lines
6.1 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/8/9
* Time: 11:51
*/
namespace common\modules\treasurebox;
use common\modules\achievement\AchievementUtil;
use common\modules\dailytask\DailyTaskUtil;
use common\modules\safe\SafeUtil;
use common\modules\systemlog\SystemLog;
use common\modules\systemlog\SystemLogUtil;
use common\modules\treasurebox\vo\TreasureBox;
use common\services\GameConfigServices;
use common\services\ItemServices;
use common\services\ResultStatusServices;
use common\services\StatusException;
use common\utils\MoneyUtil;
use common\utils\RedisKeyUtil;
/**
* 宝箱
* Class
* @package common\modules\levelgift
*/
class TreasureBoxUtil
{
const GET_GIFT = 1;//获取奖励
const NOT_GET_GIFT = 0;//没有获取奖励
const GET_GIFT_LIST = [
self::GET_GIFT,
self::NOT_GET_GIFT,
];
/**
* 添加箱子
* @param $userID
* @param $itemID
* @param $num
* @param SystemLog $logoVo
* @throws StatusException
*/
public static function addBox($userID, $itemID, $num, SystemLog $logoVo)
{
$num = (int)$num;
if ($num <= 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$config = GameConfigServices::getParamByPath('case.' . $itemID);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
for ($i = 0; $i < $num; $i++) {
$box = new TreasureBox();
$box->user_id = $userID;
$box->item_id = $itemID;
$box->end_open_time = 0;
$box->is_get_gift = self::NOT_GET_GIFT;
if (!$box->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}
if (!$logoVo->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}
/**
* 开始倒计时
* @param $userID
* @param $boxID
*/
public static function startTime($userID, $boxID)
{
SafeUtil::dbOperate(function ($userID, $boxID) {
return SafeUtil::redisLockOperate(function ($userID, $boxID) {
$box = TreasureBox::findByID($boxID);
if (empty($box) || $box->end_open_time != 0 || $box->user_id != $userID) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$config = GameConfigServices::getParamByPath('case.' . $box->item_id);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$time = (int)$config['open_time'];
if ($time < 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$box->end_open_time = time() + $time;
if (!$box->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}, [$userID, $boxID], RedisKeyUtil::lockBox($boxID));
}, [$userID, $boxID]);
}
/**
* 开箱子
* @param $userID
* @param $boxID
* @param $useGem
*/
public static function openBox($userID, $boxID, $useGem)
{
return SafeUtil::dbOperate(function ($userID, $boxID, $useGem) {
return SafeUtil::redisLockOperate(function ($userID, $boxID, $useGem) {
$useGem=(int)$useGem;
$box = TreasureBox::findByID($boxID);
if (empty($box) || $box->user_id != $userID || $box->is_get_gift == self::GET_GIFT || $box->end_open_time == 0)
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
if ($useGem != 1 && $box->end_open_time > time()) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
if ($useGem == 1 && $box->end_open_time <= time()) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$config = GameConfigServices::getParamByPath('case.' . $box->item_id);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$gem = (int)$config['Diamond'];
if ($gem <= 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
if ($useGem == 1) {
//使用钻石开宝箱
$log = SystemLogUtil::get($userID, SystemLogUtil::TYPE_GEM, -$gem, 0, SystemLogUtil::FROM_TYPE_OPEN_BOX_EXPEND, ['config' => $config]);
MoneyUtil::checkAndModifyRes($userID, false, -$gem, ItemServices::ITEM_TYPE2_GEM, $log);
}
//领取奖励
$getItem = MoneyUtil::parseAllItemOdds($config['reward_id'], true, $userID, SystemLogUtil::FROM_TYPE_GET_BOX_GIFT, ['config' => $config]);
if (empty($getItem)) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
MoneyUtil::parseItem($config['fixed_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_GET_BOX_GIFT, ['config' => $config]);
MoneyUtil::parseItem($config['fixed_id2'], 1, true, $userID, SystemLogUtil::FROM_TYPE_GET_BOX_GIFT, ['config' => $config]);
if (!$box->delete()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
//任务记录
DailyTaskUtil::addTaskCount($userID, DailyTaskUtil::TYPE_OPEN_TREASURE_BOX, $box->item_id, 1);
\Yii::warning($userID);
//成]
AchievementUtil::addCount($userID, AchievementUtil::ID_OPEN_TREASURE_BOX, 1);
return $getItem;
}, [$userID, $boxID, $useGem], RedisKeyUtil::lockBox($boxID));
}, [$userID, $boxID, $useGem]);
}
/**
* 删除箱子
* @param $userID
* @param $boxID
*/
public static function delBox($userID, $boxID)
{
SafeUtil::dbOperate(function ($userID, $boxID) {
SafeUtil::redisLockOperate(function ($userID, $boxID) {
$box = TreasureBox::findByID($boxID);
if (empty($box) || $box->user_id != $userID)
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$box->delete();
}, [$userID, $boxID], RedisKeyUtil::lockBox($boxID));
}, [$userID, $boxID]);
}
}