| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Module\Farm\Logics;
- use App\Module\Farm\Enums\BUFF_TYPE;
- use App\Module\Farm\Events\BuffActivatedEvent;
- use App\Module\Farm\Models\FarmGodBuff;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
- /**
- * 神灵加持逻辑
- */
- class BuffLogic
- {
- /**
- * 获取用户的所有神灵加持
- *
- * @param int $userId
- * @return Collection
- */
- public function getUserBuffs(int $userId): Collection
- {
- try {
- return FarmGodBuff::where('user_id', $userId)
- ->orderBy('buff_type')
- ->get();
- } catch (\Exception $e) {
- Log::error('获取用户神灵加持失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return collect();
- }
- }
-
- /**
- * 获取用户的有效神灵加持
- *
- * @param int $userId
- * @return Collection
- */
- public function getActiveBuffs(int $userId): Collection
- {
- try {
- return FarmGodBuff::where('user_id', $userId)
- ->where('expire_time', '>', now())
- ->orderBy('buff_type')
- ->get();
- } catch (\Exception $e) {
- Log::error('获取用户有效神灵加持失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return collect();
- }
- }
-
- /**
- * 获取用户指定类型的神灵加持
- *
- * @param int $userId
- * @param int $buffType
- * @return FarmGodBuff|null
- */
- public function getUserBuff(int $userId, int $buffType): ?FarmGodBuff
- {
- try {
- return FarmGodBuff::where('user_id', $userId)
- ->where('buff_type', $buffType)
- ->first();
- } catch (\Exception $e) {
- Log::error('获取用户指定类型神灵加持失败', [
- 'user_id' => $userId,
- 'buff_type' => $buffType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return null;
- }
- }
-
- /**
- * 获取用户指定类型的有效神灵加持
- *
- * @param int $userId
- * @param int $buffType
- * @return FarmGodBuff|null
- */
- public function getActiveUserBuff(int $userId, int $buffType): ?FarmGodBuff
- {
- try {
- return FarmGodBuff::where('user_id', $userId)
- ->where('buff_type', $buffType)
- ->where('expire_time', '>', now())
- ->first();
- } catch (\Exception $e) {
- Log::error('获取用户指定类型有效神灵加持失败', [
- 'user_id' => $userId,
- 'buff_type' => $buffType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return null;
- }
- }
-
- /**
- * 激活神灵加持
- *
- * @param int $userId
- * @param int $buffType
- * @param int $durationHours
- * @return FarmGodBuff|null
- */
- public function activateBuff(int $userId, int $buffType, int $durationHours): ?FarmGodBuff
- {
- try {
- // 检查buff类型是否有效
- if (!in_array($buffType, [
- BUFF_TYPE::HARVEST_GOD,
- BUFF_TYPE::RAIN_GOD,
- BUFF_TYPE::WEED_KILLER_GOD,
- BUFF_TYPE::PEST_CLEANER_GOD
- ])) {
- throw new \Exception('无效的神灵加持类型');
- }
-
- // 获取当前buff
- $buff = FarmGodBuff::where('user_id', $userId)
- ->where('buff_type', $buffType)
- ->first();
-
- $now = now();
- $expireTime = $now->copy()->addHours($durationHours);
-
- if ($buff) {
- // 如果已有buff,延长时间
- if ($buff->expire_time > $now) {
- // 如果buff未过期,在当前过期时间基础上延长
- $expireTime = $buff->expire_time->addHours($durationHours);
- }
-
- $buff->expire_time = $expireTime;
- $buff->save();
- } else {
- // 创建新buff
- $buff = new FarmGodBuff();
- $buff->user_id = $userId;
- $buff->buff_type = $buffType;
- $buff->expire_time = $expireTime;
- $buff->save();
- }
-
- // 触发buff激活事件
- event(new BuffActivatedEvent($userId, $buff));
-
- Log::info('神灵加持激活成功', [
- 'user_id' => $userId,
- 'buff_type' => $buffType,
- 'duration_hours' => $durationHours,
- 'expire_time' => $expireTime->toDateTimeString()
- ]);
-
- return $buff;
- } catch (\Exception $e) {
- Log::error('神灵加持激活失败', [
- 'user_id' => $userId,
- 'buff_type' => $buffType,
- 'duration_hours' => $durationHours,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return null;
- }
- }
-
- /**
- * 清理过期的神灵加持
- *
- * @return int
- */
- public function clearExpiredBuffs(): int
- {
- try {
- $count = FarmGodBuff::where('expire_time', '<', now())->delete();
-
- Log::info('清理过期神灵加持成功', [
- 'count' => $count
- ]);
-
- return $count;
- } catch (\Exception $e) {
- Log::error('清理过期神灵加持失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return 0;
- }
- }
- }
|