FarmLandUpgradeConfig.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use App\Module\Game\Models\GameConsumeGroup;
  6. use App\Module\Game\Models\GameConditionGroup;
  7. /**
  8. * 土地升级配置模型
  9. * field start
  10. * @property int $id 主键ID
  11. * @property int $from_type_id 起始土地类型ID
  12. * @property int $to_type_id 目标土地类型ID
  13. * @property int $materials 消耗组ID,关联game_consume_groups表
  14. * @property int $conditions 条件组ID,关联game_condition_groups表
  15. * @property \Carbon\Carbon $created_at 创建时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. */
  19. class FarmLandUpgradeConfig extends Model
  20. {
  21. /**
  22. * 与模型关联的表名
  23. *
  24. * @var string
  25. */
  26. protected $table = 'farm_land_upgrade_configs';
  27. /**
  28. * 可批量赋值的属性
  29. *
  30. * @var array
  31. */
  32. // attribute start
  33. protected $fillable = [
  34. 'from_type_id',
  35. 'to_type_id',
  36. 'materials',
  37. 'conditions',
  38. ];
  39. // attribute end
  40. /**
  41. * 应该被转换为原生类型的属性
  42. *
  43. * @var array
  44. */
  45. protected $casts = [
  46. ];
  47. /**
  48. * 获取起始土地类型
  49. *
  50. * @return BelongsTo
  51. */
  52. public function fromType(): BelongsTo
  53. {
  54. return $this->belongsTo(FarmLandType::class, 'from_type_id', 'id');
  55. }
  56. /**
  57. * 获取目标土地类型
  58. *
  59. * @return BelongsTo
  60. */
  61. public function toType(): BelongsTo
  62. {
  63. return $this->belongsTo(FarmLandType::class, 'to_type_id', 'id');
  64. }
  65. /**
  66. * 获取关联的消耗组
  67. *
  68. * @return BelongsTo
  69. */
  70. public function materialsGroup(): BelongsTo
  71. {
  72. return $this->belongsTo(GameConsumeGroup::class, 'materials_group_id', 'id');
  73. }
  74. /**
  75. * 获取关联的条件组
  76. *
  77. * @return BelongsTo
  78. */
  79. public function conditionsGroup(): BelongsTo
  80. {
  81. return $this->belongsTo(GameConditionGroup::class, 'conditions_group_id', 'id');
  82. }
  83. /**
  84. * 获取关联的消耗组(兼容旧代码)
  85. *
  86. * @return BelongsTo
  87. * @deprecated 使用 materialsGroup() 替代
  88. */
  89. public function consumeGroup(): BelongsTo
  90. {
  91. return $this->materialsGroup();
  92. }
  93. /**
  94. * 获取关联的条件组(兼容旧代码)
  95. *
  96. * @return BelongsTo
  97. * @deprecated 使用 conditionsGroup() 替代
  98. */
  99. public function conditionGroup(): BelongsTo
  100. {
  101. return $this->conditionsGroup();
  102. }
  103. /**
  104. * 获取升级所需材料
  105. *
  106. * 优先使用消耗组,如果没有则使用 materials 字段
  107. *
  108. * @return array
  109. */
  110. public function getUpgradeMaterials(): array
  111. {
  112. // 如果有关联的消耗组,则使用消耗组服务获取材料
  113. if ($this->materials && is_numeric($this->materials)) {
  114. return \App\Module\Game\Services\ConsumeGroupService::getConsumeMaterials($this->materials);
  115. }
  116. // 否则使用 materials 字段
  117. if (is_string($this->materials)) {
  118. $decoded = json_decode($this->materials, true);
  119. return $decoded['materials'] ?? [];
  120. } elseif (is_array($this->materials)) {
  121. return $this->materials['materials'] ?? [];
  122. }
  123. return [];
  124. }
  125. /**
  126. * 获取升级条件
  127. *
  128. * 优先使用条件组,如果没有则使用 conditions 字段
  129. *
  130. * @return array
  131. */
  132. public function getUpgradeConditions(): array
  133. {
  134. // 如果有关联的条件组,则使用条件组服务获取条件
  135. if ($this->conditions && is_numeric($this->conditions)) {
  136. return \App\Module\Game\Services\ConditionGroupService::getConditionItems($this->conditions);
  137. }
  138. // 否则使用 conditions 字段
  139. if (is_string($this->conditions)) {
  140. return json_decode($this->conditions, true) ?? [];
  141. } elseif (is_array($this->conditions)) {
  142. return $this->conditions ?? [];
  143. }
  144. return [];
  145. }
  146. /**
  147. * 检查用户是否满足升级条件
  148. *
  149. * @param int $userId 用户ID
  150. * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息
  151. */
  152. public function checkUpgradeConditions(int $userId): array
  153. {
  154. // 如果有关联的条件组,则使用条件组检查
  155. if ($this->conditions && is_numeric($this->conditions)) {
  156. return \App\Module\Game\Services\ConditionService::checkCondition($userId, $this->conditions);
  157. }
  158. // 否则使用旧版本的条件检查逻辑
  159. $conditions = $this->getUpgradeConditions();
  160. // 如果没有条件,则默认满足
  161. if (empty($conditions)) {
  162. return [
  163. 'success' => true,
  164. 'message' => '没有条件限制'
  165. ];
  166. }
  167. // 检查房屋等级条件
  168. if (isset($conditions['house_level_min'])) {
  169. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  170. if (!$farmUser || $farmUser->house_level < $conditions['house_level_min']) {
  171. return [
  172. 'success' => false,
  173. 'message' => "房屋等级不足,需要 {$conditions['house_level_min']} 级,实际 " . ($farmUser ? $farmUser->house_level : 0) . " 级"
  174. ];
  175. }
  176. }
  177. // 检查特殊土地数量限制
  178. if (isset($conditions['special_land_check']) && $conditions['special_land_check']) {
  179. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  180. if (!$farmUser) {
  181. return [
  182. 'success' => false,
  183. 'message' => "用户信息不存在"
  184. ];
  185. }
  186. $specialLandCount = \App\Module\Farm\Models\FarmLand::where('user_id', $userId)
  187. ->whereIn('land_type', [
  188. \App\Module\Farm\Enums\LAND_TYPE::GOLD->value,
  189. \App\Module\Farm\Enums\LAND_TYPE::BLUE->value,
  190. \App\Module\Farm\Enums\LAND_TYPE::PURPLE->value
  191. ])
  192. ->count();
  193. $houseConfig = $farmUser->houseConfig;
  194. if ($specialLandCount >= $houseConfig->special_land_limit) {
  195. return [
  196. 'success' => false,
  197. 'message' => "特殊土地数量已达上限 {$houseConfig->special_land_limit}"
  198. ];
  199. }
  200. }
  201. // 其他条件检查...
  202. return [
  203. 'success' => true,
  204. 'message' => '满足所有条件'
  205. ];
  206. }
  207. }