RewardCollectorLogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. /**
  4. * 奖励数据收集逻辑
  5. *
  6. * 使用静态变量收集本次请求中的所有奖励数据
  7. * 区别于LastData(变化但未同步的),这里收集的是本次请求的奖励
  8. */
  9. class RewardCollectorLogic
  10. {
  11. /**
  12. * 物品奖励数据
  13. * 格式:[item_id => ['item_id' => int, 'instance_id' => int, 'quantity' => int]]
  14. *
  15. * @var array
  16. */
  17. private static array $itemRewards = [];
  18. /**
  19. * 代币奖励数据
  20. * 格式:[coin_type => ['type' => int, 'quantity' => int]]
  21. *
  22. * @var array
  23. */
  24. private static array $coinRewards = [];
  25. /**
  26. * 神像奖励数据
  27. * 格式:[god_type => ['type' => int, 'diff' => int, 'quantity' => int]]
  28. *
  29. * @var array
  30. */
  31. private static array $godRewards = [];
  32. /**
  33. * 添加物品奖励
  34. *
  35. * @param int $itemId 物品ID
  36. * @param int $instanceId 物品实例ID
  37. * @param int $quantity 奖励数量
  38. * @return void
  39. */
  40. public static function addItemReward(int $itemId, int $instanceId, int $quantity): void
  41. {
  42. $key = $itemId . '_' . $instanceId;
  43. if (isset(self::$itemRewards[$key])) {
  44. // 如果已存在,累加数量
  45. self::$itemRewards[$key]['quantity'] += $quantity;
  46. } else {
  47. // 新增奖励记录
  48. self::$itemRewards[$key] = [
  49. 'item_id' => $itemId,
  50. 'instance_id' => $instanceId,
  51. 'quantity' => $quantity,
  52. ];
  53. }
  54. }
  55. /**
  56. * 添加代币奖励
  57. *
  58. * @param int $coinType 代币类型
  59. * @param int $quantity 奖励数量
  60. * @return void
  61. */
  62. public static function addCoinReward(int $coinType, int $quantity): void
  63. {
  64. if (isset(self::$coinRewards[$coinType])) {
  65. // 如果已存在,累加数量
  66. self::$coinRewards[$coinType]['quantity'] += $quantity;
  67. } else {
  68. // 新增奖励记录
  69. self::$coinRewards[$coinType] = [
  70. 'type' => $coinType,
  71. 'quantity' => $quantity,
  72. ];
  73. }
  74. }
  75. /**
  76. * 添加神像奖励
  77. *
  78. * @param int $godType 神像类型
  79. * @param int $diff 时间差值(秒)
  80. * @param int $quantity 奖励数量
  81. * @return void
  82. */
  83. public static function addGodReward(int $godType, int $diff, int $quantity): void
  84. {
  85. if (isset(self::$godRewards[$godType])) {
  86. // 如果已存在,累加数量和时间
  87. self::$godRewards[$godType]['quantity'] += $quantity;
  88. self::$godRewards[$godType]['diff'] += $diff;
  89. } else {
  90. // 新增奖励记录
  91. self::$godRewards[$godType] = [
  92. 'type' => $godType,
  93. 'diff' => $diff,
  94. 'quantity' => $quantity,
  95. ];
  96. }
  97. }
  98. /**
  99. * 获取本次请求的所有奖励数据
  100. *
  101. * @return array 奖励数据数组,包含items、coins和gods
  102. */
  103. public static function getRewards(): array
  104. {
  105. return [
  106. 'items' => array_values(self::$itemRewards),
  107. 'coins' => array_values(self::$coinRewards),
  108. 'gods' => array_values(self::$godRewards),
  109. ];
  110. }
  111. /**
  112. * 清空本次请求的奖励数据
  113. *
  114. * @return void
  115. */
  116. public static function clearRewards(): void
  117. {
  118. self::$itemRewards = [];
  119. self::$coinRewards = [];
  120. self::$godRewards = [];
  121. }
  122. /**
  123. * 检查是否有奖励数据
  124. *
  125. * @return bool
  126. */
  127. public static function hasRewards(): bool
  128. {
  129. return !empty(self::$itemRewards) || !empty(self::$coinRewards) || !empty(self::$godRewards);
  130. }
  131. /**
  132. * 获取物品奖励数据
  133. *
  134. * @return array
  135. */
  136. public static function getItemRewards(): array
  137. {
  138. return array_values(self::$itemRewards);
  139. }
  140. /**
  141. * 获取代币奖励数据
  142. *
  143. * @return array
  144. */
  145. public static function getCoinRewards(): array
  146. {
  147. return array_values(self::$coinRewards);
  148. }
  149. /**
  150. * 获取神像奖励数据
  151. *
  152. * @return array
  153. */
  154. public static function getGodRewards(): array
  155. {
  156. return array_values(self::$godRewards);
  157. }
  158. }