Browse Source

推演相关API

develop
杨栋梁 3 years ago
parent
commit
037b547ff8
  1. 78
      AX.WebDrillServer/Hubs/FireDeductionHub.cs
  2. 15
      AX.WebDrillServer/Services/FireDeductionHub/DrillShowData.cs
  3. 3
      AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoom.cs
  4. 11
      AX.WebDrillServer/Services/FireDeductionHub/PlayerOrder.cs
  5. 26
      AX.WebDrillServer/Services/FireDeductionHub/RoomManager.cs
  6. 1
      AX.WebDrillServer/Services/FireDeductionHub/RoomSendInfo.cs

78
AX.WebDrillServer/Hubs/FireDeductionHub.cs

@ -11,9 +11,11 @@ using Microsoft.VisualBasic;
using NPOI.SS.Util;
using System;
using System.Collections.Concurrent;
using System.Data;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AX.WebDrillServer.Hubs
{
@ -222,7 +224,6 @@ namespace AX.WebDrillServer.Hubs
var userId = Context.UserIdentifier;
try
{
//通过用户id查询用户所在房间,如果用户可以同时存在两个或以上房间会有问题,建议web端传入RoomId
var room = roomManager.GetRoomByUserId(userId!);
if (room == null)
@ -232,7 +233,7 @@ namespace AX.WebDrillServer.Hubs
}
else
{
if (room.RoomId == userId)//判断是否是房主
if (room.Owner == userId)//判断是否是房主
{
resultData.ResultType = DrillResult.Success;
resultData.StartData = new()
@ -242,6 +243,7 @@ namespace AX.WebDrillServer.Hubs
};
room.State = RoomState.Playing;
room.DrillName = startData.Name;
room.StartTime = DateTime.Now.Ticks;
await Clients.Group(room.RoomId).SendAsync("CallWeb_StartDrill", resultData);
return resultData;
@ -274,7 +276,7 @@ namespace AX.WebDrillServer.Hubs
}
else
{
if (room.RoomId == userId)
if (room.Owner == userId)
{
resultData.ResultType = DrillResult.Success;
resultData.EndData = new()
@ -283,6 +285,11 @@ namespace AX.WebDrillServer.Hubs
StartTime = DateTime.Now.Ticks,
};
room.State = RoomState.Over;
room.EndTime = DateTime.Now.Ticks;
//Ticks是一个以0 .1纳秒为单位的时间戳
//1 毫秒=1000000 纳秒
//如果需要换算成具体秒数 second = (room.EndTime - room.StartTime)/10000000
room.DrillTime = room.EndTime - room.StartTime;
await Clients.Group(room.RoomId).SendAsync("CallWeb_EndDrill", resultData);
return resultData;
@ -301,27 +308,58 @@ namespace AX.WebDrillServer.Hubs
}
public async Task CallServer_GetDrillShowData()
public DrillShowData CallServer_GetDrillShowData()
{
var userId = Context.UserIdentifier;
try
{
var room = roomManager.GetRoomByUserId(userId!);
if (room == null)
{
throw new Exception("用户房间信息有误!");
}
else
{
//计算DrillTime
room.DrillTime = DateTime.Now.Ticks - room.StartTime;
DrillShowData resultData = new()
{
DrillTime = room.DrillTime,
Name = room.RoomName,
Owner = roomManager.GetUser(room.Owner)!,
EndTime = room.EndTime,
StartTime = room.StartTime,
Players = room.Users,
};
return resultData;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public async Task CallServer_SendOrder(object data)
public async Task CallServer_SendOrder(PlayerOrder data)
{
//callWeb_sendOthersOrder
var userId = Context.UserIdentifier;
try
{
//通过用户id查询用户所在房间,如果用户可以同时存在两个或以上房间会有问题,建议web端传入RoomId
var room = roomManager.GetRoomByUserId(userId!);
if (room != null)
{
await Clients.Group(room.RoomId).SendAsync("callWeb_sendOthersOrder", userId, data);
data.UserId = userId!;
data.Time = DateTime.Now.Ticks;
//转发给房间中的其他人
await Clients.GroupExcept(room.RoomId, Context.ConnectionId).SendAsync("CallWeb_sendOthersOrder", data);
//存储一份数据
var info = new RoomSendInfo()
{
RoomId = room.RoomId,
UserId = userId!,
InfoData = data.ToJson(),
Time = data.Time,
InfoData = data.Data.ToJson(),
};
roomManager.SaveRoomSendInfo(info);
}
@ -331,17 +369,33 @@ namespace AX.WebDrillServer.Hubs
throw new Exception(e.Message);
}
}
public async Task<List<RoomSendInfo>> CallServer_GetCurrentState()
public List<PlayerOrder> CallServer_GetCurrentState()
{
var userId = Context.UserIdentifier;
var resultData = new List<PlayerOrder>();
try
{
//通过用户id查询用户所在房间,如果用户可以同时存在两个或以上房间会有问题,建议web端传入RoomId
var room = roomManager.GetRoomByUserId(userId!);
if (room != null)
{
return roomManager.GetRoomSendInfoByRoomId(room.RoomId);
var data = roomManager.GetRoomSendInfoByRoomId(room.RoomId);
foreach (var item in data)
{
//JsonElement json=(JsonElement)item.InfoData;
PlayerOrder result = new PlayerOrder
{
UserId = item.UserId!,
Time = item.Time,
};
resultData.Add(result);
}
return resultData;
}
else
{

15
AX.WebDrillServer/Services/FireDeductionHub/DrillShowData.cs

@ -0,0 +1,15 @@
namespace AX.WebDrillServer.Services.FireDeductionHub
{
public class DrillShowData
{
public string? Name { get; set; }
public long StartTime { get; set; }
public long EndTime { get; set; }
public long DrillTime { get; set; }
public FireDeductionUser Owner { get; set; } = null!;
public List<FireDeductionUser> Players { get; set; } = new List<FireDeductionUser>();
}
}

3
AX.WebDrillServer/Services/FireDeductionHub/FireDeductionRoom.cs

@ -27,6 +27,9 @@ namespace AX.WebDrillServer.Services.FireDeductionHub
/// 演练名称
/// </summary>
public string? DrillName { get; set; }
public long StartTime { get; set; }
public long EndTime { get; set; }
public long DrillTime { get; set; }
public List<FireDeductionUser> Users { get; set; } = new List<FireDeductionUser>();
public Dictionary<DateTime, string>? FrameData { get; set; }

11
AX.WebDrillServer/Services/FireDeductionHub/PlayerOrder.cs

@ -0,0 +1,11 @@
using System.Text.Json;
namespace AX.WebDrillServer.Services.FireDeductionHub
{
public class PlayerOrder
{
public string UserId { get; set; } = null!;
public long Time { get; set; }
public JsonElement Data { get; set; }
}
}

26
AX.WebDrillServer/Services/FireDeductionHub/RoomManager.cs

@ -6,6 +6,22 @@
private readonly List<FireDeductionUser> fireDeductionUsers = new();
private readonly List<RoomSendInfo> roomSendInfos = new();
public FireDeductionRoom? GetRoomByUserId(string userId)
{
lock (this)
{
var user = fireDeductionUsers.Where(u => u.UserId == userId).SingleOrDefault();
if (user != null)
{
return fireDeductionRooms.Where(r => r.RoomId == user.RoomId).SingleOrDefault();
}
else
{
return null;
}
}
}
public bool SaveRoomSendInfo(RoomSendInfo info)
{
@ -28,7 +44,7 @@
}
public List<RoomSendInfo> GetRoomSendInfoByRoomId(string roomId)
{
lock(this)
lock (this)
{
return roomSendInfos.Where(x => x.RoomId == roomId).ToList();
}
@ -101,13 +117,7 @@
return fireDeductionUsers.Where(r => r.ConnectionId == conneciontId).SingleOrDefault();
}
}
public FireDeductionRoom? GetRoomByUserId(string userId)
{
lock (this)
{
return fireDeductionRooms.Where(r => r.Users.Where(u => u.UserId == userId).SingleOrDefault() != null).SingleOrDefault();
}
}
public void AddRoom(FireDeductionRoom room)
{
lock (this)

1
AX.WebDrillServer/Services/FireDeductionHub/RoomSendInfo.cs

@ -4,6 +4,7 @@
{
public string RoomId { get; set; } = null!;
public string UserId { get; set; } = null!;
public long Time { get; set; }
public string? InfoData;
}
}

Loading…
Cancel
Save