RewardCollectorLogic.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. *
  28. * @param int $itemId 物品ID
  29. * @param int $instanceId 物品实例ID
  30. * @param int $quantity 奖励数量
  31. * @return void
  32. */
  33. public static function addItemReward(int $itemId, int $instanceId, int $quantity): void
  34. {
  35. $key = $itemId . '_' . $instanceId;
  36. if (isset(self::$itemRewards[$key])) {
  37. // 如果已存在,累加数量
  38. self::$itemRewards[$key]['quantity'] += $quantity;
  39. } else {
  40. // 新增奖励记录
  41. self::$itemRewards[$key] = [
  42. 'item_id' => $itemId,
  43. 'instance_id' => $instanceId,
  44. 'quantity' => $quantity,
  45. ];
  46. }
  47. }
  48. /**
  49. * 添加代币奖励
  50. *
  51. * @param int $coinType 代币类型
  52. * @param int $quantity 奖励数量
  53. * @return void
  54. */
  55. public static function addCoinReward(int $coinType, int $quantity): void
  56. {
  57. if (isset(self::$coinRewards[$coinType])) {
  58. // 如果已存在,累加数量
  59. self::$coinRewards[$coinType]['quantity'] += $quantity;
  60. } else {
  61. // 新增奖励记录
  62. self::$coinRewards[$coinType] = [
  63. 'type' => $coinType,
  64. 'quantity' => $quantity,
  65. ];
  66. }
  67. }
  68. /**
  69. * 获取本次请求的所有奖励数据
  70. *
  71. * @return array 奖励数据数组,包含items和coins
  72. */
  73. public static function getRewards(): array
  74. {
  75. return [
  76. 'items' => array_values(self::$itemRewards),
  77. 'coins' => array_values(self::$coinRewards),
  78. ];
  79. }
  80. /**
  81. * 清空本次请求的奖励数据
  82. *
  83. * @return void
  84. */
  85. public static function clearRewards(): void
  86. {
  87. self::$itemRewards = [];
  88. self::$coinRewards = [];
  89. }
  90. /**
  91. * 检查是否有奖励数据
  92. *
  93. * @return bool
  94. */
  95. public static function hasRewards(): bool
  96. {
  97. return !empty(self::$itemRewards) || !empty(self::$coinRewards);
  98. }
  99. /**
  100. * 获取物品奖励数据
  101. *
  102. * @return array
  103. */
  104. public static function getItemRewards(): array
  105. {
  106. return array_values(self::$itemRewards);
  107. }
  108. /**
  109. * 获取代币奖励数据
  110. *
  111. * @return array
  112. */
  113. public static function getCoinRewards(): array
  114. {
  115. return array_values(self::$coinRewards);
  116. }
  117. }