using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Eft.Common; using SPTarkov.Server.Core.Models.Eft.Common.Tables; using SPTarkov.Server.Core.Models.Eft.Customization; using SPTarkov.Server.Core.Models.Eft.Hideout; using SPTarkov.Server.Core.Models.Eft.ItemEvent; using SPTarkov.Server.Core.Models.Eft.Trade; using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Routers; using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils.Cloners; namespace SPTarkov.Server.Core.Controllers; [Injectable] public class CustomizationController( ISptLogger _logger, EventOutputHolder _eventOutputHolder, DatabaseService _databaseService, SaveServer _saveServer, LocalisationService _localisationService, ProfileHelper _profileHelper, ICloner _cloner, PaymentService _paymentService ) { protected const string _lowerParentClothingId = "5cd944d01388ce000a659df9"; protected const string _upperParentClothingId = "5cd944ca1388ce03a44dc2a4"; /// /// Get purchasable clothing items from trader that match players side (usec/bear) /// /// trader to look up clothing for /// Session id /// Suit array public List GetTraderSuits(string traderId, string sessionId) { var pmcData = _profileHelper.GetPmcProfile(sessionId); var clothing = _databaseService.GetCustomization(); var suits = _databaseService.GetTrader(traderId).Suits; var matchingSuits = suits?.Where(s => clothing.ContainsKey(s.SuiteId!)).ToList(); matchingSuits = matchingSuits ?.Where(s => clothing[s.SuiteId ?? string.Empty] ?.Properties?.Side?.Contains(pmcData?.Info?.Side ?? string.Empty) ?? false ) .ToList(); if (matchingSuits == null) { throw new Exception( _localisationService.GetText("customisation-unable_to_get_trader_suits", traderId) ); } return matchingSuits; } /// /// Handle CustomizationBuy event /// Purchase/unlock a clothing item from a trader /// /// Player profile /// Request object /// Session id /// ItemEventRouterResponse public ItemEventRouterResponse BuyCustomisation( PmcData pmcData, BuyClothingRequestData buyClothingRequest, string sessionId ) { var output = _eventOutputHolder.GetOutput(sessionId); var traderOffer = GetTraderClothingOffer(sessionId, buyClothingRequest.Offer); if (traderOffer is null) { _logger.Error( _localisationService.GetText( "customisation-unable_to_find_suit_by_id", buyClothingRequest.Offer ) ); return output; } var suitId = traderOffer.SuiteId; if (OutfitAlreadyPurchased(suitId ?? string.Empty, sessionId)) { var suitDetails = _databaseService.GetCustomization()!.GetValueOrDefault(suitId); _logger.Error( _localisationService.GetText( "customisation-item_already_purchased", new { itemId = suitDetails?.Id, itemName = suitDetails?.Name } ) ); return output; } // Charge player for buying item PayForClothingItems(sessionId, pmcData, buyClothingRequest.Items, output); var profile = _saveServer.GetProfile(sessionId); // TODO: Merge with function _profileHelper.addHideoutCustomisationUnlock var rewardToStore = new CustomisationStorage { Id = suitId, Source = CustomisationSource.UNLOCKED_IN_GAME, Type = CustomisationType.SUITE, }; profile.CustomisationUnlocks.Add(rewardToStore); return output; } /// /// Has an outfit been purchased by a player /// /// clothing id /// Session id of profile to check for clothing in /// true if already purchased protected bool OutfitAlreadyPurchased(object suitId, string sessionId) { var fullProfile = _profileHelper.GetFullProfile(sessionId); // Check if clothing can be found by id return fullProfile.CustomisationUnlocks.Exists(customisation => Equals(customisation.Id, suitId) ); } /// /// Get clothing offer from trader by suit id /// /// Session/Player id /// /// Suit protected Suit? GetTraderClothingOffer(string sessionId, string? offerId) { var foundSuit = GetAllTraderSuits(sessionId).FirstOrDefault(s => s.Id == offerId); if (foundSuit is null) { _logger.Error( _localisationService.GetText("customisation-unable_to_find_suit_with_id", offerId) ); } return foundSuit; } /// /// Update output object and player profile with purchase details /// /// Session id /// Player profile /// Clothing purchased /// Client response protected void PayForClothingItems( string sessionId, PmcData pmcData, List? itemsToPayForClothingWith, ItemEventRouterResponse output ) { if (itemsToPayForClothingWith is null || itemsToPayForClothingWith.Count == 0) { return; } foreach (var inventoryItemToProcess in itemsToPayForClothingWith) { var options = new ProcessBuyTradeRequestData { SchemeItems = [ new IdWithCount { Count = inventoryItemToProcess.Count.Value, Id = inventoryItemToProcess.Id, }, ], TransactionId = Traders.RAGMAN, Action = "BuyCustomization", Type = "", ItemId = "", Count = 0, SchemeId = 0, }; _paymentService.PayMoney(pmcData, options, sessionId, output); } } /// /// Get all suits from Traders /// /// Session/Player id /// protected List GetAllTraderSuits(string sessionId) { var traders = _databaseService.GetTraders(); var result = new List(); foreach (var (traderId, trader) in traders) { if ( trader.Base?.CustomizationSeller is not null && trader.Base.CustomizationSeller.Value ) { result.AddRange(GetTraderSuits(traderId, sessionId)); } } return result; } /// /// Handle client/hideout/customization/offer/list /// /// Session/Player id /// public HideoutCustomisation GetHideoutCustomisation(string sessionId) { return _databaseService.GetHideout().Customisation!; } /// /// Handle client/customization/storage /// /// Session/Player id /// public List GetCustomisationStorage(string sessionId) { var customisationResultsClone = _cloner.Clone( _databaseService.GetTemplates().CustomisationStorage ); var profile = _profileHelper.GetFullProfile(sessionId); if (profile is null) { return customisationResultsClone!; } customisationResultsClone!.AddRange(profile.CustomisationUnlocks ?? []); return customisationResultsClone; } /// /// Handle CustomizationSet event /// /// Session/Player id /// /// Players PMC profile /// public ItemEventRouterResponse SetCustomisation( string sessionId, CustomizationSetRequest request, PmcData pmcData ) { foreach (var customisation in request.Customizations) { switch (customisation.Type) { case "dogTag": pmcData.Customization!.DogTag = customisation.Id; break; case "suite": ApplyClothingItemToProfile(customisation, pmcData); break; default: _logger.Error($"Unhandled customisation type: {customisation.Type}"); break; } } return _eventOutputHolder.GetOutput(sessionId); } /// /// Applies a purchased suit to the players doll /// /// Suit to apply to profile /// Profile to update protected void ApplyClothingItemToProfile(CustomizationSetOption customisation, PmcData pmcData) { var dbSuit = _databaseService.GetCustomization()[customisation.Id!]; if (dbSuit is null) { _logger.Error( $"Unable to find suit customisation id: {customisation.Id}, cannot apply clothing to player profile: {pmcData.Id}" ); return; } // Body if (dbSuit.Parent == _upperParentClothingId) { pmcData.Customization.Body = dbSuit.Properties.Body; pmcData.Customization.Hands = dbSuit.Properties.Hands; return; } // Feet if (dbSuit.Parent == _lowerParentClothingId) { pmcData.Customization.Feet = dbSuit.Properties.Feet; } } }