using System; using System.Collections.Generic; using System.Linq; using SepCore.Definition; using SepCore.Entity; using Random = UnityEngine.Random; using UnityGameFramework.Runtime; using SepCore.CustomUtility; using UnityEngine; namespace SepCore.UI { public class LevelUpUseCase : IUIUseCase { private readonly Action _onCompleted; private readonly LevelUpRewardConfig[] _allRewards; private readonly Dictionary> _rewardsByRarity = new(); private readonly List _validRewards = new(); private int _refreshCount; private LevelUpRawData _currentModel; private readonly Player _player; public LevelUpUseCase(Player player, Action onCompleted) { _player = player; _onCompleted = onCompleted; _allRewards = GameEntry.Luban.GetTable().ToArray(); BuildRewardRarityPools(); } public LevelUpRawData CreateInitialModel() { _refreshCount = 0; if (_player.PendingLevelPoints <= 0) { _currentModel = null; return null; } _currentModel = BuildModel(GameEntry.Luban.Global.LevelUpRefreshPriceBase); return _currentModel; } public LevelUpRawData TryRefresh(int refreshCost) { if (_currentModel == null) { return null; } if (_player.Coin < refreshCost) { return null; } _player.Coin -= refreshCost; _refreshCount++; _currentModel = BuildModel((_refreshCount + 1) * GameEntry.Luban.Global.LevelUpRefreshPriceBase); return _currentModel; } public LevelUpRawData SelectReward(int selectedIndex) { if (_currentModel?.Rewards == null) { return null; } if (selectedIndex < 0 || selectedIndex >= _currentModel.Rewards.Count) { Log.Error("LevelUpFormUseCase::SelectReward: Invalid index"); return null; } ApplyReward(_currentModel.Rewards[selectedIndex]); if (--_player.PendingLevelPoints > 0) { _refreshCount = 0; _currentModel = BuildModel(GameEntry.Luban.Global.LevelUpRefreshPriceBase); return _currentModel; } _onCompleted?.Invoke(); _currentModel = null; return null; } private LevelUpRawData BuildModel(int refreshPrice) { if (_allRewards.Length == 0) { Log.Error("LevelUpFormUseCase::BuildModel(): _allRewards is empty"); return null; } int finalCount = Mathf.Min(GameEntry.Luban.Global.LevelUpRewardCount, _allRewards.Length); List selections = new List(finalCount); int currentLevel = _player.CurrentLevel; for (int i = 0; i < finalCount; i++) { Rarity rarity = RarityUtility.SelectRarityForLevel(currentLevel); LevelUpRewardConfig reward = PickRewardByRarity(rarity); if (reward == null) { Log.Error("LevelUpFormUseCase::BuildModel(): No available reward for selection."); break; } selections.Add(reward); } return new LevelUpRawData { Rewards = selections, RefreshPrice = refreshPrice }; } private void ApplyReward(LevelUpRewardConfig reward) { _player.AddProp(new PropItem(reward.StatModifierConfigs, reward.Rarity, reward.Title, reward.Sprite)); } private void BuildRewardRarityPools() { _rewardsByRarity.Clear(); _validRewards.Clear(); foreach (LevelUpRewardConfig reward in _allRewards) { Rarity rarity = reward.Rarity; _validRewards.Add(reward); if (!_rewardsByRarity.TryGetValue(rarity, out List list)) { list = new List(); _rewardsByRarity.Add(rarity, list); } list.Add(reward); } } private LevelUpRewardConfig PickRewardByRarity(Rarity rarity) { if (_rewardsByRarity.TryGetValue(rarity, out List list) && list.Count > 0) { return list[Random.Range(0, list.Count)]; } if (_validRewards.Count > 0) { return _validRewards[Random.Range(0, _validRewards.Count)]; } return null; } } }