|
|
@@ -13,11 +13,14 @@ use App\Module\Farm\Services\DisasterService;
|
|
|
use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
+use function Symfony\Component\Translation\t;
|
|
|
+
|
|
|
/**
|
|
|
* 灾害管理逻辑
|
|
|
*/
|
|
|
class DisasterLogic
|
|
|
{
|
|
|
+
|
|
|
/**
|
|
|
* 获取作物的灾害信息
|
|
|
*
|
|
|
@@ -41,8 +44,8 @@ class DisasterLogic
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取作物灾害信息失败', [
|
|
|
'crop_id' => $cropId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return collect();
|
|
|
@@ -74,8 +77,8 @@ class DisasterLogic
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取作物活跃灾害失败', [
|
|
|
'crop_id' => $cropId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return collect();
|
|
|
@@ -104,7 +107,7 @@ class DisasterLogic
|
|
|
}
|
|
|
|
|
|
// 只在发芽期和生长期生成灾害
|
|
|
- if (!in_array($crop->growth_stage, [GROWTH_STAGE::SPROUT, GROWTH_STAGE::GROWTH])) {
|
|
|
+ if (!in_array($crop->growth_stage, [ GROWTH_STAGE::SPROUT, GROWTH_STAGE::GROWTH ])) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@@ -114,7 +117,7 @@ class DisasterLogic
|
|
|
}
|
|
|
|
|
|
$userId = $crop->user_id;
|
|
|
- $seed = $crop->seed;
|
|
|
+ $seed = $crop->seed;
|
|
|
|
|
|
// 获取种子的灾害抵抗属性
|
|
|
$disasterResistance = $seed->disaster_resistance ?? [];
|
|
|
@@ -133,9 +136,9 @@ class DisasterLogic
|
|
|
|
|
|
if ($disasterInfo) {
|
|
|
Log::info('灾害生成成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'land_id' => $land->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'land_id' => $land->id,
|
|
|
'disaster_type' => $disasterInfo->type
|
|
|
]);
|
|
|
|
|
|
@@ -146,8 +149,8 @@ class DisasterLogic
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('灾害生成失败', [
|
|
|
'crop_id' => $cropId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return null;
|
|
|
@@ -170,11 +173,11 @@ class DisasterLogic
|
|
|
|
|
|
// 随机选择一种灾害类型
|
|
|
$randomDisasterType = array_rand($disasterTypes);
|
|
|
- $baseProb = $disasterTypes[$randomDisasterType];
|
|
|
+ $baseProb = $disasterTypes[$randomDisasterType];
|
|
|
|
|
|
// 计算最终概率,考虑种子抵抗、土地抵抗和神灵加持
|
|
|
$seedResistance = $disasterResistance[DisasterService::getDisasterKey($randomDisasterType)] ?? 0;
|
|
|
- $finalProb = $baseProb - $seedResistance - $landDisasterResistance;
|
|
|
+ $finalProb = $baseProb - $seedResistance - $landDisasterResistance;
|
|
|
|
|
|
// 如果有对应的神灵加持,则不生成该类型的灾害
|
|
|
$buffType = DISASTER_TYPE::getPreventBuffType($randomDisasterType);
|
|
|
@@ -204,18 +207,18 @@ class DisasterLogic
|
|
|
{
|
|
|
// 生成灾害信息
|
|
|
$disasterInfo = [
|
|
|
- 'type' => $disasterType,
|
|
|
+ 'type' => $disasterType,
|
|
|
'generated_ts' => now()->toDateTimeString(),
|
|
|
- 'status' => 'active'
|
|
|
+ 'status' => 'active'
|
|
|
];
|
|
|
|
|
|
// 更新作物灾害信息
|
|
|
- $disasters = $crop->disasters ?? [];
|
|
|
- $disasters[] = $disasterInfo;
|
|
|
+ $disasters = $crop->disasters ?? [];
|
|
|
+ $disasters[] = $disasterInfo;
|
|
|
$crop->disasters = $disasters;
|
|
|
|
|
|
// 更新土地状态为灾害
|
|
|
- $land = $crop->land;
|
|
|
+ $land = $crop->land;
|
|
|
$land->status = LAND_STATUS::DISASTER;
|
|
|
|
|
|
// 保存更改
|
|
|
@@ -234,7 +237,7 @@ class DisasterLogic
|
|
|
* @param int|null $checkIntervalMinutes 检查间隔(分钟)
|
|
|
* @return array 生成结果统计
|
|
|
*/
|
|
|
- public function batchGenerateDisasters(?int $checkIntervalMinutes = null): array
|
|
|
+ public function generateDisasterBatchs(?int $checkIntervalMinutes = null): array
|
|
|
{
|
|
|
if ($checkIntervalMinutes === null) {
|
|
|
$checkIntervalMinutes = DisasterService::getCheckInterval();
|
|
|
@@ -246,62 +249,79 @@ class DisasterLogic
|
|
|
// 1. 必须在发芽期或生长期
|
|
|
// 2. 当前阶段可以产生灾害 (can_disaster = 1)
|
|
|
// 3. 满足时间检查条件(首次检查或超过检查间隔)
|
|
|
- $crops = FarmCrop::whereIn('growth_stage', [GROWTH_STAGE::SPROUT, GROWTH_STAGE::GROWTH])
|
|
|
+ $crops = FarmCrop::whereIn('growth_stage', [ GROWTH_STAGE::SPROUT, GROWTH_STAGE::GROWTH ])
|
|
|
->where('can_disaster', true)
|
|
|
->where(function ($query) use ($checkTime) {
|
|
|
$query->whereNull('last_disaster_check_time')
|
|
|
- ->orWhere('last_disaster_check_time', '<', $checkTime);
|
|
|
+ ->orWhere('last_disaster_check_time', '<', $checkTime);
|
|
|
})
|
|
|
- ->with(['land.landType', 'seed', 'user.buffs' => function ($query) {
|
|
|
- $query->where('expire_time', '>', now());
|
|
|
- }])
|
|
|
+ ->with([
|
|
|
+ 'land.landType', 'seed', 'user.buffs' => function ($query) {
|
|
|
+ $query->where('expire_time', '>', now());
|
|
|
+ }
|
|
|
+ ])
|
|
|
->get();
|
|
|
|
|
|
- $totalCount = $crops->count();
|
|
|
+ $totalCount = $crops->count();
|
|
|
$generatedCount = 0;
|
|
|
- $skippedCount = 0;
|
|
|
+ $skippedCount = 0;
|
|
|
|
|
|
foreach ($crops as $crop) {
|
|
|
- try {
|
|
|
- // 更新检查时间
|
|
|
- $crop->last_disaster_check_time = now();
|
|
|
-
|
|
|
- // 跳过已经有灾害的土地
|
|
|
- if ($crop->land->status === LAND_STATUS::DISASTER) {
|
|
|
- $crop->save(); // 仍需更新检查时间
|
|
|
- $skippedCount++;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ $res = $this->generateDisasters($crop);
|
|
|
+ if ($res) {
|
|
|
+ $generatedCount++;
|
|
|
+ } else {
|
|
|
+ $skippedCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 获取相关数据
|
|
|
- $disasterResistance = $crop->seed->disaster_resistance ?? [];
|
|
|
- $landDisasterResistance = $crop->land->landType->disaster_resistance ?? 0;
|
|
|
- $activeBuffs = $crop->user->buffs->pluck('buff_type')->toArray();
|
|
|
+ return [
|
|
|
+ 'total' => $totalCount,
|
|
|
+ 'generated' => $generatedCount,
|
|
|
+ 'skipped' => $skippedCount
|
|
|
+ ];
|
|
|
+ }
|
|
|
|
|
|
- // 尝试生成灾害
|
|
|
- $disasterInfo = $this->tryGenerateDisasterForCrop($crop, $disasterResistance, $landDisasterResistance, $activeBuffs);
|
|
|
+ public function generateDisasters(FarmCrop $crop): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 更新检查时间
|
|
|
+ $crop->last_disaster_check_time = now();
|
|
|
|
|
|
- if ($disasterInfo) {
|
|
|
- // 生成灾害后,设置当前阶段不能再产生灾害
|
|
|
- $crop->can_disaster = false;
|
|
|
- $generatedCount++;
|
|
|
- }
|
|
|
+ // 跳过已经有灾害的土地
|
|
|
+ if ($crop->land->status === LAND_STATUS::DISASTER) {
|
|
|
+ $crop->save(); // 仍需更新检查时间
|
|
|
|
|
|
- // 保存更改
|
|
|
- $crop->save();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取相关数据
|
|
|
+ $disasterResistance = $crop->seed->disaster_resistance ?? [];
|
|
|
+ $landDisasterResistance = $crop->land->landType->disaster_resistance ?? 0;
|
|
|
+ $activeBuffs = $crop->user->buffs->pluck('buff_type')->toArray();
|
|
|
+
|
|
|
+ // 尝试生成灾害
|
|
|
+ $disasterInfo = $this->tryGenerateDisasterForCrop($crop, $disasterResistance, $landDisasterResistance, $activeBuffs);
|
|
|
+
|
|
|
+ if ($disasterInfo) {
|
|
|
+ // 生成灾害后,设置当前阶段不能再产生灾害
|
|
|
+ $crop->can_disaster = false;
|
|
|
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('批量生成灾害失败', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
}
|
|
|
+
|
|
|
+ // 保存更改
|
|
|
+ $crop->save();
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('批量生成灾害失败', [
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
- return [
|
|
|
- 'total' => $totalCount,
|
|
|
- 'generated' => $generatedCount,
|
|
|
- 'skipped' => $skippedCount
|
|
|
- ];
|
|
|
+ return false;
|
|
|
}
|
|
|
+
|
|
|
}
|