| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Module\Game\Logics\RewardProcessors;
- use App\Module\Game\Dtos\RewardItemDto;
- use App\Module\Game\Services\RewardCollectorService;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 神像奖励处理器
- *
- * 处理神像(FARM_SHRINE)类型的奖励发放
- */
- class FarmShrineRewardProcessor
- {
- /**
- * 处理神像奖励(农场buff)
- *
- * @param int $userId 用户ID
- * @param RewardItemDto $item 奖励项
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @return void
- * @throws Exception
- */
- public static function process(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(
- $userId,
- $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());
- }
- }
- }
|