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]); } }