mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2026-04-20 05:25:00 +00:00
添加断线重连机制
This commit is contained in:
parent
3c81cf7217
commit
adf70b42c2
@ -661,12 +661,14 @@ namespace Milimoe.FunGame.Server.Controller
|
|||||||
room.RoomState = RoomState.Gaming;
|
room.RoomState = RoomState.Gaming;
|
||||||
Client.NowGamingServer = FunGameSystem.GameModuleLoader.GetServerMode(room.GameModule);
|
Client.NowGamingServer = FunGameSystem.GameModuleLoader.GetServerMode(room.GameModule);
|
||||||
Client.User.OnlineState = OnlineState.Gaming;
|
Client.User.OnlineState = OnlineState.Gaming;
|
||||||
|
Client.Listener.NowGamingServers[Client.User.Id] = Client.NowGamingServer;
|
||||||
Dictionary<string, IServerModel> all = Client.Listener.UserList.Cast<IServerModel>().ToDictionary(k => k.User.Username, v => v);
|
Dictionary<string, IServerModel> all = Client.Listener.UserList.Cast<IServerModel>().ToDictionary(k => k.User.Username, v => v);
|
||||||
// 给其他玩家赋值模组服务器
|
// 给其他玩家赋值模组服务器
|
||||||
foreach (IServerModel model in all.Values.Where(s => s.User.Username != Client.User.Username))
|
foreach (IServerModel model in all.Values.Where(s => s.User.Username != Client.User.Username))
|
||||||
{
|
{
|
||||||
model.NowGamingServer = Client.NowGamingServer;
|
model.NowGamingServer = Client.NowGamingServer;
|
||||||
model.User.OnlineState = OnlineState.Gaming;
|
model.User.OnlineState = OnlineState.Gaming;
|
||||||
|
Client.Listener.NowGamingServers[model.User.Id] = Client.NowGamingServer;
|
||||||
}
|
}
|
||||||
GamingObject obj = new(room, users, Client, all);
|
GamingObject obj = new(room, users, Client, all);
|
||||||
if (Client.NowGamingServer.StartServer(obj))
|
if (Client.NowGamingServer.StartServer(obj))
|
||||||
|
|||||||
@ -33,6 +33,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
public MailSender? MailSender => _mailer;
|
public MailSender? MailSender => _mailer;
|
||||||
public bool IsDebugMode { get; }
|
public bool IsDebugMode { get; }
|
||||||
public GameModuleServer? NowGamingServer { get; set; } = null;
|
public GameModuleServer? NowGamingServer { get; set; } = null;
|
||||||
|
public GameModuleServer? NowAnonymousServer { get; set; } = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* protected
|
* protected
|
||||||
@ -85,15 +86,12 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
|
|
||||||
if (eventArgs.Cancel)
|
if (eventArgs.Cancel)
|
||||||
{
|
{
|
||||||
ServerHelper.WriteLine($"{SocketSet.GetTypeString(SocketMessageType.EndGame)} 请求已取消。", InvokeMessageType.Core, LogLevel.Warning);
|
ServerHelper.WriteLine($"{SocketSet.GetTypeString(SocketMessageType.EndGame)} 请求已取消。{(eventArgs.EventMsg != "" ? $"原因:{eventArgs.EventMsg}" : "")}", InvokeMessageType.Core, LogLevel.Warning);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (NowGamingServer != null && NowGamingServer.IsAnonymous)
|
|
||||||
{
|
|
||||||
NowGamingServer.CloseAnonymousServer(this);
|
|
||||||
}
|
|
||||||
NowGamingServer = null;
|
NowGamingServer = null;
|
||||||
|
Listener.NowGamingServers.Remove(User.Id, out _);
|
||||||
User.OnlineState = OnlineState.InRoom;
|
User.OnlineState = OnlineState.InRoom;
|
||||||
if (User.Id == InRoom.RoomMaster.Id) InRoom.RoomState = RoomState.Created;
|
if (User.Id == InRoom.RoomMaster.Id) InRoom.RoomState = RoomState.Created;
|
||||||
}
|
}
|
||||||
@ -105,6 +103,11 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
return !eventArgs.Cancel;
|
return !eventArgs.Cancel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == SocketMessageType.ReconnectToGame)
|
||||||
|
{
|
||||||
|
return await CheckGamingServerReconnect();
|
||||||
|
}
|
||||||
|
|
||||||
if (type == SocketMessageType.AnonymousGameServer)
|
if (type == SocketMessageType.AnonymousGameServer)
|
||||||
{
|
{
|
||||||
return await AnonymousGameServerHandler(obj);
|
return await AnonymousGameServerHandler(obj);
|
||||||
@ -259,17 +262,26 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
protected async Task<bool> AnonymousGameServerHandler(SocketObject obj)
|
protected async Task<bool> AnonymousGameServerHandler(SocketObject obj)
|
||||||
{
|
{
|
||||||
string serverName = "";
|
string serverName = "";
|
||||||
|
bool isDisconnect = false;
|
||||||
Dictionary<string, object> data = [];
|
Dictionary<string, object> data = [];
|
||||||
Dictionary<string, object> result = [];
|
Dictionary<string, object> result = [];
|
||||||
if (obj.Parameters.Length > 0) serverName = obj.GetParam<string>(0) ?? "";
|
if (obj.Parameters.Length > 0) serverName = obj.GetParam<string>(0) ?? "";
|
||||||
if (obj.Parameters.Length > 1) data = obj.GetParam<Dictionary<string, object>>(1) ?? [];
|
if (obj.Parameters.Length > 1) data = obj.GetParam<Dictionary<string, object>>(1) ?? [];
|
||||||
|
if (obj.Parameters.Length > 2) isDisconnect = obj.GetParam<bool>(2);
|
||||||
|
|
||||||
bool willSend = false;
|
bool willSend = false;
|
||||||
if (NowGamingServer != null)
|
if (NowAnonymousServer != null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = await NowGamingServer.AnonymousGameServerHandler(this, data);
|
if (isDisconnect)
|
||||||
|
{
|
||||||
|
NowAnonymousServer.CloseAnonymousServer(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = await NowAnonymousServer.AnonymousGameServerHandler(this, data);
|
||||||
|
}
|
||||||
willSend = true;
|
willSend = true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -278,7 +290,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
return await Send(SocketMessageType.AnonymousGameServer, result);
|
return await Send(SocketMessageType.AnonymousGameServer, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else if (!isDisconnect)
|
||||||
{
|
{
|
||||||
// 建立连接
|
// 建立连接
|
||||||
if (FunGameSystem.GameModuleLoader != null && FunGameSystem.GameModuleLoader.ModuleServers.ContainsKey(serverName))
|
if (FunGameSystem.GameModuleLoader != null && FunGameSystem.GameModuleLoader.ModuleServers.ContainsKey(serverName))
|
||||||
@ -286,10 +298,10 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
GameModuleServer mod = FunGameSystem.GameModuleLoader.GetServerMode(serverName);
|
GameModuleServer mod = FunGameSystem.GameModuleLoader.GetServerMode(serverName);
|
||||||
if (mod.StartAnonymousServer(this, data))
|
if (mod.StartAnonymousServer(this, data))
|
||||||
{
|
{
|
||||||
NowGamingServer = mod;
|
NowAnonymousServer = mod;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = await NowGamingServer.AnonymousGameServerHandler(this, data);
|
result = await NowAnonymousServer.AnonymousGameServerHandler(this, data);
|
||||||
willSend = true;
|
willSend = true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -432,7 +444,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
// 不是房主直接退出房间
|
// 不是房主直接退出房间
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.InRoom = General.HallInstance;
|
InRoom = General.HallInstance;
|
||||||
await NotifyQuitRoom(Room);
|
await NotifyQuitRoom(Room);
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
@ -597,6 +609,43 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async Task<bool> CheckGamingServerReconnect()
|
||||||
|
{
|
||||||
|
bool sendResult = false;
|
||||||
|
|
||||||
|
if (Listener.NowGamingServers.TryGetValue(User.Id, out GameModuleServer? value) && value != null)
|
||||||
|
{
|
||||||
|
GamingObject? obj = value.GetGamingObjectOfUser(User.Id);
|
||||||
|
|
||||||
|
if (obj != null && obj.Running && value.GetRoomOfUser(User.Id, obj) is Room room)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> data = await DataRequestController.GetResultData(DataRequestType.Main_IntoRoom, new()
|
||||||
|
{
|
||||||
|
{ "roomid", room.Roomid }
|
||||||
|
});
|
||||||
|
if (data.TryGetValue("result", out object? value2) && value2 is bool result && result)
|
||||||
|
{
|
||||||
|
FunGameSystem.RoomList.CancelReady(room.Roomid, User);
|
||||||
|
obj.UserReconnect(User);
|
||||||
|
sendResult = await Send(SocketMessageType.StartGame, room, obj.Users);
|
||||||
|
if (sendResult)
|
||||||
|
{
|
||||||
|
NowGamingServer = value;
|
||||||
|
User.OnlineState = OnlineState.Gaming;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sendResult)
|
||||||
|
{
|
||||||
|
Listener.NowGamingServers.Remove(User.Id, out _);
|
||||||
|
ServerHelper.WriteLine("[ " + GetClientName() + " ] " + nameof(AnonymousGameServerHandler) + ": " + sendResult, InvokeMessageType.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendResult;
|
||||||
|
}
|
||||||
|
|
||||||
protected async Task CreateStreamReader()
|
protected async Task CreateStreamReader()
|
||||||
{
|
{
|
||||||
CancellationTokenSource cts = new();
|
CancellationTokenSource cts = new();
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
using Milimoe.FunGame.Core.Api.Utility;
|
using System.Collections.Concurrent;
|
||||||
|
using Milimoe.FunGame.Core.Api.Utility;
|
||||||
using Milimoe.FunGame.Core.Interface.Base;
|
using Milimoe.FunGame.Core.Interface.Base;
|
||||||
|
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.WebAPI.Architecture
|
namespace Milimoe.FunGame.WebAPI.Architecture
|
||||||
{
|
{
|
||||||
@ -13,6 +15,8 @@ namespace Milimoe.FunGame.WebAPI.Architecture
|
|||||||
|
|
||||||
public ConcurrentModelList<IServerModel> UserList { get; } = [];
|
public ConcurrentModelList<IServerModel> UserList { get; } = [];
|
||||||
|
|
||||||
|
public ConcurrentDictionary<long, GameModuleServer> NowGamingServers { get; } = [];
|
||||||
|
|
||||||
public List<string> BannedList { get; } = [];
|
public List<string> BannedList { get; } = [];
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
using Milimoe.FunGame.Core.Api.Utility;
|
using System.Collections.Concurrent;
|
||||||
|
using Milimoe.FunGame.Core.Api.Utility;
|
||||||
using Milimoe.FunGame.Core.Interface.Base;
|
using Milimoe.FunGame.Core.Interface.Base;
|
||||||
|
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.WebAPI.Architecture
|
namespace Milimoe.FunGame.WebAPI.Architecture
|
||||||
@ -12,6 +14,8 @@ namespace Milimoe.FunGame.WebAPI.Architecture
|
|||||||
|
|
||||||
public ConcurrentModelList<IServerModel> UserList { get; } = [];
|
public ConcurrentModelList<IServerModel> UserList { get; } = [];
|
||||||
|
|
||||||
|
public ConcurrentDictionary<long, GameModuleServer> NowGamingServers { get; } = [];
|
||||||
|
|
||||||
public List<string> BannedList { get; } = [];
|
public List<string> BannedList { get; } = [];
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user