| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace App\Module\Game\Logics;
- /**
- * 奖励数据收集逻辑
- *
- * 使用静态变量收集本次请求中的所有奖励数据
- * 区别于LastData(变化但未同步的),这里收集的是本次请求的奖励
- */
- class RewardCollectorLogic
- {
- /**
- * 物品奖励数据,按用户分组
- * 格式:[user_id => [item_key => ['user_id' => int, 'item_id' => int, 'instance_id' => int, 'quantity' => int]]]
- *
- * @var array
- */
- private static array $itemRewards = [];
- /**
- * 代币奖励数据,按用户分组
- * 格式:[user_id => [coin_type => ['user_id' => int, 'type' => int, 'quantity' => int]]]
- *
- * @var array
- */
- private static array $coinRewards = [];
- /**
- * 神像奖励数据,按用户分组
- * 格式:[user_id => [god_type => ['user_id' => int, 'type' => int, 'diff' => int, 'quantity' => int]]]
- *
- * @var array
- */
- private static array $godRewards = [];
- /**
- * 添加物品奖励
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param int $instanceId 物品实例ID
- * @param int $quantity 奖励数量
- * @return void
- */
- public static function addItemReward(int $userId, int $itemId, int $instanceId, int $quantity): void
- {
- $key = "{$itemId}_{$instanceId}";
- if (!isset(self::$itemRewards[$userId])) {
- self::$itemRewards[$userId] = [];
- }
- if (isset(self::$itemRewards[$userId][$key])) {
- // 如果已存在,累加数量
- self::$itemRewards[$userId][$key]['quantity'] += $quantity;
- } else {
- // 新增奖励记录
- self::$itemRewards[$userId][$key] = [
- 'user_id' => $userId,
- 'item_id' => $itemId,
- 'instance_id' => $instanceId,
- 'quantity' => $quantity,
- ];
- }
- }
- /**
- * 添加代币奖励
- *
- * @param int $userId 用户ID
- * @param int $coinType 代币类型
- * @param int $quantity 奖励数量
- * @return void
- */
- public static function addCoinReward(int $userId, int $coinType, int $quantity): void
- {
- if (!isset(self::$coinRewards[$userId])) {
- self::$coinRewards[$userId] = [];
- }
- if (isset(self::$coinRewards[$userId][$coinType])) {
- // 如果已存在,累加数量
- self::$coinRewards[$userId][$coinType]['quantity'] += $quantity;
- } else {
- // 新增奖励记录
- self::$coinRewards[$userId][$coinType] = [
- 'user_id' => $userId,
- 'type' => $coinType,
- 'quantity' => $quantity,
- ];
- }
- }
- /**
- * 添加神像奖励
- *
- * @param int $userId 用户ID
- * @param int $godType 神像类型
- * @param int $diff 时间差值(秒)
- * @param int $quantity 奖励数量
- * @return void
- */
- public static function addGodReward(int $userId, int $godType, int $diff, int $quantity): void
- {
- if (!isset(self::$godRewards[$userId])) {
- self::$godRewards[$userId] = [];
- }
- if (isset(self::$godRewards[$userId][$godType])) {
- // 如果已存在,累加数量和时间
- self::$godRewards[$userId][$godType]['quantity'] += $quantity;
- self::$godRewards[$userId][$godType]['diff'] += $diff;
- } else {
- // 新增奖励记录
- self::$godRewards[$userId][$godType] = [
- 'user_id' => $userId,
- 'type' => $godType,
- 'diff' => $diff,
- 'quantity' => $quantity,
- ];
- }
- }
- /**
- * 获取本次请求的所有奖励数据
- *
- * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
- * @return array 奖励数据数组,包含items、coins和gods
- */
- public static function getRewards(?int $userId = null): array
- {
- if ($userId !== null) {
- // 返回指定用户的奖励数据
- return [
- 'items' => array_values(self::$itemRewards[$userId] ?? []),
- 'coins' => array_values(self::$coinRewards[$userId] ?? []),
- 'gods' => array_values(self::$godRewards[$userId] ?? []),
- ];
- }
- // 返回所有用户的奖励数据,合并为一个数组
- $allItems = [];
- $allCoins = [];
- $allGods = [];
- foreach (self::$itemRewards as $userItems) {
- $allItems = array_merge($allItems, array_values($userItems));
- }
- foreach (self::$coinRewards as $userCoins) {
- $allCoins = array_merge($allCoins, array_values($userCoins));
- }
- foreach (self::$godRewards as $userGods) {
- $allGods = array_merge($allGods, array_values($userGods));
- }
- return [
- 'items' => $allItems,
- 'coins' => $allCoins,
- 'gods' => $allGods,
- ];
- }
- /**
- * 清空本次请求的奖励数据
- *
- * @return void
- */
- public static function clearRewards(): void
- {
- self::$itemRewards = [];
- self::$coinRewards = [];
- self::$godRewards = [];
- }
- /**
- * 检查是否有奖励数据
- *
- * @param int|null $userId 用户ID,如果为null则检查所有用户
- * @return bool
- */
- public static function hasRewards(?int $userId = null): bool
- {
- if ($userId !== null) {
- return !empty(self::$itemRewards[$userId]) || !empty(self::$coinRewards[$userId]) || !empty(self::$godRewards[$userId]);
- }
- return !empty(self::$itemRewards) || !empty(self::$coinRewards) || !empty(self::$godRewards);
- }
- /**
- * 获取物品奖励数据
- *
- * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
- * @return array
- */
- public static function getItemRewards(?int $userId = null): array
- {
- if ($userId !== null) {
- return array_values(self::$itemRewards[$userId] ?? []);
- }
- $allItems = [];
- foreach (self::$itemRewards as $userItems) {
- $allItems = array_merge($allItems, array_values($userItems));
- }
- return $allItems;
- }
- /**
- * 获取代币奖励数据
- *
- * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
- * @return array
- */
- public static function getCoinRewards(?int $userId = null): array
- {
- if ($userId !== null) {
- return array_values(self::$coinRewards[$userId] ?? []);
- }
- $allCoins = [];
- foreach (self::$coinRewards as $userCoins) {
- $allCoins = array_merge($allCoins, array_values($userCoins));
- }
- return $allCoins;
- }
- /**
- * 获取神像奖励数据
- *
- * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
- * @return array
- */
- public static function getGodRewards(?int $userId = null): array
- {
- if ($userId !== null) {
- return array_values(self::$godRewards[$userId] ?? []);
- }
- $allGods = [];
- foreach (self::$godRewards as $userGods) {
- $allGods = array_merge($allGods, array_values($userGods));
- }
- return $allGods;
- }
- }
|