ConditionService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Fund\Logic\User as FundLogic;
  4. use App\Module\Game\Enums\CONDITION_OPERATOR;
  5. use App\Module\Game\Enums\CONDITION_TYPE;
  6. use App\Module\Game\Models\GameConditionGroup;
  7. use App\Module\Game\Models\GameConditionItem;
  8. use App\Module\GameItems\Services\ItemService;
  9. use App\Module\Pet\Services\PetService;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * 条件服务类
  13. *
  14. * 提供条件组相关的服务,包括检查用户是否满足条件等功能
  15. */
  16. class ConditionService
  17. {
  18. /**
  19. * 检查用户是否满足条件组的条件
  20. *
  21. * @param int $userId 用户ID
  22. * @param string|int $conditionGroupCode 条件组编码或ID
  23. * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息
  24. */
  25. public static function checkCondition(int $userId, $conditionGroupCode): array
  26. {
  27. try {
  28. // 获取条件组
  29. $conditionGroup = is_numeric($conditionGroupCode)
  30. ? GameConditionGroup::find($conditionGroupCode)
  31. : GameConditionGroup::where('code', $conditionGroupCode)->first();
  32. if (!$conditionGroup) {
  33. return [
  34. 'success' => false,
  35. 'message' => "条件组不存在: {$conditionGroupCode}"
  36. ];
  37. }
  38. // 获取条件组中的所有条件项
  39. $conditionItems = $conditionGroup->conditionItems;
  40. if ($conditionItems->isEmpty()) {
  41. return [
  42. 'success' => true,
  43. 'message' => '条件组中没有条件项'
  44. ];
  45. }
  46. // 根据逻辑类型检查条件
  47. $logicType = $conditionGroup->logic_type;
  48. $failedConditions = [];
  49. foreach ($conditionItems as $item) {
  50. $checkResult = self::checkConditionItem($userId, $item);
  51. if ($logicType == GameConditionGroup::LOGIC_TYPE_ALL && !$checkResult['success']) {
  52. // 全部满足模式下,有一个不满足就返回失败
  53. return [
  54. 'success' => false,
  55. 'message' => $checkResult['message'],
  56. 'condition_item' => $item->id,
  57. 'details' => $checkResult
  58. ];
  59. } elseif ($logicType == GameConditionGroup::LOGIC_TYPE_ANY && $checkResult['success']) {
  60. // 任一满足模式下,有一个满足就返回成功
  61. return [
  62. 'success' => true,
  63. 'message' => '满足条件',
  64. 'condition_item' => $item->id,
  65. 'details' => $checkResult
  66. ];
  67. }
  68. if (!$checkResult['success']) {
  69. $failedConditions[] = [
  70. 'condition_item' => $item->id,
  71. 'details' => $checkResult
  72. ];
  73. }
  74. }
  75. // 处理最终结果
  76. if ($logicType == GameConditionGroup::LOGIC_TYPE_ALL) {
  77. // 全部满足模式下,走到这里说明全部满足
  78. return [
  79. 'success' => true,
  80. 'message' => '满足所有条件'
  81. ];
  82. } else {
  83. // 任一满足模式下,走到这里说明没有一个满足
  84. return [
  85. 'success' => false,
  86. 'message' => '不满足任何条件',
  87. 'failed_conditions' => $failedConditions
  88. ];
  89. }
  90. } catch (\Exception $e) {
  91. Log::error('检查条件失败', [
  92. 'user_id' => $userId,
  93. 'condition_group' => $conditionGroupCode,
  94. 'error' => $e->getMessage()
  95. ]);
  96. return [
  97. 'success' => false,
  98. 'message' => '检查条件时发生错误: ' . $e->getMessage()
  99. ];
  100. }
  101. }
  102. /**
  103. * 检查单个条件项
  104. *
  105. * @param int $userId 用户ID
  106. * @param GameConditionItem $conditionItem 条件项
  107. * @return array 检查结果
  108. */
  109. protected static function checkConditionItem(int $userId, GameConditionItem $conditionItem): array
  110. {
  111. switch ($conditionItem->condition_type) {
  112. case CONDITION_TYPE::LAND_LEVEL->value:
  113. return self::checkLandLevelCondition($userId, $conditionItem);
  114. case CONDITION_TYPE::HOUSE_LEVEL->value:
  115. return self::checkHouseLevelCondition($userId, $conditionItem);
  116. case CONDITION_TYPE::PET_LEVEL->value:
  117. return self::checkPetLevelCondition($userId, $conditionItem);
  118. case CONDITION_TYPE::ITEM_COUNT->value:
  119. return self::checkItemCountCondition($userId, $conditionItem);
  120. case CONDITION_TYPE::CURRENCY_COUNT->value:
  121. return self::checkCurrencyCountCondition($userId, $conditionItem);
  122. default:
  123. return [
  124. 'success' => false,
  125. 'message' => "不支持的条件类型: {$conditionItem->condition_type}"
  126. ];
  127. }
  128. }
  129. /**
  130. * 检查土地等级条件
  131. *
  132. * @param int $userId 用户ID
  133. * @param GameConditionItem $conditionItem 条件项
  134. * @return array 检查结果
  135. */
  136. protected static function checkLandLevelCondition(int $userId, GameConditionItem $conditionItem): array
  137. {
  138. try {
  139. // 获取用户指定类型的土地
  140. $landType = $conditionItem->target_id;
  141. $lands = \App\Module\Farm\Services\LandService::getUserLands($userId);
  142. // 计算指定类型的土地数量
  143. $count = 0;
  144. foreach ($lands as $land) {
  145. if ($land->landType == $landType) {
  146. $count++;
  147. }
  148. }
  149. // 执行比较
  150. $result = CONDITION_OPERATOR::compare($count, $conditionItem->value, $conditionItem->operator);
  151. if ($result) {
  152. return [
  153. 'success' => true,
  154. 'message' => "土地等级条件满足",
  155. 'actual' => $count,
  156. 'expected' => $conditionItem->value,
  157. 'operator' => $conditionItem->getOperatorSymbol()
  158. ];
  159. } else {
  160. return [
  161. 'success' => false,
  162. 'message' => "土地等级条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$count}",
  163. 'actual' => $count,
  164. 'expected' => $conditionItem->value,
  165. 'operator' => $conditionItem->getOperatorSymbol()
  166. ];
  167. }
  168. } catch (\Exception $e) {
  169. Log::error('检查土地等级条件失败', [
  170. 'user_id' => $userId,
  171. 'condition_item' => $conditionItem->id,
  172. 'error' => $e->getMessage()
  173. ]);
  174. return [
  175. 'success' => false,
  176. 'message' => '检查土地等级条件时发生错误: ' . $e->getMessage()
  177. ];
  178. }
  179. }
  180. /**
  181. * 检查房屋等级条件
  182. *
  183. * @param int $userId 用户ID
  184. * @param GameConditionItem $conditionItem 条件项
  185. * @return array 检查结果
  186. */
  187. protected static function checkHouseLevelCondition(int $userId, GameConditionItem $conditionItem): array
  188. {
  189. try {
  190. // 获取用户房屋等级
  191. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  192. if (!$farmUser) {
  193. return [
  194. 'success' => false,
  195. 'message' => "用户没有房屋"
  196. ];
  197. }
  198. $houseLevel = $farmUser->house_level;
  199. // 执行比较
  200. $result = CONDITION_OPERATOR::compare($houseLevel, $conditionItem->value, $conditionItem->operator);
  201. if ($result) {
  202. return [
  203. 'success' => true,
  204. 'message' => "房屋等级条件满足",
  205. 'actual' => $houseLevel,
  206. 'expected' => $conditionItem->value,
  207. 'operator' => $conditionItem->getOperatorSymbol()
  208. ];
  209. } else {
  210. return [
  211. 'success' => false,
  212. 'message' => "房屋等级条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$houseLevel}",
  213. 'actual' => $houseLevel,
  214. 'expected' => $conditionItem->value,
  215. 'operator' => $conditionItem->getOperatorSymbol()
  216. ];
  217. }
  218. } catch (\Exception $e) {
  219. Log::error('检查房屋等级条件失败', [
  220. 'user_id' => $userId,
  221. 'condition_item' => $conditionItem->id,
  222. 'error' => $e->getMessage()
  223. ]);
  224. return [
  225. 'success' => false,
  226. 'message' => '检查房屋等级条件时发生错误: ' . $e->getMessage()
  227. ];
  228. }
  229. }
  230. /**
  231. * 检查宠物等级条件
  232. *
  233. * @param int $userId 用户ID
  234. * @param GameConditionItem $conditionItem 条件项
  235. * @return array 检查结果
  236. */
  237. protected static function checkPetLevelCondition(int $userId, GameConditionItem $conditionItem): array
  238. {
  239. try {
  240. // 获取用户宠物
  241. $petId = $conditionItem->target_id;
  242. $userPet = PetService::getUserPet($userId, $petId);
  243. if (!$userPet) {
  244. return [
  245. 'success' => false,
  246. 'message' => "用户没有该宠物"
  247. ];
  248. }
  249. $petLevel = $userPet->level;
  250. // 执行比较
  251. $result = CONDITION_OPERATOR::compare($petLevel, $conditionItem->value, $conditionItem->operator);
  252. if ($result) {
  253. return [
  254. 'success' => true,
  255. 'message' => "宠物等级条件满足",
  256. 'actual' => $petLevel,
  257. 'expected' => $conditionItem->value,
  258. 'operator' => $conditionItem->getOperatorSymbol()
  259. ];
  260. } else {
  261. return [
  262. 'success' => false,
  263. 'message' => "宠物等级条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$petLevel}",
  264. 'actual' => $petLevel,
  265. 'expected' => $conditionItem->value,
  266. 'operator' => $conditionItem->getOperatorSymbol()
  267. ];
  268. }
  269. } catch (\Exception $e) {
  270. Log::error('检查宠物等级条件失败', [
  271. 'user_id' => $userId,
  272. 'condition_item' => $conditionItem->id,
  273. 'error' => $e->getMessage()
  274. ]);
  275. return [
  276. 'success' => false,
  277. 'message' => '检查宠物等级条件时发生错误: ' . $e->getMessage()
  278. ];
  279. }
  280. }
  281. /**
  282. * 检查物品持有数条件
  283. *
  284. * @param int $userId 用户ID
  285. * @param GameConditionItem $conditionItem 条件项
  286. * @return array 检查结果
  287. */
  288. protected static function checkItemCountCondition(int $userId, GameConditionItem $conditionItem): array
  289. {
  290. try {
  291. // 获取用户物品
  292. $itemId = $conditionItem->target_id;
  293. $userItems = ItemService::getUserItems($userId, ['item_id' => $itemId]);
  294. // 计算物品总数
  295. $totalQuantity = 0;
  296. foreach ($userItems as $userItem) {
  297. $totalQuantity += $userItem->quantity;
  298. }
  299. // 执行比较
  300. $result = CONDITION_OPERATOR::compare($totalQuantity, $conditionItem->value, $conditionItem->operator);
  301. if ($result) {
  302. return [
  303. 'success' => true,
  304. 'message' => "物品持有数条件满足",
  305. 'actual' => $totalQuantity,
  306. 'expected' => $conditionItem->value,
  307. 'operator' => $conditionItem->getOperatorSymbol()
  308. ];
  309. } else {
  310. return [
  311. 'success' => false,
  312. 'message' => "物品持有数条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$totalQuantity}",
  313. 'actual' => $totalQuantity,
  314. 'expected' => $conditionItem->value,
  315. 'operator' => $conditionItem->getOperatorSymbol()
  316. ];
  317. }
  318. } catch (\Exception $e) {
  319. Log::error('检查物品持有数条件失败', [
  320. 'user_id' => $userId,
  321. 'condition_item' => $conditionItem->id,
  322. 'error' => $e->getMessage()
  323. ]);
  324. return [
  325. 'success' => false,
  326. 'message' => '检查物品持有数条件时发生错误: ' . $e->getMessage()
  327. ];
  328. }
  329. }
  330. /**
  331. * 检查代币持有数条件
  332. *
  333. * @param int $userId 用户ID
  334. * @param GameConditionItem $conditionItem 条件项
  335. * @return array 检查结果
  336. */
  337. protected static function checkCurrencyCountCondition(int $userId, GameConditionItem $conditionItem): array
  338. {
  339. try {
  340. // 获取用户货币账户
  341. $currencyId = $conditionItem->target_id;
  342. $account = FundLogic::get_account($userId, $currencyId);
  343. // 检查账户是否存在
  344. if ($account === false) {
  345. return [
  346. 'success' => false,
  347. 'message' => "用户没有该货币账户"
  348. ];
  349. }
  350. $balance = $account->balance;
  351. // 执行比较
  352. $result = CONDITION_OPERATOR::compare($balance, $conditionItem->value, $conditionItem->operator);
  353. if ($result) {
  354. return [
  355. 'success' => true,
  356. 'message' => "代币持有数条件满足",
  357. 'actual' => $balance,
  358. 'expected' => $conditionItem->value,
  359. 'operator' => $conditionItem->getOperatorSymbol()
  360. ];
  361. } else {
  362. return [
  363. 'success' => false,
  364. 'message' => "代币持有数条件不满足,需要 {$conditionItem->getOperatorSymbol()} {$conditionItem->value},实际 {$balance}",
  365. 'actual' => $balance,
  366. 'expected' => $conditionItem->value,
  367. 'operator' => $conditionItem->getOperatorSymbol()
  368. ];
  369. }
  370. } catch (\Exception $e) {
  371. Log::error('检查代币持有数条件失败', [
  372. 'user_id' => $userId,
  373. 'condition_item' => $conditionItem->id,
  374. 'error' => $e->getMessage()
  375. ]);
  376. return [
  377. 'success' => false,
  378. 'message' => '检查代币持有数条件时发生错误: ' . $e->getMessage()
  379. ];
  380. }
  381. }
  382. }