DataHandler.php 9.1 KB

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