2021-08-15 07:54:25 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using Lidgren.Network;
|
2021-08-14 21:49:23 +02:00
|
|
|
|
|
|
|
|
|
namespace CoopServer
|
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public class ServerScript
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public virtual void Start() { }
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public virtual void OnPlayerConnect(Entities.EntitiesPlayer player)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Logging.Info("New player [" + player.SocialClubName + " | " + player.Username + "] connected!");
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public virtual void OnPlayerDisconnect(Entities.EntitiesPlayer player, string reason)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
|
|
|
|
Logging.Info(player.Username + " left the server, reason: " + reason);
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public virtual bool OnChatMessage(string username, string message)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static List<long> GetAllConnections()
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
List<long> result = new();
|
|
|
|
|
|
|
|
|
|
lock (Server.MainNetServer.Connections)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Server.MainNetServer.Connections.ForEach(x => result.Add(x.RemoteUniqueIdentifier));
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static int GetAllPlayersCount()
|
|
|
|
|
{
|
|
|
|
|
lock (Server.Players)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
return Server.Players.Count;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static Dictionary<long, Entities.EntitiesPlayer> GetAllPlayers()
|
|
|
|
|
{
|
|
|
|
|
lock (Server.Players)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
return Server.Players;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static void KickPlayerByUsername(string username, string[] reason)
|
|
|
|
|
{
|
|
|
|
|
lock (Server.MainNetServer.Connections)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
NetConnection userConnection = Util.GetConnectionByUsername(username);
|
|
|
|
|
if (userConnection == null)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Logging.Warning("[ServerScript->KickPlayerByUsername(\"" + username + "\", \"" + string.Join(" ", reason) + "\")]: User not found!");
|
|
|
|
|
return;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
userConnection.Disconnect(string.Join(" ", reason));
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static void SendChatMessageToAll(string message, string username = "Server")
|
2021-08-14 21:49:23 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
List<NetConnection> connections = Server.MainNetServer.Connections;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
if (connections.Count != 0)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
ChatMessagePacket packet = new()
|
|
|
|
|
{
|
|
|
|
|
Username = username,
|
|
|
|
|
Message = message
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
|
|
|
|
Server.MainNetServer.SendMessage(outgoingMessage, Server.MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, 0);
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Logging.Info(username + ": " + message);
|
2021-08-14 21:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static void SendChatMessageToPlayer(string username, string message, string from = "Server")
|
2021-08-14 21:49:23 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
lock (Server.MainNetServer.Connections)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
NetConnection userConnection = Util.GetConnectionByUsername(username);
|
|
|
|
|
if (userConnection == null)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Logging.Warning("[ServerScript->SendChatMessageToPlayer(\"" + username + "\", \"" + message + "\", \"" + from + "\")]: User not found!");
|
|
|
|
|
return;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
2021-08-18 11:47:59 +02:00
|
|
|
|
|
|
|
|
|
ChatMessagePacket packet = new()
|
|
|
|
|
{
|
|
|
|
|
Username = from,
|
|
|
|
|
Message = message
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
|
|
|
|
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, 0);
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Logging.Info(from + ": " + message);
|
2021-08-14 21:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static void RegisterCommand(string name, Action<CommandContext> callback)
|
2021-08-14 21:49:23 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Server.RegisterCommand(name, callback);
|
|
|
|
|
}
|
2021-08-14 21:49:23 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
public static void RegisterCommands<T>()
|
|
|
|
|
{
|
|
|
|
|
Server.RegisterCommands<T>();
|
2021-08-14 21:49:23 +02:00
|
|
|
|
}
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Command
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
2021-08-15 07:54:25 +02:00
|
|
|
|
|
2021-08-18 11:47:59 +02:00
|
|
|
|
[AttributeUsage(AttributeTargets.Method)]
|
|
|
|
|
public class CommandAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets name of the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
public CommandAttribute(string name)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-08-18 11:47:59 +02:00
|
|
|
|
Name = name;
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
2021-08-14 21:49:23 +02:00
|
|
|
|
}
|
2021-08-18 11:47:59 +02:00
|
|
|
|
|
|
|
|
|
public class CommandContext
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the client which executed the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Entities.EntitiesPlayer Player { get; internal set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the chatdata associated with the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string[] Args { get; internal set; }
|
|
|
|
|
}
|
2021-08-14 21:49:23 +02:00
|
|
|
|
}
|