DataHandler.php 9.0 KB

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