DataHandler.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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->instance_id);
  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. $godBuffDto->expireTime = $buff->expire_time ? $buff->expire_time->toDateTimeString() : null;
  162. // 转换为Protobuf的DataGod对象
  163. $dataGod = new DataGod();
  164. $dataGod->setId($godBuffDto->buffType);
  165. // 设置激活状态(如果有过期时间且未过期,则为激活状态)
  166. $isActive = !empty($godBuffDto->expireTime) && strtotime($godBuffDto->expireTime) > time();
  167. $dataGod->setStatus($isActive);
  168. // 设置有效期(转换为时间戳)
  169. if (!empty($godBuffDto->expireTime)) {
  170. $expireTime = strtotime($godBuffDto->expireTime);
  171. $dataGod->setVaidTime($expireTime);
  172. }
  173. $godDataList[] = $dataGod;
  174. }
  175. // 设置神灵加持数据
  176. $this->response->getLastData()->setGods($godDataList);
  177. } catch (\Exception $e) {
  178. // 记录错误日志
  179. Log::error('获取用户神灵加持数据失败', [
  180. 'user_id' => $this->user_id,
  181. 'error' => $e->getMessage(),
  182. 'trace' => $e->getTraceAsString()
  183. ]);
  184. }
  185. }
  186. /**
  187. * 获取用户宠物数据
  188. *
  189. * @return void
  190. */
  191. private function getUserPets()
  192. {
  193. try {
  194. // 调用Pet模块的PetService获取用户的所有宠物
  195. $pets = PetService::getUserPets($this->user_id);
  196. // 将宠物数据转换为响应格式
  197. $petDataList = [];
  198. foreach ($pets as $pet) {
  199. try {
  200. // 获取宠物详细信息
  201. $petDataDto = PetService::getPetStatus($this->user_id, $pet->id);
  202. // 转换为Protobuf的DataPet对象
  203. $dataPet = PetDataDto::toDataPet($petDataDto);
  204. $petDataList[] = $dataPet;
  205. } catch (\Exception $e) {
  206. // 记录错误日志
  207. Log::error('获取宠物详细信息失败', [
  208. 'user_id' => $this->user_id,
  209. 'pet_id' => $pet->id,
  210. 'error' => $e->getMessage()
  211. ]);
  212. continue;
  213. }
  214. }
  215. // 设置宠物数据
  216. $this->response->getLastData()->setPets($petDataList);
  217. } catch (\Exception $e) {
  218. // 记录错误日志
  219. Log::error('获取用户宠物数据失败', [
  220. 'user_id' => $this->user_id,
  221. 'error' => $e->getMessage(),
  222. 'trace' => $e->getTraceAsString()
  223. ]);
  224. }
  225. }
  226. }