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($uinfo2->avatar);
  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. // 将土地数据转换为响应格式
  96. $landDataList = [];
  97. foreach ($lands as $land) {
  98. $landData = LandInfoDto::toDataLand($land);
  99. $landDataList[] = $landData;
  100. }
  101. $this->response->getLastData()->setLands($landDataList);
  102. }
  103. /**
  104. * 用户物品的数据
  105. *
  106. * @return void
  107. */
  108. private function getUserItems()
  109. {
  110. $itemService = new ItemService();
  111. $items = $itemService->getUserItems($this->user_id);
  112. $itemLs = [];
  113. foreach ($items as $item) {
  114. $li = new DataItem();
  115. if ($item->instance_id) {
  116. $li->setInstanceId($item->instance_id);
  117. }
  118. $li->setItemId($item->item_id);
  119. $li->setQuantity($item->quantity);
  120. $itemLs[] = $li;
  121. }
  122. $this->response->getLastData()->setItems($itemLs);
  123. }
  124. /**
  125. * 用户货币的数据
  126. *
  127. * @return void
  128. */
  129. private function getUserCoins()
  130. {
  131. $lsc = [];
  132. $coins = AccountService::list4user($this->user_id);
  133. foreach ($coins as $coin) {
  134. // dump($coin->fund_id);
  135. $dC = new DataCoin();
  136. $dC->setType($coin->fund_id);
  137. $dC->setQuantity($coin->balance);
  138. $lsc[] = $dC;
  139. }
  140. $this->response->getLastData()->setCoins($lsc);
  141. }
  142. /**
  143. * 获取用户神像数据
  144. *
  145. * @return void
  146. */
  147. private function getUserGods()
  148. {
  149. try {
  150. // 调用Farm模块的BuffService获取用户的所有神灵加持
  151. $buffs = BuffService::getUserBuffs($this->user_id);
  152. // 将神灵加持数据转换为响应格式
  153. $godDataList = [];
  154. foreach ($buffs as $buff) {
  155. // 创建Farm模块的GodBuffDto
  156. $godBuffDto = new FarmGodBuffDto();
  157. $godBuffDto->id = $buff->id;
  158. $godBuffDto->userId = $buff->user_id;
  159. $godBuffDto->buffType = $buff->buff_type;
  160. $godBuffDto->expireTime = $buff->expire_time ? $buff->expire_time->toDateTimeString() : null;
  161. // 转换为Protobuf的DataGod对象
  162. $dataGod = new DataGod();
  163. $dataGod->setId($godBuffDto->buffType);
  164. // 设置激活状态(如果有过期时间且未过期,则为激活状态)
  165. $isActive = !empty($godBuffDto->expireTime) && strtotime($godBuffDto->expireTime) > time();
  166. $dataGod->setStatus($isActive);
  167. // 设置有效期(转换为时间戳)
  168. if (!empty($godBuffDto->expireTime)) {
  169. $expireTime = strtotime($godBuffDto->expireTime);
  170. $dataGod->setVaidTime($expireTime);
  171. }
  172. $godDataList[] = $dataGod;
  173. }
  174. // 设置神灵加持数据
  175. $this->response->getLastData()->setGods($godDataList);
  176. } catch (\Exception $e) {
  177. // 记录错误日志
  178. Log::error('获取用户神灵加持数据失败', [
  179. 'user_id' => $this->user_id,
  180. 'error' => $e->getMessage(),
  181. 'trace' => $e->getTraceAsString()
  182. ]);
  183. }
  184. }
  185. /**
  186. * 获取用户宠物数据
  187. *
  188. * @return void
  189. */
  190. private function getUserPets()
  191. {
  192. try {
  193. // 调用Pet模块的PetService获取用户的所有宠物
  194. $pets = PetService::getUserPets($this->user_id);
  195. // 将宠物数据转换为响应格式
  196. $petDataList = [];
  197. foreach ($pets as $pet) {
  198. try {
  199. // 获取宠物详细信息
  200. $petDataDto = PetService::getPetStatus($this->user_id, $pet->id);
  201. // 转换为Protobuf的DataPet对象
  202. $dataPet = PetDataDto::toDataPet($petDataDto);
  203. $petDataList[] = $dataPet;
  204. } catch (\Exception $e) {
  205. // 记录错误日志
  206. Log::error('获取宠物详细信息失败', [
  207. 'user_id' => $this->user_id,
  208. 'pet_id' => $pet->id,
  209. 'error' => $e->getMessage()
  210. ]);
  211. continue;
  212. }
  213. }
  214. // 设置宠物数据
  215. $this->response->getLastData()->setPets($petDataList);
  216. } catch (\Exception $e) {
  217. // 记录错误日志
  218. Log::error('获取用户宠物数据失败', [
  219. 'user_id' => $this->user_id,
  220. 'error' => $e->getMessage(),
  221. 'trace' => $e->getTraceAsString()
  222. ]);
  223. }
  224. }
  225. }