From 66744ead65061d54eb86ad35307ff78069564755 Mon Sep 17 00:00:00 2001 From: Tiger Date: Fri, 29 Sep 2023 20:20:28 +0200 Subject: [PATCH] Added badges --- Communication/Messages/IMessageHandler.cs | 2 +- .../Messages/Incoming/IncomingHeaders.cs | 2 +- .../Inventory/Badges/GetBadgesEvent.cs | 28 +++++++++ .../Badges/SetActivatedBadgesEvent.cs | 59 +++++++++++++++++++ .../Users/GetSelectedBadgesMessageEvent.cs | 37 ++++++++++++ .../Inventory/Badges/BadgesComposer.cs | 37 ++++++++++++ .../Messages/Outgoing/OutgoingHeaders.cs | 2 +- .../Users/HabboUserBadgesMessageComposer.cs | 30 ++++++++++ Game/Habbos/Badge.cs | 9 +++ Game/Habbos/BadgeMap.cs | 16 +++++ Game/Habbos/Habbo.cs | 1 + Game/Habbos/HabboMap.cs | 1 + 12 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 Communication/Messages/Incoming/Inventory/Badges/GetBadgesEvent.cs create mode 100644 Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs create mode 100644 Communication/Messages/Incoming/Users/GetSelectedBadgesMessageEvent.cs create mode 100644 Communication/Messages/Outgoing/Inventory/Badges/BadgesComposer.cs create mode 100644 Communication/Messages/Outgoing/Users/HabboUserBadgesMessageComposer.cs create mode 100644 Game/Habbos/Badge.cs create mode 100644 Game/Habbos/BadgeMap.cs diff --git a/Communication/Messages/IMessageHandler.cs b/Communication/Messages/IMessageHandler.cs index 654f85c..f20b71d 100644 --- a/Communication/Messages/IMessageHandler.cs +++ b/Communication/Messages/IMessageHandler.cs @@ -5,5 +5,5 @@ namespace Tiger.Communication.Messages; public interface IMessageHandler { - Task TryHandleAsync(GameSession session, ClientMessage request); + Task TryHandleAsync(GameSession gameSession, ClientMessage request); } \ No newline at end of file diff --git a/Communication/Messages/Incoming/IncomingHeaders.cs b/Communication/Messages/Incoming/IncomingHeaders.cs index b7b04ce..6023ad6 100644 --- a/Communication/Messages/Incoming/IncomingHeaders.cs +++ b/Communication/Messages/Incoming/IncomingHeaders.cs @@ -150,7 +150,7 @@ public enum IncomingHeaders : short GetRoomEntryTileEvent = 3559, GetRoomSettingsEvent = 3129, GetRoomVisitsEvent = 3526, - GetSelectedBadgesEvent = 2091, + GetSelectedBadgesMessageEvent = 2091, GetSellablePetPalettesEvent = 1756, GetSongInfoEvent = 3082, GetSoundSettingsEvent = 2388, diff --git a/Communication/Messages/Incoming/Inventory/Badges/GetBadgesEvent.cs b/Communication/Messages/Incoming/Inventory/Badges/GetBadgesEvent.cs new file mode 100644 index 0000000..85df059 --- /dev/null +++ b/Communication/Messages/Incoming/Inventory/Badges/GetBadgesEvent.cs @@ -0,0 +1,28 @@ +using Tiger.Communication.Messages.Interfaces; +using Tiger.Communication.Messages.Outgoing.Inventory.Badges; +using Tiger.Communication.Messages.Types; +using Tiger.Networking.Game.Sessions; + +namespace Tiger.Communication.Messages.Incoming.Inventory.Badges; + +public class GetBadgesEvent : IMessageEvent +{ + private readonly IGameSessionManager _gameSessionManager; + + public GetBadgesEvent(IGameSessionManager gameSessionManager) + { + _gameSessionManager = gameSessionManager; + } + + public IncomingHeaders Header => IncomingHeaders.GetBadgesEvent; + public async Task HandleAsync(GameSession gameSession, ClientMessage request) + { + if (gameSession.Habbo == null) + { + await _gameSessionManager.CloseAsync("Not logged in", gameSession); + return; + } + + await gameSession.SendComposerAsync(new BadgesComposer(gameSession.Habbo.Badges)); + } +} \ No newline at end of file diff --git a/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs b/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs new file mode 100644 index 0000000..8324a5a --- /dev/null +++ b/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs @@ -0,0 +1,59 @@ +using System.Collections.ObjectModel; +using Tiger.Communication.Messages.Interfaces; +using Tiger.Communication.Messages.Outgoing.Users; +using Tiger.Communication.Messages.Types; +using Tiger.Game.Habbos; +using Tiger.Networking.Game.Sessions; +using Tiger.Storage; + +namespace Tiger.Communication.Messages.Incoming.Inventory.Badges; + +public class SetActivatedBadgesEvent : IMessageEvent +{ + private readonly IGameSessionManager _gameSessionManager; + private readonly IRepository _badgeRepository; + + public SetActivatedBadgesEvent(IGameSessionManager gameSessionManager, IRepository badgeRepository) + { + _gameSessionManager = gameSessionManager; + _badgeRepository = badgeRepository; + } + + public IncomingHeaders Header => IncomingHeaders.SetActivatedBadgesEvent; + public async Task HandleAsync(GameSession gameSession, ClientMessage request) + { + if (gameSession.Habbo == null) + { + await _gameSessionManager.CloseAsync("Not logged in", gameSession); + return; + } + + if (gameSession.Habbo.Badges == null) + return; + + var wearingBadges = new Collection(); + foreach (var currentBadge in gameSession.Habbo.Badges) + { + currentBadge.Slot = 0; + } + + for (var i = 0; i < 5; i++) + { + var slotId = request.ReadInt32(); + var code = request.ReadString(); + + if (slotId == null || slotId is < 1 or > 5 || string.IsNullOrEmpty(code)) continue; + + var badge = gameSession.Habbo.Badges.FirstOrDefault(b => b.Code == code); + + if (badge == null) continue; + + badge.Slot = slotId.Value; + wearingBadges.Add(badge); + + _badgeRepository.SaveAsync(badge); + } + + await gameSession.SendComposerAsync(new HabboUserBadgesMessageComposer(gameSession.Habbo.Id, wearingBadges)); + } +} \ No newline at end of file diff --git a/Communication/Messages/Incoming/Users/GetSelectedBadgesMessageEvent.cs b/Communication/Messages/Incoming/Users/GetSelectedBadgesMessageEvent.cs new file mode 100644 index 0000000..eaac7eb --- /dev/null +++ b/Communication/Messages/Incoming/Users/GetSelectedBadgesMessageEvent.cs @@ -0,0 +1,37 @@ +using Tiger.Communication.Messages.Interfaces; +using Tiger.Communication.Messages.Outgoing.Users; +using Tiger.Communication.Messages.Types; +using Tiger.Game.Habbos; +using Tiger.Networking.Game.Sessions; +using Tiger.Storage; + +namespace Tiger.Communication.Messages.Incoming.Users; + +public class GetSelectedBadgesMessageEvent : IMessageEvent +{ + private readonly IGameSessionManager _gameSessionManager; + private readonly IRepository _badgeRepository; + + public GetSelectedBadgesMessageEvent(IGameSessionManager gameSessionManager, IRepository badgeRepository) + { + _gameSessionManager = gameSessionManager; + _badgeRepository = badgeRepository; + } + + public IncomingHeaders Header => IncomingHeaders.GetSelectedBadgesMessageEvent; + public async Task HandleAsync(GameSession gameSession, ClientMessage request) + { + if (gameSession.Habbo == null) + { + await _gameSessionManager.CloseAsync("Not logged in", gameSession); + return; + } + + var habboId = request.ReadInt32(); + + if (habboId == null) return; + + var badges = await _badgeRepository.FindByAsync(b => b.Habbo.Id == habboId && b.Slot > 0); + await gameSession.SendComposerAsync(new HabboUserBadgesMessageComposer(habboId.Value, badges.ToList())); + } +} \ No newline at end of file diff --git a/Communication/Messages/Outgoing/Inventory/Badges/BadgesComposer.cs b/Communication/Messages/Outgoing/Inventory/Badges/BadgesComposer.cs new file mode 100644 index 0000000..50634d6 --- /dev/null +++ b/Communication/Messages/Outgoing/Inventory/Badges/BadgesComposer.cs @@ -0,0 +1,37 @@ +using Tiger.Communication.Messages.Interfaces; +using Tiger.Communication.Messages.Types; +using Tiger.Game.Habbos; + +namespace Tiger.Communication.Messages.Outgoing.Inventory.Badges; + +public class BadgesComposer : IMessageComposer +{ + private readonly IReadOnlyList _badges; + + public BadgesComposer(IEnumerable? badges) + { + _badges = badges != null ? badges.ToList() : new List(); + } + + public OutgoingHeaders Header => OutgoingHeaders.BadgesComposer; + public void Compose(ServerMessage message) + { + message.AppendInt32(_badges.Count); + + foreach (var badge in _badges) + { + message.AppendInt32(badge.Id); + message.AppendString(badge.Code); + } + + var wearingBadges = _badges.Where(b => b.Slot > 0).ToList(); + + message.AppendInt32(wearingBadges.Count); + + foreach (var wearingBadge in wearingBadges) + { + message.AppendInt32(wearingBadge.Slot); + message.AppendString(wearingBadge.Code); + } + } +} \ No newline at end of file diff --git a/Communication/Messages/Outgoing/OutgoingHeaders.cs b/Communication/Messages/Outgoing/OutgoingHeaders.cs index 8f5c3d2..c12a13b 100644 --- a/Communication/Messages/Outgoing/OutgoingHeaders.cs +++ b/Communication/Messages/Outgoing/OutgoingHeaders.cs @@ -432,7 +432,7 @@ public enum OutgoingHeaders UpdateStackHeightTileHeightComposer = 2816, UpdateThreadMessageComposer = 2528, UseObjectMessageComposer = 1774, - UserBadgesComposer = 1087, + HabboUserBadgesMessageComposer = 1087, UserBannedMessageComposer = 1683, UserBcLimitsComposer = -1, UserChangeMessageComposer = 3920, diff --git a/Communication/Messages/Outgoing/Users/HabboUserBadgesMessageComposer.cs b/Communication/Messages/Outgoing/Users/HabboUserBadgesMessageComposer.cs new file mode 100644 index 0000000..56c9d32 --- /dev/null +++ b/Communication/Messages/Outgoing/Users/HabboUserBadgesMessageComposer.cs @@ -0,0 +1,30 @@ +using Tiger.Communication.Messages.Interfaces; +using Tiger.Communication.Messages.Types; +using Tiger.Game.Habbos; + +namespace Tiger.Communication.Messages.Outgoing.Users; + +public class HabboUserBadgesMessageComposer : IMessageComposer +{ + private readonly int _habboId; + private readonly ICollection _badges; + + public HabboUserBadgesMessageComposer(int habboId, ICollection badges) + { + _habboId = habboId; + _badges = badges; + } + + public OutgoingHeaders Header => OutgoingHeaders.HabboUserBadgesMessageComposer; + public void Compose(ServerMessage message) + { + message.AppendInt32(_habboId); + message.AppendInt32(_badges.Count); + + foreach (var badge in _badges) + { + message.AppendInt32(badge.Slot); + message.AppendString(badge.Code); + } + } +} \ No newline at end of file diff --git a/Game/Habbos/Badge.cs b/Game/Habbos/Badge.cs new file mode 100644 index 0000000..d325443 --- /dev/null +++ b/Game/Habbos/Badge.cs @@ -0,0 +1,9 @@ +namespace Tiger.Game.Habbos; + +public class Badge +{ + public virtual int Id { get; set; } + public virtual Habbo Habbo { get; set; } = null!; + public virtual string Code { get; set; } = null!; + public virtual int Slot { get; set; } +} \ No newline at end of file diff --git a/Game/Habbos/BadgeMap.cs b/Game/Habbos/BadgeMap.cs new file mode 100644 index 0000000..ea4ca6e --- /dev/null +++ b/Game/Habbos/BadgeMap.cs @@ -0,0 +1,16 @@ +using FluentNHibernate.Mapping; + +namespace Tiger.Game.Habbos; + +public class BadgeMap : ClassMap +{ + public BadgeMap() + { + Table("habbo_badges"); + LazyLoad(); + Id(b => b.Id).Column("id").GeneratedBy.Identity(); + References(b => b.Habbo).Column("habbo_id").Not.Nullable(); + Map(b => b.Code).Column("code").Not.Nullable(); + Map(b => b.Slot).Column("slot").Not.Nullable(); + } +} \ No newline at end of file diff --git a/Game/Habbos/Habbo.cs b/Game/Habbos/Habbo.cs index d31fc7c..89a8c9d 100644 --- a/Game/Habbos/Habbo.cs +++ b/Game/Habbos/Habbo.cs @@ -18,4 +18,5 @@ public class Habbo public virtual int? GroupId { get; set; } public virtual string? SsoTicket { get; set; } public virtual IEnumerable? Activitypoints { get; set; } + public virtual IEnumerable? Badges { get; set; } } \ No newline at end of file diff --git a/Game/Habbos/HabboMap.cs b/Game/Habbos/HabboMap.cs index 71211ce..7e373b3 100644 --- a/Game/Habbos/HabboMap.cs +++ b/Game/Habbos/HabboMap.cs @@ -24,5 +24,6 @@ public class HabboMap : ClassMap Map(h => h.GroupId).Column("group_id").Nullable(); Map(h => h.SsoTicket).Column("sso_ticket").Nullable(); HasMany(h => h.Activitypoints).LazyLoad().Inverse().Cascade.All(); + HasMany(h => h.Badges).LazyLoad().Inverse().Cascade.All(); } } \ No newline at end of file