| | | 1 | | using System.Net; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using RaidLoop.Client.Services; |
| | | 4 | | using RaidLoop.Core; |
| | | 5 | | using RaidLoop.Core.Contracts; |
| | | 6 | | |
| | | 7 | | namespace RaidLoop.Client.Pages; |
| | | 8 | | |
| | | 9 | | public partial class Home : IDisposable |
| | | 10 | | { |
| | | 11 | | private const int FallbackKnifeItemDefId = 1; |
| | | 12 | | private const int MedkitItemDefId = 19; |
| | | 13 | | private const int MainStashCap = 30; |
| | 1 | 14 | | private static readonly string[] StatOrder = ["STR", "DEX", "CON", "INT", "WIS", "CHA"]; |
| | 66 | 15 | | private int _playerConstitution = 10; |
| | 66 | 16 | | private int _maxHealth = 30; |
| | | 17 | | #if DEBUG |
| | | 18 | | private static readonly TimeSpan LuckRunCooldown = TimeSpan.FromSeconds(5); |
| | | 19 | | #else |
| | 1 | 20 | | private static readonly TimeSpan LuckRunCooldown = TimeSpan.FromMinutes(5); |
| | | 21 | | #endif |
| | | 22 | | |
| | 66 | 23 | | private GameState _mainGame = new([]); |
| | 66 | 24 | | private List<ShopStock> _shopStock = []; |
| | 66 | 25 | | private Dictionary<int, ItemRuleSnapshot> _itemRulesById = []; |
| | 66 | 26 | | private List<OnPersonEntry> _onPersonItems = []; |
| | | 27 | | private int _money; |
| | 66 | 28 | | private PlayerStats _acceptedStats = PlayerStats.Default; |
| | 66 | 29 | | private PlayerStats _draftStats = PlayerStats.Default; |
| | 66 | 30 | | private int _availableStatPoints = PlayerStatRules.StartingPool; |
| | | 31 | | private bool _statsAccepted; |
| | | 32 | | |
| | | 33 | | private RandomCharacterState? _randomCharacter; |
| | 66 | 34 | | private DateTimeOffset _randomCharacterAvailableAt = DateTimeOffset.MinValue; |
| | | 35 | | private System.Threading.Timer? _clockTimer; |
| | | 36 | | |
| | | 37 | | private RaidState? _raid; |
| | 66 | 38 | | private bool _isLoading = true; |
| | 66 | 39 | | private string _loadErrorMessage = string.Empty; |
| | | 40 | | private bool _inRaid; |
| | | 41 | | private bool _awaitingDecision; |
| | | 42 | | private int? _raidEncumbrance; |
| | | 43 | | private int? _raidMaxEncumbrance; |
| | | 44 | | private bool _extractHoldActive; |
| | | 45 | | private DateTimeOffset? _holdAtExtractUntil; |
| | | 46 | | private bool _extractHoldResolutionInFlight; |
| | 66 | 47 | | private EncounterType _encounterType = EncounterType.Neutral; |
| | 66 | 48 | | private string _encounterDescription = string.Empty; |
| | 66 | 49 | | private string _contactState = string.Empty; |
| | 66 | 50 | | private string _surpriseSide = string.Empty; |
| | 66 | 51 | | private string _initiativeWinner = string.Empty; |
| | | 52 | | private int _openingActionsRemaining; |
| | | 53 | | private bool _surprisePersistenceEligible; |
| | | 54 | | |
| | 66 | 55 | | private string _enemyName = string.Empty; |
| | | 56 | | private int _enemyHealth; |
| | | 57 | | private int _enemyDexterity; |
| | | 58 | | private int _enemyConstitution; |
| | | 59 | | private int _enemyStrength; |
| | | 60 | | |
| | 66 | 61 | | private string _lootContainer = string.Empty; |
| | 1 | 62 | | private static readonly List<Item> EmptyItems = []; |
| | | 63 | | |
| | | 64 | | private int _ammo; |
| | | 65 | | private int _challenge; |
| | | 66 | | private int _distanceFromExtract; |
| | 66 | 67 | | private string _resultMessage = string.Empty; |
| | 66 | 68 | | private string _activeRaidId = string.Empty; |
| | 66 | 69 | | private readonly List<string> _log = []; |
| | | 70 | | |
| | | 71 | | protected override async Task OnInitializedAsync() |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | 2 | 75 | | var response = await Profiles.BootstrapAsync(); |
| | 0 | 76 | | ApplySnapshot(response.Snapshot); |
| | 0 | 77 | | NormalizeEquippedSlots(); |
| | 0 | 78 | | EnsureMainCharacterHasWeaponFallback(); |
| | 0 | 79 | | _loadErrorMessage = string.Empty; |
| | 0 | 80 | | } |
| | 2 | 81 | | catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized) |
| | | 82 | | { |
| | 1 | 83 | | await ReportHandledErrorAsync("Profile bootstrap failed due to unauthorized session.", "bootstrap", ex); |
| | 1 | 84 | | await AuthService.SignOutAsync(); |
| | 1 | 85 | | _isLoading = false; |
| | 1 | 86 | | return; |
| | | 87 | | } |
| | 1 | 88 | | catch (HttpRequestException ex) when (ex.StatusCode is HttpStatusCode.ServiceUnavailable or HttpStatusCode.BadGa |
| | | 89 | | { |
| | 1 | 90 | | await ReportHandledErrorAsync("Profile bootstrap failed because the profile service is unavailable.", "boots |
| | 1 | 91 | | _loadErrorMessage = "The profile service is temporarily unavailable. Try again in a moment."; |
| | 1 | 92 | | _isLoading = false; |
| | 1 | 93 | | return; |
| | | 94 | | } |
| | 0 | 95 | | catch (Exception ex) |
| | | 96 | | { |
| | 0 | 97 | | await ReportHandledErrorAsync("Profile bootstrap failed.", "bootstrap", ex); |
| | 0 | 98 | | _loadErrorMessage = "The profile could not be loaded right now. Refresh and try again."; |
| | 0 | 99 | | _isLoading = false; |
| | 0 | 100 | | return; |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | _clockTimer = new System.Threading.Timer(async _ => |
| | 0 | 104 | | { |
| | 0 | 105 | | await InvokeAsync(async () => |
| | 0 | 106 | | { |
| | 0 | 107 | | await ResolveExpiredExtractHoldAsync(); |
| | 0 | 108 | | StateHasChanged(); |
| | 0 | 109 | | }); |
| | 0 | 110 | | }, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)); |
| | | 111 | | |
| | 0 | 112 | | _isLoading = false; |
| | 2 | 113 | | } |
| | | 114 | | |
| | 2 | 115 | | private bool IsRandomCharacterReady => DateTimeOffset.UtcNow >= _randomCharacterAvailableAt; |
| | 7 | 116 | | private bool HasUnprocessedLuckRunLoot => _randomCharacter is not null && _randomCharacter.Inventory.Count > 0; |
| | | 117 | | |
| | 27 | 118 | | private bool HasUnequippedOnPersonItems => _onPersonItems.Any(x => IsSlotType(x.Item.Type) && !x.IsEquipped); |
| | 24 | 119 | | private bool HasEquippedWeapon => _onPersonItems.Any(x => x.IsEquipped && x.Item.Type == ItemType.Weapon); |
| | 5 | 120 | | private bool HasOverweightOnPersonItems => GetOnPersonEncumbrance() > GetMainCharacterMaxEncumbrance(); |
| | 5 | 121 | | private bool CanStartMainRaid => _statsAccepted && !HasUnprocessedLuckRunLoot && !HasUnequippedOnPersonItems && !Has |
| | 2 | 122 | | private bool CanStartLuckRunRaid => _statsAccepted && !HasUnprocessedLuckRunLoot && IsRandomCharacterReady; |
| | 1 | 123 | | private string? RaidBlockReason => GetRaidBlockReason(); |
| | 0 | 124 | | private string? LuckRunBlockReason => HasUnprocessedLuckRunLoot |
| | 0 | 125 | | ? "Luck Run loot must be sold, stored, or moved to For Raid before entering a raid." |
| | 0 | 126 | | : !_statsAccepted |
| | 0 | 127 | | ? "Accept Stats before entering a raid." |
| | 0 | 128 | | : null; |
| | 0 | 129 | | private bool CanStashOnPersonItem => _mainGame.Stash.Count < MainStashCap; |
| | | 130 | | private int GetOnPersonEncumbrance() |
| | | 131 | | { |
| | 77 | 132 | | return CombatBalance.GetTotalEncumbrance(_onPersonItems.Select(entry => entry.Item)); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | private int GetMainCharacterMaxEncumbrance() |
| | | 136 | | { |
| | 10 | 137 | | return CombatBalance.GetMaxEncumbranceFromStrength(_acceptedStats.Strength); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private bool CanAddOnPersonItem(Item item) |
| | | 141 | | { |
| | 4 | 142 | | return GetOnPersonEncumbrance() + Math.Max(0, item.Weight) <= GetMainCharacterMaxEncumbrance(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | private string GetPreRaidEncumbranceText() |
| | | 146 | | { |
| | 1 | 147 | | return $"{GetOnPersonEncumbrance()}/{GetMainCharacterMaxEncumbrance()} lbs"; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | private string? GetRaidBlockReason() |
| | | 151 | | { |
| | 1 | 152 | | var reasons = new List<string>(); |
| | | 153 | | |
| | 1 | 154 | | if (HasUnprocessedLuckRunLoot) |
| | | 155 | | { |
| | 0 | 156 | | reasons.Add("Luck Run loot must be sold, stored, or moved to For Raid before entering a raid."); |
| | | 157 | | } |
| | | 158 | | |
| | 1 | 159 | | if (!_statsAccepted) |
| | | 160 | | { |
| | 0 | 161 | | reasons.Add("Accept Stats before entering a raid."); |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | if (HasUnequippedOnPersonItems) |
| | | 165 | | { |
| | 0 | 166 | | reasons.Add("You need to move your unequipped items to stash or sell them."); |
| | | 167 | | } |
| | | 168 | | |
| | 1 | 169 | | if (HasOverweightOnPersonItems) |
| | | 170 | | { |
| | 1 | 171 | | reasons.Add($"Your loadout weight is too high. {GetPreRaidEncumbranceText()}"); |
| | | 172 | | } |
| | | 173 | | |
| | 1 | 174 | | if (!HasEquippedWeapon) |
| | | 175 | | { |
| | 1 | 176 | | reasons.Add("You don't have a weapon equipped."); |
| | | 177 | | } |
| | | 178 | | |
| | 1 | 179 | | return reasons.Count == 0 ? null : string.Join(" ", reasons); |
| | | 180 | | } |
| | | 181 | | |
| | 10 | 182 | | private bool EquippedWeaponUsesAmmo => CombatBalance.WeaponUsesAmmo(_raid?.Inventory.EquippedWeapon); |
| | 2 | 183 | | private int CurrentMagazineCapacity => CombatBalance.GetMagazineCapacity(_raid?.Inventory.EquippedWeapon); |
| | 2 | 184 | | private bool EquippedWeaponSupportsSingleShot => CombatBalance.SupportsSingleShot(_raid?.Inventory.EquippedWeapon); |
| | 2 | 185 | | private bool EquippedWeaponSupportsBurstFire => CombatBalance.SupportsBurstFire(_raid?.Inventory.EquippedWeapon); |
| | 2 | 186 | | private bool EquippedWeaponSupportsFullAuto => CombatBalance.SupportsFullAuto(_raid?.Inventory.EquippedWeapon); |
| | 2 | 187 | | private bool CanAttack => EquippedWeaponSupportsSingleShot; |
| | 2 | 188 | | private bool CanAttackEnabled => !EquippedWeaponUsesAmmo || _ammo > 0; |
| | 2 | 189 | | private bool CanBurstFire => EquippedWeaponSupportsBurstFire; |
| | 3 | 190 | | private bool CanBurstFireEnabled => EquippedWeaponUsesAmmo && _ammo >= 3; |
| | 2 | 191 | | private bool CanFullAuto => EquippedWeaponSupportsFullAuto; |
| | 4 | 192 | | private bool CanFullAutoEnabled => EquippedWeaponUsesAmmo && _ammo >= 10; |
| | 1 | 193 | | private bool CanReload => _raid is not null && EquippedWeaponUsesAmmo && CurrentMagazineCapacity > 0 && _ammo < Curr |
| | 0 | 194 | | private bool CanUseMedkit => CurrentMedkits > 0; |
| | 0 | 195 | | private int CurrentMedkits => _raid?.Inventory.MedkitCount ?? 0; |
| | 0 | 196 | | private List<Item> CurrentDiscoveredLoot => _raid?.Inventory.DiscoveredLoot ?? EmptyItems; |
| | 3 | 197 | | private List<Item> CurrentCarriedLoot => _raid?.Inventory.CarriedItems ?? EmptyItems; |
| | 9 | 198 | | private IReadOnlyList<ShopStock> VisibleShopStock => _shopStock.Where(stock => CanBuyItem(stock.Item)).ToList(); |
| | 1 | 199 | | private bool CanReallocateStats => _raid is null && _statsAccepted && _money >= GetReallocateStatCost(); |
| | | 200 | | |
| | | 201 | | private static bool IsSlotType(ItemType type) |
| | | 202 | | { |
| | 22 | 203 | | return type is ItemType.Weapon or ItemType.Armor or ItemType.Backpack; |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | private int? FindEquippedIndexForSlot(ItemType slotType) |
| | | 207 | | { |
| | 0 | 208 | | var idx = _onPersonItems.FindIndex(x => x.IsEquipped && x.Item.Type == slotType); |
| | 0 | 209 | | return idx >= 0 ? idx : null; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | private bool ShouldEquipFromStash(Item item) |
| | | 213 | | { |
| | 0 | 214 | | return IsSlotType(item.Type) && FindEquippedIndexForSlot(item.Type) is null; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | private string GetStashPrimaryActionLabel(Item item) |
| | | 218 | | { |
| | 0 | 219 | | return ShouldEquipFromStash(item) ? "Equip" : "For Raid"; |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | private async Task SellStashItemAsync(int stashIndex) |
| | | 223 | | { |
| | 3 | 224 | | if (stashIndex < 0 || stashIndex >= _mainGame.Stash.Count) |
| | | 225 | | { |
| | 0 | 226 | | return; |
| | | 227 | | } |
| | | 228 | | |
| | 3 | 229 | | var item = _mainGame.Stash[stashIndex]; |
| | 3 | 230 | | if (GetSellPrice(item) <= 0) |
| | | 231 | | { |
| | 0 | 232 | | return; |
| | | 233 | | } |
| | | 234 | | |
| | 3 | 235 | | await ExecuteProfileActionAsync("sell-stash-item", new { stashIndex }); |
| | 2 | 236 | | } |
| | | 237 | | |
| | | 238 | | private async Task MoveStashToOnPersonAsync(int stashIndex) |
| | | 239 | | { |
| | 2 | 240 | | if (stashIndex < 0 || stashIndex >= _mainGame.Stash.Count) |
| | | 241 | | { |
| | 0 | 242 | | return; |
| | | 243 | | } |
| | | 244 | | |
| | 2 | 245 | | var item = _mainGame.Stash[stashIndex]; |
| | 2 | 246 | | if (!CanAddOnPersonItem(item)) |
| | | 247 | | { |
| | 1 | 248 | | return; |
| | | 249 | | } |
| | | 250 | | |
| | 1 | 251 | | await ExecuteProfileActionAsync("move-stash-to-on-person", new { stashIndex }); |
| | 2 | 252 | | } |
| | | 253 | | |
| | | 254 | | private async Task SellOnPersonItemAsync(int onPersonIndex) |
| | | 255 | | { |
| | 0 | 256 | | if (onPersonIndex < 0 || onPersonIndex >= _onPersonItems.Count) |
| | | 257 | | { |
| | 0 | 258 | | return; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | var item = _onPersonItems[onPersonIndex].Item; |
| | 0 | 262 | | if (GetSellPrice(item) <= 0) |
| | | 263 | | { |
| | 0 | 264 | | return; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | await ExecuteProfileActionAsync("sell-on-person-item", new { onPersonIndex }); |
| | 0 | 268 | | } |
| | | 269 | | |
| | | 270 | | private async Task StashOnPersonItemAsync(int onPersonIndex) |
| | | 271 | | { |
| | 0 | 272 | | if (!CanStashOnPersonItem || onPersonIndex < 0 || onPersonIndex >= _onPersonItems.Count) |
| | | 273 | | { |
| | 0 | 274 | | return; |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | await ExecuteProfileActionAsync("stash-on-person-item", new { onPersonIndex }); |
| | 0 | 278 | | } |
| | | 279 | | |
| | | 280 | | private async Task EquipOnPersonItemAsync(int onPersonIndex) |
| | | 281 | | { |
| | 0 | 282 | | if (onPersonIndex < 0 || onPersonIndex >= _onPersonItems.Count) |
| | | 283 | | { |
| | 0 | 284 | | return; |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | var selected = _onPersonItems[onPersonIndex]; |
| | 0 | 288 | | if (!IsSlotType(selected.Item.Type)) |
| | | 289 | | { |
| | 0 | 290 | | return; |
| | | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | await ExecuteProfileActionAsync("equip-on-person-item", new { onPersonIndex }); |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | private async Task UnequipOnPersonItemAsync(int onPersonIndex) |
| | | 297 | | { |
| | 0 | 298 | | if (onPersonIndex < 0 || onPersonIndex >= _onPersonItems.Count) |
| | | 299 | | { |
| | 0 | 300 | | return; |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | var current = _onPersonItems[onPersonIndex]; |
| | 0 | 304 | | if (!IsSlotType(current.Item.Type)) |
| | | 305 | | { |
| | 0 | 306 | | return; |
| | | 307 | | } |
| | | 308 | | |
| | 0 | 309 | | await ExecuteProfileActionAsync("unequip-on-person-item", new { onPersonIndex }); |
| | 0 | 310 | | } |
| | | 311 | | |
| | | 312 | | private async Task BuyFromShopAsync(ShopStock stock) |
| | | 313 | | { |
| | 2 | 314 | | var price = stock.Price; |
| | 2 | 315 | | if (_money < price || !CanAddOnPersonItem(stock.Item)) |
| | | 316 | | { |
| | 1 | 317 | | return; |
| | | 318 | | } |
| | | 319 | | |
| | 1 | 320 | | await ExecuteProfileActionAsync("buy-from-shop", new { itemDefId = stock.ItemDefId }); |
| | 2 | 321 | | } |
| | | 322 | | |
| | | 323 | | private async Task AcceptStatsAsync() |
| | | 324 | | { |
| | 1 | 325 | | if (_statsAccepted) |
| | | 326 | | { |
| | 0 | 327 | | return; |
| | | 328 | | } |
| | | 329 | | |
| | 1 | 330 | | await ExecuteProfileActionAsync("accept-stats", new { draftStats = _draftStats }); |
| | 1 | 331 | | } |
| | | 332 | | |
| | | 333 | | private async Task ReallocateStatsAsync() |
| | | 334 | | { |
| | 2 | 335 | | if (_raid is not null || !_statsAccepted || _money < GetReallocateStatCost()) |
| | | 336 | | { |
| | 1 | 337 | | return; |
| | | 338 | | } |
| | | 339 | | |
| | 1 | 340 | | await ExecuteProfileActionAsync("reallocate-stats", new { }); |
| | 2 | 341 | | } |
| | | 342 | | |
| | | 343 | | private void IncrementDraftStat(string statKey) |
| | | 344 | | { |
| | 0 | 345 | | if (_raid is not null || _statsAccepted) |
| | | 346 | | { |
| | 0 | 347 | | return; |
| | | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | var current = GetDraftStatValue(statKey); |
| | 0 | 351 | | var cost = PlayerStatRules.GetRaiseCost(current); |
| | 0 | 352 | | if (current >= PlayerStatRules.MaximumScore || cost <= 0 || _availableStatPoints < cost) |
| | | 353 | | { |
| | 0 | 354 | | return; |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | SetDraftStatValue(statKey, current + 1); |
| | 0 | 358 | | _availableStatPoints -= cost; |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | private void DecrementDraftStat(string statKey) |
| | | 362 | | { |
| | 0 | 363 | | if (_raid is not null || _statsAccepted) |
| | | 364 | | { |
| | 0 | 365 | | return; |
| | | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | var current = GetDraftStatValue(statKey); |
| | 0 | 369 | | var refund = PlayerStatRules.GetLowerRefund(current); |
| | 0 | 370 | | if (current <= PlayerStatRules.MinimumScore || refund <= 0) |
| | | 371 | | { |
| | 0 | 372 | | return; |
| | | 373 | | } |
| | | 374 | | |
| | 0 | 375 | | SetDraftStatValue(statKey, current - 1); |
| | 0 | 376 | | _availableStatPoints += refund; |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | private int GetDraftStatValue(string statKey) |
| | | 380 | | { |
| | 1 | 381 | | return statKey switch |
| | 1 | 382 | | { |
| | 1 | 383 | | "STR" => _draftStats.Strength, |
| | 0 | 384 | | "DEX" => _draftStats.Dexterity, |
| | 0 | 385 | | "CON" => _draftStats.Constitution, |
| | 0 | 386 | | "INT" => _draftStats.Intelligence, |
| | 0 | 387 | | "WIS" => _draftStats.Wisdom, |
| | 0 | 388 | | "CHA" => _draftStats.Charisma, |
| | 0 | 389 | | _ => PlayerStatRules.MinimumScore |
| | 1 | 390 | | }; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | private int GetDraftModifier(string statKey) |
| | | 394 | | { |
| | 0 | 395 | | return PlayerStatRules.GetAbilityModifier(GetDraftStatValue(statKey)); |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | private bool CanIncreaseDraftStat(string statKey) |
| | | 399 | | { |
| | 1 | 400 | | var current = GetDraftStatValue(statKey); |
| | 1 | 401 | | var cost = PlayerStatRules.GetRaiseCost(current); |
| | 1 | 402 | | return _raid is null |
| | 1 | 403 | | && !_statsAccepted |
| | 1 | 404 | | && current < PlayerStatRules.MaximumScore |
| | 1 | 405 | | && cost > 0 |
| | 1 | 406 | | && _availableStatPoints >= cost; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | private bool CanDecreaseDraftStat(string statKey) |
| | | 410 | | { |
| | 1 | 411 | | return _raid is null |
| | 1 | 412 | | && !_statsAccepted |
| | 1 | 413 | | && GetDraftStatValue(statKey) > PlayerStatRules.MinimumScore; |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | private void SetDraftStatValue(string statKey, int value) |
| | | 417 | | { |
| | 0 | 418 | | _draftStats = statKey switch |
| | 0 | 419 | | { |
| | 0 | 420 | | "STR" => _draftStats with { Strength = value }, |
| | 0 | 421 | | "DEX" => _draftStats with { Dexterity = value }, |
| | 0 | 422 | | "CON" => _draftStats with { Constitution = value }, |
| | 0 | 423 | | "INT" => _draftStats with { Intelligence = value }, |
| | 0 | 424 | | "WIS" => _draftStats with { Wisdom = value }, |
| | 0 | 425 | | "CHA" => _draftStats with { Charisma = value }, |
| | 0 | 426 | | _ => _draftStats |
| | 0 | 427 | | }; |
| | 0 | 428 | | } |
| | | 429 | | |
| | | 430 | | private async Task StartMainRaidAsync() |
| | | 431 | | { |
| | 3 | 432 | | if (!CanStartMainRaid) |
| | | 433 | | { |
| | 0 | 434 | | return; |
| | | 435 | | } |
| | | 436 | | |
| | 3 | 437 | | _activeRaidId = Guid.NewGuid().ToString("N"); |
| | 3 | 438 | | GameEventLog.Clear(); |
| | 3 | 439 | | GameEventLog.SetRaidContext(_activeRaidId); |
| | 3 | 440 | | await ExecuteRaidActionAsync("start-main-raid", new { }); |
| | 3 | 441 | | } |
| | | 442 | | |
| | | 443 | | private async Task StartRandomRaidAsync() |
| | | 444 | | { |
| | 2 | 445 | | if (!CanStartLuckRunRaid) |
| | | 446 | | { |
| | 0 | 447 | | return; |
| | | 448 | | } |
| | | 449 | | |
| | 2 | 450 | | _activeRaidId = Guid.NewGuid().ToString("N"); |
| | 2 | 451 | | GameEventLog.Clear(); |
| | 2 | 452 | | GameEventLog.SetRaidContext(_activeRaidId); |
| | 2 | 453 | | await ExecuteRaidActionAsync("start-random-raid", new { }); |
| | 2 | 454 | | } |
| | | 455 | | |
| | | 456 | | private async Task StoreLuckRunItemAsync(int luckIndex) |
| | | 457 | | { |
| | 0 | 458 | | if (_randomCharacter is null || luckIndex < 0 || luckIndex >= _randomCharacter.Inventory.Count) |
| | | 459 | | { |
| | 0 | 460 | | return; |
| | | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | if (_mainGame.Stash.Count >= MainStashCap) |
| | | 464 | | { |
| | 0 | 465 | | return; |
| | | 466 | | } |
| | | 467 | | |
| | 0 | 468 | | await ExecuteProfileActionAsync("store-luck-run-item", new { luckIndex }); |
| | 0 | 469 | | } |
| | | 470 | | |
| | | 471 | | private async Task MoveLuckRunItemToForRaidAsync(int luckIndex) |
| | | 472 | | { |
| | 0 | 473 | | if (_randomCharacter is null || luckIndex < 0 || luckIndex >= _randomCharacter.Inventory.Count) |
| | | 474 | | { |
| | 0 | 475 | | return; |
| | | 476 | | } |
| | | 477 | | |
| | 0 | 478 | | await ExecuteProfileActionAsync("move-luck-run-item-to-on-person", new { luckIndex }); |
| | 0 | 479 | | } |
| | | 480 | | |
| | | 481 | | private async Task SellLuckRunItemAsync(int luckIndex) |
| | | 482 | | { |
| | 1 | 483 | | if (_randomCharacter is null || luckIndex < 0 || luckIndex >= _randomCharacter.Inventory.Count) |
| | | 484 | | { |
| | 0 | 485 | | return; |
| | | 486 | | } |
| | | 487 | | |
| | 1 | 488 | | var item = _randomCharacter.Inventory[luckIndex]; |
| | 1 | 489 | | if (GetSellPrice(item) <= 0) |
| | | 490 | | { |
| | 0 | 491 | | return; |
| | | 492 | | } |
| | | 493 | | |
| | 1 | 494 | | await ExecuteProfileActionAsync("sell-luck-run-item", new { luckIndex }); |
| | 1 | 495 | | } |
| | | 496 | | |
| | | 497 | | private async Task SignOutAsync() |
| | | 498 | | { |
| | 0 | 499 | | await AuthService.SignOutAsync(); |
| | 0 | 500 | | } |
| | | 501 | | |
| | | 502 | | private async Task ExecuteProfileActionAsync(string action, object payload) |
| | | 503 | | { |
| | | 504 | | try |
| | | 505 | | { |
| | 8 | 506 | | var result = await Actions.SendAsync(action, payload); |
| | 7 | 507 | | ApplyActionResult(result); |
| | 7 | 508 | | NormalizeEquippedSlots(); |
| | 7 | 509 | | EnsureMainCharacterHasWeaponFallback(); |
| | 7 | 510 | | } |
| | 1 | 511 | | catch (Exception ex) |
| | | 512 | | { |
| | 1 | 513 | | await ReportHandledErrorAsync($"Profile action '{action}' failed.", "profile-action", ex, new { action, payl |
| | 1 | 514 | | throw; |
| | | 515 | | } |
| | 7 | 516 | | } |
| | | 517 | | |
| | | 518 | | private async Task ExecuteRaidActionAsync(string action, object payload) |
| | | 519 | | { |
| | 17 | 520 | | var raidPayload = CreateRaidActionPayload(payload); |
| | | 521 | | |
| | | 522 | | try |
| | | 523 | | { |
| | 17 | 524 | | var result = await Actions.SendAsync(action, raidPayload); |
| | 17 | 525 | | ApplyActionResult(result); |
| | 17 | 526 | | } |
| | 0 | 527 | | catch (Exception ex) |
| | | 528 | | { |
| | 0 | 529 | | await ReportHandledErrorAsync($"Raid action '{action}' failed.", "raid-action", ex, new { action, payload = |
| | 0 | 530 | | throw; |
| | | 531 | | } |
| | 17 | 532 | | } |
| | | 533 | | |
| | | 534 | | private void ApplyActionResult(GameActionResult result) |
| | | 535 | | { |
| | 39 | 536 | | if (result.Projections is { ValueKind: JsonValueKind.Object } projections) |
| | | 537 | | { |
| | 37 | 538 | | ApplyProjectedState(projections); |
| | | 539 | | } |
| | | 540 | | |
| | 39 | 541 | | if (!string.IsNullOrWhiteSpace(result.Message)) |
| | | 542 | | { |
| | 12 | 543 | | _resultMessage = result.Message; |
| | | 544 | | } |
| | 39 | 545 | | } |
| | | 546 | | |
| | | 547 | | private void ApplyProjectedState(JsonElement projections) |
| | | 548 | | { |
| | 37 | 549 | | var updatedStash = false; |
| | 37 | 550 | | var updatedLoadout = false; |
| | | 551 | | |
| | 37 | 552 | | if (TryGetProjection(projections, "economy", out var economy) |
| | 37 | 553 | | && TryGetInt32(economy, "money", out var money)) |
| | | 554 | | { |
| | 11 | 555 | | _money = money; |
| | | 556 | | } |
| | | 557 | | |
| | 37 | 558 | | if (TryGetProjection(projections, "stash", out var stash) |
| | 37 | 559 | | && TryGetProjection(stash, "mainStash", out var mainStash)) |
| | | 560 | | { |
| | 12 | 561 | | if (TryReadItemList(mainStash, out var parsedStash)) |
| | | 562 | | { |
| | 11 | 563 | | _mainGame = new GameState(parsedStash); |
| | 11 | 564 | | updatedStash = true; |
| | | 565 | | } |
| | | 566 | | } |
| | | 567 | | |
| | 37 | 568 | | if (TryGetProjection(projections, "loadout", out var loadout) |
| | 37 | 569 | | && TryGetProjection(loadout, "onPersonItems", out var onPersonItems)) |
| | | 570 | | { |
| | 13 | 571 | | if (TryReadOnPersonEntries(onPersonItems, out var parsedOnPersonItems)) |
| | | 572 | | { |
| | 12 | 573 | | _onPersonItems = parsedOnPersonItems; |
| | 12 | 574 | | updatedLoadout = true; |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | 37 | 578 | | if (TryGetProjection(projections, "player", out var playerProjection)) |
| | | 579 | | { |
| | 8 | 580 | | ApplyPlayerProjection(playerProjection); |
| | | 581 | | } |
| | | 582 | | |
| | 37 | 583 | | if (TryGetProjection(projections, "luckRun", out var luckRun)) |
| | | 584 | | { |
| | 6 | 585 | | if (TryGetString(luckRun, "randomCharacterAvailableAt", out var availableAtText) |
| | 6 | 586 | | && DateTimeOffset.TryParse(availableAtText, out var availableAt)) |
| | | 587 | | { |
| | 5 | 588 | | _randomCharacterAvailableAt = availableAt; |
| | | 589 | | } |
| | | 590 | | |
| | 6 | 591 | | if (TryGetProjection(luckRun, "randomCharacter", out var randomCharacter)) |
| | | 592 | | { |
| | 6 | 593 | | if (randomCharacter.ValueKind == JsonValueKind.Null) |
| | | 594 | | { |
| | 1 | 595 | | _randomCharacter = null; |
| | | 596 | | } |
| | 5 | 597 | | else if (TryReadRandomCharacter(randomCharacter, out var parsedRandomCharacter)) |
| | | 598 | | { |
| | 4 | 599 | | _randomCharacter = parsedRandomCharacter; |
| | | 600 | | } |
| | | 601 | | |
| | 6 | 602 | | if (_randomCharacter is not null && _randomCharacter.Inventory.Count == 0) |
| | | 603 | | { |
| | 0 | 604 | | _randomCharacter = null; |
| | | 605 | | } |
| | | 606 | | } |
| | | 607 | | } |
| | | 608 | | |
| | 37 | 609 | | if (TryGetProjection(projections, "raid", out var raid)) |
| | | 610 | | { |
| | 26 | 611 | | if (raid.ValueKind == JsonValueKind.Null) |
| | | 612 | | { |
| | 1 | 613 | | ClearRaidState(); |
| | | 614 | | } |
| | | 615 | | else |
| | | 616 | | { |
| | 25 | 617 | | ApplyRaidProjection(raid); |
| | | 618 | | } |
| | | 619 | | } |
| | | 620 | | |
| | 37 | 621 | | if (updatedStash || updatedLoadout) |
| | | 622 | | { |
| | 12 | 623 | | EnsureMainCharacterHasWeaponFallback(); |
| | | 624 | | } |
| | | 625 | | |
| | 37 | 626 | | if (updatedLoadout) |
| | | 627 | | { |
| | 12 | 628 | | NormalizeEquippedSlots(); |
| | | 629 | | } |
| | 37 | 630 | | } |
| | | 631 | | |
| | | 632 | | private void ApplyPlayerProjection(JsonElement playerProjection) |
| | | 633 | | { |
| | 8 | 634 | | if (TryGetProjection(playerProjection, "acceptedStats", out var acceptedStats) |
| | 8 | 635 | | && TryReadPlayerStats(acceptedStats, out var parsedAcceptedStats)) |
| | | 636 | | { |
| | 8 | 637 | | _acceptedStats = parsedAcceptedStats; |
| | 8 | 638 | | _playerConstitution = parsedAcceptedStats.Constitution; |
| | | 639 | | } |
| | | 640 | | |
| | 8 | 641 | | if (TryGetInt32(playerProjection, "playerConstitution", out var playerConstitution)) |
| | | 642 | | { |
| | 2 | 643 | | _playerConstitution = playerConstitution; |
| | | 644 | | } |
| | | 645 | | |
| | 8 | 646 | | if (TryGetInt32(playerProjection, "playerMaxHealth", out var playerMaxHealth)) |
| | | 647 | | { |
| | 2 | 648 | | _maxHealth = playerMaxHealth; |
| | | 649 | | } |
| | | 650 | | |
| | 8 | 651 | | if (TryGetProjection(playerProjection, "draftStats", out var draftStats) |
| | 8 | 652 | | && TryReadPlayerStats(draftStats, out var parsedDraftStats)) |
| | | 653 | | { |
| | 7 | 654 | | _draftStats = parsedDraftStats; |
| | | 655 | | } |
| | | 656 | | |
| | 8 | 657 | | if (TryGetInt32(playerProjection, "availableStatPoints", out var availableStatPoints)) |
| | | 658 | | { |
| | 4 | 659 | | _availableStatPoints = availableStatPoints; |
| | | 660 | | } |
| | | 661 | | |
| | 8 | 662 | | if (TryGetBool(playerProjection, "statsAccepted", out var statsAccepted)) |
| | | 663 | | { |
| | 4 | 664 | | _statsAccepted = statsAccepted; |
| | | 665 | | } |
| | 8 | 666 | | } |
| | | 667 | | |
| | | 668 | | private void ApplyRaidProjection(JsonElement raid) |
| | | 669 | | { |
| | 25 | 670 | | var freshRaid = _raid is null; |
| | 25 | 671 | | var raidEncumbranceProjected = false; |
| | 25 | 672 | | var raidMaxEncumbranceProjected = false; |
| | 25 | 673 | | var inventoryChanged = false; |
| | 25 | 674 | | if (freshRaid) |
| | | 675 | | { |
| | 7 | 676 | | _raidEncumbrance = null; |
| | 7 | 677 | | _raidMaxEncumbrance = null; |
| | 7 | 678 | | _extractHoldActive = false; |
| | 7 | 679 | | _holdAtExtractUntil = null; |
| | 7 | 680 | | _extractHoldResolutionInFlight = false; |
| | 7 | 681 | | _raid = new RaidState(_maxHealth, new RaidInventory()); |
| | 7 | 682 | | _inRaid = true; |
| | 7 | 683 | | _awaitingDecision = false; |
| | 7 | 684 | | _challenge = 0; |
| | 7 | 685 | | _distanceFromExtract = 3; |
| | 7 | 686 | | _ammo = 0; |
| | 7 | 687 | | _encounterDescription = string.Empty; |
| | 7 | 688 | | _contactState = string.Empty; |
| | 7 | 689 | | _surpriseSide = string.Empty; |
| | 7 | 690 | | _initiativeWinner = string.Empty; |
| | 7 | 691 | | _openingActionsRemaining = 0; |
| | 7 | 692 | | _surprisePersistenceEligible = false; |
| | 7 | 693 | | _enemyName = string.Empty; |
| | 7 | 694 | | _enemyHealth = 0; |
| | 7 | 695 | | _enemyDexterity = 0; |
| | 7 | 696 | | _enemyConstitution = 0; |
| | 7 | 697 | | _enemyStrength = 0; |
| | 7 | 698 | | _lootContainer = string.Empty; |
| | 7 | 699 | | _log.Clear(); |
| | 7 | 700 | | _encounterType = EncounterType.Neutral; |
| | | 701 | | } |
| | | 702 | | |
| | 25 | 703 | | var raidState = _raid!; |
| | 25 | 704 | | var inventory = raidState.Inventory; |
| | 25 | 705 | | var health = raidState.Health; |
| | 25 | 706 | | var backpackCapacity = raidState.BackpackCapacity; |
| | 25 | 707 | | var hasRaidPatch = freshRaid; |
| | 25 | 708 | | var holdProjectionSeen = false; |
| | 25 | 709 | | var parsedExtractHoldActive = false; |
| | 25 | 710 | | DateTimeOffset? parsedHoldAtExtractUntil = null; |
| | 25 | 711 | | var extractHoldActiveSeen = false; |
| | 25 | 712 | | var holdAtExtractUntilSeen = false; |
| | | 713 | | |
| | 25 | 714 | | if (TryGetInt32(raid, "health", out var parsedHealth)) |
| | | 715 | | { |
| | 19 | 716 | | health = parsedHealth; |
| | 19 | 717 | | hasRaidPatch = true; |
| | | 718 | | } |
| | | 719 | | |
| | 25 | 720 | | if (TryGetInt32(raid, "backpackCapacity", out var parsedBackpackCapacity)) |
| | | 721 | | { |
| | 6 | 722 | | backpackCapacity = parsedBackpackCapacity; |
| | 6 | 723 | | hasRaidPatch = true; |
| | | 724 | | } |
| | | 725 | | |
| | 25 | 726 | | if (TryGetInt32(raid, "encumbrance", out var parsedEncumbrance)) |
| | | 727 | | { |
| | 6 | 728 | | _raidEncumbrance = parsedEncumbrance; |
| | 6 | 729 | | raidEncumbranceProjected = true; |
| | 6 | 730 | | hasRaidPatch = true; |
| | | 731 | | } |
| | | 732 | | |
| | 25 | 733 | | if (TryGetInt32(raid, "maxEncumbrance", out var parsedMaxEncumbrance)) |
| | | 734 | | { |
| | 6 | 735 | | _raidMaxEncumbrance = parsedMaxEncumbrance; |
| | 6 | 736 | | raidState.MaxEncumbrance = parsedMaxEncumbrance; |
| | 6 | 737 | | raidMaxEncumbranceProjected = true; |
| | 6 | 738 | | hasRaidPatch = true; |
| | | 739 | | } |
| | | 740 | | |
| | 25 | 741 | | if (TryGetBool(raid, "extractHoldActive", out var extractHoldActive)) |
| | | 742 | | { |
| | 5 | 743 | | parsedExtractHoldActive = extractHoldActive; |
| | 5 | 744 | | extractHoldActiveSeen = true; |
| | 5 | 745 | | holdProjectionSeen = true; |
| | 5 | 746 | | hasRaidPatch = true; |
| | | 747 | | } |
| | | 748 | | |
| | 25 | 749 | | if (TryGetNullableDateTimeOffset(raid, "holdAtExtractUntil", out var holdAtExtractUntil)) |
| | | 750 | | { |
| | 4 | 751 | | parsedHoldAtExtractUntil = holdAtExtractUntil; |
| | 4 | 752 | | holdAtExtractUntilSeen = true; |
| | 4 | 753 | | holdProjectionSeen = true; |
| | 4 | 754 | | hasRaidPatch = true; |
| | | 755 | | } |
| | | 756 | | |
| | 25 | 757 | | if (holdProjectionSeen) |
| | | 758 | | { |
| | 5 | 759 | | _extractHoldActive = extractHoldActiveSeen ? parsedExtractHoldActive : false; |
| | 5 | 760 | | _holdAtExtractUntil = holdAtExtractUntilSeen ? parsedHoldAtExtractUntil : null; |
| | | 761 | | } |
| | | 762 | | |
| | 25 | 763 | | if (TryGetProjection(raid, "equippedItems", out var equippedItems)) |
| | | 764 | | { |
| | 18 | 765 | | if (TryReadItemList(equippedItems, out var items)) |
| | | 766 | | { |
| | 32 | 767 | | inventory.EquippedWeapon = items.FirstOrDefault(item => item.Type == ItemType.Weapon); |
| | 44 | 768 | | inventory.EquippedArmor = items.FirstOrDefault(item => item.Type == ItemType.Armor); |
| | 44 | 769 | | inventory.EquippedBackpack = items.FirstOrDefault(item => item.Type == ItemType.Backpack); |
| | 17 | 770 | | inventoryChanged = true; |
| | 17 | 771 | | hasRaidPatch = true; |
| | | 772 | | } |
| | | 773 | | } |
| | | 774 | | |
| | 25 | 775 | | if (TryGetProjection(raid, "carriedLoot", out var carriedLoot)) |
| | | 776 | | { |
| | 18 | 777 | | if (TryReadItemList(carriedLoot, out var items)) |
| | | 778 | | { |
| | 17 | 779 | | inventory.CarriedItems.Clear(); |
| | 17 | 780 | | inventory.CarriedItems.AddRange(items); |
| | 17 | 781 | | inventoryChanged = true; |
| | 17 | 782 | | hasRaidPatch = true; |
| | | 783 | | } |
| | | 784 | | } |
| | | 785 | | |
| | 25 | 786 | | if (TryGetProjection(raid, "discoveredLoot", out var discoveredLoot)) |
| | | 787 | | { |
| | 17 | 788 | | if (TryReadItemList(discoveredLoot, out var items)) |
| | | 789 | | { |
| | 16 | 790 | | inventory.DiscoveredLoot.Clear(); |
| | 16 | 791 | | inventory.DiscoveredLoot.AddRange(items); |
| | 16 | 792 | | inventoryChanged = true; |
| | 16 | 793 | | hasRaidPatch = true; |
| | | 794 | | } |
| | | 795 | | } |
| | | 796 | | |
| | 25 | 797 | | if (TryGetInt32(raid, "medkits", out var medkits)) |
| | | 798 | | { |
| | 6 | 799 | | inventory.MedkitCount = medkits; |
| | 6 | 800 | | inventoryChanged = true; |
| | 6 | 801 | | hasRaidPatch = true; |
| | | 802 | | } |
| | | 803 | | |
| | 25 | 804 | | if (TryGetInt32(raid, "ammo", out var ammo)) |
| | | 805 | | { |
| | 18 | 806 | | _ammo = ammo; |
| | 18 | 807 | | hasRaidPatch = true; |
| | | 808 | | } |
| | | 809 | | |
| | 25 | 810 | | if (TryGetInt32(raid, "challenge", out var challenge)) |
| | | 811 | | { |
| | 13 | 812 | | _challenge = challenge; |
| | 13 | 813 | | hasRaidPatch = true; |
| | | 814 | | } |
| | | 815 | | |
| | 25 | 816 | | if (TryGetInt32(raid, "distanceFromExtract", out var distanceFromExtract)) |
| | | 817 | | { |
| | 13 | 818 | | _distanceFromExtract = distanceFromExtract; |
| | 13 | 819 | | hasRaidPatch = true; |
| | | 820 | | } |
| | | 821 | | |
| | 25 | 822 | | if (TryGetBool(raid, "awaitingDecision", out var awaitingDecision)) |
| | | 823 | | { |
| | 16 | 824 | | _awaitingDecision = awaitingDecision; |
| | 16 | 825 | | hasRaidPatch = true; |
| | | 826 | | } |
| | | 827 | | |
| | 25 | 828 | | string? encounterDescription = null; |
| | 25 | 829 | | string? encounterDescriptionKey = null; |
| | 25 | 830 | | if (TryGetString(raid, "encounterDescription", out var encounterDescriptionText)) |
| | | 831 | | { |
| | 16 | 832 | | encounterDescription = encounterDescriptionText; |
| | 16 | 833 | | hasRaidPatch = true; |
| | | 834 | | } |
| | 25 | 835 | | if (TryGetString(raid, "encounterDescriptionKey", out var encounterDescriptionKeyText)) |
| | | 836 | | { |
| | 0 | 837 | | encounterDescriptionKey = encounterDescriptionKeyText; |
| | 0 | 838 | | hasRaidPatch = true; |
| | | 839 | | } |
| | | 840 | | |
| | 25 | 841 | | if (TryGetString(raid, "contactState", out var contactState)) |
| | | 842 | | { |
| | 15 | 843 | | _contactState = string.IsNullOrWhiteSpace(contactState) ? string.Empty : contactState; |
| | 15 | 844 | | hasRaidPatch = true; |
| | | 845 | | } |
| | | 846 | | |
| | 25 | 847 | | if (TryGetString(raid, "surpriseSide", out var surpriseSide)) |
| | | 848 | | { |
| | 15 | 849 | | _surpriseSide = string.IsNullOrWhiteSpace(surpriseSide) ? string.Empty : surpriseSide; |
| | 15 | 850 | | hasRaidPatch = true; |
| | | 851 | | } |
| | | 852 | | |
| | 25 | 853 | | if (TryGetString(raid, "initiativeWinner", out var initiativeWinner)) |
| | | 854 | | { |
| | 15 | 855 | | _initiativeWinner = string.IsNullOrWhiteSpace(initiativeWinner) ? string.Empty : initiativeWinner; |
| | 15 | 856 | | hasRaidPatch = true; |
| | | 857 | | } |
| | | 858 | | |
| | 25 | 859 | | if (TryGetInt32(raid, "openingActionsRemaining", out var openingActionsRemaining)) |
| | | 860 | | { |
| | 15 | 861 | | _openingActionsRemaining = openingActionsRemaining; |
| | 15 | 862 | | hasRaidPatch = true; |
| | | 863 | | } |
| | | 864 | | |
| | 25 | 865 | | if (TryGetBool(raid, "surprisePersistenceEligible", out var surprisePersistenceEligible)) |
| | | 866 | | { |
| | 15 | 867 | | _surprisePersistenceEligible = surprisePersistenceEligible; |
| | 15 | 868 | | hasRaidPatch = true; |
| | | 869 | | } |
| | | 870 | | |
| | 25 | 871 | | string? enemyName = null; |
| | 25 | 872 | | string? enemyKey = null; |
| | 25 | 873 | | if (TryGetString(raid, "enemyName", out var enemyNameText)) |
| | | 874 | | { |
| | 16 | 875 | | enemyName = enemyNameText; |
| | 16 | 876 | | hasRaidPatch = true; |
| | | 877 | | } |
| | 25 | 878 | | if (TryGetString(raid, "enemyKey", out var enemyKeyText)) |
| | | 879 | | { |
| | 0 | 880 | | enemyKey = enemyKeyText; |
| | 0 | 881 | | hasRaidPatch = true; |
| | | 882 | | } |
| | | 883 | | |
| | 25 | 884 | | if (TryGetInt32(raid, "enemyHealth", out var enemyHealth)) |
| | | 885 | | { |
| | 19 | 886 | | _enemyHealth = enemyHealth; |
| | 19 | 887 | | hasRaidPatch = true; |
| | | 888 | | } |
| | | 889 | | |
| | 25 | 890 | | if (TryGetInt32(raid, "enemyDexterity", out var enemyDexterity)) |
| | | 891 | | { |
| | 0 | 892 | | _enemyDexterity = enemyDexterity; |
| | 0 | 893 | | hasRaidPatch = true; |
| | | 894 | | } |
| | | 895 | | |
| | 25 | 896 | | if (TryGetInt32(raid, "enemyConstitution", out var enemyConstitution)) |
| | | 897 | | { |
| | 1 | 898 | | _enemyConstitution = enemyConstitution; |
| | 1 | 899 | | hasRaidPatch = true; |
| | | 900 | | } |
| | | 901 | | |
| | 25 | 902 | | if (TryGetInt32(raid, "enemyStrength", out var enemyStrength)) |
| | | 903 | | { |
| | 1 | 904 | | _enemyStrength = enemyStrength; |
| | 1 | 905 | | hasRaidPatch = true; |
| | | 906 | | } |
| | | 907 | | |
| | 25 | 908 | | if (TryGetString(raid, "lootContainer", out var lootContainer)) |
| | | 909 | | { |
| | 16 | 910 | | _lootContainer = lootContainer; |
| | 16 | 911 | | hasRaidPatch = true; |
| | | 912 | | } |
| | | 913 | | |
| | 25 | 914 | | if (TryGetString(raid, "encounterType", out var encounterTypeText) |
| | 25 | 915 | | && Enum.TryParse<EncounterType>(encounterTypeText, ignoreCase: true, out var encounterType)) |
| | | 916 | | { |
| | 16 | 917 | | _encounterType = encounterType; |
| | 16 | 918 | | hasRaidPatch = true; |
| | | 919 | | } |
| | | 920 | | |
| | 25 | 921 | | if (encounterDescription is not null) |
| | | 922 | | { |
| | 16 | 923 | | _encounterDescription = RaidPresentationCatalog.GetEncounterDescription(encounterDescriptionKey, _encounterT |
| | | 924 | | } |
| | | 925 | | |
| | 25 | 926 | | if (enemyName is not null || enemyKey is not null) |
| | | 927 | | { |
| | 16 | 928 | | _enemyName = RaidPresentationCatalog.GetEnemyLabel(enemyKey, enemyName); |
| | | 929 | | } |
| | | 930 | | |
| | 25 | 931 | | if (TryGetProjection(raid, "logEntriesAdded", out var logEntriesAdded)) |
| | | 932 | | { |
| | 1 | 933 | | if (logEntriesAdded.ValueKind == JsonValueKind.Array) |
| | | 934 | | { |
| | 1 | 935 | | _log.AddRange(ReadStringListFromProperty(raid, "logEntriesAdded").Select(RaidPresentationCatalog.Localiz |
| | 1 | 936 | | hasRaidPatch = true; |
| | | 937 | | } |
| | | 938 | | } |
| | 24 | 939 | | else if (TryGetProjection(raid, "logEntries", out var logEntries)) |
| | | 940 | | { |
| | 18 | 941 | | if (logEntries.ValueKind == JsonValueKind.Array) |
| | | 942 | | { |
| | 18 | 943 | | _log.Clear(); |
| | 18 | 944 | | _log.AddRange(ReadStringListFromProperty(raid, "logEntries").Select(RaidPresentationCatalog.LocalizeLogE |
| | 18 | 945 | | hasRaidPatch = true; |
| | | 946 | | } |
| | | 947 | | } |
| | | 948 | | |
| | 25 | 949 | | inventory.BackpackCapacity = backpackCapacity; |
| | 25 | 950 | | raidState.Health = health; |
| | 25 | 951 | | raidState.BackpackCapacity = backpackCapacity; |
| | | 952 | | |
| | 25 | 953 | | if (inventoryChanged && !raidEncumbranceProjected) |
| | | 954 | | { |
| | 13 | 955 | | _raidEncumbrance = null; |
| | | 956 | | } |
| | | 957 | | |
| | 25 | 958 | | if (inventoryChanged && !raidMaxEncumbranceProjected) |
| | | 959 | | { |
| | 13 | 960 | | _raidMaxEncumbrance = null; |
| | | 961 | | } |
| | | 962 | | |
| | 25 | 963 | | if (!freshRaid && hasRaidPatch) |
| | | 964 | | { |
| | 18 | 965 | | _raid = raidState; |
| | | 966 | | } |
| | | 967 | | |
| | 25 | 968 | | if (freshRaid || hasRaidPatch) |
| | | 969 | | { |
| | 25 | 970 | | _inRaid = true; |
| | | 971 | | } |
| | 25 | 972 | | } |
| | | 973 | | |
| | | 974 | | private object CreateRaidActionPayload(object payload) |
| | | 975 | | { |
| | 17 | 976 | | return payload switch |
| | 17 | 977 | | { |
| | 0 | 978 | | null => new { knownLogCount = _log.Count }, |
| | 17 | 979 | | _ => JsonSerializer.Deserialize<Dictionary<string, object?>>( |
| | 17 | 980 | | JsonSerializer.Serialize(payload)) is { } values |
| | 17 | 981 | | ? AddKnownLogCount(values) |
| | 17 | 982 | | : new Dictionary<string, object?> { ["knownLogCount"] = _log.Count } |
| | 17 | 983 | | }; |
| | | 984 | | } |
| | | 985 | | |
| | | 986 | | private Dictionary<string, object?> AddKnownLogCount(Dictionary<string, object?> values) |
| | | 987 | | { |
| | 17 | 988 | | values["knownLogCount"] = _log.Count; |
| | 17 | 989 | | return values; |
| | | 990 | | } |
| | | 991 | | |
| | | 992 | | private void ClearRaidState() |
| | | 993 | | { |
| | 1 | 994 | | _raid = null; |
| | 1 | 995 | | _raidEncumbrance = null; |
| | 1 | 996 | | _raidMaxEncumbrance = null; |
| | 1 | 997 | | _extractHoldActive = false; |
| | 1 | 998 | | _holdAtExtractUntil = null; |
| | 1 | 999 | | _extractHoldResolutionInFlight = false; |
| | 1 | 1000 | | _inRaid = false; |
| | 1 | 1001 | | _awaitingDecision = false; |
| | 1 | 1002 | | _challenge = 0; |
| | 1 | 1003 | | _distanceFromExtract = 0; |
| | 1 | 1004 | | _ammo = 0; |
| | 1 | 1005 | | _encounterType = EncounterType.Neutral; |
| | 1 | 1006 | | _encounterDescription = string.Empty; |
| | 1 | 1007 | | _contactState = string.Empty; |
| | 1 | 1008 | | _surpriseSide = string.Empty; |
| | 1 | 1009 | | _initiativeWinner = string.Empty; |
| | 1 | 1010 | | _openingActionsRemaining = 0; |
| | 1 | 1011 | | _surprisePersistenceEligible = false; |
| | 1 | 1012 | | _enemyName = string.Empty; |
| | 1 | 1013 | | _enemyHealth = 0; |
| | 1 | 1014 | | _enemyDexterity = 0; |
| | 1 | 1015 | | _enemyConstitution = 0; |
| | 1 | 1016 | | _enemyStrength = 0; |
| | 1 | 1017 | | _lootContainer = string.Empty; |
| | 1 | 1018 | | _log.Clear(); |
| | 1 | 1019 | | } |
| | | 1020 | | |
| | | 1021 | | private static List<Item> ReadItemList(JsonElement items) |
| | | 1022 | | { |
| | 0 | 1023 | | return TryReadItemList(items, out var parsedItems) ? parsedItems : []; |
| | | 1024 | | } |
| | | 1025 | | |
| | | 1026 | | private static List<Item> ReadItemListFromProperty(JsonElement parent, string propertyName) |
| | | 1027 | | { |
| | 0 | 1028 | | return TryGetProjection(parent, propertyName, out var items) ? ReadItemList(items) : []; |
| | | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | private static List<OnPersonEntry> ReadOnPersonEntries(JsonElement onPersonItems) |
| | | 1032 | | { |
| | 0 | 1033 | | return TryReadOnPersonEntries(onPersonItems, out var parsedEntries) ? parsedEntries : []; |
| | | 1034 | | } |
| | | 1035 | | |
| | | 1036 | | private static bool TryReadOnPersonEntries(JsonElement onPersonItems, out List<OnPersonEntry> entries) |
| | | 1037 | | { |
| | 13 | 1038 | | entries = []; |
| | 13 | 1039 | | if (onPersonItems.ValueKind != JsonValueKind.Array) |
| | | 1040 | | { |
| | 0 | 1041 | | return false; |
| | | 1042 | | } |
| | | 1043 | | |
| | 13 | 1044 | | var hasValidEntry = false; |
| | 46 | 1045 | | foreach (var entry in onPersonItems.EnumerateArray()) |
| | | 1046 | | { |
| | 10 | 1047 | | if (!TryGetProjection(entry, "item", out var itemElement) |
| | 10 | 1048 | | && !TryGetProjection(entry, "Item", out itemElement)) |
| | | 1049 | | { |
| | | 1050 | | continue; |
| | | 1051 | | } |
| | | 1052 | | |
| | 9 | 1053 | | if (!TryReadItem(itemElement, out var parsedItem)) |
| | | 1054 | | { |
| | | 1055 | | continue; |
| | | 1056 | | } |
| | | 1057 | | |
| | 9 | 1058 | | entries.Add(new OnPersonEntry( |
| | 9 | 1059 | | parsedItem, |
| | 9 | 1060 | | TryGetBool(entry, "isEquipped", out var isEquipped) && isEquipped)); |
| | 9 | 1061 | | hasValidEntry = true; |
| | | 1062 | | } |
| | | 1063 | | |
| | 13 | 1064 | | return hasValidEntry || onPersonItems.GetArrayLength() == 0; |
| | | 1065 | | } |
| | | 1066 | | |
| | | 1067 | | private static bool TryReadRandomCharacter(JsonElement randomCharacter, out RandomCharacterState parsedRandomCharact |
| | | 1068 | | { |
| | 6 | 1069 | | parsedRandomCharacter = null!; |
| | 6 | 1070 | | var name = TryGetString(randomCharacter, "name", out var randomCharacterName) |
| | 6 | 1071 | | ? randomCharacterName |
| | 6 | 1072 | | : string.Empty; |
| | 6 | 1073 | | var inventory = TryGetProjection(randomCharacter, "inventory", out var inventoryItems) |
| | 6 | 1074 | | && TryReadItemList(inventoryItems, out var parsedInventory) |
| | 6 | 1075 | | ? parsedInventory |
| | 6 | 1076 | | : []; |
| | 6 | 1077 | | if (!TryGetProjection(randomCharacter, "stats", out var statsElement) || !TryReadPlayerStats(statsElement, out v |
| | | 1078 | | { |
| | 1 | 1079 | | return false; |
| | | 1080 | | } |
| | | 1081 | | |
| | 5 | 1082 | | parsedRandomCharacter = new RandomCharacterState(name, inventory, parsedStats); |
| | 5 | 1083 | | return true; |
| | | 1084 | | } |
| | | 1085 | | |
| | | 1086 | | private static bool TryReadItemList(JsonElement items, out List<Item> parsedItems) |
| | | 1087 | | { |
| | 71 | 1088 | | parsedItems = []; |
| | 71 | 1089 | | if (items.ValueKind != JsonValueKind.Array) |
| | | 1090 | | { |
| | 0 | 1091 | | return false; |
| | | 1092 | | } |
| | | 1093 | | |
| | 71 | 1094 | | var hasValidItem = false; |
| | 230 | 1095 | | foreach (var entry in items.EnumerateArray()) |
| | | 1096 | | { |
| | 44 | 1097 | | if (!TryReadItem(entry, out var parsedItem)) |
| | | 1098 | | { |
| | | 1099 | | continue; |
| | | 1100 | | } |
| | | 1101 | | |
| | 40 | 1102 | | parsedItems.Add(parsedItem); |
| | 40 | 1103 | | hasValidItem = true; |
| | | 1104 | | } |
| | | 1105 | | |
| | 71 | 1106 | | return hasValidItem || items.GetArrayLength() == 0; |
| | | 1107 | | } |
| | | 1108 | | |
| | | 1109 | | private static bool TryReadItem(JsonElement item, out Item parsedItem) |
| | | 1110 | | { |
| | 57 | 1111 | | var hasItemDefId = TryGetInt32(item, "itemDefId", out var itemDefId) && itemDefId > 0; |
| | 57 | 1112 | | var itemKey = TryGetString(item, "itemKey", out var itemKeyValue) |
| | 57 | 1113 | | ? itemKeyValue |
| | 57 | 1114 | | : TryGetString(item, "ItemKey", out var itemKeyUpperCase) |
| | 57 | 1115 | | ? itemKeyUpperCase |
| | 57 | 1116 | | : string.Empty; |
| | 57 | 1117 | | var name = TryGetString(item, "name", out var itemName) |
| | 57 | 1118 | | ? itemName |
| | 57 | 1119 | | : TryGetString(item, "Name", out var itemNameUpperCase) |
| | 57 | 1120 | | ? itemNameUpperCase |
| | 57 | 1121 | | : string.Empty; |
| | | 1122 | | |
| | 57 | 1123 | | if (hasItemDefId |
| | 57 | 1124 | | && ItemCatalog.TryGetByItemDefId(itemDefId, out var catalogItemById) |
| | 57 | 1125 | | && catalogItemById is not null) |
| | | 1126 | | { |
| | 37 | 1127 | | parsedItem = catalogItemById; |
| | 37 | 1128 | | return true; |
| | | 1129 | | } |
| | | 1130 | | |
| | 20 | 1131 | | if (!hasItemDefId |
| | 20 | 1132 | | && !string.IsNullOrWhiteSpace(itemKey) |
| | 20 | 1133 | | && ItemCatalog.TryGetByKey(itemKey, out var catalogItemByKey) |
| | 20 | 1134 | | && catalogItemByKey is not null) |
| | | 1135 | | { |
| | 0 | 1136 | | parsedItem = catalogItemByKey; |
| | 0 | 1137 | | return true; |
| | | 1138 | | } |
| | | 1139 | | |
| | 20 | 1140 | | if (!hasItemDefId |
| | 20 | 1141 | | && !string.IsNullOrWhiteSpace(name) |
| | 20 | 1142 | | && ItemCatalog.TryGet(name, out var catalogItem)) |
| | | 1143 | | { |
| | 14 | 1144 | | parsedItem = catalogItem!; |
| | 14 | 1145 | | return true; |
| | | 1146 | | } |
| | | 1147 | | |
| | 6 | 1148 | | if (!hasItemDefId && string.IsNullOrWhiteSpace(name) && string.IsNullOrWhiteSpace(itemKey)) |
| | | 1149 | | { |
| | 4 | 1150 | | parsedItem = default!; |
| | 4 | 1151 | | return false; |
| | | 1152 | | } |
| | | 1153 | | |
| | 2 | 1154 | | var type = TryGetInt32(item, "type", out var parsedType) && Enum.IsDefined(typeof(ItemType), parsedType) |
| | 2 | 1155 | | ? (ItemType)parsedType |
| | 2 | 1156 | | : TryGetInt32(item, "Type", out var parsedTypeUpperCase) && Enum.IsDefined(typeof(ItemType), parsedTypeUpper |
| | 2 | 1157 | | ? (ItemType)parsedTypeUpperCase |
| | 2 | 1158 | | : ItemType.Sellable; |
| | 2 | 1159 | | var value = TryGetInt32(item, "value", out var parsedValue) |
| | 2 | 1160 | | ? parsedValue |
| | 2 | 1161 | | : TryGetInt32(item, "Value", out var parsedValueUpperCase) |
| | 2 | 1162 | | ? parsedValueUpperCase |
| | 2 | 1163 | | : 1; |
| | 2 | 1164 | | if (!TryGetInt32(item, "weight", out var parsedWeight) |
| | 2 | 1165 | | && !TryGetInt32(item, "Weight", out parsedWeight)) |
| | | 1166 | | { |
| | 0 | 1167 | | parsedItem = default!; |
| | 0 | 1168 | | return false; |
| | | 1169 | | } |
| | 2 | 1170 | | var slots = TryGetInt32(item, "slots", out var parsedSlots) |
| | 2 | 1171 | | ? parsedSlots |
| | 2 | 1172 | | : TryGetInt32(item, "Slots", out var parsedSlotsUpperCase) |
| | 2 | 1173 | | ? parsedSlotsUpperCase |
| | 2 | 1174 | | : 1; |
| | 2 | 1175 | | var rarity = TryGetInt32(item, "rarity", out var parsedRarity) && Enum.IsDefined(typeof(Rarity), parsedRarity) |
| | 2 | 1176 | | ? (Rarity)parsedRarity |
| | 2 | 1177 | | : TryGetInt32(item, "Rarity", out var parsedRarityUpperCase) && Enum.IsDefined(typeof(Rarity), parsedRarityU |
| | 2 | 1178 | | ? (Rarity)parsedRarityUpperCase |
| | 2 | 1179 | | : Rarity.Common; |
| | 2 | 1180 | | var displayRarity = TryGetInt32(item, "displayRarity", out var parsedDisplayRarity) && Enum.IsDefined(typeof(Dis |
| | 2 | 1181 | | ? (DisplayRarity)parsedDisplayRarity |
| | 2 | 1182 | | : TryGetInt32(item, "DisplayRarity", out var parsedDisplayRarityUpperCase) && Enum.IsDefined(typeof(DisplayR |
| | 2 | 1183 | | ? (DisplayRarity)parsedDisplayRarityUpperCase |
| | 2 | 1184 | | : DisplayRarity.Common; |
| | | 1185 | | |
| | 2 | 1186 | | parsedItem = new Item(string.IsNullOrWhiteSpace(name) ? itemKey : name, type, parsedWeight, value, slots, rarity |
| | 2 | 1187 | | { |
| | 2 | 1188 | | ItemDefId = hasItemDefId ? itemDefId : 0, |
| | 2 | 1189 | | Key = itemKey |
| | 2 | 1190 | | }; |
| | 2 | 1191 | | return true; |
| | | 1192 | | } |
| | | 1193 | | |
| | | 1194 | | private void ApplyItemRulesProjection(JsonElement itemRules) |
| | | 1195 | | { |
| | 0 | 1196 | | if (itemRules.ValueKind != JsonValueKind.Array) |
| | | 1197 | | { |
| | 0 | 1198 | | return; |
| | | 1199 | | } |
| | | 1200 | | |
| | 0 | 1201 | | var updatedRules = new Dictionary<int, ItemRuleSnapshot>(); |
| | 0 | 1202 | | foreach (var rule in itemRules.EnumerateArray()) |
| | | 1203 | | { |
| | 0 | 1204 | | if (!TryGetInt32(rule, "itemDefId", out var itemDefId) || itemDefId <= 0) |
| | | 1205 | | { |
| | | 1206 | | continue; |
| | | 1207 | | } |
| | | 1208 | | |
| | 0 | 1209 | | var type = TryGetInt32(rule, "type", out var parsedType) && Enum.IsDefined(typeof(ItemType), parsedType) |
| | 0 | 1210 | | ? (ItemType)parsedType |
| | 0 | 1211 | | : ItemType.Sellable; |
| | 0 | 1212 | | var weight = TryGetInt32(rule, "weight", out var parsedWeight) ? parsedWeight : 0; |
| | 0 | 1213 | | var slots = TryGetInt32(rule, "slots", out var parsedSlots) ? parsedSlots : 1; |
| | 0 | 1214 | | var rarity = TryGetInt32(rule, "rarity", out var parsedRarity) && Enum.IsDefined(typeof(Rarity), parsedRarit |
| | 0 | 1215 | | ? (Rarity)parsedRarity |
| | 0 | 1216 | | : Rarity.Common; |
| | | 1217 | | |
| | 0 | 1218 | | updatedRules[itemDefId] = new ItemRuleSnapshot(itemDefId, type, weight, slots, rarity); |
| | | 1219 | | } |
| | | 1220 | | |
| | 0 | 1221 | | if (updatedRules.Count > 0) |
| | | 1222 | | { |
| | 0 | 1223 | | _itemRulesById = updatedRules; |
| | | 1224 | | } |
| | 0 | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | private static bool TryReadPlayerStats(JsonElement statsElement, out PlayerStats parsedStats) |
| | | 1228 | | { |
| | 20 | 1229 | | var strength = TryGetInt32(statsElement, "strength", out var parsedStrength) |
| | 20 | 1230 | | ? parsedStrength |
| | 20 | 1231 | | : TryGetInt32(statsElement, "Strength", out var parsedStrengthUpper) |
| | 20 | 1232 | | ? parsedStrengthUpper |
| | 20 | 1233 | | : PlayerStatRules.MinimumScore; |
| | 20 | 1234 | | var dexterity = TryGetInt32(statsElement, "dexterity", out var parsedDexterity) |
| | 20 | 1235 | | ? parsedDexterity |
| | 20 | 1236 | | : TryGetInt32(statsElement, "Dexterity", out var parsedDexterityUpper) |
| | 20 | 1237 | | ? parsedDexterityUpper |
| | 20 | 1238 | | : PlayerStatRules.MinimumScore; |
| | 20 | 1239 | | var constitution = TryGetInt32(statsElement, "constitution", out var parsedConstitution) |
| | 20 | 1240 | | ? parsedConstitution |
| | 20 | 1241 | | : TryGetInt32(statsElement, "Constitution", out var parsedConstitutionUpper) |
| | 20 | 1242 | | ? parsedConstitutionUpper |
| | 20 | 1243 | | : PlayerStatRules.MinimumScore; |
| | 20 | 1244 | | var intelligence = TryGetInt32(statsElement, "intelligence", out var parsedIntelligence) |
| | 20 | 1245 | | ? parsedIntelligence |
| | 20 | 1246 | | : TryGetInt32(statsElement, "Intelligence", out var parsedIntelligenceUpper) |
| | 20 | 1247 | | ? parsedIntelligenceUpper |
| | 20 | 1248 | | : PlayerStatRules.MinimumScore; |
| | 20 | 1249 | | var wisdom = TryGetInt32(statsElement, "wisdom", out var parsedWisdom) |
| | 20 | 1250 | | ? parsedWisdom |
| | 20 | 1251 | | : TryGetInt32(statsElement, "Wisdom", out var parsedWisdomUpper) |
| | 20 | 1252 | | ? parsedWisdomUpper |
| | 20 | 1253 | | : PlayerStatRules.MinimumScore; |
| | 20 | 1254 | | var charisma = TryGetInt32(statsElement, "charisma", out var parsedCharisma) |
| | 20 | 1255 | | ? parsedCharisma |
| | 20 | 1256 | | : TryGetInt32(statsElement, "Charisma", out var parsedCharismaUpper) |
| | 20 | 1257 | | ? parsedCharismaUpper |
| | 20 | 1258 | | : PlayerStatRules.MinimumScore; |
| | | 1259 | | |
| | 20 | 1260 | | parsedStats = new PlayerStats(strength, dexterity, constitution, intelligence, wisdom, charisma); |
| | 20 | 1261 | | return true; |
| | | 1262 | | } |
| | | 1263 | | |
| | | 1264 | | private static List<string> ReadStringListFromProperty(JsonElement parent, string propertyName) |
| | | 1265 | | { |
| | 19 | 1266 | | if (!TryGetProjection(parent, propertyName, out var items) || items.ValueKind != JsonValueKind.Array) |
| | | 1267 | | { |
| | 0 | 1268 | | return []; |
| | | 1269 | | } |
| | | 1270 | | |
| | 19 | 1271 | | return items.EnumerateArray() |
| | 21 | 1272 | | .Select(item => item.ValueKind == JsonValueKind.String ? item.GetString() ?? string.Empty : string.Empty) |
| | 21 | 1273 | | .Where(item => !string.IsNullOrWhiteSpace(item)) |
| | 19 | 1274 | | .ToList(); |
| | | 1275 | | } |
| | | 1276 | | |
| | | 1277 | | private static bool TryGetProjection(JsonElement parent, string propertyName, out JsonElement value) |
| | | 1278 | | { |
| | 1601 | 1279 | | if (parent.ValueKind == JsonValueKind.Object) |
| | | 1280 | | { |
| | 23051 | 1281 | | foreach (var property in parent.EnumerateObject()) |
| | | 1282 | | { |
| | 10372 | 1283 | | if (string.Equals(property.Name, propertyName, StringComparison.OrdinalIgnoreCase)) |
| | | 1284 | | { |
| | 703 | 1285 | | value = property.Value; |
| | 703 | 1286 | | return true; |
| | | 1287 | | } |
| | | 1288 | | } |
| | | 1289 | | } |
| | | 1290 | | |
| | 898 | 1291 | | value = default; |
| | 898 | 1292 | | return false; |
| | 703 | 1293 | | } |
| | | 1294 | | |
| | | 1295 | | private static bool TryGetInt32(JsonElement parent, string propertyName, out int value) |
| | | 1296 | | { |
| | 601 | 1297 | | if (TryGetProjection(parent, propertyName, out var property)) |
| | | 1298 | | { |
| | 276 | 1299 | | if (property.ValueKind == JsonValueKind.Number && property.TryGetInt32(out value)) |
| | | 1300 | | { |
| | 260 | 1301 | | return true; |
| | | 1302 | | } |
| | | 1303 | | |
| | 16 | 1304 | | if (property.ValueKind == JsonValueKind.String && int.TryParse(property.GetString(), out value)) |
| | | 1305 | | { |
| | 0 | 1306 | | return true; |
| | | 1307 | | } |
| | | 1308 | | } |
| | | 1309 | | |
| | 341 | 1310 | | value = default; |
| | 341 | 1311 | | return false; |
| | | 1312 | | } |
| | | 1313 | | |
| | | 1314 | | private static bool TryGetBool(JsonElement parent, string propertyName, out bool value) |
| | | 1315 | | { |
| | 92 | 1316 | | if (TryGetProjection(parent, propertyName, out var property)) |
| | | 1317 | | { |
| | 53 | 1318 | | if (property.ValueKind is JsonValueKind.True or JsonValueKind.False) |
| | | 1319 | | { |
| | 49 | 1320 | | value = property.GetBoolean(); |
| | 49 | 1321 | | return true; |
| | | 1322 | | } |
| | | 1323 | | |
| | 4 | 1324 | | if (property.ValueKind == JsonValueKind.String && bool.TryParse(property.GetString(), out value)) |
| | | 1325 | | { |
| | 0 | 1326 | | return true; |
| | | 1327 | | } |
| | | 1328 | | } |
| | | 1329 | | |
| | 43 | 1330 | | value = default; |
| | 43 | 1331 | | return false; |
| | | 1332 | | } |
| | | 1333 | | |
| | | 1334 | | private static bool TryGetString(JsonElement parent, string propertyName, out string value) |
| | | 1335 | | { |
| | 448 | 1336 | | if (TryGetProjection(parent, propertyName, out var property) && property.ValueKind == JsonValueKind.String) |
| | | 1337 | | { |
| | 137 | 1338 | | value = property.GetString() ?? string.Empty; |
| | 137 | 1339 | | return true; |
| | | 1340 | | } |
| | | 1341 | | |
| | 311 | 1342 | | value = string.Empty; |
| | 311 | 1343 | | return false; |
| | | 1344 | | } |
| | | 1345 | | |
| | | 1346 | | private static bool TryGetNullableDateTimeOffset(JsonElement parent, string propertyName, out DateTimeOffset? value) |
| | | 1347 | | { |
| | 25 | 1348 | | if (TryGetProjection(parent, propertyName, out var property)) |
| | | 1349 | | { |
| | 4 | 1350 | | if (property.ValueKind == JsonValueKind.Null) |
| | | 1351 | | { |
| | 2 | 1352 | | value = null; |
| | 2 | 1353 | | return true; |
| | | 1354 | | } |
| | | 1355 | | |
| | 2 | 1356 | | if (property.ValueKind == JsonValueKind.String |
| | 2 | 1357 | | && DateTimeOffset.TryParse(property.GetString(), out var parsed)) |
| | | 1358 | | { |
| | 2 | 1359 | | value = parsed; |
| | 2 | 1360 | | return true; |
| | | 1361 | | } |
| | | 1362 | | } |
| | | 1363 | | |
| | 21 | 1364 | | value = default; |
| | 21 | 1365 | | return false; |
| | | 1366 | | } |
| | | 1367 | | |
| | | 1368 | | private async Task ExecuteLootActionAsync(string action, Item item, string eventName) |
| | | 1369 | | { |
| | 2 | 1370 | | await ExecuteRaidActionAsync(action, new { itemDefId = item.ItemDefId }); |
| | 2 | 1371 | | GameEventLog.Append(new GameEvent( |
| | 2 | 1372 | | eventName, |
| | 2 | 1373 | | _activeRaidId, |
| | 2 | 1374 | | GameEventLog.CreateItemSnapshots([item]), |
| | 2 | 1375 | | DateTimeOffset.UtcNow)); |
| | 2 | 1376 | | } |
| | | 1377 | | |
| | | 1378 | | private string GetRandomCooldownText() |
| | | 1379 | | { |
| | 1 | 1380 | | var left = _randomCharacterAvailableAt - DateTimeOffset.UtcNow; |
| | 1 | 1381 | | if (left < TimeSpan.Zero) |
| | | 1382 | | { |
| | 0 | 1383 | | left = TimeSpan.Zero; |
| | | 1384 | | } |
| | | 1385 | | |
| | 1 | 1386 | | return left.TotalHours >= 1 |
| | 1 | 1387 | | ? $"{(int)left.TotalHours}:{left.Minutes:D2}:{left.Seconds:D2}" |
| | 1 | 1388 | | : $"{left.Minutes:D2}:{left.Seconds:D2}"; |
| | | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | private string GetEquippedWeaponName() |
| | | 1392 | | { |
| | 0 | 1393 | | var weapon = _raid?.Inventory.EquippedWeapon; |
| | 0 | 1394 | | return ItemPresentationCatalog.GetLabel(weapon) is { Length: > 0 } label |
| | 0 | 1395 | | ? label |
| | 0 | 1396 | | : ItemPresentationCatalog.GetLabel(ItemCatalog.GetByItemDefId(FallbackKnifeItemDefId)); |
| | | 1397 | | } |
| | | 1398 | | |
| | | 1399 | | private string GetAmmoHudText() |
| | | 1400 | | { |
| | 0 | 1401 | | if (!EquippedWeaponUsesAmmo) |
| | | 1402 | | { |
| | 0 | 1403 | | return "Ammo: ∞"; |
| | | 1404 | | } |
| | | 1405 | | |
| | 0 | 1406 | | return $"Ammo: {_ammo} / {CurrentMagazineCapacity}"; |
| | | 1407 | | } |
| | | 1408 | | |
| | | 1409 | | private string GetEncounterTitle() |
| | | 1410 | | { |
| | 0 | 1411 | | return _encounterType switch |
| | 0 | 1412 | | { |
| | 0 | 1413 | | EncounterType.Combat => "Combat Encounter", |
| | 0 | 1414 | | EncounterType.Loot => "Loot Encounter", |
| | 0 | 1415 | | EncounterType.Neutral => "Area Clear", |
| | 0 | 1416 | | EncounterType.Extraction => "Extraction Opportunity", |
| | 0 | 1417 | | _ => "Encounter" |
| | 0 | 1418 | | }; |
| | | 1419 | | } |
| | | 1420 | | |
| | | 1421 | | private async Task AttackAsync() |
| | | 1422 | | { |
| | 2 | 1423 | | if (_raid is null || _encounterType != EncounterType.Combat) |
| | | 1424 | | { |
| | 0 | 1425 | | return; |
| | | 1426 | | } |
| | 2 | 1427 | | await ExecuteRaidActionAsync("attack", new { target = "enemy" }); |
| | 2 | 1428 | | } |
| | | 1429 | | |
| | | 1430 | | private async Task BurstFireAsync() |
| | | 1431 | | { |
| | 0 | 1432 | | if (_raid is null || _encounterType != EncounterType.Combat) |
| | | 1433 | | { |
| | 0 | 1434 | | return; |
| | | 1435 | | } |
| | 0 | 1436 | | await ExecuteRaidActionAsync("burst-fire", new { target = "enemy" }); |
| | 0 | 1437 | | } |
| | | 1438 | | |
| | | 1439 | | private async Task FullAutoAsync() |
| | | 1440 | | { |
| | 0 | 1441 | | if (_raid is null || _encounterType != EncounterType.Combat) |
| | | 1442 | | { |
| | 0 | 1443 | | return; |
| | | 1444 | | } |
| | | 1445 | | |
| | 0 | 1446 | | await ExecuteRaidActionAsync("full-auto", new { target = "enemy" }); |
| | 0 | 1447 | | } |
| | | 1448 | | |
| | | 1449 | | private async Task UseMedkitAsync() |
| | | 1450 | | { |
| | 0 | 1451 | | if (_raid is null) |
| | | 1452 | | { |
| | 0 | 1453 | | return; |
| | | 1454 | | } |
| | 0 | 1455 | | await ExecuteRaidActionAsync("use-medkit", new { }); |
| | 0 | 1456 | | } |
| | | 1457 | | |
| | | 1458 | | private async Task ReloadAsync() |
| | | 1459 | | { |
| | 1 | 1460 | | if (!CanReload) |
| | | 1461 | | { |
| | 0 | 1462 | | return; |
| | | 1463 | | } |
| | 1 | 1464 | | await ExecuteRaidActionAsync("reload", new { }); |
| | 1 | 1465 | | } |
| | | 1466 | | |
| | | 1467 | | private async Task FleeAsync() |
| | | 1468 | | { |
| | 0 | 1469 | | if (_encounterType != EncounterType.Combat) |
| | | 1470 | | { |
| | 0 | 1471 | | return; |
| | | 1472 | | } |
| | 0 | 1473 | | await ExecuteRaidActionAsync("flee", new { }); |
| | 0 | 1474 | | } |
| | | 1475 | | |
| | | 1476 | | |
| | | 1477 | | private Task TakeLootAsync(Item lootItem) |
| | | 1478 | | { |
| | 2 | 1479 | | if (_raid is null) |
| | | 1480 | | { |
| | 0 | 1481 | | return Task.CompletedTask; |
| | | 1482 | | } |
| | 2 | 1483 | | return ExecuteLootActionAsync("take-loot", lootItem, "loot.acquired"); |
| | | 1484 | | } |
| | | 1485 | | |
| | | 1486 | | private Task DropCarriedAsync(Item item) |
| | | 1487 | | { |
| | 0 | 1488 | | if (_raid is null) |
| | | 1489 | | { |
| | 0 | 1490 | | return Task.CompletedTask; |
| | | 1491 | | } |
| | 0 | 1492 | | return ExecuteRaidActionAsync("drop-carried", new { itemDefId = item.ItemDefId }); |
| | | 1493 | | } |
| | | 1494 | | |
| | | 1495 | | private Task DropEquippedAsync(ItemType slotType) |
| | | 1496 | | { |
| | 0 | 1497 | | if (_raid is null) |
| | | 1498 | | { |
| | 0 | 1499 | | return Task.CompletedTask; |
| | | 1500 | | } |
| | 0 | 1501 | | return ExecuteRaidActionAsync("drop-equipped", new { slotType = slotType.ToString() }); |
| | | 1502 | | } |
| | | 1503 | | |
| | | 1504 | | private Task EquipFromDiscoveredAsync(Item item) |
| | | 1505 | | { |
| | 0 | 1506 | | if (_raid is null) |
| | | 1507 | | { |
| | 0 | 1508 | | return Task.CompletedTask; |
| | | 1509 | | } |
| | 0 | 1510 | | return ExecuteLootActionAsync("equip-from-discovered", item, "player.equip"); |
| | | 1511 | | } |
| | | 1512 | | |
| | | 1513 | | private Task EquipFromCarriedAsync(Item item) |
| | | 1514 | | { |
| | 0 | 1515 | | if (_raid is null) |
| | | 1516 | | { |
| | 0 | 1517 | | return Task.CompletedTask; |
| | | 1518 | | } |
| | 0 | 1519 | | return ExecuteLootActionAsync("equip-from-carried", item, "player.equip"); |
| | | 1520 | | } |
| | | 1521 | | |
| | | 1522 | | private int GetRaidEncumbrance() |
| | | 1523 | | { |
| | 10 | 1524 | | if (_raid is null) |
| | | 1525 | | { |
| | 0 | 1526 | | return 0; |
| | | 1527 | | } |
| | | 1528 | | |
| | 10 | 1529 | | return _raidEncumbrance ?? CombatBalance.GetTotalEncumbrance(_raid.Inventory.GetExtractableItems()); |
| | | 1530 | | } |
| | | 1531 | | |
| | | 1532 | | private int GetRaidMaxEncumbrance() |
| | | 1533 | | { |
| | 10 | 1534 | | if (_raid is null) |
| | | 1535 | | { |
| | 0 | 1536 | | return 0; |
| | | 1537 | | } |
| | | 1538 | | |
| | 10 | 1539 | | return _raidMaxEncumbrance ?? _raid.MaxEncumbrance; |
| | | 1540 | | } |
| | | 1541 | | |
| | | 1542 | | private string GetRaidEncumbranceText() |
| | | 1543 | | { |
| | 5 | 1544 | | return $"{GetRaidEncumbrance()}/{GetRaidMaxEncumbrance()} lbs"; |
| | | 1545 | | } |
| | | 1546 | | |
| | | 1547 | | private static bool IsEquipableRaidItem(Item item) |
| | | 1548 | | { |
| | 3 | 1549 | | return item.Type is ItemType.Weapon or ItemType.Armor or ItemType.Backpack; |
| | | 1550 | | } |
| | | 1551 | | |
| | | 1552 | | private bool CanEquipRaidItem(Item item) |
| | | 1553 | | { |
| | 3 | 1554 | | if (_raid is null || !IsEquipableRaidItem(item)) |
| | | 1555 | | { |
| | 0 | 1556 | | return false; |
| | | 1557 | | } |
| | | 1558 | | |
| | 3 | 1559 | | var carriedItems = CurrentCarriedLoot.ToList(); |
| | 4 | 1560 | | var carriedIndex = carriedItems.FindIndex(current => ReferenceEquals(current, item)); |
| | 3 | 1561 | | var itemWasCarried = carriedIndex >= 0; |
| | 3 | 1562 | | if (carriedIndex >= 0) |
| | | 1563 | | { |
| | 0 | 1564 | | carriedItems.RemoveAt(carriedIndex); |
| | | 1565 | | } |
| | 3 | 1566 | | var currentEncumbrance = GetRaidEncumbrance(); |
| | 3 | 1567 | | var projectedEncumbrance = currentEncumbrance; |
| | 9 | 1568 | | var replacedItem = GetEquippedItems().FirstOrDefault(existing => existing.Type == item.Type); |
| | 3 | 1569 | | if (replacedItem is not null) |
| | | 1570 | | { |
| | 0 | 1571 | | projectedEncumbrance -= Math.Max(0, replacedItem.Weight); |
| | | 1572 | | } |
| | | 1573 | | |
| | 3 | 1574 | | if (item.Type == ItemType.Backpack) |
| | | 1575 | | { |
| | 0 | 1576 | | var backpackCapacity = CombatBalance.GetBackpackCapacity(item); |
| | 0 | 1577 | | var currentSlots = carriedItems.Sum(x => x.Slots); |
| | 0 | 1578 | | var spilledWeight = 0; |
| | 0 | 1579 | | while (currentSlots > backpackCapacity && carriedItems.Count > 0) |
| | | 1580 | | { |
| | 0 | 1581 | | var spill = carriedItems[^1]; |
| | 0 | 1582 | | carriedItems.RemoveAt(carriedItems.Count - 1); |
| | 0 | 1583 | | currentSlots -= spill.Slots; |
| | 0 | 1584 | | spilledWeight += Math.Max(0, spill.Weight); |
| | | 1585 | | } |
| | | 1586 | | |
| | 0 | 1587 | | projectedEncumbrance -= spilledWeight; |
| | | 1588 | | } |
| | | 1589 | | |
| | 3 | 1590 | | if (!itemWasCarried) |
| | | 1591 | | { |
| | 3 | 1592 | | projectedEncumbrance += Math.Max(0, item.Weight); |
| | | 1593 | | } |
| | | 1594 | | |
| | 3 | 1595 | | return projectedEncumbrance <= GetRaidMaxEncumbrance(); |
| | | 1596 | | } |
| | | 1597 | | |
| | | 1598 | | private IEnumerable<Item> GetEquippedItems() |
| | | 1599 | | { |
| | 3 | 1600 | | if (_raid?.Inventory.EquippedWeapon is not null) |
| | | 1601 | | { |
| | 3 | 1602 | | yield return _raid.Inventory.EquippedWeapon; |
| | | 1603 | | } |
| | | 1604 | | |
| | 3 | 1605 | | if (_raid?.Inventory.EquippedArmor is not null) |
| | | 1606 | | { |
| | 0 | 1607 | | yield return _raid.Inventory.EquippedArmor; |
| | | 1608 | | } |
| | | 1609 | | |
| | 3 | 1610 | | if (_raid?.Inventory.EquippedBackpack is not null) |
| | | 1611 | | { |
| | 3 | 1612 | | yield return _raid.Inventory.EquippedBackpack; |
| | | 1613 | | } |
| | 3 | 1614 | | } |
| | | 1615 | | |
| | | 1616 | | private async Task AttemptExtractAsync() |
| | | 1617 | | { |
| | 2 | 1618 | | if (_raid is null) |
| | | 1619 | | { |
| | 1 | 1620 | | return; |
| | | 1621 | | } |
| | 1 | 1622 | | await ExecuteRaidActionAsync("attempt-extract", new { }); |
| | 2 | 1623 | | } |
| | | 1624 | | |
| | | 1625 | | private async Task GoDeeper() |
| | | 1626 | | { |
| | 2 | 1627 | | if (_raid is null) |
| | | 1628 | | { |
| | 1 | 1629 | | return; |
| | | 1630 | | } |
| | | 1631 | | |
| | 1 | 1632 | | await ExecuteRaidActionAsync("go-deeper", new { }); |
| | 2 | 1633 | | } |
| | | 1634 | | |
| | | 1635 | | private async Task MoveTowardExtract() |
| | | 1636 | | { |
| | 2 | 1637 | | if (_raid is null) |
| | | 1638 | | { |
| | 1 | 1639 | | return; |
| | | 1640 | | } |
| | | 1641 | | |
| | 1 | 1642 | | await ExecuteRaidActionAsync("move-toward-extract", new { }); |
| | 2 | 1643 | | } |
| | | 1644 | | |
| | | 1645 | | private async Task StartExtractHoldAsync() |
| | | 1646 | | { |
| | 4 | 1647 | | if (_raid is null || IsExtractHoldEffectivelyActive() || _extractHoldResolutionInFlight) |
| | | 1648 | | { |
| | 2 | 1649 | | return; |
| | | 1650 | | } |
| | | 1651 | | |
| | 2 | 1652 | | await ExecuteRaidActionAsync("start-extract-hold", new { }); |
| | 4 | 1653 | | } |
| | | 1654 | | |
| | | 1655 | | private async Task CancelExtractHoldAsync() |
| | | 1656 | | { |
| | 1 | 1657 | | if (_raid is null || !_extractHoldActive || _extractHoldResolutionInFlight) |
| | | 1658 | | { |
| | 0 | 1659 | | return; |
| | | 1660 | | } |
| | | 1661 | | |
| | 1 | 1662 | | await ExecuteRaidActionAsync("cancel-extract-hold", new { }); |
| | 1 | 1663 | | } |
| | | 1664 | | |
| | | 1665 | | private async Task ResolveExpiredExtractHoldAsync() |
| | | 1666 | | { |
| | 1 | 1667 | | if (_raid is null || !HasExpiredExtractHold() || _extractHoldResolutionInFlight) |
| | | 1668 | | { |
| | 0 | 1669 | | return; |
| | | 1670 | | } |
| | | 1671 | | |
| | 1 | 1672 | | _extractHoldResolutionInFlight = true; |
| | | 1673 | | try |
| | | 1674 | | { |
| | 1 | 1675 | | await ExecuteRaidActionAsync("resolve-extract-hold", new |
| | 1 | 1676 | | { |
| | 1 | 1677 | | holdAtExtractUntil = _holdAtExtractUntil?.ToString("O") |
| | 1 | 1678 | | }); |
| | 1 | 1679 | | } |
| | | 1680 | | finally |
| | | 1681 | | { |
| | 1 | 1682 | | _extractHoldResolutionInFlight = false; |
| | | 1683 | | } |
| | 1 | 1684 | | } |
| | | 1685 | | |
| | | 1686 | | private bool IsExtractHoldEffectivelyActive() |
| | | 1687 | | { |
| | 4 | 1688 | | if (!_extractHoldActive) |
| | | 1689 | | { |
| | 2 | 1690 | | return false; |
| | | 1691 | | } |
| | | 1692 | | |
| | 2 | 1693 | | return _holdAtExtractUntil is null || _holdAtExtractUntil > DateTimeOffset.UtcNow; |
| | | 1694 | | } |
| | | 1695 | | |
| | | 1696 | | private bool HasExpiredExtractHold() |
| | | 1697 | | { |
| | 1 | 1698 | | return _extractHoldActive |
| | 1 | 1699 | | && _holdAtExtractUntil is not null |
| | 1 | 1700 | | && _holdAtExtractUntil <= DateTimeOffset.UtcNow; |
| | | 1701 | | } |
| | | 1702 | | |
| | | 1703 | | private ValueTask ReportHandledErrorAsync(string message, string source, Exception exception, object? context = null |
| | | 1704 | | { |
| | 3 | 1705 | | return Telemetry.ReportErrorAsync( |
| | 3 | 1706 | | message, |
| | 3 | 1707 | | new |
| | 3 | 1708 | | { |
| | 3 | 1709 | | source, |
| | 3 | 1710 | | exception = exception.GetType().FullName, |
| | 3 | 1711 | | exceptionMessage = exception.Message, |
| | 3 | 1712 | | stack = exception.ToString(), |
| | 3 | 1713 | | context |
| | 3 | 1714 | | }); |
| | | 1715 | | } |
| | | 1716 | | |
| | | 1717 | | private int GetLootSlotCount() |
| | | 1718 | | { |
| | 0 | 1719 | | return _raid?.Inventory.CarriedItems.Sum(x => x.Slots) ?? 0; |
| | | 1720 | | } |
| | | 1721 | | |
| | | 1722 | | private int GetReallocateStatCost() |
| | | 1723 | | { |
| | 2 | 1724 | | return (int)Math.Round(_money / 2m, MidpointRounding.AwayFromZero); |
| | | 1725 | | } |
| | | 1726 | | |
| | | 1727 | | private string GetReallocateStatCostLabel() |
| | | 1728 | | { |
| | 0 | 1729 | | return $"${GetReallocateStatCost()}"; |
| | | 1730 | | } |
| | | 1731 | | |
| | | 1732 | | private int GetBuyPrice(Item item) |
| | | 1733 | | { |
| | 0 | 1734 | | return CombatBalance.GetShopPrice( |
| | 0 | 1735 | | CombatBalance.GetBuyPrice(item), |
| | 0 | 1736 | | CombatBalance.GetCharismaModifier(_acceptedStats.Charisma), |
| | 0 | 1737 | | isBuying: true); |
| | | 1738 | | } |
| | | 1739 | | |
| | | 1740 | | private bool CanBuyItem(Item item) |
| | | 1741 | | { |
| | 8 | 1742 | | return item.Rarity <= CombatBalance.GetMaxShopRarityFromChaBonus( |
| | 8 | 1743 | | CombatBalance.GetCharismaModifier(_acceptedStats.Charisma)); |
| | | 1744 | | } |
| | | 1745 | | |
| | | 1746 | | private int GetSellPrice(Item item) |
| | | 1747 | | { |
| | 4 | 1748 | | if (item.ItemDefId == FallbackKnifeItemDefId) |
| | | 1749 | | { |
| | 0 | 1750 | | return 0; |
| | | 1751 | | } |
| | | 1752 | | |
| | 4 | 1753 | | return item.Value; |
| | | 1754 | | } |
| | | 1755 | | |
| | | 1756 | | private bool CanSellItem(Item item) |
| | | 1757 | | { |
| | 0 | 1758 | | return GetSellPrice(item) > 0; |
| | | 1759 | | } |
| | | 1760 | | |
| | | 1761 | | private bool CanLootItem(Item item) |
| | | 1762 | | { |
| | 2 | 1763 | | if (_raid is null) |
| | | 1764 | | { |
| | 0 | 1765 | | return false; |
| | | 1766 | | } |
| | | 1767 | | |
| | 2 | 1768 | | var currentEncumbrance = GetRaidEncumbrance(); |
| | 2 | 1769 | | if (currentEncumbrance + Math.Max(0, item.Weight) > GetRaidMaxEncumbrance()) |
| | | 1770 | | { |
| | 2 | 1771 | | return false; |
| | | 1772 | | } |
| | | 1773 | | |
| | 0 | 1774 | | if (item.ItemDefId == MedkitItemDefId) |
| | | 1775 | | { |
| | 0 | 1776 | | return true; |
| | | 1777 | | } |
| | | 1778 | | |
| | 0 | 1779 | | var currentSlots = _raid.Inventory.CarriedItems.Sum(x => x.Slots); |
| | 0 | 1780 | | return currentSlots + item.Slots <= _raid.BackpackCapacity; |
| | | 1781 | | } |
| | | 1782 | | |
| | | 1783 | | private void EnsureMainCharacterHasWeaponFallback() |
| | | 1784 | | { |
| | 28 | 1785 | | var stashHasWeapon = _mainGame.Stash.Any(item => item.Type == ItemType.Weapon); |
| | 32 | 1786 | | var onPersonHasWeapon = _onPersonItems.Any(entry => entry.Item.Type == ItemType.Weapon); |
| | 20 | 1787 | | if (!stashHasWeapon && !onPersonHasWeapon) |
| | | 1788 | | { |
| | 4 | 1789 | | _mainGame.Stash.Add(ItemCatalog.Create("Rusty Knife")); |
| | | 1790 | | } |
| | 20 | 1791 | | } |
| | | 1792 | | |
| | | 1793 | | private void NormalizeEquippedSlots() |
| | | 1794 | | { |
| | 19 | 1795 | | NormalizeEquippedSlot(ItemType.Weapon); |
| | 19 | 1796 | | NormalizeEquippedSlot(ItemType.Armor); |
| | 19 | 1797 | | NormalizeEquippedSlot(ItemType.Backpack); |
| | 19 | 1798 | | } |
| | | 1799 | | |
| | | 1800 | | private void NormalizeEquippedSlot(ItemType slotType) |
| | | 1801 | | { |
| | 57 | 1802 | | var found = false; |
| | 192 | 1803 | | for (var i = 0; i < _onPersonItems.Count; i++) |
| | | 1804 | | { |
| | 39 | 1805 | | var entry = _onPersonItems[i]; |
| | 39 | 1806 | | if (entry.Item.Type != slotType || !entry.IsEquipped) |
| | | 1807 | | { |
| | | 1808 | | continue; |
| | | 1809 | | } |
| | | 1810 | | |
| | 9 | 1811 | | if (!found) |
| | | 1812 | | { |
| | 9 | 1813 | | found = true; |
| | 9 | 1814 | | continue; |
| | | 1815 | | } |
| | | 1816 | | |
| | 0 | 1817 | | _onPersonItems[i] = entry with { IsEquipped = false }; |
| | | 1818 | | } |
| | 57 | 1819 | | } |
| | | 1820 | | |
| | | 1821 | | private void ApplySnapshot(PlayerSnapshot snapshot) |
| | | 1822 | | { |
| | 14 | 1823 | | _mainGame = new GameState([.. snapshot.MainStash]); |
| | 14 | 1824 | | _randomCharacter = snapshot.RandomCharacter is null |
| | 14 | 1825 | | ? null |
| | 14 | 1826 | | : new RandomCharacterState(snapshot.RandomCharacter.Name, [.. snapshot.RandomCharacter.Inventory], snapshot. |
| | 14 | 1827 | | _randomCharacterAvailableAt = snapshot.RandomCharacterAvailableAt; |
| | | 1828 | | |
| | 14 | 1829 | | if (_randomCharacter is not null && _randomCharacter.Inventory.Count == 0) |
| | | 1830 | | { |
| | 2 | 1831 | | _randomCharacter = null; |
| | | 1832 | | } |
| | | 1833 | | |
| | 14 | 1834 | | _money = snapshot.Money; |
| | 14 | 1835 | | _shopStock = snapshot.ShopStock |
| | 20 | 1836 | | .Select(offer => ItemCatalog.TryGetByItemDefId(offer.ItemDefId, out var item) && item is not null |
| | 20 | 1837 | | ? new ShopStock(offer, item) |
| | 20 | 1838 | | : null) |
| | 20 | 1839 | | .Where(stock => stock is not null) |
| | 14 | 1840 | | .Cast<ShopStock>() |
| | 14 | 1841 | | .ToList(); |
| | 17 | 1842 | | _itemRulesById = snapshot.ItemRules.ToDictionary(rule => rule.ItemDefId); |
| | 14 | 1843 | | _acceptedStats = snapshot.AcceptedStats; |
| | 14 | 1844 | | _draftStats = snapshot.DraftStats; |
| | 14 | 1845 | | _availableStatPoints = snapshot.AvailableStatPoints; |
| | 14 | 1846 | | _statsAccepted = snapshot.StatsAccepted; |
| | 14 | 1847 | | _playerConstitution = snapshot.AcceptedStats.Constitution; |
| | 14 | 1848 | | _maxHealth = snapshot.PlayerMaxHealth; |
| | 14 | 1849 | | _onPersonItems = snapshot.OnPersonItems |
| | 5 | 1850 | | .Select(entry => new OnPersonEntry(entry.Item, entry.IsEquipped)) |
| | 14 | 1851 | | .ToList(); |
| | 14 | 1852 | | _raidEncumbrance = null; |
| | 14 | 1853 | | _raidMaxEncumbrance = null; |
| | | 1854 | | |
| | 14 | 1855 | | if (snapshot.ActiveRaid is null) |
| | | 1856 | | { |
| | 12 | 1857 | | _raid = null; |
| | 12 | 1858 | | _inRaid = false; |
| | 12 | 1859 | | _extractHoldActive = false; |
| | 12 | 1860 | | _holdAtExtractUntil = null; |
| | 12 | 1861 | | _extractHoldResolutionInFlight = false; |
| | 12 | 1862 | | _contactState = string.Empty; |
| | 12 | 1863 | | _surpriseSide = string.Empty; |
| | 12 | 1864 | | _initiativeWinner = string.Empty; |
| | 12 | 1865 | | _openingActionsRemaining = 0; |
| | 12 | 1866 | | _surprisePersistenceEligible = false; |
| | 12 | 1867 | | return; |
| | | 1868 | | } |
| | | 1869 | | |
| | 2 | 1870 | | ApplyActiveRaidSnapshot(snapshot.ActiveRaid); |
| | 2 | 1871 | | } |
| | | 1872 | | |
| | | 1873 | | private void ApplyActiveRaidSnapshot(RaidSnapshot snapshot) |
| | | 1874 | | { |
| | 4 | 1875 | | var broughtItems = (snapshot.EquippedItems ?? []).ToList(); |
| | 4 | 1876 | | var carriedItems = (snapshot.CarriedLoot ?? []).ToList(); |
| | 4 | 1877 | | _raid = new RaidState( |
| | 4 | 1878 | | snapshot.Health, |
| | 4 | 1879 | | RaidInventory.FromItems(broughtItems, carriedItems, snapshot.BackpackCapacity)); |
| | 4 | 1880 | | _raid.Inventory.DiscoveredLoot.Clear(); |
| | 4 | 1881 | | _raid.Inventory.DiscoveredLoot.AddRange(snapshot.DiscoveredLoot ?? []); |
| | 4 | 1882 | | _raid.Inventory.MedkitCount = snapshot.Medkits; |
| | 4 | 1883 | | _raid.Inventory.BackpackCapacity = snapshot.BackpackCapacity; |
| | 4 | 1884 | | _raidEncumbrance = snapshot.Encumbrance > 0 || snapshot.MaxEncumbrance > 0 ? snapshot.Encumbrance : null; |
| | 4 | 1885 | | _raidMaxEncumbrance = snapshot.MaxEncumbrance > 0 ? snapshot.MaxEncumbrance : null; |
| | 4 | 1886 | | _extractHoldActive = snapshot.ExtractHoldActive; |
| | 4 | 1887 | | _holdAtExtractUntil = snapshot.HoldAtExtractUntil; |
| | 4 | 1888 | | if (_raidMaxEncumbrance is not null) |
| | | 1889 | | { |
| | 0 | 1890 | | _raid.MaxEncumbrance = _raidMaxEncumbrance.Value; |
| | | 1891 | | } |
| | 4 | 1892 | | _inRaid = true; |
| | 4 | 1893 | | _awaitingDecision = snapshot.AwaitingDecision; |
| | 4 | 1894 | | _challenge = snapshot.Challenge; |
| | 4 | 1895 | | _distanceFromExtract = snapshot.DistanceFromExtract; |
| | 4 | 1896 | | _ammo = snapshot.Ammo; |
| | 4 | 1897 | | _encounterDescription = RaidPresentationCatalog.GetEncounterDescription( |
| | 4 | 1898 | | snapshot.EncounterDescriptionKey, |
| | 4 | 1899 | | Enum.TryParse<EncounterType>(snapshot.EncounterType, ignoreCase: true, out var snapshotEncounterType) |
| | 4 | 1900 | | ? snapshotEncounterType |
| | 4 | 1901 | | : EncounterType.Neutral, |
| | 4 | 1902 | | snapshot.EncounterDescription, |
| | 4 | 1903 | | snapshot.ExtractHoldActive); |
| | 4 | 1904 | | _contactState = string.IsNullOrWhiteSpace(snapshot.ContactState) ? string.Empty : snapshot.ContactState; |
| | 4 | 1905 | | _surpriseSide = string.IsNullOrWhiteSpace(snapshot.SurpriseSide) ? string.Empty : snapshot.SurpriseSide; |
| | 4 | 1906 | | _initiativeWinner = string.IsNullOrWhiteSpace(snapshot.InitiativeWinner) ? string.Empty : snapshot.InitiativeWin |
| | 4 | 1907 | | _openingActionsRemaining = snapshot.OpeningActionsRemaining; |
| | 4 | 1908 | | _surprisePersistenceEligible = snapshot.SurprisePersistenceEligible; |
| | 4 | 1909 | | _enemyName = RaidPresentationCatalog.GetEnemyLabel(snapshot.EnemyKey, snapshot.EnemyName); |
| | 4 | 1910 | | _enemyHealth = snapshot.EnemyHealth; |
| | 4 | 1911 | | _enemyDexterity = snapshot.EnemyDexterity; |
| | 4 | 1912 | | _enemyConstitution = snapshot.EnemyConstitution; |
| | 4 | 1913 | | _enemyStrength = snapshot.EnemyStrength; |
| | 4 | 1914 | | _lootContainer = snapshot.LootContainer; |
| | 4 | 1915 | | _log.Clear(); |
| | 4 | 1916 | | _log.AddRange((snapshot.LogEntries ?? []).Select(RaidPresentationCatalog.LocalizeLogEntry)); |
| | | 1917 | | |
| | 4 | 1918 | | if (!Enum.TryParse<EncounterType>(snapshot.EncounterType, ignoreCase: true, out var encounterType)) |
| | | 1919 | | { |
| | 0 | 1920 | | encounterType = EncounterType.Neutral; |
| | | 1921 | | } |
| | | 1922 | | |
| | 4 | 1923 | | _encounterType = encounterType; |
| | 4 | 1924 | | } |
| | | 1925 | | |
| | | 1926 | | public void Dispose() |
| | | 1927 | | { |
| | 0 | 1928 | | _clockTimer?.Dispose(); |
| | 0 | 1929 | | } |
| | | 1930 | | |
| | | 1931 | | } |