milimoe 0ed458fb77
匹配房间功能 (#25)
* 匹配房间 第一部分

* elo匹配

* 设置匹配结束标记

* 传输的room对象错误

* 修复错误的UpdateRoom; 添加SQLMode开关

* 删除创建房间时重复的加入房间代码

* 添加在加入房间时检查是否存在房间

---------

Co-authored-by: yeziuku <yezi@wrss.org>
2023-10-25 00:10:34 +08:00

121 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Model;
using MySql.Data.MySqlClient;
namespace Milimoe.FunGame.Server.Utility.DataUtility
{
public class ConnectProperties
{
public static string Name { get; set; } = "";
public static string DataSource { get; set; } = "";
public static string Port { get; set; } = "";
public static string DataBase { get; set; } = "";
public static string User { get; set; } = "";
public static string Password { get; set; } = "";
/// <summary>
/// 读取MySQL服务器配置文件
/// </summary>
/// <returns></returns>
public static string GetConnectProperties()
{
if (Name == "" && DataSource == "" && Port == "" && DataBase == "" && User == "" && Password == "")
{
if (INIHelper.ExistINIFile())
{
DataSource = INIHelper.ReadINI("MySQL", "DBServer");
Port = INIHelper.ReadINI("MySQL", "DBPort");
DataBase = INIHelper.ReadINI("MySQL", "DBName");
User = INIHelper.ReadINI("MySQL", "DBUser");
Password = INIHelper.ReadINI("MySQL", "DBPassword");
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
}
else ServerHelper.Error(new MySQLConfigException());
}
return "data source = " + DataSource + "; port = " + Port + "; database = " + DataBase + "; user = " + User + "; password = " + Password + "; charset = utf8mb4;";
}
}
public class MySQLConnection
{
public MySqlConnection? Connection
{
get
{
return _Connection;
}
}
public SQLServerInfo ServerInfo
{
get
{
return _ServerInfo ?? SQLServerInfo.Create();
}
}
private MySqlConnection? _Connection;
private SQLServerInfo? _ServerInfo;
/// <summary>
/// 创建SQL连接
/// </summary>
/// <param name="serverInfo"></param>
public MySQLConnection(out SQLServerInfo? serverInfo)
{
_Connection = Connect(out serverInfo);
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
if (_Connection != null && _Connection.State == System.Data.ConnectionState.Open)
{
_Connection.Close();
}
_Connection = null;
}
/// <summary>
/// 连接MySQL服务器
/// </summary>
/// <param name="serverInfo">服务器信息</param>
/// <returns>连接对象</returns>
/// <exception cref="MySQLConfigException">MySQL服务启动失败无法找到MySQL配置文件</exception>
private MySqlConnection? Connect(out SQLServerInfo? serverInfo)
{
try
{
string _GetConnection = ConnectProperties.GetConnectProperties();
if (_GetConnection != null)
{
string[] DataSetting = _GetConnection.Split(";");
if (DataSetting.Length > 1 && DataSetting[0].Length > 14 && DataSetting[1].Length > 8)
{
ServerHelper.WriteLine("Connect -> MySQL://" + DataSetting[0][14..] + ":" + DataSetting[1][8..]);
}
_Connection = new MySqlConnection(_GetConnection);
_Connection.Open();
if (_Connection.State == System.Data.ConnectionState.Open)
{
_ServerInfo = SQLServerInfo.Create(ConnectProperties.Name, ConnectProperties.DataSource, ConnectProperties.Port, ConnectProperties.DataBase, ConnectProperties.User, ConnectProperties.Password);
ServerHelper.WriteLine("Connected: MySQL服务器连接成功");
}
}
else
{
throw new MySQLConfigException();
}
}
catch (Exception e)
{
ServerHelper.Error(e);
}
serverInfo = _ServerInfo;
return _Connection;
}
}
}