|
|
@@ -5,19 +5,16 @@ namespace App\Module\Game\Logics;
|
|
|
use App\Module\Game\Dtos\RewardGroupDto;
|
|
|
use App\Module\Game\Dtos\RewardItemDto;
|
|
|
use App\Module\Game\Dtos\RewardResultDto;
|
|
|
-use App\Module\Game\Enums\REWARD_TYPE;
|
|
|
use App\Module\Game\Enums\REWARD_MODE;
|
|
|
use App\Module\Game\Events\RewardGrantedEvent;
|
|
|
use App\Module\Game\Models\GameRewardGroup;
|
|
|
use App\Module\Game\Models\GameRewardLog;
|
|
|
-use App\Module\Game\Services\RewardCollectorService;
|
|
|
use App\Module\Game\Services\PityService;
|
|
|
use App\Module\Game\Logics\WeightSelectionReward;
|
|
|
use App\Module\Game\Logics\IndependentProbabilityReward;
|
|
|
-use App\Module\GameItems\Services\ItemService;
|
|
|
+use App\Module\Game\Logics\RewardProcessors\RewardProcessorDispatcher;
|
|
|
use Exception;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
-use ReflectionClass;
|
|
|
use UCore\Db\Helper;
|
|
|
|
|
|
/**
|
|
|
@@ -79,7 +76,7 @@ class RewardLogic
|
|
|
|
|
|
// 发放各类奖励
|
|
|
foreach ($rewardItems as $item) {
|
|
|
- $this->processRewardItem($userId, $item, $sourceType, $sourceId);
|
|
|
+ RewardProcessorDispatcher::process($userId, $item, $sourceType, $sourceId);
|
|
|
}
|
|
|
|
|
|
// 记录奖励日志
|
|
|
@@ -138,68 +135,7 @@ class RewardLogic
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 处理单个奖励项
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processRewardItem(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- switch ($item->rewardType) {
|
|
|
- case REWARD_TYPE::ITEM->valueInt():
|
|
|
- $this->processItemReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::FUND_CONFIG->valueInt():
|
|
|
- $this->processFundConfigReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::CURRENCY->valueInt():
|
|
|
- $this->processCurrencyReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::PET_EXP->valueInt():
|
|
|
- $this->processPetExpReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::PET_ENERGY->valueInt():
|
|
|
- $this->processPetEnergyReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::FARM_SHRINE->valueInt():
|
|
|
- $this->processFarmShrineReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::PET->valueInt():
|
|
|
- $this->processPetReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::PET_POWER->valueInt():
|
|
|
- $this->processPetPowerReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- case REWARD_TYPE::OTHER->valueInt():
|
|
|
- $this->processOtherReward($userId, $item, $sourceType, $sourceId);
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- // 未知奖励类型,记录日志
|
|
|
- Log::warning("未知的奖励类型", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'rewardType' => $item->rewardType,
|
|
|
- 'targetId' => $item->targetId,
|
|
|
- 'quantity' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 记录奖励日志
|
|
|
@@ -236,527 +172,25 @@ class RewardLogic
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理物品奖励
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processItemReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 发放物品奖励,传递来源信息
|
|
|
- ItemService::addItem($userId, $item->targetId, $item->quantity, [
|
|
|
- 'param1' => $item->param1,
|
|
|
- 'param2' => $item->param2,
|
|
|
- 'source_type' => $sourceType,
|
|
|
- 'source_id' => $sourceId,
|
|
|
- 'source' => 'reward', // 保持向后兼容
|
|
|
- 'extra_data' => $item->extraData
|
|
|
- ]);
|
|
|
-
|
|
|
- Log::info("物品奖励发放成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'itemId' => $item->targetId,
|
|
|
- 'quantity' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("物品奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'itemId' => $item->targetId,
|
|
|
- 'quantity' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("物品奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理账户种类奖励(FUND_CONFIG)
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processFundConfigReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 使用Fund模块的User类来处理资金变更
|
|
|
- // target_id 是 fund_config 表的 id(账户种类ID)
|
|
|
- $result = \App\Module\Fund\Logic\User::handle(
|
|
|
- $userId,
|
|
|
- $item->targetId, // fund_config_id
|
|
|
- $item->quantity, // 金额
|
|
|
- \App\Module\Fund\Enums\LOG_TYPE::TRADE, // 日志类型为贸易
|
|
|
- $sourceId, // 关联ID,使用来源ID
|
|
|
- "奖励发放:账户种类奖励 - {$sourceType}#{$sourceId}"
|
|
|
- );
|
|
|
-
|
|
|
- if (is_string($result)) {
|
|
|
- throw new Exception("账户种类奖励发放失败: " . $result);
|
|
|
- }
|
|
|
-
|
|
|
- Log::info("账户种类奖励发放成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'fundConfigId' => $item->targetId,
|
|
|
- 'amount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("账户种类奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'fundConfigId' => $item->targetId,
|
|
|
- 'amount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("账户种类奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理币种奖励(CURRENCY)
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processCurrencyReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 对于币种奖励,需要先根据币种ID找到对应的账户种类ID
|
|
|
- // 这里需要查询 fund_config 表,找到对应币种的账户种类
|
|
|
- $fundConfig = \App\Module\Fund\Models\FundConfigModel::where('currency_id', $item->targetId)->first();
|
|
|
-
|
|
|
- if (!$fundConfig) {
|
|
|
- throw new Exception("找不到币种ID为 {$item->targetId} 的账户配置");
|
|
|
- }
|
|
|
-
|
|
|
- // 使用找到的账户种类ID来发放奖励
|
|
|
- $result = \App\Module\Fund\Logic\User::handle(
|
|
|
- $userId,
|
|
|
- $fundConfig->id, // fund_config_id
|
|
|
- $item->quantity, // 金额
|
|
|
- \App\Module\Fund\Enums\LOG_TYPE::TRADE, // 日志类型
|
|
|
- $sourceId, // 关联ID,使用来源ID
|
|
|
- "奖励发放:币种奖励 - {$sourceType}#{$sourceId}"
|
|
|
- );
|
|
|
-
|
|
|
- if (is_string($result)) {
|
|
|
- throw new Exception("币种奖励发放失败: " . $result);
|
|
|
- }
|
|
|
-
|
|
|
- Log::info("币种奖励发放成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'currencyId' => $item->targetId,
|
|
|
- 'fundConfigId' => $fundConfig->id,
|
|
|
- 'amount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("币种奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'currencyId' => $item->targetId,
|
|
|
- 'amount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("币种奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理宠物经验奖励
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processPetExpReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 获取用户的宠物
|
|
|
- $pet = \App\Module\Pet\Models\PetUser::where('user_id', $userId)
|
|
|
- ->where('id', $item->targetId)
|
|
|
- ->first();
|
|
|
-
|
|
|
- if (!$pet) {
|
|
|
- throw new Exception("找不到ID为 {$item->targetId} 的宠物");
|
|
|
- }
|
|
|
-
|
|
|
- // 使用宠物逻辑类来增加经验
|
|
|
- $petLogic = new \App\Module\Pet\Logic\PetLogic();
|
|
|
- $reflection = new \ReflectionClass($petLogic);
|
|
|
- $method = $reflection->getMethod('addExperience');
|
|
|
- $method->setAccessible(true);
|
|
|
-
|
|
|
- $levelUpOccurred = $method->invoke($petLogic, $item->targetId, $item->quantity);
|
|
|
-
|
|
|
- Log::info("宠物经验奖励发放成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $item->targetId,
|
|
|
- 'expAmount' => $item->quantity,
|
|
|
- 'levelUp' => $levelUpOccurred,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("宠物经验奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $item->targetId,
|
|
|
- 'expAmount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("宠物经验奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理宠物体力奖励
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processPetEnergyReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 获取用户的宠物
|
|
|
- $pet = \App\Module\Pet\Models\PetUser::where('user_id', $userId)
|
|
|
- ->where('id', $item->targetId)
|
|
|
- ->first();
|
|
|
|
|
|
- if (!$pet) {
|
|
|
- throw new Exception("找不到ID为 {$item->targetId} 的宠物");
|
|
|
- }
|
|
|
|
|
|
- // 使用宠物逻辑类来增加体力
|
|
|
- $petLogic = new \App\Module\Pet\Logic\PetLogic();
|
|
|
- $reflection = new \ReflectionClass($petLogic);
|
|
|
- $method = $reflection->getMethod('addStamina');
|
|
|
- $method->setAccessible(true);
|
|
|
|
|
|
- $actualGained = $method->invoke($petLogic, $item->targetId, $item->quantity);
|
|
|
|
|
|
- Log::info("宠物体力奖励发放成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $item->targetId,
|
|
|
- 'staminaAmount' => $item->quantity,
|
|
|
- 'actualGained' => $actualGained,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("宠物体力奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $item->targetId,
|
|
|
- 'staminaAmount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("宠物体力奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 处理宠物奖励
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processPetReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 获取宠物配置参数
|
|
|
- $petConfigId = $item->targetId; // 宠物配置ID或宠物类型
|
|
|
- $petCount = $item->quantity > 0 ? $item->quantity : 1; // 宠物数量,默认1只
|
|
|
- $petGrade = $item->param1 > 0 ? $item->param1 : null; // 宠物品阶,为null时随机生成
|
|
|
-
|
|
|
- // 验证宠物配置是否存在
|
|
|
- $petConfig = \App\Module\Pet\Models\PetConfig::find($petConfigId);
|
|
|
- if (!$petConfig) {
|
|
|
- throw new Exception("宠物配置不存在,配置ID: {$petConfigId}");
|
|
|
- }
|
|
|
|
|
|
- // 循环创建宠物(支持一次奖励多只宠物)
|
|
|
- $createdPets = [];
|
|
|
- for ($i = 0; $i < $petCount; $i++) {
|
|
|
- // 生成宠物名称
|
|
|
- $petName = $this->generatePetName($petConfig->name, $i + 1, $petCount);
|
|
|
-
|
|
|
- // 创建宠物
|
|
|
- $result = \App\Module\Pet\Services\PetService::createPet($userId, $petName, $petGrade, [
|
|
|
- 'pet_type' => $petConfig->pet_type,
|
|
|
- 'source' => 'reward',
|
|
|
- 'source_type' => $sourceType,
|
|
|
- 'source_id' => $sourceId
|
|
|
- ]);
|
|
|
-
|
|
|
- if (!$result['success']) {
|
|
|
- throw new Exception("宠物创建失败: " . ($result['message'] ?? '未知错误'));
|
|
|
- }
|
|
|
-
|
|
|
- $createdPets[] = [
|
|
|
- 'pet_id' => $result['pet_id'],
|
|
|
- 'name' => $petName,
|
|
|
- 'grade' => $result['grade']
|
|
|
- ];
|
|
|
-
|
|
|
- Log::info("宠物奖励创建成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $result['pet_id'],
|
|
|
- 'petName' => $petName,
|
|
|
- 'petGrade' => $result['grade'],
|
|
|
- 'petConfigId' => $petConfigId,
|
|
|
- 'petType' => $petConfig->pet_type,
|
|
|
- 'index' => $i + 1,
|
|
|
- 'totalCount' => $petCount,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- }
|
|
|
|
|
|
- Log::info("宠物奖励发放完成", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petConfigId' => $petConfigId,
|
|
|
- 'petType' => $petConfig->pet_type,
|
|
|
- 'petCount' => $petCount,
|
|
|
- 'petGrade' => $petGrade,
|
|
|
- 'createdPets' => $createdPets,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("宠物奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petConfigId' => $item->targetId,
|
|
|
- 'petCount' => $item->quantity,
|
|
|
- 'petGrade' => $item->param1,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("宠物奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 处理宠物体力奖励
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processPetPowerReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 获取宠物ID和体力数量
|
|
|
- $petId = $item->targetId;
|
|
|
- $powerAmount = $item->quantity;
|
|
|
|
|
|
- // 验证宠物是否存在且属于该用户
|
|
|
- $pet = \App\Module\Pet\Models\PetUser::where('id', $petId)
|
|
|
- ->where('user_id', $userId)
|
|
|
- ->first();
|
|
|
|
|
|
- if (!$pet) {
|
|
|
- throw new Exception("宠物不存在或不属于该用户,宠物ID: {$petId},用户ID: {$userId}");
|
|
|
- }
|
|
|
-
|
|
|
- // 使用宠物逻辑类来增加体力
|
|
|
- $petLogic = new \App\Module\Pet\Logic\PetLogic();
|
|
|
- $reflection = new \ReflectionClass($petLogic);
|
|
|
- $method = $reflection->getMethod('addStamina');
|
|
|
- $method->setAccessible(true);
|
|
|
-
|
|
|
- $actualGained = $method->invoke($petLogic, $petId, $powerAmount);
|
|
|
-
|
|
|
- Log::info("宠物体力奖励发放成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $petId,
|
|
|
- 'petName' => $pet->name,
|
|
|
- 'powerAmount' => $powerAmount,
|
|
|
- 'actualGained' => $actualGained,
|
|
|
- 'currentPower' => $pet->fresh()->stamina,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("宠物体力奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'petId' => $item->targetId,
|
|
|
- 'powerAmount' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("宠物体力奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 生成宠物名称
|
|
|
- *
|
|
|
- * @param string $baseName 基础名称
|
|
|
- * @param int $index 索引
|
|
|
- * @param int $total 总数
|
|
|
- * @return string 生成的宠物名称
|
|
|
- */
|
|
|
- private function generatePetName(string $baseName, int $index, int $total): string
|
|
|
- {
|
|
|
- if ($total == 1) {
|
|
|
- return $baseName;
|
|
|
- }
|
|
|
|
|
|
- return $baseName . $index;
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 处理神像奖励(农场buff)
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function processFarmShrineReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- try {
|
|
|
- // 获取神像类型和持续时间
|
|
|
- $shrineType = $item->targetId; // 神像类型(1=丰收之神,2=雨露之神,3=屠草之神,4=拭虫之神)
|
|
|
- $durationHours = $item->param2 > 0 ? $item->param2 : 24; // 持续时间(小时),默认24小时
|
|
|
- $activationCount = $item->quantity > 0 ? $item->quantity : 1; // 激活次数,默认1次
|
|
|
-
|
|
|
- // 验证神像类型是否有效
|
|
|
- if (!in_array($shrineType, [1, 2, 3, 4])) {
|
|
|
- throw new Exception("无效的神像类型: {$shrineType}");
|
|
|
- }
|
|
|
|
|
|
- // 验证用户是否存在
|
|
|
- $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
|
|
|
- if (!$farmUser) {
|
|
|
- throw new Exception("用户农场数据不存在,用户ID: {$userId}");
|
|
|
- }
|
|
|
|
|
|
- // 循环激活神像(支持多次激活)
|
|
|
- for ($i = 0; $i < $activationCount; $i++) {
|
|
|
- // 使用BuffService激活神像
|
|
|
- $buff = \App\Module\Farm\Services\BuffService::activateBuff($userId, $shrineType, $durationHours);
|
|
|
-
|
|
|
- if (!$buff) {
|
|
|
- throw new Exception("神像激活失败,神像类型: {$shrineType},用户ID: {$userId}");
|
|
|
- }
|
|
|
-
|
|
|
- Log::info("神像奖励激活成功", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'shrineType' => $shrineType,
|
|
|
- 'shrineName' => \App\Module\Farm\Enums\BUFF_TYPE::getName($shrineType),
|
|
|
- 'durationHours' => $durationHours,
|
|
|
- 'activationIndex' => $i + 1,
|
|
|
- 'totalActivations' => $activationCount,
|
|
|
- 'expireTime' => $buff->expire_time ? $buff->expire_time->toDateTimeString() : null,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- }
|
|
|
|
|
|
- // 收集神像奖励到Response.Reward中
|
|
|
- RewardCollectorService::addGodReward(
|
|
|
- $shrineType,
|
|
|
- $durationHours * 3600, // 转换为秒
|
|
|
- $activationCount
|
|
|
- );
|
|
|
|
|
|
- Log::info("神像奖励发放完成", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'shrineType' => $shrineType,
|
|
|
- 'shrineName' => \App\Module\Farm\Enums\BUFF_TYPE::getName($shrineType),
|
|
|
- 'durationHours' => $durationHours,
|
|
|
- 'totalActivations' => $activationCount,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
- } catch (Exception $e) {
|
|
|
- Log::error("神像奖励发放失败", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'shrineType' => $item->targetId,
|
|
|
- 'durationHours' => $item->param2,
|
|
|
- 'quantity' => $item->quantity,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- throw new Exception("神像奖励发放失败: " . $e->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 处理其他类型奖励
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param RewardItemDto $item 奖励项
|
|
|
- * @param string $sourceType 来源类型
|
|
|
- * @param int $sourceId 来源ID
|
|
|
- * @return void
|
|
|
- */
|
|
|
- private function processOtherReward(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
|
|
|
- {
|
|
|
- // 其他类型奖励的处理逻辑
|
|
|
- // 这里可以根据具体需求实现,比如称号、成就等
|
|
|
- Log::info("其他类型奖励处理", [
|
|
|
- 'userId' => $userId,
|
|
|
- 'rewardType' => $item->rewardType,
|
|
|
- 'targetId' => $item->targetId,
|
|
|
- 'quantity' => $item->quantity,
|
|
|
- 'param1' => $item->param1,
|
|
|
- 'param2' => $item->param2,
|
|
|
- 'extraData' => $item->extraData,
|
|
|
- 'sourceType' => $sourceType,
|
|
|
- 'sourceId' => $sourceId
|
|
|
- ]);
|
|
|
|
|
|
- // 目前只记录日志,具体实现可以根据业务需求扩展
|
|
|
- // 例如:
|
|
|
- // - 称号奖励:更新用户称号
|
|
|
- // - 成就奖励:解锁成就
|
|
|
- // - 特殊权限:更新用户权限
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 模拟奖励发放(不实际发放,仅返回奖励结果)
|
|
|
@@ -825,7 +259,7 @@ class RewardLogic
|
|
|
|
|
|
// 发放各类奖励
|
|
|
foreach ($rewardItems as $item) {
|
|
|
- $this->processRewardItem($userId, $item, $sourceType, $sourceId);
|
|
|
+ RewardProcessorDispatcher::process($userId, $item, $sourceType, $sourceId);
|
|
|
}
|
|
|
|
|
|
// 更新保底计数(如果启用保底机制)
|