using Microsoft.Extensions.Logging; using Tiger.Communication.Messages.Outgoing.Catalog; using Tiger.Communication.Messages.Outgoing.Inventory.Currency; using Tiger.Communication.Messages.Outgoing.Inventory.Subscription; using Tiger.Communication.Messages.Outgoing.Notifications; using Tiger.Game.Achievements; using Tiger.Game.Habbos; using Tiger.Networking.Game.Sessions; using Tiger.Storage; namespace Tiger.Game.Catalogue; public class CatalogueManager : ICatalogueManager { private readonly IRepository _pagesRepository; private readonly ILogger _logger; private readonly IRepository _featuredPagesRepository; private readonly IRepository _clubOfferRepository; private readonly IRepository _habboSubscriptionRepository; private readonly IAchievementManager _achievementManager; public IDictionary Pages { get; private set; } public IDictionary FeaturedPages { get; private set; } public IDictionary ClubOffers { get; private set; } public CatalogueManager(IRepository pagesRepository, ILogger logger, IRepository featuredPagesRepository, IRepository clubOfferRepository, IRepository habboSubscriptionRepository, IAchievementManager achievementManager) { _pagesRepository = pagesRepository; _logger = logger; _featuredPagesRepository = featuredPagesRepository; _clubOfferRepository = clubOfferRepository; _habboSubscriptionRepository = habboSubscriptionRepository; _achievementManager = achievementManager; Pages = new Dictionary(); FeaturedPages = new Dictionary(); ClubOffers = new Dictionary(); } public async Task LoadPagesAsync() { Pages = (await _pagesRepository.FindByAsync()).ToDictionary(p => p.Id, p => p); _logger.LogInformation("Loaded {Count} catalogue pages", Pages.Count); } public async Task LoadFeaturedPagesAsync() { FeaturedPages = (await _featuredPagesRepository.FindByAsync()) .ToDictionary(p => p.SlotId, p => p); _logger.LogInformation("Loaded {Count} catalogue featured pages", FeaturedPages.Count); } public async Task LoadClubOffersAsync() { ClubOffers = (await _clubOfferRepository.FindByAsync()) .ToDictionary(co => co.Id); _logger.LogInformation("Loaded {Count} club offers", ClubOffers.Count); } public async Task PurchaseClubOffer(int offerId, GameSession gameSession) { if (!ClubOffers.TryGetValue(offerId, out var clubOffer) || gameSession.Habbo == null) { return; } var tooShortOnCoins = clubOffer.PriceCredits > 0 && clubOffer.PriceCredits > gameSession.Habbo.Credits; var tooShortOnActivityPoints = clubOffer.PriceActivitypoints > 0 && clubOffer.PriceActivitypoints > gameSession.Habbo.GetActivityPoints(clubOffer.ActivitypointsType); if (tooShortOnCoins || tooShortOnActivityPoints) { await gameSession.SendComposerAsync(new NotEnoughBalanceMessageComposer(tooShortOnCoins, tooShortOnActivityPoints, clubOffer.ActivitypointsType)); return; } if (clubOffer.PriceCredits > 0) { gameSession.Habbo.Credits -= clubOffer.PriceCredits; gameSession.SendComposerAsync(new UserCreditsComposer(gameSession.Habbo.Credits)); } if (clubOffer.PriceActivitypoints > 0) { gameSession.Habbo.UpdateCurrency(clubOffer.ActivitypointsType, -clubOffer.PriceActivitypoints); gameSession.SendComposerAsync(new ActivityPointNotificationMessageComposer( gameSession.Habbo.GetActivityPoints(clubOffer.ActivitypointsType), -clubOffer.PriceActivitypoints, clubOffer.ActivitypointsType)); } var currentSubscription = gameSession.Habbo.GetActiveSubscription(); if (currentSubscription != null) { currentSubscription.Expires = currentSubscription.Expires.AddDays(clubOffer.Days); currentSubscription.Habbo.Credits -= clubOffer.PriceCredits; currentSubscription.Habbo.UpdateCurrency(clubOffer.ActivitypointsType, -clubOffer.PriceActivitypoints); _habboSubscriptionRepository.SaveAsync(currentSubscription); gameSession.SendComposerAsync(new UserSubscriptionComposer(currentSubscription, gameSession.Habbo.GetPastSubscriptionDays())); } else { var subscription = new HabboSubscription { Expires = DateTime.Now.AddDays(clubOffer.Days), SubscriptionType = "habbo_club", Started = DateTime.Now, Habbo = gameSession.Habbo }; _habboSubscriptionRepository.SaveAsync(subscription); gameSession.SendComposerAsync(new UserSubscriptionComposer(subscription, gameSession.Habbo.GetPastSubscriptionDays())); _achievementManager.UpdateAchievementAsync("ACH_HC", 1, gameSession); } } }