From 6e9b6d04495fb99d85cce0c8c9020198d1fc2444 Mon Sep 17 00:00:00 2001 From: Tiger Date: Sat, 23 Sep 2023 18:51:16 +0200 Subject: [PATCH] Replaced uint to int and added profiles packet and some logging changes --- .../Incoming/Handshake/SsoTicketEvent.cs | 10 ++++- .../Incoming/Users/GetExtendedProfileEvent.cs | 42 +++++++++++++++++++ .../Handshake/UserObjectMessageComposer.cs | 2 +- .../Notifications/ActivityPointsComposer.cs | 2 +- .../Outgoing/Users/CreditBalanceComposer.cs | 4 +- .../Outgoing/Users/ExtendedProfileComposer.cs | 34 +++++++++++++++ Communication/Messages/Types/ServerMessage.cs | 5 --- Game/Habbos/Activitypoints.cs | 4 +- Game/Habbos/Habbo.cs | 12 +++--- Networking/Game/WebSocketServer.cs | 22 ++++++++-- Storage/Repository.cs | 1 + TigerEmu.csproj | 3 -- Utils/ByteUtils.cs | 7 +--- 13 files changed, 117 insertions(+), 31 deletions(-) create mode 100644 Communication/Messages/Incoming/Users/GetExtendedProfileEvent.cs create mode 100644 Communication/Messages/Outgoing/Users/ExtendedProfileComposer.cs diff --git a/Communication/Messages/Incoming/Handshake/SsoTicketEvent.cs b/Communication/Messages/Incoming/Handshake/SsoTicketEvent.cs index 31026e4..336b3cb 100644 --- a/Communication/Messages/Incoming/Handshake/SsoTicketEvent.cs +++ b/Communication/Messages/Incoming/Handshake/SsoTicketEvent.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Logging; using Tiger.Communication.Messages.Interfaces; using Tiger.Communication.Messages.Outgoing.Generic.Alerts; using Tiger.Communication.Messages.Outgoing.Handshake; @@ -14,12 +15,14 @@ public class SsoTicketEvent : IMessageEvent private readonly IRepository _habboRepository; private readonly IGameSessionManager _gameSessionManager; private readonly ISettingManager _settingManager; + private readonly ILogger _logger; - public SsoTicketEvent(IRepository habboRepository, IGameSessionManager gameSessionManager, ISettingManager settingManager) + public SsoTicketEvent(IRepository habboRepository, IGameSessionManager gameSessionManager, ISettingManager settingManager, ILogger logger) { _habboRepository = habboRepository; _gameSessionManager = gameSessionManager; _settingManager = settingManager; + _logger = logger; } public IncomingHeaders Header => IncomingHeaders.SSoTicketEvent; @@ -43,6 +46,11 @@ public class SsoTicketEvent : IMessageEvent } gameSession.Habbo = habbo; + gameSession.Habbo.LastLogin = DateTime.Now; + gameSession.Habbo.Online = true; + _habboRepository.SaveAsync(gameSession.Habbo); + + _logger.LogInformation("{User} logged in", gameSession.Habbo.Username); await gameSession.SendComposerAsync(new AuthenticationOkMessageComposer()); await gameSession.SendComposerAsync(new HabboBroadcastMessageComposer( diff --git a/Communication/Messages/Incoming/Users/GetExtendedProfileEvent.cs b/Communication/Messages/Incoming/Users/GetExtendedProfileEvent.cs new file mode 100644 index 0000000..9c46536 --- /dev/null +++ b/Communication/Messages/Incoming/Users/GetExtendedProfileEvent.cs @@ -0,0 +1,42 @@ +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 GetExtendedProfileEvent : IMessageEvent +{ + private readonly IGameSessionManager _gameSessionManager; + private readonly IRepository _habboRepository; + + public GetExtendedProfileEvent(IGameSessionManager gameSessionManager, IRepository habboRepository) + { + _gameSessionManager = gameSessionManager; + _habboRepository = habboRepository; + } + + public IncomingHeaders Header => IncomingHeaders.GetExtendedProfileEvent; + public async Task HandleAsync(GameSession gameSession, ClientMessage request) + { + var habboId = request.ReadInt32(); + + if (habboId == null) + { + await _gameSessionManager.CloseAsync("Malformed packet", gameSession); + return; + } + + var habbo = await _habboRepository.FindAsync(habboId); + + if (habbo == null) + { + await _gameSessionManager.CloseAsync("Habbo not found", gameSession); + return; + } + + await gameSession.SendComposerAsync(new ExtendedProfileComposer(habbo)); + } +} \ No newline at end of file diff --git a/Communication/Messages/Outgoing/Handshake/UserObjectMessageComposer.cs b/Communication/Messages/Outgoing/Handshake/UserObjectMessageComposer.cs index e9b4240..386db52 100644 --- a/Communication/Messages/Outgoing/Handshake/UserObjectMessageComposer.cs +++ b/Communication/Messages/Outgoing/Handshake/UserObjectMessageComposer.cs @@ -17,7 +17,7 @@ public class UserObjectMessageComposer : IMessageComposer public OutgoingHeaders Header => OutgoingHeaders.UserObjectComposer; public void Compose(ServerMessage message) { - message.AppendUInt32(_habbo.Id); + message.AppendInt32(_habbo.Id); message.AppendString(_habbo.Username); message.AppendString(_habbo.Figure); message.AppendString(_habbo.Gender); diff --git a/Communication/Messages/Outgoing/Notifications/ActivityPointsComposer.cs b/Communication/Messages/Outgoing/Notifications/ActivityPointsComposer.cs index 9bd3390..314f310 100644 --- a/Communication/Messages/Outgoing/Notifications/ActivityPointsComposer.cs +++ b/Communication/Messages/Outgoing/Notifications/ActivityPointsComposer.cs @@ -23,7 +23,7 @@ public class ActivityPointsComposer : IMessageComposer foreach (var activitypoint in _activitypoints) { message.AppendInt32(activitypoint.Type); - message.AppendUInt32(activitypoint.Amount); + message.AppendInt32(activitypoint.Amount); } } } \ No newline at end of file diff --git a/Communication/Messages/Outgoing/Users/CreditBalanceComposer.cs b/Communication/Messages/Outgoing/Users/CreditBalanceComposer.cs index 5040a35..d9fac55 100644 --- a/Communication/Messages/Outgoing/Users/CreditBalanceComposer.cs +++ b/Communication/Messages/Outgoing/Users/CreditBalanceComposer.cs @@ -5,9 +5,9 @@ namespace Tiger.Communication.Messages.Outgoing.Users; public class CreditBalanceComposer : IMessageComposer { - private readonly uint _credits; + private readonly int _credits; - public CreditBalanceComposer(uint credits) + public CreditBalanceComposer(int credits) { _credits = credits; } diff --git a/Communication/Messages/Outgoing/Users/ExtendedProfileComposer.cs b/Communication/Messages/Outgoing/Users/ExtendedProfileComposer.cs new file mode 100644 index 0000000..799f0d4 --- /dev/null +++ b/Communication/Messages/Outgoing/Users/ExtendedProfileComposer.cs @@ -0,0 +1,34 @@ +using System.Globalization; +using Tiger.Communication.Messages.Interfaces; +using Tiger.Communication.Messages.Types; +using Tiger.Game.Habbos; + +namespace Tiger.Communication.Messages.Outgoing.Users; + +public class ExtendedProfileComposer : IMessageComposer +{ + private readonly Habbo _habbo; + + public ExtendedProfileComposer(Habbo habbo) + { + _habbo = habbo; + } + + public OutgoingHeaders Header => OutgoingHeaders.ExtendedProfileMessageComposer; + public void Compose(ServerMessage message) + { + message.AppendInt32(_habbo.Id); + message.AppendString(_habbo.Username); + message.AppendString(_habbo.Figure); + message.AppendString(_habbo.Motto); + message.AppendString(_habbo.AccountCreated.ToString(CultureInfo.CurrentCulture)); + message.AppendInt32(_habbo.AchievementScore); + message.AppendInt32(0); // friend count + message.AppendBoolean(false); // is friend + message.AppendBoolean(false); // friend request sent + message.AppendBoolean(_habbo.Online); + message.AppendInt32(0); // groups + message.AppendInt32((int)(_habbo.LastLogin == null ? 0 : (DateTime.Now - _habbo.LastLogin.Value).TotalSeconds)); + message.AppendBoolean(true); // open but why would it not open? + } +} \ No newline at end of file diff --git a/Communication/Messages/Types/ServerMessage.cs b/Communication/Messages/Types/ServerMessage.cs index 4abd177..7e6852a 100644 --- a/Communication/Messages/Types/ServerMessage.cs +++ b/Communication/Messages/Types/ServerMessage.cs @@ -21,11 +21,6 @@ public class ServerMessage { _packet.AddRange(ByteUtils.Int32ToArray(value)); } - - public void AppendUInt32(uint value) - { - _packet.AddRange(ByteUtils.UInt32ToArray(value)); - } public void AppendBoolean(bool value) { diff --git a/Game/Habbos/Activitypoints.cs b/Game/Habbos/Activitypoints.cs index caa37fe..262107a 100644 --- a/Game/Habbos/Activitypoints.cs +++ b/Game/Habbos/Activitypoints.cs @@ -3,8 +3,8 @@ namespace Tiger.Game.Habbos; public class Activitypoints { public virtual Habbo Habbo { get; set; } = null!; - public virtual byte Type { get; set; } - public virtual uint Amount { get; set; } + public virtual int Type { get; set; } + public virtual int Amount { get; set; } public override bool Equals(object? obj) { diff --git a/Game/Habbos/Habbo.cs b/Game/Habbos/Habbo.cs index 883f092..d31fc7c 100644 --- a/Game/Habbos/Habbo.cs +++ b/Game/Habbos/Habbo.cs @@ -2,7 +2,7 @@ namespace Tiger.Game.Habbos; public class Habbo { - public virtual uint Id { get; set; } + public virtual int Id { get; set; } public virtual string Username { get; set; } = null!; public virtual string Email { get; set; } = null!; public virtual DateTime AccountCreated { get; set; } @@ -10,12 +10,12 @@ public class Habbo public virtual string Motto { get; set; } = null!; public virtual string Figure { get; set; } = null!; public virtual string Gender { get; set; } = null!; - public virtual byte Rank { get; set; } - public virtual uint Credits { get; set; } + public virtual int Rank { get; set; } + public virtual int Credits { get; set; } public virtual bool Online { get; set; } - public virtual uint HomeRoom { get; set; } - public virtual uint AchievementScore { get; set; } - public virtual uint? GroupId { get; set; } + public virtual int HomeRoom { get; set; } + public virtual int AchievementScore { get; set; } + public virtual int? GroupId { get; set; } public virtual string? SsoTicket { get; set; } public virtual IEnumerable? Activitypoints { get; set; } } \ No newline at end of file diff --git a/Networking/Game/WebSocketServer.cs b/Networking/Game/WebSocketServer.cs index 67b3d50..9549ee8 100644 --- a/Networking/Game/WebSocketServer.cs +++ b/Networking/Game/WebSocketServer.cs @@ -1,8 +1,11 @@ using System.Net; using System.Net.WebSockets; +using Microsoft.Extensions.Logging; using Tiger.Communication.Messages; using Tiger.Communication.Messages.Types; +using Tiger.Game.Habbos; using Tiger.Networking.Game.Sessions; +using Tiger.Storage; using Tiger.Utils; namespace Tiger.Networking.Game; @@ -12,11 +15,15 @@ public class WebSocketServer : IWebSocketServer private readonly HttpListener _httpListener = new(); private readonly IGameSessionManager _gameSessionManager; private readonly IMessageHandler _messageHandler; + private readonly IRepository _habboRepository; + private readonly ILogger _logger; - public WebSocketServer(IGameSessionManager gameSessionManager, IMessageHandler messageHandler) + public WebSocketServer(IGameSessionManager gameSessionManager, IMessageHandler messageHandler, IRepository habboRepository, ILogger logger) { _gameSessionManager = gameSessionManager; _messageHandler = messageHandler; + _habboRepository = habboRepository; + _logger = logger; } public async Task Start(string uriPrefix) @@ -24,7 +31,7 @@ public class WebSocketServer : IWebSocketServer _httpListener.Prefixes.Add(uriPrefix); _httpListener.Start(); - Console.WriteLine($"Listening on {uriPrefix}..."); + _logger.LogInformation("WebSocket server running on {uriPrefix}...", uriPrefix); while (true) { @@ -52,7 +59,6 @@ public class WebSocketServer : IWebSocketServer webSocket = webSocketContext.WebSocket; var session = _gameSessionManager.AddSession(webSocket); - Console.WriteLine($"WebSocket Session {session.SessionId} has connected."); await ReceiveMessageAsync(session.SessionId); } @@ -73,7 +79,8 @@ public class WebSocketServer : IWebSocketServer private async Task ReceiveMessageAsync(string sessionId) { var buffer = new byte[1024 * 4]; - while (_gameSessionManager.GetSession(sessionId) is { WebSocket.State: WebSocketState.Open } gameSession) + GameSession? gameSession; + while ((gameSession = _gameSessionManager.GetSession(sessionId)) is { WebSocket.State: WebSocketState.Open }) { var result = await gameSession.WebSocket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); var offset = 0; @@ -100,5 +107,12 @@ public class WebSocketServer : IWebSocketServer await gameSession.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } } + + if (gameSession is { Habbo: not null }) + { + gameSession.Habbo.Online = false; + await _habboRepository.SaveAsync(gameSession.Habbo); + _logger.LogInformation("{User} logged out", gameSession.Habbo.Username); + } } } \ No newline at end of file diff --git a/Storage/Repository.cs b/Storage/Repository.cs index 868c198..4070c69 100644 --- a/Storage/Repository.cs +++ b/Storage/Repository.cs @@ -21,6 +21,7 @@ public class Repository : IRepository where T : class public async Task SaveAsync(T entity) { await _session.SaveOrUpdateAsync(entity); + await _session.FlushAsync(); } public async Task> FindByAsync(Expression>? expression) diff --git a/TigerEmu.csproj b/TigerEmu.csproj index 29e06e8..2fedcd8 100644 --- a/TigerEmu.csproj +++ b/TigerEmu.csproj @@ -26,7 +26,4 @@ PreserveNewest - - - diff --git a/Utils/ByteUtils.cs b/Utils/ByteUtils.cs index 05b4a07..6a31382 100644 --- a/Utils/ByteUtils.cs +++ b/Utils/ByteUtils.cs @@ -6,16 +6,11 @@ public static class ByteUtils { return (array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3]; } - + public static short GetInt16(byte[] array) { return (short)((array[0] << 8) | array[1]); } - - public static IEnumerable UInt32ToArray(uint value) - { - return new[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value }; - } public static IEnumerable Int32ToArray(int value) {