| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace App\Module\Game\Logics;
- /**
- * 奖励数据收集逻辑
- *
- * 使用静态变量收集本次请求中的所有奖励数据
- * 区别于LastData(变化但未同步的),这里收集的是本次请求的奖励
- */
- class RewardCollectorLogic
- {
- /**
- * 物品奖励数据
- * 格式:[item_id => ['item_id' => int, 'instance_id' => int, 'quantity' => int]]
- *
- * @var array
- */
- private static array $itemRewards = [];
- /**
- * 代币奖励数据
- * 格式:[coin_type => ['type' => int, 'quantity' => int]]
- *
- * @var array
- */
- private static array $coinRewards = [];
- /**
- * 添加物品奖励
- *
- * @param int $itemId 物品ID
- * @param int $instanceId 物品实例ID
- * @param int $quantity 奖励数量
- * @return void
- */
- public static function addItemReward(int $itemId, int $instanceId, int $quantity): void
- {
- $key = $itemId . '_' . $instanceId;
-
- if (isset(self::$itemRewards[$key])) {
- // 如果已存在,累加数量
- self::$itemRewards[$key]['quantity'] += $quantity;
- } else {
- // 新增奖励记录
- self::$itemRewards[$key] = [
- 'item_id' => $itemId,
- 'instance_id' => $instanceId,
- 'quantity' => $quantity,
- ];
- }
- }
- /**
- * 添加代币奖励
- *
- * @param int $coinType 代币类型
- * @param int $quantity 奖励数量
- * @return void
- */
- public static function addCoinReward(int $coinType, int $quantity): void
- {
- if (isset(self::$coinRewards[$coinType])) {
- // 如果已存在,累加数量
- self::$coinRewards[$coinType]['quantity'] += $quantity;
- } else {
- // 新增奖励记录
- self::$coinRewards[$coinType] = [
- 'type' => $coinType,
- 'quantity' => $quantity,
- ];
- }
- }
- /**
- * 获取本次请求的所有奖励数据
- *
- * @return array 奖励数据数组,包含items和coins
- */
- public static function getRewards(): array
- {
- return [
- 'items' => array_values(self::$itemRewards),
- 'coins' => array_values(self::$coinRewards),
- ];
- }
- /**
- * 清空本次请求的奖励数据
- *
- * @return void
- */
- public static function clearRewards(): void
- {
- self::$itemRewards = [];
- self::$coinRewards = [];
- }
- /**
- * 检查是否有奖励数据
- *
- * @return bool
- */
- public static function hasRewards(): bool
- {
- return !empty(self::$itemRewards) || !empty(self::$coinRewards);
- }
- /**
- * 获取物品奖励数据
- *
- * @return array
- */
- public static function getItemRewards(): array
- {
- return array_values(self::$itemRewards);
- }
- /**
- * 获取代币奖励数据
- *
- * @return array
- */
- public static function getCoinRewards(): array
- {
- return array_values(self::$coinRewards);
- }
- }
|