DataHandler.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. if($item->quantity <= 0){
  135. continue;
  136. }
  137. $li->setQuantity($item->quantity);
  138. // 设置堆id(item_user表的id)
  139. $li->setIuId($item->id);
  140. // 设置过期时间
  141. if ($item->expireAt) {
  142. $expireTime = strtotime($item->expireAt);
  143. $li->setExpireTime($expireTime);
  144. }
  145. // 设置冻结状态
  146. $li->setIsFrozen($item->isFrozen);
  147. $itemLs[] = $li;
  148. }
  149. $this->response->getLastData()->setItems($itemLs);
  150. }
  151. /**
  152. * 用户货币的数据
  153. *
  154. * @return void
  155. */
  156. private function getUserCoins()
  157. {
  158. $lsc = [];
  159. $coins = AccountService::list4user($this->user_id);
  160. foreach ($coins as $coin) {
  161. // dump($coin->fund_id);
  162. $dC = new DataCoin();
  163. $dC->setType($coin->getFundId());
  164. $dC->setQuantity($coin->balance);
  165. $lsc[] = $dC;
  166. }
  167. $this->response->getLastData()->setCoins($lsc);
  168. }
  169. /**
  170. * 获取用户神像数据
  171. *
  172. * @return void
  173. */
  174. private function getUserGods()
  175. {
  176. try {
  177. // 调用Farm模块的BuffService获取用户的所有神灵加持
  178. $buffs = BuffService::getUserBuffs($this->user_id);
  179. // 将神灵加持数据转换为响应格式
  180. $godDataList = [];
  181. foreach ($buffs as $buff) {
  182. // 创建Farm模块的GodBuffDto
  183. $godBuffDto = new FarmGodBuffDto();
  184. $godBuffDto->id = $buff->id;
  185. $godBuffDto->userId = $buff->user_id;
  186. $godBuffDto->buffType = $buff->buff_type;
  187. // 安全地处理过期时间,确保正确的类型转换
  188. if ($buff->expire_time) {
  189. if (is_string($buff->expire_time)) {
  190. $godBuffDto->expireTime = $buff->expire_time;
  191. } elseif ($buff->expire_time instanceof \Carbon\Carbon) {
  192. $godBuffDto->expireTime = $buff->expire_time->toDateTimeString();
  193. } else {
  194. $godBuffDto->expireTime = (string)$buff->expire_time;
  195. }
  196. } else {
  197. $godBuffDto->expireTime = null;
  198. }
  199. // 转换为Protobuf的DataGod对象
  200. $dataGod = new DataGod();
  201. $dataGod->setId($godBuffDto->buffType);
  202. // 设置激活状态(如果有过期时间且未过期,则为激活状态)
  203. $isActive = !empty($godBuffDto->expireTime) && strtotime($godBuffDto->expireTime) > time();
  204. $dataGod->setStatus($isActive);
  205. // 设置有效期(转换为时间戳)
  206. if (!empty($godBuffDto->expireTime)) {
  207. $expireTime = strtotime($godBuffDto->expireTime);
  208. $dataGod->setVaidTime($expireTime);
  209. }
  210. $godDataList[] = $dataGod;
  211. }
  212. // 设置神灵加持数据
  213. $this->response->getLastData()->setGods($godDataList);
  214. } catch (\Exception $e) {
  215. // 记录错误日志
  216. Log::error('获取用户神灵加持数据失败', [
  217. 'user_id' => $this->user_id,
  218. 'error' => $e->getMessage(),
  219. 'trace' => $e->getTraceAsString()
  220. ]);
  221. }
  222. }
  223. /**
  224. * 获取用户宠物数据
  225. *
  226. * @return void
  227. */
  228. private function getUserPets()
  229. {
  230. try {
  231. // 调用Pet模块的PetService获取用户的所有宠物
  232. $pets = PetService::getUserPets($this->user_id);
  233. // 将宠物数据转换为响应格式
  234. $petDataList = [];
  235. foreach ($pets as $pet) {
  236. try {
  237. // 获取宠物详细信息
  238. $petDataDto = PetService::getPetStatus($this->user_id, $pet->id);
  239. // 转换为Protobuf的DataPet对象
  240. $dataPet = PetDataDto::toDataPet($petDataDto);
  241. $petDataList[] = $dataPet;
  242. } catch (\Exception $e) {
  243. // 记录错误日志
  244. Log::error('获取宠物详细信息失败', [
  245. 'user_id' => $this->user_id,
  246. 'pet_id' => $pet->id,
  247. 'error' => $e->getMessage()
  248. ]);
  249. continue;
  250. }
  251. }
  252. // 设置宠物数据
  253. $this->response->getLastData()->setPets($petDataList);
  254. } catch (\Exception $e) {
  255. // 记录错误日志
  256. Log::error('获取用户宠物数据失败', [
  257. 'user_id' => $this->user_id,
  258. 'error' => $e->getMessage(),
  259. 'trace' => $e->getTraceAsString()
  260. ]);
  261. }
  262. }
  263. }