DeductCollectorLogic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. /**
  4. * 扣除数据收集逻辑
  5. *
  6. * 使用静态变量收集本次请求中的所有扣除数据
  7. * 区别于LastData(变化但未同步的),这里收集的是本次请求的扣除
  8. */
  9. class DeductCollectorLogic
  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 $itemDeducts = [];
  18. /**
  19. * 代币扣除数据,按用户分组
  20. * 格式:[user_id => [coin_type => ['user_id' => int, 'type' => int, 'quantity' => int]]]
  21. *
  22. * @var array
  23. */
  24. private static array $coinDeducts = [];
  25. /**
  26. * 添加物品扣除
  27. *
  28. * @param int $userId 用户ID
  29. * @param int $itemId 物品ID
  30. * @param int $instanceId 物品实例ID
  31. * @param int $quantity 扣除数量
  32. * @return void
  33. */
  34. public static function addItemDeduct(int $userId, int $itemId, int $instanceId, int $quantity): void
  35. {
  36. $key = "{$itemId}_{$instanceId}";
  37. if (!isset(self::$itemDeducts[$userId])) {
  38. self::$itemDeducts[$userId] = [];
  39. }
  40. if (isset(self::$itemDeducts[$userId][$key])) {
  41. // 如果已存在,累加数量
  42. self::$itemDeducts[$userId][$key]['quantity'] += $quantity;
  43. } else {
  44. // 新增扣除记录
  45. self::$itemDeducts[$userId][$key] = [
  46. 'user_id' => $userId,
  47. 'item_id' => $itemId,
  48. 'instance_id' => $instanceId,
  49. 'quantity' => $quantity,
  50. ];
  51. }
  52. }
  53. /**
  54. * 添加代币扣除
  55. *
  56. * @param int $userId 用户ID
  57. * @param int $coinType 代币类型
  58. * @param int $quantity 扣除数量
  59. * @return void
  60. */
  61. public static function addCoinDeduct(int $userId, int $coinType, int $quantity): void
  62. {
  63. if (!isset(self::$coinDeducts[$userId])) {
  64. self::$coinDeducts[$userId] = [];
  65. }
  66. if (isset(self::$coinDeducts[$userId][$coinType])) {
  67. // 如果已存在,累加数量
  68. self::$coinDeducts[$userId][$coinType]['quantity'] += $quantity;
  69. } else {
  70. // 新增扣除记录
  71. self::$coinDeducts[$userId][$coinType] = [
  72. 'user_id' => $userId,
  73. 'type' => $coinType,
  74. 'quantity' => $quantity,
  75. ];
  76. }
  77. }
  78. /**
  79. * 获取本次请求的所有扣除数据
  80. *
  81. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  82. * @return array 扣除数据数组,包含items和coins
  83. */
  84. public static function getDeducts(?int $userId = null): array
  85. {
  86. if ($userId !== null) {
  87. // 返回指定用户的扣除数据
  88. return [
  89. 'items' => array_values(self::$itemDeducts[$userId] ?? []),
  90. 'coins' => array_values(self::$coinDeducts[$userId] ?? []),
  91. ];
  92. }
  93. // 返回所有用户的扣除数据,合并为一个数组
  94. $allItems = [];
  95. $allCoins = [];
  96. foreach (self::$itemDeducts as $userItems) {
  97. $allItems = array_merge($allItems, array_values($userItems));
  98. }
  99. foreach (self::$coinDeducts as $userCoins) {
  100. $allCoins = array_merge($allCoins, array_values($userCoins));
  101. }
  102. return [
  103. 'items' => $allItems,
  104. 'coins' => $allCoins,
  105. ];
  106. }
  107. /**
  108. * 清空本次请求的扣除数据
  109. *
  110. * @return void
  111. */
  112. public static function clearDeducts(): void
  113. {
  114. self::$itemDeducts = [];
  115. self::$coinDeducts = [];
  116. }
  117. /**
  118. * 检查是否有扣除数据
  119. *
  120. * @param int|null $userId 用户ID,如果为null则检查所有用户
  121. * @return bool
  122. */
  123. public static function hasDeducts(?int $userId = null): bool
  124. {
  125. if ($userId !== null) {
  126. return !empty(self::$itemDeducts[$userId]) || !empty(self::$coinDeducts[$userId]);
  127. }
  128. return !empty(self::$itemDeducts) || !empty(self::$coinDeducts);
  129. }
  130. /**
  131. * 获取物品扣除数据
  132. *
  133. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  134. * @return array
  135. */
  136. public static function getItemDeducts(?int $userId = null): array
  137. {
  138. if ($userId !== null) {
  139. return array_values(self::$itemDeducts[$userId] ?? []);
  140. }
  141. $allItems = [];
  142. foreach (self::$itemDeducts as $userItems) {
  143. $allItems = array_merge($allItems, array_values($userItems));
  144. }
  145. return $allItems;
  146. }
  147. /**
  148. * 获取代币扣除数据
  149. *
  150. * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
  151. * @return array
  152. */
  153. public static function getCoinDeducts(?int $userId = null): array
  154. {
  155. if ($userId !== null) {
  156. return array_values(self::$coinDeducts[$userId] ?? []);
  157. }
  158. $allItems = [];
  159. foreach (self::$coinDeducts as $userCoins) {
  160. $allItems = array_merge($allItems, array_values($userCoins));
  161. }
  162. return $allItems;
  163. }
  164. }