diff --git a/AX.WebDrillServer/Hubs/FireDeductionHub.cs b/AX.WebDrillServer/Hubs/FireDeductionHub.cs index 1fe3687..c27c1ae 100644 --- a/AX.WebDrillServer/Hubs/FireDeductionHub.cs +++ b/AX.WebDrillServer/Hubs/FireDeductionHub.cs @@ -9,9 +9,11 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Microsoft.VisualBasic; using NPOI.SS.Util; +using System; using System.Collections.Concurrent; using System.Runtime.CompilerServices; using System.Security.Claims; +using System.Text.Json; namespace AX.WebDrillServer.Hubs { @@ -33,6 +35,7 @@ namespace AX.WebDrillServer.Hubs _logger = logger; this.roomManager = roomManager; } + #region 房间相关API /// /// 获取当前房间列表 /// @@ -210,18 +213,149 @@ namespace AX.WebDrillServer.Hubs throw; } } - public async Task RoomSendMessage(string RoomId, string Message) + #endregion + + #region 推演API + public async Task CallServer_StartDrill(StartDrillData startData) + { + StartDrillResultData resultData = new(); + var userId = Context.UserIdentifier; + try + { + //通过用户id查询用户所在房间,如果用户可以同时存在两个或以上房间会有问题,建议web端传入RoomId + var room = roomManager.GetRoomByUserId(userId!); + + if (room == null) + { + resultData.ResultType = DrillResult.Failed; + return resultData; + } + else + { + if (room.RoomId == userId)//判断是否是房主 + { + resultData.ResultType = DrillResult.Success; + resultData.StartData = new() + { + Name = startData.Name, + StartTime = DateTime.Now.Ticks, + }; + room.State = RoomState.Playing; + room.DrillName = startData.Name; + + await Clients.Group(room.RoomId).SendAsync("CallWeb_StartDrill", resultData); + return resultData; + } + else + { + resultData.ResultType = DrillResult.Failed; + return resultData; + } + } + } + catch (Exception e) + { + throw new Exception(e.Message); + } + + } + public async Task CallServer_EndDrill() + { + EndDrillResultData resultData = new(); + var userId = Context.UserIdentifier; + try + { + var room = roomManager.GetRoomByUserId(userId!); + + if (room == null) + { + resultData.ResultType = DrillResult.Failed; + return resultData; + } + else + { + if (room.RoomId == userId) + { + resultData.ResultType = DrillResult.Success; + resultData.EndData = new() + { + Name = room.DrillName!, + StartTime = DateTime.Now.Ticks, + }; + room.State = RoomState.Over; + + await Clients.Group(room.RoomId).SendAsync("CallWeb_EndDrill", resultData); + return resultData; + } + else + { + resultData.ResultType = DrillResult.Failed; + return resultData; + } + } + } + catch (Exception e) + { + throw new Exception(e.Message); + } + + } + + public async Task CallServer_GetDrillShowData() { - await Clients.Group(RoomId).SendAsync(Message); - //TODO:保存数据 + } + public async Task CallServer_SendOrder(object data) + { + //callWeb_sendOthersOrder + var userId = Context.UserIdentifier; + try + { + //通过用户id查询用户所在房间,如果用户可以同时存在两个或以上房间会有问题,建议web端传入RoomId + var room = roomManager.GetRoomByUserId(userId!); - public async Task SendMessage(string user, string Message) + if (room != null) + { + await Clients.Group(room.RoomId).SendAsync("callWeb_sendOthersOrder", userId, data); + var info = new RoomSendInfo() + { + RoomId = room.RoomId, + UserId = userId!, + InfoData = data.ToJson(), + }; + roomManager.SaveRoomSendInfo(info); + } + } + catch (Exception e) + { + throw new Exception(e.Message); + } + } + public async Task> CallServer_GetCurrentState() { - await Clients.All.SendAsync(Message); - //TODO:保存数据 + var userId = Context.UserIdentifier; + try + { + //通过用户id查询用户所在房间,如果用户可以同时存在两个或以上房间会有问题,建议web端传入RoomId + var room = roomManager.GetRoomByUserId(userId!); + if (room != null) + { + return roomManager.GetRoomSendInfoByRoomId(room.RoomId); + } + else + { + throw (new Exception("房间信息有误!")); + } + } + catch (Exception e) + { + throw new Exception(e.Message); + } } + + #endregion + #region 上线下线处理 /// /// 上线处理 /// @@ -315,4 +449,5 @@ namespace AX.WebDrillServer.Hubs } } } + #endregion } diff --git a/AX.WebDrillServer/Services/FireDeductionHub/EndDrillData.cs b/AX.WebDrillServer/Services/FireDeductionHub/EndDrillData.cs new file mode 100644 index 0000000..5ed2083 --- /dev/null +++ b/AX.WebDrillServer/Services/FireDeductionHub/EndDrillData.cs @@ -0,0 +1,8 @@ +namespace AX.WebDrillServer.Services.FireDeductionHub +{ + public class EndDrillData + { + public string Name { get; set; } = null!; + public long StartTime { get; set; } + } +} diff --git a/AX.WebDrillServer/Services/FireDeductionHub/EndDrillResultData.cs b/AX.WebDrillServer/Services/FireDeductionHub/EndDrillResultData.cs new file mode 100644 index 0000000..4c4e00d --- /dev/null +++ b/AX.WebDrillServer/Services/FireDeductionHub/EndDrillResultData.cs @@ -0,0 +1,8 @@ +namespace AX.WebDrillServer.Services.FireDeductionHub +{ + public class EndDrillResultData + { + public DrillResult ResultType { get; set; } + public EndDrillData EndData { get; set; } = null!; + } +} diff --git a/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoom.cs b/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoom.cs index bc13d21..94b1aa2 100644 --- a/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoom.cs +++ b/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoom.cs @@ -23,6 +23,11 @@ namespace AX.WebDrillServer.Services.FireDeductionHub /// public string Owner { get; set; } = null!; public string OwnerName { get; set; } = null!; + /// + /// 演练名称 + /// + public string? DrillName { get; set; } + public long DrillTime { get; set; } public List Users { get; set; } = new List(); public Dictionary? FrameData { get; set; } public Dictionary? EventData { get; set; } diff --git a/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoomEnums.cs b/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoomEnums.cs index 856f5f7..91aa6ae 100644 --- a/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoomEnums.cs +++ b/AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoomEnums.cs @@ -32,6 +32,13 @@ OnLine,//上线 OffLine,//下线 } + + public enum DrillResult + { + Success,//成功 + Failed,//失败 + } + public class FireDeductionRoomEnums { diff --git a/AX.WebDrillServer/Services/FireDeductionHub/RoomManager.cs b/AX.WebDrillServer/Services/FireDeductionHub/RoomManager.cs index c4f33ad..50c73a3 100644 --- a/AX.WebDrillServer/Services/FireDeductionHub/RoomManager.cs +++ b/AX.WebDrillServer/Services/FireDeductionHub/RoomManager.cs @@ -4,8 +4,35 @@ { private readonly List fireDeductionRooms = new(); private readonly List fireDeductionUsers = new(); + private readonly List roomSendInfos = new(); + public bool SaveRoomSendInfo(RoomSendInfo info) + { + bool result = false; + lock (this) + { + if (roomSendInfos.Contains(info)) + { + roomSendInfos.Add(info); + result = true; + //TODO:存储数据方法 + } + else + { + result = false; + } + + return result; + } + } + public List GetRoomSendInfoByRoomId(string roomId) + { + lock(this) + { + return roomSendInfos.Where(x => x.RoomId == roomId).ToList(); + } + } public RoomShowInfo RoomToInfo(FireDeductionRoom source, RoomShowInfo? info = null) { info ??= new(); diff --git a/AX.WebDrillServer/Services/FireDeductionHub/RoomSendInfo.cs b/AX.WebDrillServer/Services/FireDeductionHub/RoomSendInfo.cs new file mode 100644 index 0000000..962b521 --- /dev/null +++ b/AX.WebDrillServer/Services/FireDeductionHub/RoomSendInfo.cs @@ -0,0 +1,9 @@ +namespace AX.WebDrillServer.Services.FireDeductionHub +{ + public class RoomSendInfo + { + public string RoomId { get; set; } = null!; + public string UserId { get; set; } = null!; + public string? InfoData; + } +} diff --git a/AX.WebDrillServer/Services/FireDeductionHub/StartDrillData.cs b/AX.WebDrillServer/Services/FireDeductionHub/StartDrillData.cs new file mode 100644 index 0000000..010cfd7 --- /dev/null +++ b/AX.WebDrillServer/Services/FireDeductionHub/StartDrillData.cs @@ -0,0 +1,10 @@ +using System.Data; + +namespace AX.WebDrillServer.Services.FireDeductionHub +{ + public class StartDrillData + { + public string Name { get; set; } = null!; + public long StartTime { get; set; } + } +} diff --git a/AX.WebDrillServer/Services/FireDeductionHub/StartDrillResultData.cs b/AX.WebDrillServer/Services/FireDeductionHub/StartDrillResultData.cs new file mode 100644 index 0000000..fdbe0af --- /dev/null +++ b/AX.WebDrillServer/Services/FireDeductionHub/StartDrillResultData.cs @@ -0,0 +1,8 @@ +namespace AX.WebDrillServer.Services.FireDeductionHub +{ + public class StartDrillResultData + { + public DrillResult ResultType { get; set; } + public StartDrillData StartData { get; set; } = null!; + } +}