mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-21 19:49:36 +08:00
127 lines
4.8 KiB
C#
127 lines
4.8 KiB
C#
using System.Collections;
|
||
using MySql.Data.MySqlClient;
|
||
using Milimoe.FunGame.Core.Api.Utility;
|
||
using Milimoe.FunGame.Core.Entity;
|
||
using Milimoe.FunGame.Server.Others;
|
||
|
||
namespace Milimoe.FunGame.Server.Utility
|
||
{
|
||
public class ServerHelper
|
||
{
|
||
public static string GetPrefix()
|
||
{
|
||
DateTime now = System.DateTime.Now;
|
||
return now.AddMilliseconds(-now.Millisecond).ToString() + " " + Config.ServerName + ":";
|
||
}
|
||
|
||
public static void Error(Exception e)
|
||
{
|
||
Console.Write("\r" + GetPrefix() + e.Message + "\n" + e.StackTrace + "\n\r> ");
|
||
}
|
||
|
||
public static void WriteLine(string? msg)
|
||
{
|
||
Console.Write("\r" + GetPrefix() + msg + "\n\r> ");
|
||
}
|
||
|
||
public static void Type()
|
||
{
|
||
Console.Write("\r> ");
|
||
}
|
||
|
||
private static Hashtable GetServerSettingHashtable()
|
||
{
|
||
Hashtable settings = new();
|
||
if (INIHelper.ExistINIFile())
|
||
{
|
||
settings.Add("Name", INIHelper.ReadINI("Server", "Name"));
|
||
settings.Add("Password", INIHelper.ReadINI("Server", "Password"));
|
||
settings.Add("Describe", INIHelper.ReadINI("Server", "Describe"));
|
||
settings.Add("Notice", INIHelper.ReadINI("Server", "Notice"));
|
||
settings.Add("Key", INIHelper.ReadINI("Server", "Key"));
|
||
settings.Add("Status", Convert.ToInt32(INIHelper.ReadINI("Server", "Status")));
|
||
settings.Add("Port", Convert.ToInt32(INIHelper.ReadINI("Socket", "Port")));
|
||
settings.Add("MaxPlayer", Convert.ToInt32(INIHelper.ReadINI("Socket", "MaxPlayer")));
|
||
settings.Add("MaxConnectFailed", Convert.ToInt32(INIHelper.ReadINI("Socket", "MaxConnectFailed")));
|
||
}
|
||
return settings;
|
||
}
|
||
|
||
public static void GetServerSettings()
|
||
{
|
||
try
|
||
{
|
||
Hashtable settings = GetServerSettingHashtable();
|
||
if (settings != null)
|
||
{
|
||
string? Name = (string?)settings["Name"];
|
||
string? Password = (string?)settings["Password"];
|
||
string? Describe = (string?)settings["Describe"];
|
||
string? Notice = (string?)settings["Notice"];
|
||
string? Key = (string?)settings["Key"];
|
||
if (Name != null) Config.ServerName = Name;
|
||
if (Password != null) Config.ServerPassword = Password;
|
||
if (Describe != null) Config.ServerDescription = Describe;
|
||
if (Notice != null) Config.ServerNotice = Notice;
|
||
if (Key != null) Config.ServerKey = Key;
|
||
int? Status = (int?)settings["Status"];
|
||
int? Port = (int?)settings["Port"];
|
||
int? MaxPlayer = (int?)settings["MaxPlayer"];
|
||
int? MaxConnectFailed = (int?)settings["MaxConnectFailed"];
|
||
if (Status != null) Config.ServerStatus = (int)Status;
|
||
if (Port != null) Config.ServerPort = (int)Port;
|
||
if (MaxPlayer != null) Config.MaxPlayers = (int)MaxPlayer;
|
||
if (MaxConnectFailed != null) Config.MaxConnectionFaileds = (int)MaxConnectFailed;
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
ServerHelper.WriteLine(e.StackTrace);
|
||
}
|
||
}
|
||
|
||
public static void InitOrderList()
|
||
{
|
||
Config.OrderList.Clear();
|
||
Config.OrderList.Add(OrderDictionary.Help, "Milimoe -> 帮助");
|
||
Config.OrderList.Add(OrderDictionary.Quit, "关闭服务器");
|
||
Config.OrderList.Add(OrderDictionary.Exit, "关闭服务器");
|
||
Config.OrderList.Add(OrderDictionary.Close, "关闭服务器");
|
||
Config.OrderList.Add(OrderDictionary.Restart, "重启服务器");
|
||
}
|
||
}
|
||
|
||
public class SocketHelper
|
||
{
|
||
public static int GetType(string msg)
|
||
{
|
||
int index = msg.IndexOf(';') - 1;
|
||
if (index > 0)
|
||
return Convert.ToInt32(msg[..index]);
|
||
else
|
||
return Convert.ToInt32(msg[..1]);
|
||
}
|
||
|
||
public static string GetMessage(string msg)
|
||
{
|
||
int index = msg.IndexOf(';') + 1;
|
||
return msg[index..];
|
||
}
|
||
|
||
public static string MakeMessage(int type, string msg)
|
||
{
|
||
return type + ";" + msg;
|
||
}
|
||
|
||
public static string MakeClientName(string name, User? user = null)
|
||
{
|
||
if (user != null)
|
||
{
|
||
return "玩家 " + user.Username;
|
||
}
|
||
if (name != "") return "客户端(" + name + ")";
|
||
return "客户端";
|
||
}
|
||
}
|
||
}
|