2023-10-06 16:12:35 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-10-21 05:19:21 +00:00
|
|
|
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;
|
2023-10-06 16:12:35 +00:00
|
|
|
using Tiger.Storage;
|
|
|
|
|
|
|
|
namespace Tiger.Game.Catalogue;
|
|
|
|
|
|
|
|
public class CatalogueManager : ICatalogueManager
|
|
|
|
{
|
|
|
|
private readonly IRepository<CataloguePage> _pagesRepository;
|
|
|
|
private readonly ILogger<ICatalogueManager> _logger;
|
2023-10-14 17:35:03 +00:00
|
|
|
private readonly IRepository<CatalogueFeaturedPage> _featuredPagesRepository;
|
2023-10-21 05:19:21 +00:00
|
|
|
private readonly IRepository<ClubOffer> _clubOfferRepository;
|
|
|
|
private readonly IRepository<HabboSubscription> _habboSubscriptionRepository;
|
|
|
|
private readonly IAchievementManager _achievementManager;
|
2023-10-06 16:12:35 +00:00
|
|
|
|
|
|
|
public IDictionary<int, CataloguePage> Pages { get; private set; }
|
2023-10-14 17:35:03 +00:00
|
|
|
public IDictionary<int, CatalogueFeaturedPage> FeaturedPages { get; private set; }
|
2023-10-21 05:19:21 +00:00
|
|
|
public IDictionary<int, ClubOffer> ClubOffers { get; private set; }
|
2023-10-06 16:12:35 +00:00
|
|
|
|
2023-10-14 17:35:03 +00:00
|
|
|
public CatalogueManager(IRepository<CataloguePage> pagesRepository, ILogger<ICatalogueManager> logger,
|
2023-10-21 05:19:21 +00:00
|
|
|
IRepository<CatalogueFeaturedPage> featuredPagesRepository, IRepository<ClubOffer> clubOfferRepository,
|
|
|
|
IRepository<HabboSubscription> habboSubscriptionRepository, IAchievementManager achievementManager)
|
2023-10-06 16:12:35 +00:00
|
|
|
{
|
|
|
|
_pagesRepository = pagesRepository;
|
|
|
|
_logger = logger;
|
2023-10-14 17:35:03 +00:00
|
|
|
_featuredPagesRepository = featuredPagesRepository;
|
2023-10-21 05:19:21 +00:00
|
|
|
_clubOfferRepository = clubOfferRepository;
|
|
|
|
_habboSubscriptionRepository = habboSubscriptionRepository;
|
|
|
|
_achievementManager = achievementManager;
|
2023-10-06 16:12:35 +00:00
|
|
|
Pages = new Dictionary<int, CataloguePage>();
|
2023-10-14 17:35:03 +00:00
|
|
|
FeaturedPages = new Dictionary<int, CatalogueFeaturedPage>();
|
2023-10-21 05:19:21 +00:00
|
|
|
ClubOffers = new Dictionary<int, ClubOffer>();
|
2023-10-06 16:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task LoadPagesAsync()
|
|
|
|
{
|
|
|
|
Pages = (await _pagesRepository.FindByAsync()).ToDictionary(p => p.Id, p => p);
|
|
|
|
|
|
|
|
_logger.LogInformation("Loaded {Count} catalogue pages", Pages.Count);
|
|
|
|
}
|
2023-10-14 17:35:03 +00:00
|
|
|
|
|
|
|
public async Task LoadFeaturedPagesAsync()
|
|
|
|
{
|
2023-10-21 05:19:21 +00:00
|
|
|
FeaturedPages = (await _featuredPagesRepository.FindByAsync())
|
2023-10-14 17:35:03 +00:00
|
|
|
.ToDictionary(p => p.SlotId, p => p);
|
|
|
|
|
|
|
|
_logger.LogInformation("Loaded {Count} catalogue featured pages", FeaturedPages.Count);
|
|
|
|
}
|
2023-10-21 05:19:21 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 16:12:35 +00:00
|
|
|
}
|