feat(auth): add login or register user logic

This commit is contained in:
EmirHanMamak 2026-01-19 11:39:46 +03:00
parent e7703970b1
commit 4b9d76f1ea
12 changed files with 43 additions and 9 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using LiteNetLib;
using Playvoi.Server.Data.Interface;
using Playvoi.Server.Data.Model;
@ -11,8 +12,36 @@ public class UsersManager
public UsersManager(IUserRepository userRepository)
{
_connections = new Dictionary<int, ServerConnection>();
_userRepository = userRepository;
}
public void AddConnection(NetPeer peer)
{
_connections.Add(peer.Id, new ServerConnection()
{
Peer = peer,
});
}
public void Disconnect(int peerId)
{
var connection = GetConnection(peerId);
if (connection.User != null)
{
var userId = connection.User.Id;
_userRepository.SetOffline(userId);
//Matchmaker Unregister
//gamemanger close game
}
_connections.Remove(peerId);
}
public ServerConnection GetConnection(int peerId)
{
return _connections[peerId];
}
public bool LoginOrRegister(int connectionId, string username, string password)
{
var dbUser = _userRepository.Get(username);

View file

@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging;
using Playvoi.Server.Data.Interface;
using Playvoi.Server.Data.Repository;
using Playvoi.Server.Extensions;
using Playvoi.Server.Games;
using Playvoi.Server.Shared.Registry;
namespace Playvoi.Server.Infrastructure;
@ -24,6 +25,7 @@ public static class Container
services.AddSingleton<NetworkServer>();
services.AddSingleton<PacketRegistry>();
services.AddSingleton<HandlerRegistry>();
services.AddSingleton<UsersManager>();
services.AddSingleton<IUserRepository, InMemoryUserRepository>();
services.AddPacketHandlers();
}

View file

@ -8,6 +8,7 @@ using _Network.Shared.Interface;
using LiteNetLib;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Playvoi.Server.Games;
using Playvoi.Server.Shared;
using Playvoi.Server.Shared.Interface;
using Playvoi.Server.Shared.Registry;
@ -19,7 +20,7 @@ public class NetworkServer : INetEventListener
private readonly ILogger<NetworkServer> _logger;
private readonly IServiceProvider _serviceProvider;
private NetManager _netManager;
private Dictionary<int, NetPeer> _connections;
private UsersManager _usersManager;
public NetworkServer(ILogger<NetworkServer> logger, IServiceProvider serviceProvider)
{
@ -29,12 +30,12 @@ public class NetworkServer : INetEventListener
public void Start()
{
_connections = new Dictionary<int, NetPeer>();
_netManager = new NetManager(this)
{
DisconnectTimeout = 10000
};
_netManager.Start(9050);
_usersManager = _serviceProvider.GetRequiredService<UsersManager>();
Console.WriteLine("Server listening on port 9050");
}
@ -65,14 +66,16 @@ public class NetworkServer : INetEventListener
public void OnPeerConnected(NetPeer peer)
{
Console.WriteLine($"Client connected:{peer.Id}:{peer.Address}:{peer.Port}");
_connections.Add(peer.Id, peer);
_logger.LogInformation($"{peer?.Id} connect to {peer.Address}");
_usersManager.AddConnection(peer);
}
public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
Console.WriteLine($"Client disconnected:{peer.Id}:{peer.Address}:{peer.Port}");
_connections.Remove(peer.Id);
var connection = _usersManager.GetConnection(peer.Id);
_netManager.DisconnectPeer(peer);
_usersManager.Disconnect(peer.Id);
_logger.LogInformation($"{connection?.User?.Id} disconnect from {peer.Address}");
}
public void OnConnectionRequest(ConnectionRequest request)

View file

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Playvoi.Server")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b0197489b16432dc1deafec8d7ad46fc335f2e41")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+e7703970b18467faa340642a873fe57df256dfa8")]
[assembly: System.Reflection.AssemblyProductAttribute("Playvoi.Server")]
[assembly: System.Reflection.AssemblyTitleAttribute("Playvoi.Server")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View file

@ -1 +1 @@
57beb62731dfeffdc95d0f7c3f52acf0b72edd970c67dadd581310b517f12af2
ef8056fde80eec98e3c67c1ecf688c0246764b60fad29197d2e428f1d5445736

View file

@ -1 +1 @@
452610c42f1fac1e20e1510ff73f6e49a68de5b7968e9eef0a9411f71e75fe52
c9422bab56cb8e21565dd835cfce88bec85d56c65db2af6f28efc3cb68e28b09