| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?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)) {
- // 获取消耗组
- $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($this->materials);
- if ($consumeGroup && $consumeGroup->consumeItems) {
- $materials = [];
- foreach ($consumeGroup->consumeItems as $item) {
- if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
- // 获取物品信息
- $itemInfo = \App\Module\GameItems\Models\Item::find($item->target_id);
- $materials[] = [
- 'item_id' => $item->target_id,
- 'item_name' => $itemInfo ? $itemInfo->name : "物品 {$item->target_id}",
- 'amount' => $item->quantity
- ];
- } elseif ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value) {
- // 获取货币信息
- $currencyInfo = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
- $materials[] = [
- 'currency_id' => $item->target_id,
- 'currency_name' => $currencyInfo ? $currencyInfo->name : "货币 {$item->target_id}",
- 'amount' => $item->quantity
- ];
- } elseif ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::FUND->value) {
- // 获取代币账户信息
- $fundInfo = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
- $materials[] = [
- 'fund_id' => $item->target_id,
- 'fund_name' => $fundInfo ? $fundInfo->name : "代币账户 {$item->target_id}",
- 'amount' => $item->quantity
- ];
- }
- }
- return $materials;
- }
- }
- // 否则使用 materials 字段
- if (is_string($this->materials)) {
- return json_decode($this->materials, true)['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)) {
- // 获取条件组
- $conditionGroup = \App\Module\Game\Models\GameConditionGroup::find($this->conditions);
- if ($conditionGroup) {
- $conditions = [
- 'conditions_group_id' => $this->conditions,
- 'conditions_group_code' => $conditionGroup->code,
- 'conditions_group_name' => $conditionGroup->name,
- 'logic_type' => $conditionGroup->logic_type,
- 'items' => []
- ];
- // 获取条件项
- if ($conditionGroup->conditionItems) {
- foreach ($conditionGroup->conditionItems as $item) {
- $conditionItem = [
- 'id' => $item->id,
- 'condition_type' => $item->condition_type,
- 'target_id' => $item->target_id,
- 'operator' => $item->operator,
- 'value' => $item->value
- ];
- // 根据条件类型获取目标名称
- if ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::LAND_LEVEL->value) {
- $landType = \App\Module\Farm\Models\FarmLandType::find($item->target_id);
- $conditionItem['target_name'] = $landType ? $landType->name : "土地类型 {$item->target_id}";
- } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::HOUSE_LEVEL->value) {
- $conditionItem['target_name'] = "房屋";
- } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::PET_LEVEL->value) {
- $pet = \App\Module\Pet\Models\PetConfig::find($item->target_id);
- $conditionItem['target_name'] = $pet ? $pet->name : "宠物 {$item->target_id}";
- } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::ITEM_COUNT->value) {
- $itemInfo = \App\Module\GameItems\Models\Item::find($item->target_id);
- $conditionItem['target_name'] = $itemInfo ? $itemInfo->name : "物品 {$item->target_id}";
- } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::CURRENCY_COUNT->value) {
- $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
- $conditionItem['target_name'] = $currency ? $currency->name : "货币 {$item->target_id}";
- } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::FUND_COUNT->value) {
- $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
- $conditionItem['target_name'] = $fund ? $fund->name : "代币账户 {$item->target_id}";
- }
- $conditions['items'][] = $conditionItem;
- }
- }
- return $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' => '满足所有条件'
- ];
- }
- }
|