TigerEmu/Game/Catalogue/CatalogueManager.cs

122 lines
5.3 KiB
C#
Raw Normal View History

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<CataloguePage> _pagesRepository;
private readonly ILogger<ICatalogueManager> _logger;
private readonly IRepository<CatalogueFeaturedPage> _featuredPagesRepository;
private readonly IRepository<ClubOffer> _clubOfferRepository;
private readonly IRepository<HabboSubscription> _habboSubscriptionRepository;
private readonly IAchievementManager _achievementManager;
public IDictionary<int, CataloguePage> Pages { get; private set; }
public IDictionary<int, CatalogueFeaturedPage> FeaturedPages { get; private set; }
public IDictionary<int, ClubOffer> ClubOffers { get; private set; }
public CatalogueManager(IRepository<CataloguePage> pagesRepository, ILogger<ICatalogueManager> logger,
IRepository<CatalogueFeaturedPage> featuredPagesRepository, IRepository<ClubOffer> clubOfferRepository,
IRepository<HabboSubscription> habboSubscriptionRepository, IAchievementManager achievementManager)
{
_pagesRepository = pagesRepository;
_logger = logger;
_featuredPagesRepository = featuredPagesRepository;
_clubOfferRepository = clubOfferRepository;
_habboSubscriptionRepository = habboSubscriptionRepository;
_achievementManager = achievementManager;
Pages = new Dictionary<int, CataloguePage>();
FeaturedPages = new Dictionary<int, CatalogueFeaturedPage>();
ClubOffers = new Dictionary<int, ClubOffer>();
}
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);
}
}
}