DataHandler.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace App\Module\AppGame\Handler\User;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\AppGame\Proto\FarmInfoDto;
  5. use App\Module\AppGame\Proto\GodBuffDto;
  6. use App\Module\AppGame\Proto\LandInfoDto;
  7. use App\Module\AppGame\Proto\PetDataDto;
  8. use App\Module\Farm\Dtos\GodBuffDto as FarmGodBuffDto;
  9. use App\Module\Farm\Services\BuffService;
  10. use App\Module\Farm\Services\FarmService;
  11. use App\Module\Farm\Services\LandService;
  12. use App\Module\Fund\Services\AccountService;
  13. use App\Module\GameItems\Services\ItemService;
  14. use App\Module\Pet\Services\PetService;
  15. use App\Module\User\Services\UserService;
  16. use Google\Protobuf\Internal\Message;
  17. use Illuminate\Support\Facades\Log;
  18. use Uraus\Kku\Common\DataCoin;
  19. use Uraus\Kku\Common\DataGod;
  20. use Uraus\Kku\Common\DataItem;
  21. use Uraus\Kku\Common\DataLand;
  22. use Uraus\Kku\Common\DataPet;
  23. use Uraus\Kku\Common\LastData;
  24. use Uraus\Kku\Request\RequestUserData;
  25. use Uraus\Kku\Response\ResponseUserData;
  26. use Uraus\Kku\Response\UserInfo;
  27. /**
  28. * 处理用户信息请求
  29. */
  30. class DataHandler extends BaseHandler
  31. {
  32. /**
  33. * 是否需要登录
  34. *
  35. * @var bool
  36. */
  37. protected bool $need_login = true;
  38. /**
  39. * 处理用户信息请求
  40. *
  41. * @param RequestUserData $data 用户信息请求数据
  42. * @return ResponseUserData 用户信息响应
  43. */
  44. public function handle(Message $data): Message
  45. {
  46. $lastData = new LastData();
  47. $this->response->setLastData($lastData);
  48. // 创建响应对象
  49. $uinfo = UserService::info($this->user_id);
  50. $uinfo2 = UserService::infoinfo($this->user_id);
  51. $response = new ResponseUserData();
  52. $info = new UserInfo();
  53. $info->setMobile($uinfo->username);
  54. $info->setUserId($uinfo->id);
  55. $info->setSkinId(1);
  56. $info->setAvatar($uinfo2->avatar);
  57. $info->setNickName($uinfo2->nickname);
  58. $response->setInfo($info);
  59. // 激活 物品 lastData
  60. $this->getUserItems();
  61. // 激活 货币 lastData
  62. $this->getUserCoins();
  63. // 房屋 的 lastData
  64. $this->getUserHouse();
  65. // 土地 的 lastData
  66. $this->getUserLands();
  67. // 神像数据
  68. $this->getUserGods();
  69. // 宠物数据
  70. $this->getUserPets();
  71. return $response;
  72. }
  73. /**
  74. * 用户房屋数据
  75. *
  76. * @return void
  77. */
  78. private function getUserHouse()
  79. {
  80. $farmInfo = FarmService::getOrInitializeFarm($this->user_id);
  81. // 或者使用getOrInitializeFarm方法,如果用户没有农场信息,会自动初始化
  82. // 直接转换为DataHourse对象
  83. $dataHourse = FarmInfoDto::toDataHourse($farmInfo);
  84. $this->response->getLastData()->setHourse($dataHourse);
  85. }
  86. /**
  87. * 用户土地数据
  88. *
  89. * @return void
  90. */
  91. private function getUserLands()
  92. {
  93. // 调用Farm模块的LandService获取用户的所有土地
  94. $lands = LandService::getUserLands($this->user_id);
  95. // dd($lands);
  96. // 将土地数据转换为响应格式
  97. $landDataList = [];
  98. foreach ($lands as $land) {
  99. $landData = LandInfoDto::toDataLand($land);
  100. $landDataList[] = $landData;
  101. }
  102. $this->response->getLastData()->setLands($landDataList);
  103. }
  104. /**
  105. * 用户物品的数据
  106. *
  107. * @return void
  108. */
  109. private function getUserItems()
  110. {
  111. $itemService = new ItemService();
  112. $items = $itemService->getUserItems($this->user_id);
  113. $itemLs = [];
  114. foreach ($items as $item) {
  115. $li = new DataItem();
  116. if ($item->instanceId) {
  117. $li->setInstanceId($item->instanceId);
  118. }
  119. $li->setItemId($item->itemId);
  120. $li->setQuantity($item->quantity);
  121. $itemLs[] = $li;
  122. }
  123. $this->response->getLastData()->setItems($itemLs);
  124. }
  125. /**
  126. * 用户货币的数据
  127. *
  128. * @return void
  129. */
  130. private function getUserCoins()
  131. {
  132. $lsc = [];
  133. $coins = AccountService::list4user($this->user_id);
  134. foreach ($coins as $coin) {
  135. // dump($coin->fund_id);
  136. $dC = new DataCoin();
  137. $dC->setType($coin->getFundId());
  138. $dC->setQuantity($coin->balance);
  139. $lsc[] = $dC;
  140. }
  141. $this->response->getLastData()->setCoins($lsc);
  142. }
  143. /**
  144. * 获取用户神像数据
  145. *
  146. * @return void
  147. */
  148. private function getUserGods()
  149. {
  150. try {
  151. // 调用Farm模块的BuffService获取用户的所有神灵加持
  152. $buffs = BuffService::getUserBuffs($this->user_id);
  153. // 将神灵加持数据转换为响应格式
  154. $godDataList = [];
  155. foreach ($buffs as $buff) {
  156. // 创建Farm模块的GodBuffDto
  157. $godBuffDto = new FarmGodBuffDto();
  158. $godBuffDto->id = $buff->id;
  159. $godBuffDto->userId = $buff->user_id;
  160. $godBuffDto->buffType = $buff->buff_type;
  161. // 安全地处理过期时间,确保正确的类型转换
  162. if ($buff->expire_time) {
  163. if (is_string($buff->expire_time)) {
  164. $godBuffDto->expireTime = $buff->expire_time;
  165. } elseif ($buff->expire_time instanceof \Carbon\Carbon) {
  166. $godBuffDto->expireTime = $buff->expire_time->toDateTimeString();
  167. } else {
  168. $godBuffDto->expireTime = (string)$buff->expire_time;
  169. }
  170. } else {
  171. $godBuffDto->expireTime = null;
  172. }
  173. // 转换为Protobuf的DataGod对象
  174. $dataGod = new DataGod();
  175. $dataGod->setId($godBuffDto->buffType);
  176. // 设置激活状态(如果有过期时间且未过期,则为激活状态)
  177. $isActive = !empty($godBuffDto->expireTime) && strtotime($godBuffDto->expireTime) > time();
  178. $dataGod->setStatus($isActive);
  179. // 设置有效期(转换为时间戳)
  180. if (!empty($godBuffDto->expireTime)) {
  181. $expireTime = strtotime($godBuffDto->expireTime);
  182. $dataGod->setVaidTime($expireTime);
  183. }
  184. $godDataList[] = $dataGod;
  185. }
  186. // 设置神灵加持数据
  187. $this->response->getLastData()->setGods($godDataList);
  188. } catch (\Exception $e) {
  189. // 记录错误日志
  190. Log::error('获取用户神灵加持数据失败', [
  191. 'user_id' => $this->user_id,
  192. 'error' => $e->getMessage(),
  193. 'trace' => $e->getTraceAsString()
  194. ]);
  195. }
  196. }
  197. /**
  198. * 获取用户宠物数据
  199. *
  200. * @return void
  201. */
  202. private function getUserPets()
  203. {
  204. try {
  205. // 调用Pet模块的PetService获取用户的所有宠物
  206. $pets = PetService::getUserPets($this->user_id);
  207. // 将宠物数据转换为响应格式
  208. $petDataList = [];
  209. foreach ($pets as $pet) {
  210. try {
  211. // 获取宠物详细信息
  212. $petDataDto = PetService::getPetStatus($this->user_id, $pet->id);
  213. // 转换为Protobuf的DataPet对象
  214. $dataPet = PetDataDto::toDataPet($petDataDto);
  215. $petDataList[] = $dataPet;
  216. } catch (\Exception $e) {
  217. // 记录错误日志
  218. Log::error('获取宠物详细信息失败', [
  219. 'user_id' => $this->user_id,
  220. 'pet_id' => $pet->id,
  221. 'error' => $e->getMessage()
  222. ]);
  223. continue;
  224. }
  225. }
  226. // 设置宠物数据
  227. $this->response->getLastData()->setPets($petDataList);
  228. } catch (\Exception $e) {
  229. // 记录错误日志
  230. Log::error('获取用户宠物数据失败', [
  231. 'user_id' => $this->user_id,
  232. 'error' => $e->getMessage(),
  233. 'trace' => $e->getTraceAsString()
  234. ]);
  235. }
  236. }
  237. }