DataHandler.php 8.4 KB

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