From f56198e9cb8195efd9c0aeb3ac70b0625613d46b Mon Sep 17 00:00:00 2001 From: Tiger Date: Sat, 14 Oct 2023 19:35:03 +0200 Subject: [PATCH] Added catalogue features pages + add some extra checks in catalogue index + pages --- .../Incoming/Catalog/GetCatalogIndexEvent.cs | 11 +++++--- .../Incoming/Catalog/GetCatalogPageEvent.cs | 9 +++++-- .../Catalog/CatalogPageMessageComposer.cs | 26 ++++++++++++++++++- .../Catalog/CatalogPagesListComposer.cs | 2 +- Game/Catalogue/CatalogueFeaturedPage.cs | 11 ++++++++ Game/Catalogue/CatalogueFeaturedPageMap.cs | 17 ++++++++++++ Game/Catalogue/CatalogueFeaturedPageType.cs | 8 ++++++ Game/Catalogue/CatalogueManager.cs | 15 ++++++++++- Game/Catalogue/ICatalogueManager.cs | 2 ++ Program.cs | 1 + 10 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 Game/Catalogue/CatalogueFeaturedPage.cs create mode 100644 Game/Catalogue/CatalogueFeaturedPageMap.cs create mode 100644 Game/Catalogue/CatalogueFeaturedPageType.cs diff --git a/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs b/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs index 90c5969..df6c436 100644 --- a/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs +++ b/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs @@ -25,10 +25,13 @@ public class GetCatalogIndexEvent : IMessageEvent await _gameSessionManager.CloseAsync("Not logged in", gameSession); return; } - - var categories = _catalogueManager.Pages.Values.Where(p => p.Parent is null); - await gameSession.SendComposerAsync(new CatalogPagesListComposer(categories, - request.ReadString() ?? "normal")); + var mode = request.ReadString() ?? "normal"; + + var categories = + _catalogueManager.Pages.Values.Where( + p => p.Parent is null && p.Modes.Contains(mode) && p.MinRank <= gameSession.Habbo.Rank); + + await gameSession.SendComposerAsync(new CatalogPagesListComposer(categories, mode)); } } \ No newline at end of file diff --git a/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs b/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs index 8582409..7e16944 100644 --- a/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs +++ b/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs @@ -30,11 +30,16 @@ public class GetCatalogPageEvent : IMessageEvent var offerId = request.ReadInt32() ?? 0; var mode = request.ReadString() ?? "normal"; - if (!_catalogueManager.Pages.TryGetValue(pageId, out var page)) + if (!_catalogueManager.Pages.TryGetValue(pageId, out var page) || !page.Enabled || + page.MinRank > gameSession.Habbo.Rank || !page.Modes.Contains(mode)) { return; } - await gameSession.SendComposerAsync(new CatalogPageMessageComposer(page, offerId, mode)); + var featuredPages = page.Layout.Equals("frontpage4") + ? _catalogueManager.FeaturedPages.Values + : new List(); + + await gameSession.SendComposerAsync(new CatalogPageMessageComposer(page, offerId, mode, featuredPages)); } } \ No newline at end of file diff --git a/Communication/Messages/Outgoing/Catalog/CatalogPageMessageComposer.cs b/Communication/Messages/Outgoing/Catalog/CatalogPageMessageComposer.cs index 9afe70c..c5ce42e 100644 --- a/Communication/Messages/Outgoing/Catalog/CatalogPageMessageComposer.cs +++ b/Communication/Messages/Outgoing/Catalog/CatalogPageMessageComposer.cs @@ -9,12 +9,15 @@ public class CatalogPageMessageComposer : IMessageComposer private readonly CataloguePage _page; private readonly int _offerId; private readonly string _mode; + private readonly ICollection _featuredPages; - public CatalogPageMessageComposer(CataloguePage page, int offerId, string mode) + public CatalogPageMessageComposer(CataloguePage page, int offerId, string mode, + ICollection featuredPages) { _page = page; _offerId = offerId; _mode = mode; + _featuredPages = featuredPages; } public OutgoingHeaders Header => OutgoingHeaders.CatalogPage; @@ -40,5 +43,26 @@ public class CatalogPageMessageComposer : IMessageComposer message.AppendInt32(0); message.AppendInt32(_offerId); message.AppendBoolean(_page.SeasonalCurrency); + message.AppendInt32(_featuredPages.Count); + + foreach (var featuredPage in _featuredPages) + { + message.AppendInt32(featuredPage.SlotId); + message.AppendString(featuredPage.Caption); + message.AppendString(featuredPage.Image); + message.AppendInt32((int)featuredPage.Type); + switch (featuredPage.Type) + { + default: + case CatalogueFeaturedPageType.PageName: + case CatalogueFeaturedPageType.ProductName: + message.AppendString(featuredPage.Data); + break; + case CatalogueFeaturedPageType.PageId: + message.AppendInt32(int.TryParse(featuredPage.Data, out var pageId) ? pageId : -1); + break; + } + message.AppendInt32((int)(DateTime.Now - featuredPage.Expire).TotalSeconds); + } } } \ No newline at end of file diff --git a/Communication/Messages/Outgoing/Catalog/CatalogPagesListComposer.cs b/Communication/Messages/Outgoing/Catalog/CatalogPagesListComposer.cs index 4224711..6fe09bd 100644 --- a/Communication/Messages/Outgoing/Catalog/CatalogPagesListComposer.cs +++ b/Communication/Messages/Outgoing/Catalog/CatalogPagesListComposer.cs @@ -21,7 +21,7 @@ public class CatalogPagesListComposer : IMessageComposer message.AppendBoolean(true); message.AppendInt32(0); message.AppendInt32(-1); - message.AppendString(string.Empty); + message.AppendString("root"); message.AppendString(string.Empty); message.AppendInt32(0); message.AppendInt32(_pages.Count()); diff --git a/Game/Catalogue/CatalogueFeaturedPage.cs b/Game/Catalogue/CatalogueFeaturedPage.cs new file mode 100644 index 0000000..79641cc --- /dev/null +++ b/Game/Catalogue/CatalogueFeaturedPage.cs @@ -0,0 +1,11 @@ +namespace Tiger.Game.Catalogue; + +public class CatalogueFeaturedPage +{ + public virtual int SlotId { get; set; } + public virtual string Image { get; set; } = null!; + public virtual string Caption { get; set; } = null!; + public virtual CatalogueFeaturedPageType Type { get; set; } + public virtual DateTime Expire { get; set; } + public virtual string Data { get; set; } = null!; +} \ No newline at end of file diff --git a/Game/Catalogue/CatalogueFeaturedPageMap.cs b/Game/Catalogue/CatalogueFeaturedPageMap.cs new file mode 100644 index 0000000..aedbb8b --- /dev/null +++ b/Game/Catalogue/CatalogueFeaturedPageMap.cs @@ -0,0 +1,17 @@ +using FluentNHibernate.Mapping; + +namespace Tiger.Game.Catalogue; + +public class CatalogueFeaturedPageMap : ClassMap +{ + public CatalogueFeaturedPageMap() + { + Table("catalogue_featured_pages"); + Id(fp => fp.SlotId).Column("slot_id").GeneratedBy.Identity(); + Map(fp => fp.Image).Column("image").Not.Nullable(); + Map(fp => fp.Caption).Column("caption").Not.Nullable(); + Map(fp => fp.Type).CustomType().Column("type").Not.Nullable(); + Map(fp => fp.Expire).Column("expire").Not.Nullable(); + Map(fp => fp.Data).Column("data").Not.Nullable(); + } +} \ No newline at end of file diff --git a/Game/Catalogue/CatalogueFeaturedPageType.cs b/Game/Catalogue/CatalogueFeaturedPageType.cs new file mode 100644 index 0000000..c4bce1a --- /dev/null +++ b/Game/Catalogue/CatalogueFeaturedPageType.cs @@ -0,0 +1,8 @@ +namespace Tiger.Game.Catalogue; + +public enum CatalogueFeaturedPageType +{ + PageName, + PageId, + ProductName +} \ No newline at end of file diff --git a/Game/Catalogue/CatalogueManager.cs b/Game/Catalogue/CatalogueManager.cs index 646db5c..44c84ae 100644 --- a/Game/Catalogue/CatalogueManager.cs +++ b/Game/Catalogue/CatalogueManager.cs @@ -7,14 +7,19 @@ public class CatalogueManager : ICatalogueManager { private readonly IRepository _pagesRepository; private readonly ILogger _logger; + private readonly IRepository _featuredPagesRepository; public IDictionary Pages { get; private set; } + public IDictionary FeaturedPages { get; private set; } - public CatalogueManager(IRepository pagesRepository, ILogger logger) + public CatalogueManager(IRepository pagesRepository, ILogger logger, + IRepository featuredPagesRepository) { _pagesRepository = pagesRepository; _logger = logger; + _featuredPagesRepository = featuredPagesRepository; Pages = new Dictionary(); + FeaturedPages = new Dictionary(); } public async Task LoadPagesAsync() @@ -23,4 +28,12 @@ public class CatalogueManager : ICatalogueManager _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); + } } \ No newline at end of file diff --git a/Game/Catalogue/ICatalogueManager.cs b/Game/Catalogue/ICatalogueManager.cs index dd33833..b9e1987 100644 --- a/Game/Catalogue/ICatalogueManager.cs +++ b/Game/Catalogue/ICatalogueManager.cs @@ -3,5 +3,7 @@ namespace Tiger.Game.Catalogue; public interface ICatalogueManager { public IDictionary Pages { get; } + public IDictionary FeaturedPages { get; } Task LoadPagesAsync(); + Task LoadFeaturedPagesAsync(); } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 99fb035..cfdbc66 100644 --- a/Program.cs +++ b/Program.cs @@ -47,6 +47,7 @@ var provider = collection.BuildServiceProvider(); await provider.GetRequiredService().ReloadSettingsAsync(); await provider.GetRequiredService().LoadPagesAsync(); +await provider.GetRequiredService().LoadFeaturedPagesAsync(); provider.GetRequiredService(); await provider.GetRequiredService().LoadPromoArticlesAsync(); await provider.GetRequiredService().LoadAchievementsAsync();