RewardCollectorLogic.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. /**
  4. * 奖励数据收集逻辑
  5. *
  6. * 使用静态变量收集本次请求中的所有奖励数据
  7. * 区别于LastData(变化但未同步的),这里收集的是本次请求的奖励
  8. */
  9. class RewardCollectorLogic
  10. {
  11. /**
  12. * 物品奖励数据,按用户分组
  13. * 格式:[user_id => [item_key => ['user_id' => int, 'item_id' => int, 'instance_id' => int, 'quantity' => int]]]
  14. *
  15. * @var array
  16. */
  17. private static array $itemRewards = [];
  18. /**
  19. * 代币奖励数据,按用户分组
  20. * 格式:[user_id => [coin_type => ['user_id' => int, 'type' => int, 'quantity' => int]]]
  21. *
  22. * @var array
  23. */
  24. private static array $coinRewards = [];
  25. /**
  26. * 神像奖励数据,按用户分组
  27. * 格式:[user_id => [god_type => ['user_id' => int, 'type' => int, 'diff' => int, 'quantity' => int]]]
  28. *
  29. * @var array
  30. */
  31. private static array $godRewards = [];
  32. /**
  33. * 添加物品奖励
  34. *
  35. * @param int $userId 用户ID
  36. * @param int $itemId 物品ID
  37. * @param int $instanceId 物品实例ID
  38. * @param int $quantity 奖励数量
  39. * @return void
  40. */
  41. public static function addItemReward(int $userId, int $itemId, int $instanceId, int $quantity): void
  42. {
  43. $key = "{$itemId}_{$instanceId}";
  44. if (!isset(self::$itemRewards[$userId])) {
  45. self::$itemRewards[$userId] = [];
  46. }
  47. if (isset(self::$itemRewards[$userId][$key])) {
  48. // 如果已存在,累加数量
  49. self::$itemRewards[$userId][$key]['quantity'] += $quantity;
  50. } else {
  51. // 新增奖励记录
  52. self::$itemRewards[$userId][$key] = [
  53. 'user_id' => $userId,
  54. 'item_id' => $itemId,
  55. 'instance_id' => $instanceId,
  56. 'quantity' => $quantity,
  57. ];
  58. }
  59. }
  60. /**
  61. * 添加代币奖励
  62. *
  63. * @param int $userId 用户ID
  64. * @param int $coinType 代币类型
  65. * @param int $quantity 奖励数量
  66. * @return void
  67. */
  68. public static function addCoinReward(int $userId, int $coinType, int $quantity): void
  69. {
  70. if (!isset(self::$coinRewards[$userId])) {
  71. self::$coinRewards[$userId] = [];
  72. }
  73. if (isset(self::$coinRewards[$userId][$coinType])) {
  74. // 如果已存在,累加数量
  75. self::$coinRewards[$userId][$coinType]['quantity'] += $quantity;
  76. } else {
  77. // 新增奖励记录
  78. self::$coinRewards[$userId][$coinType] = [
  79. 'user_id' => $userId,
  80. 'type' => $coinType,
  81. 'quantity' => $quantity,
  82. ];
  83. }
  84. }
  85. /**
  86. * 添加神像奖励
  87. *
  88. * @param int $userId 用户ID
  89. * @param int $godType 神像类型
  90. * @param int $diff 时间差值(秒)
  91. * @param int $quantity 奖励数量
  92. * @return void
  93. */
  94. public static function addGodReward(int $userId, int $godType, int $diff, int $quantity): void
  95. {
  96. if (!isset(self::$godRewards[$userId])) {
  97. self::$godRewards[$userId] = [];
  98. }
  99. if (isset(self::$godRewards[$userId][$godType])) {
  100. // 如果已存在,累加数量和时间
  101. self::$godRewards[$userId][$godType]['quantity'] += $quantity;
  102. self::$godRewards[$userId][$godType]['diff'] += $diff;
  103. } else {
  104. // 新增奖励记录
  105. self::$godRewards[$userId][$godType] = [
  106. 'user_id' => $userId,
  107. 'type' => $godType,
  108. 'diff' => $diff,
  109. 'quantity' => $quantity,
  110. ];
  111. }
  112. }
  113. /**
  114. * 获取本次请求的所有奖励数据
  115. *
  116. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  117. * @return array 奖励数据数组,包含items、coins和gods
  118. */
  119. public static function getRewards(?int $userId = null): array
  120. {
  121. if ($userId !== null) {
  122. // 返回指定用户的奖励数据
  123. return [
  124. 'items' => array_values(self::$itemRewards[$userId] ?? []),
  125. 'coins' => array_values(self::$coinRewards[$userId] ?? []),
  126. 'gods' => array_values(self::$godRewards[$userId] ?? []),
  127. ];
  128. }
  129. // 返回所有用户的奖励数据,合并为一个数组
  130. $allItems = [];
  131. $allCoins = [];
  132. $allGods = [];
  133. foreach (self::$itemRewards as $userItems) {
  134. $allItems = array_merge($allItems, array_values($userItems));
  135. }
  136. foreach (self::$coinRewards as $userCoins) {
  137. $allCoins = array_merge($allCoins, array_values($userCoins));
  138. }
  139. foreach (self::$godRewards as $userGods) {
  140. $allGods = array_merge($allGods, array_values($userGods));
  141. }
  142. return [
  143. 'items' => $allItems,
  144. 'coins' => $allCoins,
  145. 'gods' => $allGods,
  146. ];
  147. }
  148. /**
  149. * 清空本次请求的奖励数据
  150. *
  151. * @return void
  152. */
  153. public static function clearRewards(): void
  154. {
  155. self::$itemRewards = [];
  156. self::$coinRewards = [];
  157. self::$godRewards = [];
  158. }
  159. /**
  160. * 检查是否有奖励数据
  161. *
  162. * @param int|null $userId 用户ID,如果为null则检查所有用户
  163. * @return bool
  164. */
  165. public static function hasRewards(?int $userId = null): bool
  166. {
  167. if ($userId !== null) {
  168. return !empty(self::$itemRewards[$userId]) || !empty(self::$coinRewards[$userId]) || !empty(self::$godRewards[$userId]);
  169. }
  170. return !empty(self::$itemRewards) || !empty(self::$coinRewards) || !empty(self::$godRewards);
  171. }
  172. /**
  173. * 获取物品奖励数据
  174. *
  175. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  176. * @return array
  177. */
  178. public static function getItemRewards(?int $userId = null): array
  179. {
  180. if ($userId !== null) {
  181. return array_values(self::$itemRewards[$userId] ?? []);
  182. }
  183. $allItems = [];
  184. foreach (self::$itemRewards as $userItems) {
  185. $allItems = array_merge($allItems, array_values($userItems));
  186. }
  187. return $allItems;
  188. }
  189. /**
  190. * 获取代币奖励数据
  191. *
  192. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  193. * @return array
  194. */
  195. public static function getCoinRewards(?int $userId = null): array
  196. {
  197. if ($userId !== null) {
  198. return array_values(self::$coinRewards[$userId] ?? []);
  199. }
  200. $allCoins = [];
  201. foreach (self::$coinRewards as $userCoins) {
  202. $allCoins = array_merge($allCoins, array_values($userCoins));
  203. }
  204. return $allCoins;
  205. }
  206. /**
  207. * 获取神像奖励数据
  208. *
  209. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  210. * @return array
  211. */
  212. public static function getGodRewards(?int $userId = null): array
  213. {
  214. if ($userId !== null) {
  215. return array_values(self::$godRewards[$userId] ?? []);
  216. }
  217. $allGods = [];
  218. foreach (self::$godRewards as $userGods) {
  219. $allGods = array_merge($allGods, array_values($userGods));
  220. }
  221. return $allGods;
  222. }
  223. }