DataHandler.php 8.7 KB

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