From 01aa33ba941040b6849fcd78d3ad6e0cf8a270c4 Mon Sep 17 00:00:00 2001
From: lishuo <>
Date: Tue, 3 Feb 2026 11:38:29 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=E6=94=B9?=
=?UTF-8?q?=E4=B8=BA20s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Model/HostModel.cs | 2 +-
Model/MsgModel.cs | 48 +--
ViewModels/MainWindowViewModel.cs | 477 +-----------------------------
3 files changed, 36 insertions(+), 491 deletions(-)
diff --git a/Model/HostModel.cs b/Model/HostModel.cs
index 33bdfed..eb99ee3 100644
--- a/Model/HostModel.cs
+++ b/Model/HostModel.cs
@@ -53,7 +53,7 @@ namespace OnlineUserPool.Model
{
get
{
- return DateTime.Now.Subtract(LastActiveTime).TotalSeconds > 60;
+ return DateTime.Now.Subtract(LastActiveTime).TotalSeconds > 20;
}
}
diff --git a/Model/MsgModel.cs b/Model/MsgModel.cs
index 8b60af3..5533a2d 100644
--- a/Model/MsgModel.cs
+++ b/Model/MsgModel.cs
@@ -64,11 +64,6 @@ namespace OnlineUserPool.Model
set => SetProperty(ref _TotalTicks, value);
}
- /////
- ///// 需要展示的属性
- /////
- //public string Prop { get; set; }
-
private double _PreDistance = 0;
public double PreDistance {
get => _PreDistance;
@@ -83,8 +78,6 @@ namespace OnlineUserPool.Model
//public bool IsVirtual { get; set; }
- //public double Power { get; set; }
- //public double Weight { get; set; }
///
/// 功率体重比
///
@@ -106,12 +99,10 @@ namespace OnlineUserPool.Model
set => SetProperty(ref _Saved, value);
}
- //public bool ShowVirtual { get; set; }
-
- private int _FrameRate = 0;
- public int FrameRate {
- get => _FrameRate;
- set => SetProperty(ref _FrameRate, value);
+ private int _Frame = 0;
+ public int Frame {
+ get => _Frame;
+ set => SetProperty(ref _Frame, value);
}
private int _RoomId = 0;
@@ -133,18 +124,35 @@ namespace OnlineUserPool.Model
get => _Model;
set => SetProperty(ref _Model, value);
}
+
+ private string _UserName;
+ public string UserName {
+ get => _UserName;
+ set => SetProperty(ref _UserName, value);
+ }
+
+ private string _RouteName;
+ public string RouteName {
+ get => _RouteName;
+ set => SetProperty(ref _RouteName, value);
+ }
+
+ private string _WxHeadImage;
+ public string WxHeadImage {
+ get => _WxHeadImage;
+ set => SetProperty(ref _WxHeadImage, value);
+ }
- public string ToString(int v)
+ public override string ToString()
{
- if (v == 2)
- {
- return $"{ RouteId },{ MemberId },{ string.Join(":", Point) },{ Convert.ToInt32(IsCompleted) },{ Speed },{ PreDistance },{ EndDistance },{ WeightKg },{ CompetitionId },{ Convert.ToInt32(Saved)},{ Power} ,{ HeartRate},{ Cadence},{ TotalTicks},{ FrameRate},{RoomId},{StartTime}";
- }
- return $"{ RouteId },{ MemberId },{ string.Join(":", Point) },{ Convert.ToInt32(IsCompleted) },{ Speed },{ PreDistance },{ EndDistance },{ WeightKg },{ CompetitionId },{ Convert.ToInt32(Saved)}";
+ return $"{RouteName},{RouteId},{UserName},{MemberId},{WxHeadImage},{string.Join(":", Point)},{Speed},{PreDistance},{EndDistance},{WeightKg},{Power},{HeartRate},{Cadence},{TotalTicks},{Convert.ToInt32(IsCompleted)},{Frame}";
}
public void Update(MsgModel model)
{
+ this.RouteName = model.RouteName;
+ this.UserName = model.UserName;
+ this.WxHeadImage = model.WxHeadImage;
this.CompetitionId = model.CompetitionId;
this.EndDistance = model.EndDistance;
this.HeartRate = model.HeartRate;
@@ -159,7 +167,7 @@ namespace OnlineUserPool.Model
this.HeartRate = model.HeartRate;
this.Cadence = model.Cadence;
this.TotalTicks = model.TotalTicks;
- this.FrameRate = model.FrameRate;
+ this.Frame = model.Frame;
this.RoomId = model.RoomId;
this.StartTime = model.StartTime;
this.Request = model.Request;
diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs
index f5d4915..6c019ce 100644
--- a/ViewModels/MainWindowViewModel.cs
+++ b/ViewModels/MainWindowViewModel.cs
@@ -72,17 +72,9 @@ namespace OnlineUserPool.ViewModels
private const int ROOM_TIME_OUT = 60;
private const int TOTAL_PROCESS = 100;
- //private IService _udpService;
public MainWindowViewModel()
{
- //初始化对战房间信息
- var r = GameRoomHelper.Get();
- foreach (var item in r)
- {
- RoomList.Add(item);
- }
- RoomMaxId = GameRoomHelper.GetMaxId();
//初测程序关闭事件
Application.Current.MainWindow.Closing += MainWindow_Closing;
Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }";
@@ -118,8 +110,7 @@ namespace OnlineUserPool.ViewModels
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
- GameRoomHelper.Set(RoomList.ToList());
- GameRoomHelper.SetMaxId(RoomMaxId);
+
}
private void ReceivedData(IPEndPoint remoteIpEndPoint, ReceiveModel msg, IService service)
@@ -192,12 +183,7 @@ namespace OnlineUserPool.ViewModels
client.IsWatch = msg1.IsWatch;
}
break;
- //GameRoom
- case 3:
- {
- HandleGameRoom(msg);
- }
- break;
+
}
});
}
@@ -206,12 +192,6 @@ namespace OnlineUserPool.ViewModels
{
Ticks++;
NotifyClient();
- //定时保存当前房间信息
- if (Ticks % 5 == 0)
- {
- GameRoomHelper.SetMaxId(RoomMaxId);
- GameRoomHelper.Set(RoomList.ToList());
- }
}
private void HandleGameRoomSaved(MsgModel msg1)
@@ -248,392 +228,6 @@ namespace OnlineUserPool.ViewModels
}
}
- private void HandleGameRoom(ReceiveModel msg)
- {
- //收
- switch (msg.SubType)
- {
- case 0: HandleCreateGameRoom(msg); break;
- case 1: HandleJoinGameRoom(msg); break;
- case 2: HandleGameRoomReadyStatus(msg); break;
- case 3: HandleGameRoomStart(msg); break;
- case 4: HandleGameRoomKick(msg); break;
- case 5: HandleGameRoomProcess(msg); break;
- case 6: HandleQueryGameRoomList(msg); break;//查询房间列表
- default:
- break;
- }
- switch (msg.SubType)
- {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4: BroadCastGameRoomList(); break;
- }
- }
-
- private void SendGameRoomMessage(RoomModel gameRoom)
- {
- if (gameRoom == null)
- return;
- var list = new List();
- list.Add(new MsgModel()
- {
- V = 2,
- Point = new double[] { 0d, 0d },
- });
- string ss = "";
- if (gameRoom.List != null && gameRoom.List.Count > 0)
- {
- ss = string.Join("|", gameRoom.List.Select(c => c.ToString()));
- }
- int total = 0;
- var G = $"{total}l[{gameRoom},detail{{{ss}}}]";
- var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|";
- var strV21 = $"l{{{ temp }}};g{{{ G}}};";
- var ddd = new NetworkData(strV21);
-
- var needList = Clients.Where(c => c.RoomId == gameRoom.RoomId).ToList();
- foreach (var client in needList)
- {
- bool isZip = client.Encoding == "gzip";
- client.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, client.IPEndPoint);
- client.Request = "";
- }
- }
-
- private void BroadCastGameRoomList()
- {
- //广播
- var needlist = Clients.Where(c => c.Model != null && c.Model.Equals("GameRoom") && !string.IsNullOrEmpty(c.Request));
-
- var list = new List();
- list.Add(new MsgModel()
- {
- V = 2,
- Point = new double[] { 0d, 0d },
- });
- var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|";
- foreach (var item in needlist)
- {
- var G = GameRoomMessageHandler(item);
- var strV21 = $"l{{{ temp }}};g{{{G}}};";
- var ddd = new NetworkData(strV21);
- bool isZip = item.Encoding == "gzip";
- item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint);
- }
- }
-
- //查询房间列表
- private void HandleQueryGameRoomList(ReceiveModel msg)
- {
- var query = msg as QueryGameRoomListCommand;
- var needSend = Clients.Where(c => c.MemberId == query.UserId).FirstOrDefault();
- if (needSend == null)
- return;
-
- needSend.Request = $"List/{query.PageIndex}/{query.PageSize}/{query.Name}";
-
- var list = new List();
- list.Add(new MsgModel()
- {
- V = 2,
- Point = new double[] { 0d, 0d },
- });
-
- var temp = string.Join("|", list.Where(m => m.CompetitionId == needSend.Competitionid).Select(m => m.ToString(2))) + "|";
- List rooms;
- if (!string.IsNullOrEmpty(query.Name))
- {
- rooms = RoomList.ToList().Where(c => c.Name.Contains(query.Name) || c.RoomId.ToString() == query.Name).OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
- }
- else
- {
- rooms = RoomList.ToList().OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
- }
- //计算总页数
- int total = 0;
- if (query.PageSize != 0)
- {
- total = rooms.Count() / query.PageSize;
- total = rooms.Count() % query.PageSize == 0 ? total : total + 1;
- }
- rooms = rooms.Skip(query.PageIndex * query.PageSize).Take(query.PageSize).ToList();
- string roomInfo = $"{total}l";
- foreach (var r in rooms)
- {
- string ss = "";
- if (r.List != null && r.List.Count > 0)
- {
- ss = string.Join("|", r.List.Select(c => c.ToString()));
- }
- roomInfo += $"[{r.ToString()},detail{{{ss}}}]";
- }
-
- var strV21 = $"l{{{ temp }}};g{{{roomInfo}}};";
- var ddd = new NetworkData(strV21);
- bool isZip = needSend.Encoding == "gzip";
- needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint);
- }
-
- //创建房间
- private void HandleCreateGameRoom(ReceiveModel msg)
- {
- var createMsg = msg as CreateGameRoomCommand;
-
- RemoveOtherRoomInfo(createMsg.UserId, createMsg.Id);
-
- var roomId = RoomMaxId + 1;
- var list = new List();
- list.Add(new RoomDetailModel()
- {
- RoomId = roomId,
- UserId = createMsg.UserId,
- JoinAt = DateTime.Now.ToUniversalTime(),
- IsOwner = true,
- });
-
- var room = new RoomModel
- {
- RoomId = roomId,
- Name = createMsg.Name,
- Password = createMsg.Password,
- CloseTime = createMsg.CloseTime,
- CreateTime = createMsg.CreateTime,
- MapRouteId = createMsg.RouteId,
- MapRouteName = createMsg.MapRouteName,
- AltitudeGraph = createMsg.AltitudeGraph,
- MaxMembers = createMsg.MaxMembers,
- UserId = createMsg.UserId,
- AverageGrade = createMsg.AverageGrade,
- TotalClimb = createMsg.TotalClimb,
- Distance = createMsg.Distance,
- EnableAR = createMsg.EnableAR,
- Enable3D = createMsg.Enable3D,
- FileName = createMsg.FileName,
- FileUrl = createMsg.FileUrl,
- IsLock = !string.IsNullOrEmpty(createMsg.Password),
- List = list,
- };
- var mine = Clients.FirstOrDefault(c => c.MemberId == createMsg.UserId);
- if (mine != null)
- {
- mine.RoomId = room.RoomId;
- }
- RoomList.Add(room);
- RoomMaxId = room.RoomId;
- }
-
- //先移除当前用户在其他房间的信息(考虑服务器重启的时候没有响应客户端退出命令)
- private void RemoveOtherRoomInfo(int userId, int roomId)
- {
- var preRoom = RoomList.ToList().FirstOrDefault(c => c.Status == 0 && c.List.Any(a => a.UserId == userId) && c.RoomId != roomId);
- if (preRoom != null)
- {
- var needRemove = preRoom.List.FirstOrDefault(c => c.UserId == userId && c.Saved == false);
- preRoom.List.Remove(needRemove);
- if (preRoom.List.Count == 0)
- {
- RoomList.Remove(preRoom);
- }
- //发送给这个房间的人
- SendGameRoomMessage(preRoom);
- }
- }
-
- //处理当前用户加入房间
- private void HandleJoinGameRoom(ReceiveModel msg)
- {
- var msg1 = msg as JoinGameRoomCommand;
- RemoveOtherRoomInfo(msg1.UserId,msg1.RoomId);
- var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
- var client = Clients.FirstOrDefault(n => n.MemberId.Equals(msg1.UserId));
- //更新房间信息
- if (room != null)
- {
- var list = room.List;
- var member = list.FirstOrDefault(c => c.UserId == msg1.UserId);
- if (member != null)
- {
- member.JoinAt = msg1.JoinAt;
- }
- else
- {
- //如果房间人数已经满了就拒绝加入
- var currentCount = room.List.Count;
- if (currentCount + 1 <= room.MaxMembers)
- {
- list.Add(new RoomDetailModel
- {
- UserId = msg1.UserId,
- JoinAt = msg1.JoinAt,
- RoomId = msg1.RoomId
- });
- if (client != null)
- {
- client.RoomId = room.RoomId;
- client.Request = "";
- }
- }
- }
- }
- //发送当前房间的信息给当前房间内的人
- SendGameRoomMessage(room);
- }
-
- //处理当前用户准备状态
- private void HandleGameRoomReadyStatus(ReceiveModel msg)
- {
- var msg1 = msg as GameRoomReadyCommand;
- var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault();
- if (room != null)
- {
- var list = room.List;
- var member = list.Where(c => c.UserId == msg1.UserId).FirstOrDefault();
- if (member != null)
- {
- member.Status = msg1.Status;
- }
- }
- //发送信息给当前房间内的人
- SendGameRoomMessage(room);
- }
-
- //处理当前用户准备状态
- private void HandleGameRoomStart(ReceiveModel msg)
- {
- var msg1 = msg as GameRoomStartCommand;
- var client = Clients.FirstOrDefault(c => c.MemberId == msg1.UserId);
- if (client != null)
- {
- client.RoomId = msg1.RoomId;
- }
- var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
- if (room != null)
- {
- room.Status = 1;
- room.StatusChangedTime = DateTime.Now;
- }
- //发送信息给当前房间内的人
- SendGameRoomMessage(room);
- }
-
- //处理房间踢人的操作
- private void HandleGameRoomKick(ReceiveModel msg)
- {
- var msg1 = msg as GameRoomKickCommand;
- var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
- if (room != null && room.Status !=2)
- {
- var list = room.List;
- var needRemove = list.FirstOrDefault(c => c.UserId == msg1.UserId);
- if (needRemove != null)
- {
- list.Remove(needRemove);
- //如果当前是房主退出房间,房主替换成其他人, 房主客户端中断也算
- if (needRemove.IsOwner && list.Any())
- {
- var newOwner = list.FirstOrDefault();
- newOwner.IsOwner = true;
- room.UserId = newOwner.UserId;
- }
- }
- //房间没有产生记录且人数为0,删除房间
- if (!room.Saved && !list.Any())
- {
- RoomList.Remove(room);
- }
-
- //发送信息给当前房间内的人
- SendGameRoomMessage(room);
-
- var mine = Clients.FirstOrDefault(c => c.MemberId == msg1.UserId);
- if (mine != null)
- {
- mine.RoomId = 0;
- }
- }
- }
-
- //处理房间内人loading进度
- private void HandleGameRoomProcess(ReceiveModel msg)
- {
- var msg1 = msg as GameRoomProcessCommand;
- var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
- var client = Clients.FirstOrDefault(c => c.MemberId == msg1.UserId);
- if (room != null)
- {
- var list = room.List;
- var player = list.FirstOrDefault(c => c.UserId == msg1.UserId);
- if (player != null)
- {
- player.Process = msg1.Process;
- }
-
- var notReady = list.Any(c => c.Process < TOTAL_PROCESS);
- var now = DateTime.Now;
- var timespan = now - room.StatusChangedTime;
- //超时时间1分钟所有人强制开始
- if ((notReady && timespan.TotalSeconds > ROOM_TIME_OUT) || !notReady)
- {
- room.StartTime = now.AddSeconds(10).ToUniversalTime();
- }
- if (client != null)
- {
- client.RoomId = room.RoomId;
- }
- SendGameRoomMessage(room);
- }
- }
-
- private string GameRoomMessageHandler(HostModel item)
- {
- //如果当前Client有请求房间列表信息,发送房间列表信息
- var G = "";
- if (!string.IsNullOrEmpty(item.Request))
- {
- var param = item.Request.Split('/');
- var pageInfo = param[0];
- if (pageInfo.Equals("List"))
- {
- var pageIndex = Convert.ToInt32(param[1]);
- var pageSize = Convert.ToInt32(param[2]);
- var querName = param[3].ToString();
- List rooms;
-
- if (!string.IsNullOrEmpty(querName))
- {
- rooms = RoomList.ToList().Where(c => c.Name.Contains(querName) || c.RoomId.ToString() == querName).OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
- }
- else
- {
- rooms = RoomList.ToList().OrderBy(c => c.Status).ThenByDescending(c => c.CreateTime).ToList();
- }
- //计算总页数
- int total = 0;
- if (pageSize != 0)
- {
- total = rooms.Count() / pageSize;
- total = rooms.Count() % pageSize == 0 ? total : total + 1;
- }
- G = $"{total}l";
- rooms = rooms.Skip(pageIndex * pageSize).Take(pageSize).ToList();
-
- foreach (var r in rooms)
- {
- string ss = "";
- if (r.List != null && r.List.Count > 0)
- {
- ss = string.Join("|", r.List.Select(c => c.ToString()));
- }
- G += $"[{r.ToString()},detail{{{ss}}}]";
- }
- }
- }
- return G;
- }
-
private void NotifyClient()
{
try
@@ -772,7 +366,7 @@ namespace OnlineUserPool.ViewModels
m.WeightKg,
Competitionid = m.CompetitionId,
m.Saved,
- m.FrameRate,
+ m.Frame,
}));
var data = Encoding.ASCII.GetBytes(jsonString);
item.Service.Send(data, data.Length, item.IPEndPoint);
@@ -780,46 +374,13 @@ namespace OnlineUserPool.ViewModels
continue;
}
- if (item.V == 1 && item.Service is UdpService)
- {
- sb.Clear();
-
- foreach (var obj in list)
- {
- sb.Append(obj.ToString(1));
- sb.Append("|");
- }
-
- var strV1 = sb.ToString();
- var data1ForUdp = new NetworkData(strV1.Last() == '|' ? strV1.Substring(0, strV1.Length - 1) : strV1);
- item.Service.Send(data1ForUdp.GetBytes(), data1ForUdp.GetBytes().Length, item.IPEndPoint);
- data1ForUdp = null;
- }
-
if(item.Service is TcpService)
{
NetworkData networkData = null;
- if (item.V == 1)
- {
- sb.Clear();
- foreach (var obj in list)
- {
- sb.Append(obj.ToString(1));
- sb.Append("|");
- }
-
- var strV1 = sb.ToString();
- var data1 = new NetworkData(strV1);
- networkData = data1;
- }
- else if (item.V == 2)
+ if (item.V == 2)
{
networkData = HandleGzipNetworkData(list,item);
}
- else
- {
- continue;
- }
bool isZip = item.Encoding == "gzip";
item.Service.Send(networkData.GetBytes(isZip), networkData.GetBytes(isZip).Length, item.IPEndPoint);
@@ -842,7 +403,7 @@ namespace OnlineUserPool.ViewModels
{
sb.Clear();
sb.Append("l{");
- var runtimeList = list.Where(m => m.CompetitionId == item.Competitionid).Select(m => m.ToString(2)).ToList();
+ var runtimeList = list.Where(m => m.CompetitionId == item.Competitionid).Select(m => m.ToString()).ToList();
foreach (var obj in runtimeList)
{
sb.Append(obj);
@@ -868,10 +429,10 @@ namespace OnlineUserPool.ViewModels
sb.Append("};e{");
- var errorList = list.Where(c => c.RoomId > 0 || c.FrameRate > 0).ToList();
+ var errorList = list.Where(c => c.RoomId > 0 || c.Frame > 0).ToList();
foreach (var c in errorList)
{
- sb.Append($"{c.MemberId},{c.RoomId},{c.FrameRate},{c.TotalTicks}");
+ sb.Append($"{c.MemberId},{c.RoomId},{c.Frame},{c.TotalTicks}");
sb.Append("|");
}
@@ -887,29 +448,6 @@ namespace OnlineUserPool.ViewModels
return data2;
}
- private void GameRoomDisConnectHandler(HostModel client)
- {
- ////如果是在对战房间或者对战列表掉线执行,骑行阶段断开连接不移除房间信息
- //if (client.Model != null && !client.Model.Equals("GameRoom"))
- // return;
-
- RoomList.ToList().ForEach(o => {
- if (o.List != null && o.Status != 2)
- {
- var needRemove = o.List.FirstOrDefault(c => c.UserId == client.MemberId && c.Saved == false);
- o.List.Remove(needRemove);
- }
- });
-
- var needRemoveList = RoomList.ToList().Where(c => c.List.Count == 0 && c.Status != 2).ToList();
- foreach (var item in needRemoveList)
- {
- RoomList.Remove(item);
- }
-
- BroadCastGameRoomList();
- }
-
void WriteLine(string str)
{
//Debug.WriteLine(str);
@@ -942,7 +480,6 @@ namespace OnlineUserPool.ViewModels
if (client != null)
{
RemoveClient(client);
- GameRoomDisConnectHandler(client);
}
}
catch(Exception ex)