FarmLandUpgradeConfig.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // 获取消耗组
  115. $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($this->materials);
  116. if ($consumeGroup && $consumeGroup->consumeItems) {
  117. $materials = [];
  118. foreach ($consumeGroup->consumeItems as $item) {
  119. if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
  120. // 获取物品信息
  121. $itemInfo = \App\Module\GameItems\Models\Item::find($item->target_id);
  122. $materials[] = [
  123. 'item_id' => $item->target_id,
  124. 'item_name' => $itemInfo ? $itemInfo->name : "物品 {$item->target_id}",
  125. 'amount' => $item->quantity
  126. ];
  127. } elseif ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value) {
  128. // 获取货币信息
  129. $currencyInfo = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
  130. $materials[] = [
  131. 'currency_id' => $item->target_id,
  132. 'currency_name' => $currencyInfo ? $currencyInfo->name : "货币 {$item->target_id}",
  133. 'amount' => $item->quantity
  134. ];
  135. } elseif ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::FUND->value) {
  136. // 获取代币账户信息
  137. $fundInfo = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
  138. $materials[] = [
  139. 'fund_id' => $item->target_id,
  140. 'fund_name' => $fundInfo ? $fundInfo->name : "代币账户 {$item->target_id}",
  141. 'amount' => $item->quantity
  142. ];
  143. }
  144. }
  145. return $materials;
  146. }
  147. }
  148. // 否则使用 materials 字段
  149. if (is_string($this->materials)) {
  150. return json_decode($this->materials, true)['materials'] ?? [];
  151. } elseif (is_array($this->materials)) {
  152. return $this->materials['materials'] ?? [];
  153. }
  154. return [];
  155. }
  156. /**
  157. * 获取升级条件
  158. *
  159. * 优先使用条件组,如果没有则使用 conditions 字段
  160. *
  161. * @return array
  162. */
  163. public function getUpgradeConditions(): array
  164. {
  165. // 如果有关联的条件组,则使用条件组
  166. if ($this->conditions && is_numeric($this->conditions)) {
  167. // 获取条件组
  168. $conditionGroup = \App\Module\Game\Models\GameConditionGroup::find($this->conditions);
  169. if ($conditionGroup) {
  170. $conditions = [
  171. 'conditions_group_id' => $this->conditions,
  172. 'conditions_group_code' => $conditionGroup->code,
  173. 'conditions_group_name' => $conditionGroup->name,
  174. 'logic_type' => $conditionGroup->logic_type,
  175. 'items' => []
  176. ];
  177. // 获取条件项
  178. if ($conditionGroup->conditionItems) {
  179. foreach ($conditionGroup->conditionItems as $item) {
  180. $conditionItem = [
  181. 'id' => $item->id,
  182. 'condition_type' => $item->condition_type,
  183. 'target_id' => $item->target_id,
  184. 'operator' => $item->operator,
  185. 'value' => $item->value
  186. ];
  187. // 根据条件类型获取目标名称
  188. if ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::LAND_LEVEL->value) {
  189. $landType = \App\Module\Farm\Models\FarmLandType::find($item->target_id);
  190. $conditionItem['target_name'] = $landType ? $landType->name : "土地类型 {$item->target_id}";
  191. } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::HOUSE_LEVEL->value) {
  192. $conditionItem['target_name'] = "房屋";
  193. } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::PET_LEVEL->value) {
  194. $pet = \App\Module\Pet\Models\PetConfig::find($item->target_id);
  195. $conditionItem['target_name'] = $pet ? $pet->name : "宠物 {$item->target_id}";
  196. } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::ITEM_COUNT->value) {
  197. $itemInfo = \App\Module\GameItems\Models\Item::find($item->target_id);
  198. $conditionItem['target_name'] = $itemInfo ? $itemInfo->name : "物品 {$item->target_id}";
  199. } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::CURRENCY_COUNT->value) {
  200. $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
  201. $conditionItem['target_name'] = $currency ? $currency->name : "货币 {$item->target_id}";
  202. } elseif ($item->condition_type == \App\Module\Game\Enums\CONDITION_TYPE::FUND_COUNT->value) {
  203. $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
  204. $conditionItem['target_name'] = $fund ? $fund->name : "代币账户 {$item->target_id}";
  205. }
  206. $conditions['items'][] = $conditionItem;
  207. }
  208. }
  209. return $conditions;
  210. }
  211. }
  212. // 否则使用 conditions 字段
  213. if (is_string($this->conditions)) {
  214. return json_decode($this->conditions, true) ?? [];
  215. } elseif (is_array($this->conditions)) {
  216. return $this->conditions ?? [];
  217. }
  218. return [];
  219. }
  220. /**
  221. * 检查用户是否满足升级条件
  222. *
  223. * @param int $userId 用户ID
  224. * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息
  225. */
  226. public function checkUpgradeConditions(int $userId): array
  227. {
  228. // 如果有关联的条件组,则使用条件组检查
  229. if ($this->conditions && is_numeric($this->conditions)) {
  230. return \App\Module\Game\Services\ConditionService::checkCondition($userId, $this->conditions);
  231. }
  232. // 否则使用旧版本的条件检查逻辑
  233. $conditions = $this->getUpgradeConditions();
  234. // 如果没有条件,则默认满足
  235. if (empty($conditions)) {
  236. return [
  237. 'success' => true,
  238. 'message' => '没有条件限制'
  239. ];
  240. }
  241. // 检查房屋等级条件
  242. if (isset($conditions['house_level_min'])) {
  243. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  244. if (!$farmUser || $farmUser->house_level < $conditions['house_level_min']) {
  245. return [
  246. 'success' => false,
  247. 'message' => "房屋等级不足,需要 {$conditions['house_level_min']} 级,实际 " . ($farmUser ? $farmUser->house_level : 0) . " 级"
  248. ];
  249. }
  250. }
  251. // 检查特殊土地数量限制
  252. if (isset($conditions['special_land_check']) && $conditions['special_land_check']) {
  253. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  254. if (!$farmUser) {
  255. return [
  256. 'success' => false,
  257. 'message' => "用户信息不存在"
  258. ];
  259. }
  260. $specialLandCount = \App\Module\Farm\Models\FarmLand::where('user_id', $userId)
  261. ->whereIn('land_type', [
  262. \App\Module\Farm\Enums\LAND_TYPE::GOLD->value,
  263. \App\Module\Farm\Enums\LAND_TYPE::BLUE->value,
  264. \App\Module\Farm\Enums\LAND_TYPE::PURPLE->value
  265. ])
  266. ->count();
  267. $houseConfig = $farmUser->houseConfig;
  268. if ($specialLandCount >= $houseConfig->special_land_limit) {
  269. return [
  270. 'success' => false,
  271. 'message' => "特殊土地数量已达上限 {$houseConfig->special_land_limit}"
  272. ];
  273. }
  274. }
  275. // 其他条件检查...
  276. return [
  277. 'success' => true,
  278. 'message' => '满足所有条件'
  279. ];
  280. }
  281. }