Replaced uint to int and added profiles packet and some logging changes
parent
5ed8eb6913
commit
6e9b6d0449
|
@ -1,3 +1,4 @@
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Tiger.Communication.Messages.Interfaces;
|
using Tiger.Communication.Messages.Interfaces;
|
||||||
using Tiger.Communication.Messages.Outgoing.Generic.Alerts;
|
using Tiger.Communication.Messages.Outgoing.Generic.Alerts;
|
||||||
using Tiger.Communication.Messages.Outgoing.Handshake;
|
using Tiger.Communication.Messages.Outgoing.Handshake;
|
||||||
|
@ -14,12 +15,14 @@ public class SsoTicketEvent : IMessageEvent
|
||||||
private readonly IRepository<Habbo> _habboRepository;
|
private readonly IRepository<Habbo> _habboRepository;
|
||||||
private readonly IGameSessionManager _gameSessionManager;
|
private readonly IGameSessionManager _gameSessionManager;
|
||||||
private readonly ISettingManager _settingManager;
|
private readonly ISettingManager _settingManager;
|
||||||
|
private readonly ILogger<SsoTicketEvent> _logger;
|
||||||
|
|
||||||
public SsoTicketEvent(IRepository<Habbo> habboRepository, IGameSessionManager gameSessionManager, ISettingManager settingManager)
|
public SsoTicketEvent(IRepository<Habbo> habboRepository, IGameSessionManager gameSessionManager, ISettingManager settingManager, ILogger<SsoTicketEvent> logger)
|
||||||
{
|
{
|
||||||
_habboRepository = habboRepository;
|
_habboRepository = habboRepository;
|
||||||
_gameSessionManager = gameSessionManager;
|
_gameSessionManager = gameSessionManager;
|
||||||
_settingManager = settingManager;
|
_settingManager = settingManager;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IncomingHeaders Header => IncomingHeaders.SSoTicketEvent;
|
public IncomingHeaders Header => IncomingHeaders.SSoTicketEvent;
|
||||||
|
@ -43,6 +46,11 @@ public class SsoTicketEvent : IMessageEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
gameSession.Habbo = habbo;
|
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 AuthenticationOkMessageComposer());
|
||||||
await gameSession.SendComposerAsync(new HabboBroadcastMessageComposer(
|
await gameSession.SendComposerAsync(new HabboBroadcastMessageComposer(
|
||||||
|
|
|
@ -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<Habbo> _habboRepository;
|
||||||
|
|
||||||
|
public GetExtendedProfileEvent(IGameSessionManager gameSessionManager, IRepository<Habbo> 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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ public class UserObjectMessageComposer : IMessageComposer
|
||||||
public OutgoingHeaders Header => OutgoingHeaders.UserObjectComposer;
|
public OutgoingHeaders Header => OutgoingHeaders.UserObjectComposer;
|
||||||
public void Compose(ServerMessage message)
|
public void Compose(ServerMessage message)
|
||||||
{
|
{
|
||||||
message.AppendUInt32(_habbo.Id);
|
message.AppendInt32(_habbo.Id);
|
||||||
message.AppendString(_habbo.Username);
|
message.AppendString(_habbo.Username);
|
||||||
message.AppendString(_habbo.Figure);
|
message.AppendString(_habbo.Figure);
|
||||||
message.AppendString(_habbo.Gender);
|
message.AppendString(_habbo.Gender);
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class ActivityPointsComposer : IMessageComposer
|
||||||
foreach (var activitypoint in _activitypoints)
|
foreach (var activitypoint in _activitypoints)
|
||||||
{
|
{
|
||||||
message.AppendInt32(activitypoint.Type);
|
message.AppendInt32(activitypoint.Type);
|
||||||
message.AppendUInt32(activitypoint.Amount);
|
message.AppendInt32(activitypoint.Amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,9 +5,9 @@ namespace Tiger.Communication.Messages.Outgoing.Users;
|
||||||
|
|
||||||
public class CreditBalanceComposer : IMessageComposer
|
public class CreditBalanceComposer : IMessageComposer
|
||||||
{
|
{
|
||||||
private readonly uint _credits;
|
private readonly int _credits;
|
||||||
|
|
||||||
public CreditBalanceComposer(uint credits)
|
public CreditBalanceComposer(int credits)
|
||||||
{
|
{
|
||||||
_credits = credits;
|
_credits = credits;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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?
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,11 +22,6 @@ public class ServerMessage
|
||||||
_packet.AddRange(ByteUtils.Int32ToArray(value));
|
_packet.AddRange(ByteUtils.Int32ToArray(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AppendUInt32(uint value)
|
|
||||||
{
|
|
||||||
_packet.AddRange(ByteUtils.UInt32ToArray(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AppendBoolean(bool value)
|
public void AppendBoolean(bool value)
|
||||||
{
|
{
|
||||||
_packet.Add((byte)(value ? 1 : 0));
|
_packet.Add((byte)(value ? 1 : 0));
|
||||||
|
|
|
@ -3,8 +3,8 @@ namespace Tiger.Game.Habbos;
|
||||||
public class Activitypoints
|
public class Activitypoints
|
||||||
{
|
{
|
||||||
public virtual Habbo Habbo { get; set; } = null!;
|
public virtual Habbo Habbo { get; set; } = null!;
|
||||||
public virtual byte Type { get; set; }
|
public virtual int Type { get; set; }
|
||||||
public virtual uint Amount { get; set; }
|
public virtual int Amount { get; set; }
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
public override bool Equals(object? obj)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@ namespace Tiger.Game.Habbos;
|
||||||
|
|
||||||
public class Habbo
|
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 Username { get; set; } = null!;
|
||||||
public virtual string Email { get; set; } = null!;
|
public virtual string Email { get; set; } = null!;
|
||||||
public virtual DateTime AccountCreated { get; set; }
|
public virtual DateTime AccountCreated { get; set; }
|
||||||
|
@ -10,12 +10,12 @@ public class Habbo
|
||||||
public virtual string Motto { get; set; } = null!;
|
public virtual string Motto { get; set; } = null!;
|
||||||
public virtual string Figure { get; set; } = null!;
|
public virtual string Figure { get; set; } = null!;
|
||||||
public virtual string Gender { get; set; } = null!;
|
public virtual string Gender { get; set; } = null!;
|
||||||
public virtual byte Rank { get; set; }
|
public virtual int Rank { get; set; }
|
||||||
public virtual uint Credits { get; set; }
|
public virtual int Credits { get; set; }
|
||||||
public virtual bool Online { get; set; }
|
public virtual bool Online { get; set; }
|
||||||
public virtual uint HomeRoom { get; set; }
|
public virtual int HomeRoom { get; set; }
|
||||||
public virtual uint AchievementScore { get; set; }
|
public virtual int AchievementScore { get; set; }
|
||||||
public virtual uint? GroupId { get; set; }
|
public virtual int? GroupId { get; set; }
|
||||||
public virtual string? SsoTicket { get; set; }
|
public virtual string? SsoTicket { get; set; }
|
||||||
public virtual IEnumerable<Activitypoints>? Activitypoints { get; set; }
|
public virtual IEnumerable<Activitypoints>? Activitypoints { get; set; }
|
||||||
}
|
}
|
|
@ -1,8 +1,11 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Tiger.Communication.Messages;
|
using Tiger.Communication.Messages;
|
||||||
using Tiger.Communication.Messages.Types;
|
using Tiger.Communication.Messages.Types;
|
||||||
|
using Tiger.Game.Habbos;
|
||||||
using Tiger.Networking.Game.Sessions;
|
using Tiger.Networking.Game.Sessions;
|
||||||
|
using Tiger.Storage;
|
||||||
using Tiger.Utils;
|
using Tiger.Utils;
|
||||||
|
|
||||||
namespace Tiger.Networking.Game;
|
namespace Tiger.Networking.Game;
|
||||||
|
@ -12,11 +15,15 @@ public class WebSocketServer : IWebSocketServer
|
||||||
private readonly HttpListener _httpListener = new();
|
private readonly HttpListener _httpListener = new();
|
||||||
private readonly IGameSessionManager _gameSessionManager;
|
private readonly IGameSessionManager _gameSessionManager;
|
||||||
private readonly IMessageHandler _messageHandler;
|
private readonly IMessageHandler _messageHandler;
|
||||||
|
private readonly IRepository<Habbo> _habboRepository;
|
||||||
|
private readonly ILogger<IWebSocketServer> _logger;
|
||||||
|
|
||||||
public WebSocketServer(IGameSessionManager gameSessionManager, IMessageHandler messageHandler)
|
public WebSocketServer(IGameSessionManager gameSessionManager, IMessageHandler messageHandler, IRepository<Habbo> habboRepository, ILogger<IWebSocketServer> logger)
|
||||||
{
|
{
|
||||||
_gameSessionManager = gameSessionManager;
|
_gameSessionManager = gameSessionManager;
|
||||||
_messageHandler = messageHandler;
|
_messageHandler = messageHandler;
|
||||||
|
_habboRepository = habboRepository;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Start(string uriPrefix)
|
public async Task Start(string uriPrefix)
|
||||||
|
@ -24,7 +31,7 @@ public class WebSocketServer : IWebSocketServer
|
||||||
_httpListener.Prefixes.Add(uriPrefix);
|
_httpListener.Prefixes.Add(uriPrefix);
|
||||||
_httpListener.Start();
|
_httpListener.Start();
|
||||||
|
|
||||||
Console.WriteLine($"Listening on {uriPrefix}...");
|
_logger.LogInformation("WebSocket server running on {uriPrefix}...", uriPrefix);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -52,7 +59,6 @@ public class WebSocketServer : IWebSocketServer
|
||||||
webSocket = webSocketContext.WebSocket;
|
webSocket = webSocketContext.WebSocket;
|
||||||
|
|
||||||
var session = _gameSessionManager.AddSession(webSocket);
|
var session = _gameSessionManager.AddSession(webSocket);
|
||||||
Console.WriteLine($"WebSocket Session {session.SessionId} has connected.");
|
|
||||||
|
|
||||||
await ReceiveMessageAsync(session.SessionId);
|
await ReceiveMessageAsync(session.SessionId);
|
||||||
}
|
}
|
||||||
|
@ -73,7 +79,8 @@ public class WebSocketServer : IWebSocketServer
|
||||||
private async Task ReceiveMessageAsync(string sessionId)
|
private async Task ReceiveMessageAsync(string sessionId)
|
||||||
{
|
{
|
||||||
var buffer = new byte[1024 * 4];
|
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<byte>(buffer), CancellationToken.None);
|
var result = await gameSession.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||||
var offset = 0;
|
var offset = 0;
|
||||||
|
@ -100,5 +107,12 @@ public class WebSocketServer : IWebSocketServer
|
||||||
await gameSession.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -21,6 +21,7 @@ public class Repository<T> : IRepository<T> where T : class
|
||||||
public async Task SaveAsync(T entity)
|
public async Task SaveAsync(T entity)
|
||||||
{
|
{
|
||||||
await _session.SaveOrUpdateAsync(entity);
|
await _session.SaveOrUpdateAsync(entity);
|
||||||
|
await _session.FlushAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<T>> FindByAsync(Expression<Func<T, bool>>? expression)
|
public async Task<IEnumerable<T>> FindByAsync(Expression<Func<T, bool>>? expression)
|
||||||
|
|
|
@ -26,7 +26,4 @@
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Communication\Messages\Incoming\Users\" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -12,11 +12,6 @@ public static class ByteUtils
|
||||||
return (short)((array[0] << 8) | array[1]);
|
return (short)((array[0] << 8) | array[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<byte> UInt32ToArray(uint value)
|
|
||||||
{
|
|
||||||
return new[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value };
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IEnumerable<byte> Int32ToArray(int value)
|
public static IEnumerable<byte> Int32ToArray(int value)
|
||||||
{
|
{
|
||||||
return new[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value };
|
return new[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value };
|
||||||
|
|
Loading…
Reference in New Issue