93 lines
2.9 KiB
C#

using System.Net.Sockets;
using System.Text.Json;
using DX86;
using Library;
namespace Server;
public class CommandLibrary
{
[Command("example")]
public static string ExampleCommand(string[] args, TcpClient? client, TcpServer? socket) =>
$"Example command executed with arguments: {string.Join(", ", args)}";
#region Basic Commands
[Command("help")]
public static string HelpCommand() =>
"Available commands:\n" +
"1. help - Show this help message\n" +
"2. exit - Exit the server\n" +
"3. user - User management\n" +
"4. settings - Server settings\n" +
"5. start - Start the server\n" +
"6. stop - Stop the server\n";
#endregion
#region Client Commands
[Command("login")]
public static string LoginCommand(string[] args, TcpClient? client, TcpServer? socket)
{
if (args.Length < 2)
throw new CommandException("Missing arguments: usage is login <username> <password>");
string username = args[0];
string password = args[1];
if (socket?.LoggedInClients.ContainsKey(client) == true)
throw new CommandException("User already logged in.");
if (username == "TEST" && password == "1234")
{
if (client != null)
{
socket?.LoggedInClients.Add(client, username);
return "success";
}
throw new CommandException("No client connection detected.");
}
throw new CommandException("Invalid username or password.");
}
[Command("logout")]
public static string LogoutCommand(TcpClient? client, TcpServer? socket)
{
if (client != null && socket?.LoggedInClients.ContainsKey(client) == true)
{
socket.LoggedInClients.Remove(client);
return "success";
}
throw new CommandException("No client provided or not logged in");
}
[Command("getSelfUser")]
public static string GetSelfUserCommand(TcpClient? client, TcpServer? socket)
{
if (client == null || socket == null)
throw new CommandException("No client connection detected.");
if (socket.LoggedInClients.TryGetValue(client, out var username))
{
Employee returnEmployee = new Employee(username);
string jsonEmployee = JsonSerializer.Serialize(returnEmployee);
return jsonEmployee;
}
throw new CommandException("User not logged in.");
}
#endregion
#region Administration Commands
[Command("get")]
public static string GetCommand(string[] args, TcpClient? client, TcpServer? socket) =>
$"not implemented yet, args: {string.Join(", ", args)}";
#endregion
}