[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; } }