mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2026-04-20 05:24:59 +00:00
添加断线重连机制
This commit is contained in:
parent
4b5fcfccb1
commit
cedc4d29e2
@ -1,4 +1,6 @@
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using System.Collections.Concurrent;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Interface.Base
|
||||
{
|
||||
@ -15,10 +17,15 @@ namespace Milimoe.FunGame.Core.Interface.Base
|
||||
public ConcurrentModelList<IServerModel> ClientList { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 已登录的用户列表
|
||||
/// 已登录的用户列表
|
||||
/// </summary>
|
||||
public ConcurrentModelList<IServerModel> UserList { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 记录玩家与正在游戏的服务器
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<long, GameModuleServer> NowGamingServers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 黑名单IP地址列表
|
||||
/// </summary>
|
||||
|
||||
@ -277,7 +277,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon.Example
|
||||
Workers.Remove(obj.Room.Roomid, out _);
|
||||
}
|
||||
|
||||
private async Task WaitForUsers(int waitSeconds, Func<Task<bool>> waitSomething, int delay, Func<Task> onTimeout, Func<Task> onCompleted)
|
||||
protected override async Task WaitForUsers(int waitSeconds, Func<Task<bool>> waitSomething, int delay, Func<Task> onTimeout, Func<Task> onCompleted)
|
||||
{
|
||||
// 这是一个用于等待的通用辅助方法
|
||||
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(waitSeconds));
|
||||
@ -433,7 +433,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon.Example
|
||||
hp.TryAdd(c, Calculation.Round4Digits(c.HP / c.MaxHP));
|
||||
}
|
||||
double maxhp = hp.Values.Max();
|
||||
Character winner = hp.Keys.Where(c => hp[c] == maxhp).First();
|
||||
Character winner = hp.Keys.First(c => hp[c] == maxhp);
|
||||
await SendAllTextMessage(obj, "[ " + winner + " ] 成为了天选之人!!");
|
||||
foreach (Character c in finalList.Where(c => c != winner && c.HP > 0))
|
||||
{
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Controller;
|
||||
using Milimoe.FunGame.Core.Entity;
|
||||
using Milimoe.FunGame.Core.Interface.Addons;
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
using Milimoe.FunGame.Core.Library.Common.Event;
|
||||
@ -198,6 +199,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon
|
||||
/// <param name="obj"></param>
|
||||
public virtual async void SendEndGame(GamingObject obj)
|
||||
{
|
||||
obj.Running = false;
|
||||
GamingObjects.TryRemove(obj.Room.Roomid, out _);
|
||||
await Send(obj.All.Values, SocketMessageType.EndGame, obj.Room, obj.Users);
|
||||
foreach (IServerModel model in obj.All.Values)
|
||||
@ -206,6 +208,66 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取玩家所在的游戏对象
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public GamingObject? GetGamingObjectOfUser(long id)
|
||||
{
|
||||
GamingObject? obj = GamingObjects.Values.FirstOrDefault(obj => obj.HasUser(id));
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取玩家所在的房间
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public Room GetRoomOfUser(long id, GamingObject? obj = null)
|
||||
{
|
||||
obj ??= GamingObjects.Values.FirstOrDefault(obj => obj.HasUser(id));
|
||||
return obj?.Room ?? Room.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 这是一个用于等待的通用辅助方法
|
||||
/// </summary>
|
||||
/// <param name="waitSeconds">等待时间(秒)</param>
|
||||
/// <param name="waitSomething">等待的条件</param>
|
||||
/// <param name="delay">检查间隔(毫秒)</param>
|
||||
/// <param name="onTimeout">任务超时则...</param>
|
||||
/// <param name="onCompleted">任务完成则...</param>
|
||||
/// <returns></returns>
|
||||
protected virtual async Task WaitForUsers(int waitSeconds, Func<Task<bool>> waitSomething, int delay, Func<Task> onTimeout, Func<Task> onCompleted)
|
||||
{
|
||||
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(waitSeconds));
|
||||
CancellationToken ct = cts.Token;
|
||||
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (await waitSomething())
|
||||
{
|
||||
await onCompleted();
|
||||
return;
|
||||
}
|
||||
await Task.Delay(delay, ct);
|
||||
}
|
||||
catch (System.Exception e) when (e is not OperationCanceledException)
|
||||
{
|
||||
Controller.Error(e);
|
||||
await onTimeout();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 异常和超时都走超时逻辑
|
||||
await onTimeout();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给客户端发送局内消息
|
||||
/// </summary>
|
||||
|
||||
@ -3,11 +3,23 @@ using Milimoe.FunGame.Core.Interface.Base;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Common.Addon
|
||||
{
|
||||
public readonly struct GamingObject(Room room, List<User> users, IServerModel roomMaster, Dictionary<string, IServerModel> serverModels)
|
||||
public class GamingObject(Room room, List<User> users, IServerModel roomMaster, Dictionary<string, IServerModel> serverModels)
|
||||
{
|
||||
public bool Running { get; set; } = true;
|
||||
public Room Room { get; } = room;
|
||||
public List<User> Users { get; } = users;
|
||||
public IServerModel RoomMaster { get; } = roomMaster;
|
||||
public Dictionary<string, IServerModel> All { get; } = serverModels;
|
||||
|
||||
public bool HasUser(long id)
|
||||
{
|
||||
return Users.Any(u => u.Id == id);
|
||||
}
|
||||
|
||||
public void UserReconnect(User newUser)
|
||||
{
|
||||
Users.RemoveAll(u => u.Id == newUser.Id);
|
||||
Users.Add(newUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
using System.Net;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
using Milimoe.FunGame.Core.Interface.HTTP;
|
||||
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
@ -16,6 +18,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public Guid Token { get; } = Guid.Empty;
|
||||
public ConcurrentModelList<IServerModel> ClientList { get; } = [];
|
||||
public ConcurrentModelList<IServerModel> UserList { get; } = [];
|
||||
public ConcurrentDictionary<long, GameModuleServer> NowGamingServers { get; } = [];
|
||||
public List<string> BannedList { get; } = [];
|
||||
|
||||
private HTTPListener(HttpListener instance)
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
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.Sockets;
|
||||
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
@ -15,6 +17,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public bool Connected => Instance != null && Instance.Connected;
|
||||
public ConcurrentModelList<IServerModel> ClientList { get; } = [];
|
||||
public ConcurrentModelList<IServerModel> UserList { get; } = [];
|
||||
public ConcurrentDictionary<long, GameModuleServer> NowGamingServers { get; } = [];
|
||||
public List<string> BannedList { get; } = [];
|
||||
|
||||
private SocketListener(System.Net.Sockets.Socket instance)
|
||||
|
||||
@ -127,6 +127,7 @@ namespace Milimoe.FunGame.Core.Library.Constant
|
||||
public const string EndGame = "EndGame";
|
||||
public const string Gaming = "Gaming";
|
||||
public const string AnonymousGameServer = "AnonymousGameServer";
|
||||
public const string ReconnectToGame = "ReconnectToGame";
|
||||
|
||||
/// <summary>
|
||||
/// 将通信类型的枚举转换为字符串
|
||||
|
||||
@ -78,7 +78,8 @@ namespace Milimoe.FunGame.Core.Library.Constant
|
||||
StartGame,
|
||||
EndGame,
|
||||
Gaming,
|
||||
AnonymousGameServer
|
||||
AnonymousGameServer,
|
||||
ReconnectToGame
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user