| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- namespace App\Module\Farm\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use App\Module\Game\Models\GameConsumeGroup;
- use App\Module\Game\Models\GameConditionGroup;
- /**
- * 土地升级配置模型
- * field start
- * @property int $id 主键ID
- * @property int $from_type_id 起始土地类型ID
- * @property int $to_type_id 目标土地类型ID
- * @property int $materials 消耗组ID,关联game_consume_groups表
- * @property int $conditions 条件组ID,关联game_condition_groups表
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class FarmLandUpgradeConfig extends Model
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'farm_land_upgrade_configs';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- // attribute start
- protected $fillable = [
- 'from_type_id',
- 'to_type_id',
- 'materials',
- 'conditions',
- ];
- // attribute end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- ];
- /**
- * 获取起始土地类型
- *
- * @return BelongsTo
- */
- public function fromType(): BelongsTo
- {
- return $this->belongsTo(FarmLandType::class, 'from_type_id', 'id');
- }
- /**
- * 获取目标土地类型
- *
- * @return BelongsTo
- */
- public function toType(): BelongsTo
- {
- return $this->belongsTo(FarmLandType::class, 'to_type_id', 'id');
- }
- /**
- * 获取关联的消耗组
- *
- * @return BelongsTo
- */
- public function materialsGroup(): BelongsTo
- {
- return $this->belongsTo(GameConsumeGroup::class, 'materials_group_id', 'id');
- }
- /**
- * 获取关联的条件组
- *
- * @return BelongsTo
- */
- public function conditionsGroup(): BelongsTo
- {
- return $this->belongsTo(GameConditionGroup::class, 'conditions_group_id', 'id');
- }
- /**
- * 获取关联的消耗组(兼容旧代码)
- *
- * @return BelongsTo
- * @deprecated 使用 materialsGroup() 替代
- */
- public function consumeGroup(): BelongsTo
- {
- return $this->materialsGroup();
- }
- /**
- * 获取关联的条件组(兼容旧代码)
- *
- * @return BelongsTo
- * @deprecated 使用 conditionsGroup() 替代
- */
- public function conditionGroup(): BelongsTo
- {
- return $this->conditionsGroup();
- }
- /**
- * 获取升级所需材料
- *
- * 优先使用消耗组,如果没有则使用 materials 字段
- *
- * @return array
- */
- public function getUpgradeMaterials(): array
- {
- // 如果有关联的消耗组,则使用消耗组服务获取材料
- if ($this->materials && is_numeric($this->materials)) {
- return \App\Module\Game\Services\ConsumeGroupService::getConsumeMaterials($this->materials);
- }
- // 否则使用 materials 字段
- if (is_string($this->materials)) {
- $decoded = json_decode($this->materials, true);
- return $decoded['materials'] ?? [];
- } elseif (is_array($this->materials)) {
- return $this->materials['materials'] ?? [];
- }
- return [];
- }
- /**
- * 获取升级条件
- *
- * 优先使用条件组,如果没有则使用 conditions 字段
- *
- * @return array
- */
- public function getUpgradeConditions(): array
- {
- // 如果有关联的条件组,则使用条件组服务获取条件
- if ($this->conditions && is_numeric($this->conditions)) {
- return \App\Module\Game\Services\ConditionGroupService::getConditionItems($this->conditions);
- }
- // 否则使用 conditions 字段
- if (is_string($this->conditions)) {
- return json_decode($this->conditions, true) ?? [];
- } elseif (is_array($this->conditions)) {
- return $this->conditions ?? [];
- }
- return [];
- }
- /**
- * 检查用户是否满足升级条件
- *
- * @param int $userId 用户ID
- * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息
- */
- public function checkUpgradeConditions(int $userId): array
- {
- // 如果有关联的条件组,则使用条件组检查
- if ($this->conditions && is_numeric($this->conditions)) {
- return \App\Module\Game\Services\ConditionService::checkCondition($userId, $this->conditions);
- }
- // 否则使用旧版本的条件检查逻辑
- $conditions = $this->getUpgradeConditions();
- // 如果没有条件,则默认满足
- if (empty($conditions)) {
- return [
- 'success' => true,
- 'message' => '没有条件限制'
- ];
- }
- // 检查房屋等级条件
- if (isset($conditions['house_level_min'])) {
- $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
- if (!$farmUser || $farmUser->house_level < $conditions['house_level_min']) {
- return [
- 'success' => false,
- 'message' => "房屋等级不足,需要 {$conditions['house_level_min']} 级,实际 " . ($farmUser ? $farmUser->house_level : 0) . " 级"
- ];
- }
- }
- // 检查特殊土地数量限制
- if (isset($conditions['special_land_check']) && $conditions['special_land_check']) {
- $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
- if (!$farmUser) {
- return [
- 'success' => false,
- 'message' => "用户信息不存在"
- ];
- }
- $specialLandCount = \App\Module\Farm\Models\FarmLand::where('user_id', $userId)
- ->whereIn('land_type', [
- \App\Module\Farm\Enums\LAND_TYPE::GOLD->value,
- \App\Module\Farm\Enums\LAND_TYPE::BLUE->value,
- \App\Module\Farm\Enums\LAND_TYPE::PURPLE->value
- ])
- ->count();
- $houseConfig = $farmUser->houseConfig;
- if ($specialLandCount >= $houseConfig->special_land_limit) {
- return [
- 'success' => false,
- 'message' => "特殊土地数量已达上限 {$houseConfig->special_land_limit}"
- ];
- }
- }
- // 其他条件检查...
- return [
- 'success' => true,
- 'message' => '满足所有条件'
- ];
- }
- }
|