mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-23 12:39:36 +08:00
* 关闭SQL时会自动提交事务;Fix #28 * 将客户端踢出服务器时也会先移出房间
This commit is contained in:
parent
5f8ae48859
commit
c7310dc54e
@ -221,44 +221,7 @@ namespace Milimoe.FunGame.Server.Controller
|
|||||||
|
|
||||||
if (roomid != "-1" && Config.RoomList.IsExist(roomid))
|
if (roomid != "-1" && Config.RoomList.IsExist(roomid))
|
||||||
{
|
{
|
||||||
Config.RoomList.CancelReady(roomid, Server.User);
|
result = Server.QuitRoom(roomid, isMaster);
|
||||||
Config.RoomList.QuitRoom(roomid, Server.User);
|
|
||||||
Room Room = Config.RoomList[roomid] ?? General.HallInstance;
|
|
||||||
// 是否是房主
|
|
||||||
if (isMaster)
|
|
||||||
{
|
|
||||||
List<User> users = Config.RoomList.GetPlayerList(roomid);
|
|
||||||
if (users.Count > 0) // 如果此时房间还有人,更新房主
|
|
||||||
{
|
|
||||||
User NewMaster = users[0];
|
|
||||||
Room.RoomMaster = NewMaster;
|
|
||||||
SQLHelper.Execute(RoomQuery.Update_QuitRoom(roomid, Server.User.Id, NewMaster.Id));
|
|
||||||
if (SQLHelper.Result == SQLResult.Success)
|
|
||||||
{
|
|
||||||
Server.Room = General.HallInstance;
|
|
||||||
Server.UpdateRoomMaster(Room, true);
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else // 没人了就解散房间
|
|
||||||
{
|
|
||||||
Config.RoomList.RemoveRoom(roomid);
|
|
||||||
SQLHelper.Execute(RoomQuery.Delete_QuitRoom(roomid, Server.User.Id));
|
|
||||||
if (SQLHelper.Result == SQLResult.Success)
|
|
||||||
{
|
|
||||||
Server.Room = General.HallInstance;
|
|
||||||
ServerHelper.WriteLine("[ " + Server.GetClientName() + " ] 解散了房间 " + roomid);
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 不是房主直接退出房间
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Server.Room = General.HallInstance;
|
|
||||||
Server.UpdateRoomMaster(Room);
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ResultData.Add("result", result);
|
ResultData.Add("result", result);
|
||||||
|
@ -234,17 +234,67 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
ServerModel serverTask = (ServerModel)Server.GetUser(username == "" ? UserName : username);
|
ServerModel serverTask = (ServerModel)Server.GetUser(username == "" ? UserName : username);
|
||||||
if (serverTask.Socket != null)
|
if (serverTask.Socket != null)
|
||||||
{
|
{
|
||||||
|
serverTask.Room = General.HallInstance;
|
||||||
|
foreach (Room room in Config.RoomList.Cast<Room>())
|
||||||
|
{
|
||||||
|
QuitRoom(room.Roomid, room.RoomMaster.Id == User.Id);
|
||||||
|
}
|
||||||
serverTask.Send(serverTask.Socket, SocketMessageType.ForceLogout, msg);
|
serverTask.Send(serverTask.Socket, SocketMessageType.ForceLogout, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool QuitRoom(string roomid, bool isMaster)
|
||||||
|
{
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
Config.RoomList.CancelReady(roomid, User);
|
||||||
|
Config.RoomList.QuitRoom(roomid, User);
|
||||||
|
Room Room = Config.RoomList[roomid] ?? General.HallInstance;
|
||||||
|
// 是否是房主
|
||||||
|
if (isMaster)
|
||||||
|
{
|
||||||
|
List<User> users = Config.RoomList.GetPlayerList(roomid);
|
||||||
|
if (users.Count > 0) // 如果此时房间还有人,更新房主
|
||||||
|
{
|
||||||
|
User NewMaster = users[0];
|
||||||
|
Room.RoomMaster = NewMaster;
|
||||||
|
SQLHelper?.Execute(RoomQuery.Update_QuitRoom(roomid, User.Id, NewMaster.Id));
|
||||||
|
this.Room = General.HallInstance;
|
||||||
|
UpdateRoomMaster(Room, true);
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
else // 没人了就解散房间
|
||||||
|
{
|
||||||
|
Config.RoomList.RemoveRoom(roomid);
|
||||||
|
SQLHelper?.Execute(RoomQuery.Delete_QuitRoom(roomid, User.Id));
|
||||||
|
this.Room = General.HallInstance;
|
||||||
|
ServerHelper.WriteLine("[ " + GetClientName() + " ] 解散了房间 " + roomid);
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 不是房主直接退出房间
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.Room = General.HallInstance;
|
||||||
|
UpdateRoomMaster(Room);
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public void Kick(string msg, string clientname = "")
|
public void Kick(string msg, string clientname = "")
|
||||||
{
|
{
|
||||||
// 将客户端踢出服务器
|
// 将客户端踢出服务器
|
||||||
RemoveUser();
|
|
||||||
ServerModel serverTask = (ServerModel)Server.GetClient(clientname == "" ? ClientName : clientname);
|
ServerModel serverTask = (ServerModel)Server.GetClient(clientname == "" ? ClientName : clientname);
|
||||||
if (serverTask.Socket != null)
|
if (serverTask.Socket != null)
|
||||||
{
|
{
|
||||||
|
serverTask.Room = General.HallInstance;
|
||||||
|
foreach (Room room in Config.RoomList.Cast<Room>())
|
||||||
|
{
|
||||||
|
QuitRoom(room.Roomid, room.RoomMaster.Id == User.Id);
|
||||||
|
}
|
||||||
|
RemoveUser();
|
||||||
serverTask.Send(serverTask.Socket, SocketMessageType.Disconnect, msg);
|
serverTask.Send(serverTask.Socket, SocketMessageType.Disconnect, msg);
|
||||||
}
|
}
|
||||||
Close();
|
Close();
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Data;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||||
using Milimoe.FunGame.Core.Api.Utility;
|
|
||||||
using Milimoe.FunGame.Core.Entity;
|
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
using Milimoe.FunGame.Core.Library.SQLScript.Common;
|
using Milimoe.FunGame.Core.Library.SQLScript.Common;
|
||||||
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
|
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
|
||||||
@ -52,7 +49,7 @@ namespace Milimoe.FunGame.Server.Others
|
|||||||
{
|
{
|
||||||
SQLMode = true;
|
SQLMode = true;
|
||||||
ServerLogin();
|
ServerLogin();
|
||||||
InitRoomList();
|
ClearRoomList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -65,19 +62,16 @@ namespace Milimoe.FunGame.Server.Others
|
|||||||
{
|
{
|
||||||
if (SQLMode)
|
if (SQLMode)
|
||||||
{
|
{
|
||||||
SQLHelper.Execute(ServerLoginLogs.Insert_ServerLoginLogs(Config.ServerName, Config.ServerKey));
|
SQLHelper.Execute(ServerLoginLogs.Insert_ServerLoginLogs(ServerName, ServerKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void InitRoomList()
|
public static void ClearRoomList()
|
||||||
{
|
{
|
||||||
if (SQLMode)
|
if (SQLMode)
|
||||||
{
|
{
|
||||||
// 初始化服务器中的房间列表
|
// 重启服务器后,所有房间都会被删除
|
||||||
DataSet DsRoomTemp = SQLHelper.ExecuteDataSet(RoomQuery.Select_Rooms);
|
SQLHelper.Execute(RoomQuery.Delete_Rooms());
|
||||||
DataSet DsUserTemp = SQLHelper.ExecuteDataSet(UserQuery.Select_Users);
|
|
||||||
List<Room> rooms = Factory.GetRooms(DsRoomTemp, DsUserTemp);
|
|
||||||
RoomList.AddRooms(rooms);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,11 +93,12 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 关闭连接
|
/// 关闭连接 如有事务会自动提交事务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
// _IsOneTime = false需要手动调用此方法
|
// _IsOneTime = false需要手动调用此方法
|
||||||
|
Commit();
|
||||||
_Connection?.Close();
|
_Connection?.Close();
|
||||||
ServerHelper.WriteLine($"{(GetClientName() == string.Empty ? "" : GetClientName())}已释放MySQL连接");
|
ServerHelper.WriteLine($"{(GetClientName() == string.Empty ? "" : GetClientName())}已释放MySQL连接");
|
||||||
}
|
}
|
||||||
@ -134,7 +135,7 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建一个SQL事务
|
/// 创建一个SQL事务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void NewTransaction()
|
public override void NewTransaction()
|
||||||
{
|
{
|
||||||
_Transaction ??= _Connection?.Connection?.BeginTransaction();
|
_Transaction ??= _Connection?.Connection?.BeginTransaction();
|
||||||
}
|
}
|
||||||
@ -142,7 +143,7 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 提交事务
|
/// 提交事务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Commit()
|
public override void Commit()
|
||||||
{
|
{
|
||||||
_Transaction?.Commit();
|
_Transaction?.Commit();
|
||||||
_Transaction = null;
|
_Transaction = null;
|
||||||
@ -151,7 +152,7 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 回滚事务
|
/// 回滚事务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Rollback()
|
public override void Rollback()
|
||||||
{
|
{
|
||||||
_Transaction?.Rollback();
|
_Transaction?.Rollback();
|
||||||
_Transaction = null;
|
_Transaction = null;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user