RAGECOOP-V/Server/Util.cs

108 lines
3.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Xml;
2021-07-07 13:36:25 +02:00
using System.Xml.Serialization;
2021-08-18 11:47:59 +02:00
using System.Linq;
using System.Collections.Generic;
using Lidgren.Network;
2021-07-07 13:36:25 +02:00
namespace CoopServer
{
2021-12-03 20:41:44 +01:00
internal class Util
2021-07-07 13:36:25 +02:00
{
public static Client GetClientByNetHandle(long netHandle)
2021-12-11 12:35:17 +01:00
{
Client result = Server.Clients.Find(x => x.NetHandle == netHandle);
2021-12-11 12:35:17 +01:00
if (result == null)
{
NetConnection localConn = Server.MainNetServer.Connections.Find(x => netHandle == x.RemoteUniqueIdentifier);
2021-12-11 12:35:17 +01:00
if (localConn != null)
{
localConn.Disconnect("No data found!");
}
return null;
}
return result;
}
2021-08-21 16:52:17 +02:00
public static NetConnection GetConnectionByUsername(string username)
2021-08-18 11:47:59 +02:00
{
2021-12-11 12:35:17 +01:00
Client client = Server.Clients.Find(x => x.Player.Username.ToLower() == username.ToLower());
if (client == null)
2021-08-21 16:52:17 +02:00
{
return null;
}
return Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == client.NetHandle);
2021-08-18 11:47:59 +02:00
}
// Return a list of all connections but not the local connection
public static List<NetConnection> FilterAllLocal(NetConnection local)
{
return new(Server.MainNetServer.Connections.Where(e => e != local));
}
public static List<NetConnection> FilterAllLocal(long local)
{
return new(Server.MainNetServer.Connections.Where(e => e.RemoteUniqueIdentifier != local));
}
// Return a list of players within range of ...
public static List<NetConnection> GetAllInRange(LVector3 position, float range)
{
return new(Server.MainNetServer.Connections.FindAll(e =>
{
Client client = Server.Clients.First(x => x.NetHandle == e.RemoteUniqueIdentifier);
2021-12-11 12:35:17 +01:00
return client != null && client.Player.IsInRangeOf(position, range);
}));
2021-08-18 11:47:59 +02:00
}
// Return a list of players within range of ... but not the local one
public static List<NetConnection> GetAllInRange(LVector3 position, float range, NetConnection local)
{
return new(Server.MainNetServer.Connections.Where(e =>
{
Client client = Server.Clients.First(x => x.NetHandle == e.RemoteUniqueIdentifier);
2021-12-11 12:35:17 +01:00
return e != local && client != null && client.Player.IsInRangeOf(position, range);
}));
2021-08-18 11:47:59 +02:00
}
2021-07-07 13:36:25 +02:00
public static T Read<T>(string file) where T : new()
{
XmlSerializer ser = new(typeof(T));
XmlWriterSettings settings = new()
{
Indent = true,
IndentChars = ("\t"),
OmitXmlDeclaration = true
};
2021-07-12 01:47:01 +02:00
string path = AppContext.BaseDirectory + file;
2021-08-14 21:49:23 +02:00
T data;
2021-07-07 13:36:25 +02:00
if (File.Exists(path))
{
using (XmlReader stream = XmlReader.Create(path))
2021-07-07 13:36:25 +02:00
{
2021-08-14 21:49:23 +02:00
data = (T)ser.Deserialize(stream);
2021-07-07 13:36:25 +02:00
}
using (XmlWriter stream = XmlWriter.Create(path, settings))
2021-07-07 13:36:25 +02:00
{
2021-08-14 21:49:23 +02:00
ser.Serialize(stream, data);
2021-07-07 13:36:25 +02:00
}
}
else
{
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
ser.Serialize(stream, data = new T());
}
2021-07-07 13:36:25 +02:00
}
2021-08-14 21:49:23 +02:00
return data;
2021-07-07 13:36:25 +02:00
}
}
}