mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-20 19:19:39 +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))
|
||||
{
|
||||
Config.RoomList.CancelReady(roomid, Server.User);
|
||||
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;
|
||||
}
|
||||
result = Server.QuitRoom(roomid, isMaster);
|
||||
}
|
||||
}
|
||||
ResultData.Add("result", result);
|
||||
|
@ -234,17 +234,67 @@ namespace Milimoe.FunGame.Server.Model
|
||||
ServerModel serverTask = (ServerModel)Server.GetUser(username == "" ? UserName : username);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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 = "")
|
||||
{
|
||||
// 将客户端踢出服务器
|
||||
RemoveUser();
|
||||
ServerModel serverTask = (ServerModel)Server.GetClient(clientname == "" ? ClientName : clientname);
|
||||
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);
|
||||
}
|
||||
Close();
|
||||
|
@ -1,9 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
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.SQLScript.Common;
|
||||
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
|
||||
@ -52,7 +49,7 @@ namespace Milimoe.FunGame.Server.Others
|
||||
{
|
||||
SQLMode = true;
|
||||
ServerLogin();
|
||||
InitRoomList();
|
||||
ClearRoomList();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
@ -65,19 +62,16 @@ namespace Milimoe.FunGame.Server.Others
|
||||
{
|
||||
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)
|
||||
{
|
||||
// 初始化服务器中的房间列表
|
||||
DataSet DsRoomTemp = SQLHelper.ExecuteDataSet(RoomQuery.Select_Rooms);
|
||||
DataSet DsUserTemp = SQLHelper.ExecuteDataSet(UserQuery.Select_Users);
|
||||
List<Room> rooms = Factory.GetRooms(DsRoomTemp, DsUserTemp);
|
||||
RoomList.AddRooms(rooms);
|
||||
// 重启服务器后,所有房间都会被删除
|
||||
SQLHelper.Execute(RoomQuery.Delete_Rooms());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,11 +93,12 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭连接
|
||||
/// 关闭连接 如有事务会自动提交事务
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
// _IsOneTime = false需要手动调用此方法
|
||||
Commit();
|
||||
_Connection?.Close();
|
||||
ServerHelper.WriteLine($"{(GetClientName() == string.Empty ? "" : GetClientName())}已释放MySQL连接");
|
||||
}
|
||||
@ -134,7 +135,7 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
/// <summary>
|
||||
/// 创建一个SQL事务
|
||||
/// </summary>
|
||||
public void NewTransaction()
|
||||
public override void NewTransaction()
|
||||
{
|
||||
_Transaction ??= _Connection?.Connection?.BeginTransaction();
|
||||
}
|
||||
@ -142,7 +143,7 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
/// <summary>
|
||||
/// 提交事务
|
||||
/// </summary>
|
||||
public void Commit()
|
||||
public override void Commit()
|
||||
{
|
||||
_Transaction?.Commit();
|
||||
_Transaction = null;
|
||||
@ -151,7 +152,7 @@ namespace Milimoe.FunGame.Server.Utility
|
||||
/// <summary>
|
||||
/// 回滚事务
|
||||
/// </summary>
|
||||
public void Rollback()
|
||||
public override void Rollback()
|
||||
{
|
||||
_Transaction?.Rollback();
|
||||
_Transaction = null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user