-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleHandler.cs
More file actions
80 lines (79 loc) · 2.65 KB
/
ConsoleHandler.cs
File metadata and controls
80 lines (79 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace MovementSystemServer
{
public class ConsoleHandler
{
public Program program;
public void StartConsole()
{
while (true)
{
string input = Console.ReadLine();
if (input.ToLower() == "shutdown")
{
Console.WriteLine("Shutting down server...");
program.server.Stop(); // Stop the server
lock (Program.clients)
{
foreach (var client in Program.clients)
{
client.Close();
}
}
Environment.Exit(0); // Terminate the application
}
else if (input.ToLower() == "status")
{
Logger.Log($"Connected clients: {Program.clients.Count}\n {ListClients(Program.clients)}");
Logger.Log($"Active players: {Program.serverPlayers.Count}\n {ListPlayers(Program.serverPlayers)}");
Logger.Log($"Master: {Program.serverInfo.master.playerName}");
program.BroadcastServerInfo();
}
else
{
Console.WriteLine("Unknown command.");
}
}
}
private string ListClients(List<TcpClient> tcps)
{
string s = string.Empty;
int i = 0;
foreach (TcpClient client in tcps)
{
i++;
IPEndPoint ip = client.Client.RemoteEndPoint as IPEndPoint;
s += $"{i}: {ip.Address}:{ip.Port}\n ";
}
return s;
}
public string ListPlayers(List<ServerPlayer> sps)
{
string s = string.Empty;
int i = 0;
foreach (ServerPlayer p in sps)
{
try
{
PlayerInfo pi = p.info;
TcpClient tcp = p.tcpClient;
IPEndPoint ipe = tcp.Client.RemoteEndPoint as IPEndPoint;
i++;
s += $"IPv4: {ipe.Address}:{ipe.Port}\n Name: {pi.playerName}\n PuppetID:{pi.puppetID}\n ";
}
catch (Exception e)
{
Logger.LogError(e.ToString());
Program.Disconnect(p.tcpClient);
}
}
return s;
}
}
}