PlayerDataHandler.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace App\Module\AppGame\Handler\Public;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Farm\Services\BuffService;
  5. use App\Module\Farm\Services\LandService;
  6. use App\Module\Fund\Services\AccountService;
  7. use App\Module\Game\Services\SkinService;
  8. use App\Module\GameItems\Services\ItemService;
  9. use App\Module\Pet\Services\PetService;
  10. use App\Module\User\Services\UserService;
  11. use Google\Protobuf\Internal\Message;
  12. use Uraus\Kku\Request\RequestPublicPlayerData;
  13. use Uraus\Kku\Response\ResponsePublicPlayerData;
  14. use Uraus\Kku\Response\PublicUserInfo;
  15. use Uraus\Kku\Response\PublicGod;
  16. use Uraus\Kku\Response\PublicLand;
  17. use Uraus\Kku\Response\PublicPet;
  18. use Uraus\Kku\Response\PublicZichan;
  19. /**
  20. * 处理玩家数据请求(公共数据)
  21. *
  22. */
  23. class PlayerDataHandler extends BaseHandler
  24. {
  25. /**
  26. * 是否需要登录
  27. * @var bool
  28. */
  29. protected bool $need_login = true;
  30. /**
  31. * 处理玩家数据请求
  32. *
  33. * @param RequestPublicPlayerData $data 玩家数据请求数据
  34. * @return ResponsePublicPlayerData 玩家数据响应
  35. */
  36. public function handle(Message $data): Message
  37. {
  38. // 获取请求的用户ID
  39. $targetUserId = $data->getUserId();
  40. // 创建响应对象
  41. $response = new ResponsePublicPlayerData();
  42. // 获取用户基本信息
  43. $this->setUserInfo($response, $targetUserId);
  44. // 获取神像数据
  45. $this->setGodsInfo($response, $targetUserId);
  46. // 获取土地数据
  47. $this->setLandInfo($response, $targetUserId);
  48. // 获取宠物数据
  49. $this->setPetInfo($response, $targetUserId);
  50. // 获取资产数据
  51. $this->setZichanInfo($response, $targetUserId);
  52. return $response;
  53. }
  54. /**
  55. * 设置用户基本信息
  56. *
  57. * @param ResponsePublicPlayerData $response
  58. * @param int $userId
  59. * @return void
  60. */
  61. private function setUserInfo(ResponsePublicPlayerData $response, int $userId): void
  62. {
  63. // 获取用户基本信息
  64. $userInfo = UserService::info($userId);
  65. $userDetailInfo = UserService::infoinfo($userId);
  66. if (!$userInfo || !$userDetailInfo) {
  67. return;
  68. }
  69. // 获取用户皮肤信息
  70. $currentSkinId = SkinService::getCurrentSkinId($userId);
  71. // 创建PublicUserInfo对象
  72. $publicUserInfo = new PublicUserInfo();
  73. $publicUserInfo->setUserD($userId); // 注意:protobuf中字段名是user_d
  74. $publicUserInfo->setSkinId($currentSkinId);
  75. $publicUserInfo->setAvatar($userDetailInfo->avatar ?? 0);
  76. $publicUserInfo->setNickName($userDetailInfo->nickname ?? '');
  77. $response->setUserInfo($publicUserInfo);
  78. }
  79. /**
  80. * 设置神像数据
  81. *
  82. * @param ResponsePublicPlayerData $response
  83. * @param int $userId
  84. * @return void
  85. */
  86. private function setGodsInfo(ResponsePublicPlayerData $response, int $userId): void
  87. {
  88. $godsInfo = [];
  89. // 遍历所有可能的神像类型(1-4)
  90. for ($godId = 1; $godId <= 4; $godId++) {
  91. $publicGod = new PublicGod();
  92. $publicGod->setId($godId);
  93. // 检查用户是否有该类型的有效神灵加持
  94. $activeBuff = BuffService::getActiveUserBuff($userId, $godId);
  95. $publicGod->setStatus($activeBuff !== null);
  96. $godsInfo[] = $publicGod;
  97. }
  98. $response->setGodsInfo($godsInfo);
  99. }
  100. /**
  101. * 设置土地数据
  102. *
  103. * @param ResponsePublicPlayerData $response
  104. * @param int $userId
  105. * @return void
  106. */
  107. private function setLandInfo(ResponsePublicPlayerData $response, int $userId): void
  108. {
  109. // 获取用户的所有土地
  110. $userLands = LandService::getUserLands($userId);
  111. $landInfo = [];
  112. foreach ($userLands as $landDto) {
  113. $publicLand = new PublicLand();
  114. $publicLand->setId($landDto->id);
  115. $publicLand->setIndex($landDto->position); // 使用position作为index
  116. $publicLand->setLevel($landDto->landType); // 使用landType作为level
  117. $publicLand->setStatus($landDto->status); // status已经是int类型
  118. // 设置种植相关信息
  119. if ($landDto->crop) {
  120. // 种植时间(使用plantTime字段)
  121. $plantTime = 0;
  122. if ($landDto->crop->plantTime) {
  123. if (is_string($landDto->crop->plantTime)) {
  124. $plantTime = strtotime($landDto->crop->plantTime);
  125. } elseif ($landDto->crop->plantTime instanceof \Carbon\Carbon) {
  126. $plantTime = $landDto->crop->plantTime->timestamp;
  127. } else {
  128. $plantTime = $landDto->crop->plantTime;
  129. }
  130. }
  131. $publicLand->setSeedPlantingTimes($plantTime);
  132. $publicLand->setSeedId($landDto->crop->seedId ?? 0);
  133. $publicLand->setFruitId($landDto->crop->finalOutputItemId ?? 0);
  134. $publicLand->setPlantId($landDto->crop->id ?? 0); // 使用作物ID作为植株ID
  135. $publicLand->setSeedStatus($landDto->crop->growthStage ?? 0);
  136. // 阶段开始时间
  137. $stageStartTime = 0;
  138. if ($landDto->crop->stageStartTime) {
  139. $stageStartTime = $landDto->crop->stageStartTime->timestamp ?? 0;
  140. }
  141. $publicLand->setStageStartTimes($stageStartTime);
  142. // 阶段结束时间
  143. $stageEndTime = 0;
  144. if ($landDto->crop->stageEndTime) {
  145. $stageEndTime = $landDto->crop->stageEndTime->timestamp ?? 0;
  146. }
  147. $publicLand->setStageNextTimes($stageEndTime);
  148. } else {
  149. $publicLand->setSeedPlantingTimes(0);
  150. $publicLand->setSeedId(0);
  151. $publicLand->setFruitId(0);
  152. $publicLand->setPlantId(0);
  153. $publicLand->setSeedStatus(0);
  154. $publicLand->setStageStartTimes(0);
  155. $publicLand->setStageNextTimes(0);
  156. }
  157. // 设置灾害信息(暂时为空数组)
  158. $publicLand->setDisasters([]);
  159. $landInfo[] = $publicLand;
  160. }
  161. $response->setLandInfo($landInfo);
  162. }
  163. /**
  164. * 设置宠物数据
  165. *
  166. * @param ResponsePublicPlayerData $response
  167. * @param int $userId
  168. * @return void
  169. */
  170. private function setPetInfo(ResponsePublicPlayerData $response, int $userId): void
  171. {
  172. try {
  173. // 获取用户的宠物列表
  174. $userPets = PetService::getUserPets($userId);
  175. // 如果用户没有宠物,则不设置宠物信息
  176. if ($userPets->isEmpty()) {
  177. return;
  178. }
  179. // 获取第一个宠物作为公共展示的宠物(通常是主宠物)
  180. $mainPet = $userPets->first();
  181. // 创建PublicPet对象
  182. $publicPet = new PublicPet();
  183. // 宠物种族ID:由于当前数据库设计中PetUser表没有type_id字段,
  184. // 我们使用默认值1(对应松狮),或者可以根据业务需求进行调整
  185. $typeId = 1; // 默认为松狮(ID=1)
  186. $publicPet->setTypeId($typeId);
  187. $publicPet->setName($mainPet->name ?? '');
  188. $publicPet->setLevel($mainPet->level ?? 1);
  189. $publicPet->setPower($mainPet->stamina ?? 0);
  190. $response->setPetInfo($publicPet);
  191. } catch (\Exception $e) {
  192. // 如果获取宠物信息失败,记录日志但不影响其他数据的返回
  193. \Illuminate\Support\Facades\Log::warning('获取用户宠物公共信息失败', [
  194. 'user_id' => $userId,
  195. 'error' => $e->getMessage()
  196. ]);
  197. }
  198. }
  199. /**
  200. * 设置资产数据
  201. *
  202. * @param ResponsePublicPlayerData $response
  203. * @param int $userId
  204. * @return void
  205. */
  206. private function setZichanInfo(ResponsePublicPlayerData $response, int $userId): void
  207. {
  208. try {
  209. // 创建PublicZichan对象
  210. $publicZichan = new PublicZichan();
  211. // 获取钻石余额(fund_id = 2)
  212. $diamondBalance = 0;
  213. try {
  214. $accounts = AccountService::list4user($userId);
  215. foreach ($accounts as $account) {
  216. if ($account->getFundId() == 2) { // 钻石的fund_id是2
  217. $diamondBalance = $account->balance;
  218. break;
  219. }
  220. }
  221. } catch (\Exception $e) {
  222. \Illuminate\Support\Facades\Log::warning('获取用户钻石余额失败', [
  223. 'user_id' => $userId,
  224. 'error' => $e->getMessage()
  225. ]);
  226. }
  227. // 获取建筑材料数量
  228. $woodQuantity = 0; // 木材 (item_id = 33)
  229. $stoneQuantity = 0; // 石材 (item_id = 34)
  230. $steelQuantity = 0; // 钢材 (item_id = 35)
  231. try {
  232. // 获取木材数量
  233. $woodItems = ItemService::getUserItems($userId, ['item_id' => 33]);
  234. $woodQuantity = $woodItems->sum('quantity');
  235. // 获取石材数量
  236. $stoneItems = ItemService::getUserItems($userId, ['item_id' => 34]);
  237. $stoneQuantity = $stoneItems->sum('quantity');
  238. // 获取钢材数量
  239. $steelItems = ItemService::getUserItems($userId, ['item_id' => 35]);
  240. $steelQuantity = $steelItems->sum('quantity');
  241. } catch (\Exception $e) {
  242. \Illuminate\Support\Facades\Log::warning('获取用户建筑材料数量失败', [
  243. 'user_id' => $userId,
  244. 'error' => $e->getMessage()
  245. ]);
  246. }
  247. // 设置资产信息
  248. $publicZichan->setZuanshi((float)$diamondBalance);
  249. $publicZichan->setMucai((float)$woodQuantity);
  250. $publicZichan->setShicai((float)$stoneQuantity);
  251. $publicZichan->setGangcai((float)$steelQuantity);
  252. $response->setZichan($publicZichan);
  253. } catch (\Exception $e) {
  254. // 如果获取资产信息失败,记录日志但不影响其他数据的返回
  255. \Illuminate\Support\Facades\Log::warning('获取用户资产公共信息失败', [
  256. 'user_id' => $userId,
  257. 'error' => $e->getMessage()
  258. ]);
  259. }
  260. }
  261. }