PetTempService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Game\Dtos\PetDataSimpleTempDto;
  4. use App\Module\Game\Dtos\PetStatusTempDto;
  5. use App\Module\Game\Logics\PetTemp;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 宠物临时数据服务类
  9. *
  10. * 提供宠物临时数据相关的服务方法,用于外部调用
  11. */
  12. class PetTempService
  13. {
  14. /**
  15. * 获取用户的宠物创建临时数据
  16. *
  17. * @param int $userId 用户ID
  18. * @return PetStatusTempDto[] 用户的宠物创建数据
  19. */
  20. public function getUserPetCreated(int $userId): array
  21. {
  22. try {
  23. return PetTemp::getUserPetCreated($userId);
  24. } catch (\Exception $e) {
  25. Log::error('获取用户宠物创建临时数据失败', [
  26. 'error' => $e->getMessage(),
  27. 'user_id' => $userId,
  28. ]);
  29. return [];
  30. }
  31. }
  32. /**
  33. * 获取用户特定宠物的创建临时数据
  34. *
  35. * @param int $userId 用户ID
  36. * @param int $petId 宠物ID
  37. * @return PetStatusTempDto|null 宠物创建数据,不存在时返回null
  38. */
  39. public function getUserPetCreatedById(int $userId, int $petId): ?PetStatusTempDto
  40. {
  41. try {
  42. return PetTemp::getUserPetCreatedById($userId, $petId);
  43. } catch (\Exception $e) {
  44. Log::error('获取用户特定宠物创建临时数据失败', [
  45. 'error' => $e->getMessage(),
  46. 'user_id' => $userId,
  47. 'pet_id' => $petId,
  48. ]);
  49. return null;
  50. }
  51. }
  52. /**
  53. * 获取用户的宠物状态变更临时数据
  54. *
  55. * @param int $userId 用户ID
  56. * @return PetDataSimpleTempDto[] 用户的宠物状态变更数据
  57. */
  58. public function getUserPetStatus(int $userId): array
  59. {
  60. try {
  61. return PetTemp::getUserPetStatus($userId);
  62. } catch (\Exception $e) {
  63. Log::error('获取用户宠物状态变更临时数据失败', [
  64. 'error' => $e->getMessage(),
  65. 'user_id' => $userId,
  66. ]);
  67. return [];
  68. }
  69. }
  70. /**
  71. * 获取用户特定宠物的状态变更临时数据
  72. *
  73. * @param int $userId 用户ID
  74. * @param int $petId 宠物ID
  75. * @return PetDataSimpleTempDto|null 宠物状态变更数据,不存在时返回null
  76. */
  77. public function getUserPetStatusById(int $userId, int $petId): ?PetDataSimpleTempDto
  78. {
  79. try {
  80. return PetTemp::getUserPetStatusById($userId, $petId);
  81. } catch (\Exception $e) {
  82. Log::error('获取用户特定宠物状态变更临时数据失败', [
  83. 'error' => $e->getMessage(),
  84. 'user_id' => $userId,
  85. 'pet_id' => $petId,
  86. ]);
  87. return null;
  88. }
  89. }
  90. /**
  91. * 获取用户的宠物更新临时数据
  92. *
  93. * @param int $userId 用户ID
  94. * @return PetStatusTempDto[] 用户的宠物更新数据
  95. */
  96. public function getUserPetUpdates(int $userId): array
  97. {
  98. try {
  99. return PetTemp::getUserPetUpdates($userId);
  100. } catch (\Exception $e) {
  101. Log::error('获取用户宠物更新临时数据失败', [
  102. 'error' => $e->getMessage(),
  103. 'user_id' => $userId,
  104. ]);
  105. return [];
  106. }
  107. }
  108. /**
  109. * 获取用户特定宠物的更新临时数据
  110. *
  111. * @param int $userId 用户ID
  112. * @param int $petId 宠物ID
  113. * @return PetStatusTempDto|null 宠物更新数据,不存在时返回null
  114. */
  115. public function getUserPetUpdateById(int $userId, int $petId): ?PetStatusTempDto
  116. {
  117. try {
  118. return PetTemp::getUserPetUpdateById($userId, $petId);
  119. } catch (\Exception $e) {
  120. Log::error('获取用户特定宠物更新临时数据失败', [
  121. 'error' => $e->getMessage(),
  122. 'user_id' => $userId,
  123. 'pet_id' => $petId,
  124. ]);
  125. return null;
  126. }
  127. }
  128. /**
  129. * 清除用户的宠物创建临时数据
  130. *
  131. * @param int $userId 用户ID
  132. * @return bool 操作是否成功
  133. */
  134. public function clearUserPetCreated(int $userId): bool
  135. {
  136. try {
  137. PetTemp::clearUserPetCreated($userId);
  138. return true;
  139. } catch (\Exception $e) {
  140. Log::error('清除用户宠物创建临时数据失败', [
  141. 'error' => $e->getMessage(),
  142. 'user_id' => $userId,
  143. ]);
  144. return false;
  145. }
  146. }
  147. /**
  148. * 清除用户的宠物状态变更临时数据
  149. *
  150. * @param int $userId 用户ID
  151. * @return bool 操作是否成功
  152. */
  153. public function clearUserPetStatus(int $userId): bool
  154. {
  155. try {
  156. PetTemp::clearUserPetStatus($userId);
  157. return true;
  158. } catch (\Exception $e) {
  159. Log::error('清除用户宠物状态变更临时数据失败', [
  160. 'error' => $e->getMessage(),
  161. 'user_id' => $userId,
  162. ]);
  163. return false;
  164. }
  165. }
  166. /**
  167. * 清除用户的所有宠物临时数据
  168. *
  169. * @param int $userId 用户ID
  170. * @return bool 操作是否成功
  171. */
  172. public function clearUserAllPetTemp(int $userId): bool
  173. {
  174. try {
  175. PetTemp::clearUserAllPetTemp($userId);
  176. return true;
  177. } catch (\Exception $e) {
  178. Log::error('清除用户所有宠物临时数据失败', [
  179. 'error' => $e->getMessage(),
  180. 'user_id' => $userId,
  181. ]);
  182. return false;
  183. }
  184. }
  185. }