memberId为0的问题

This commit is contained in:
lishuo 2024-01-08 10:44:46 +08:00
parent 3fc9ac8234
commit 8f0845faaa

View File

@ -9,23 +9,16 @@ using OnlineUserPool.Unility;
using Serilog; using Serilog;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows.Threading; using System.Windows.Threading;
using System.Diagnostics; using System.Diagnostics;
using OnlineUserPool.Services; using OnlineUserPool.Services;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.IO.Compression;
using System.IO;
using System.Windows; using System.Windows;
using OnlineUserPool.Api; using OnlineUserPool.Api;
using OnlineUserPool.Api.Model;
namespace OnlineUserPool.ViewModels namespace OnlineUserPool.ViewModels
{ {
public class MainWindowViewModel : BindableBase public class MainWindowViewModel : BindableBase
{ {
public ObservableCollection<HostModel> Clients { get; private set; } = new ObservableCollection<HostModel>(); public ObservableCollection<HostModel> Clients { get; private set; } = new ObservableCollection<HostModel>();
@ -97,27 +90,21 @@ namespace OnlineUserPool.ViewModels
dispatcher = Dispatcher.CurrentDispatcher; dispatcher = Dispatcher.CurrentDispatcher;
//Customers.Add("suntao");
WriteLine(DateTime.Now.ToShortDateString()); WriteLine(DateTime.Now.ToShortDateString());
LogHelper.Init(); LogHelper.Init();
mapRecordRankingHander = new MapRecordRankingHander();// mapRecordRankingHander = new MapRecordRankingHander();//
//mapRecordRankingHander = new MultiUserHandle(); //mapRecordRankingHander = new MultiUserHandle();
Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}"); Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}");
new TcpService().RunServer(ReceivedData, CientDisconnected); new TcpService().RunServer(ReceivedData, CientDisconnected);
//var tet = new System.Collections.Concurrent.ConcurrentBag<object>();
var _udpService = new UdpService(); var _udpService = new UdpService();
_udpService.RunServer(ReceivedData); _udpService.RunServer(ReceivedData);
Log.Information("服务启动成功"); Log.Information("服务启动成功");
timer = new System.Timers.Timer(500); timer = new System.Timers.Timer(500);
timer.Elapsed += Timer_Elapsed; timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true; timer.AutoReset = true;
timer.Start(); timer.Start();
Log.Information("等待连接"); Log.Information("等待连接");
//Console.ReadKey();
} }
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
@ -132,17 +119,18 @@ namespace OnlineUserPool.ViewModels
{ {
if (!Clients.Any(c => c.Equals(remoteIpEndPoint))) if (!Clients.Any(c => c.Equals(remoteIpEndPoint)))
{ {
Clients.Add(new HostModel if (msg is SetClientCommand client)
{ {
IPEndPoint = remoteIpEndPoint, Clients.Add(new HostModel
LastActiveTime = DateTime.Now, {
//RouteId = msg.RouteId, IPEndPoint = remoteIpEndPoint,
//MemberId = msg.MemberId, LastActiveTime = DateTime.Now,
//V = msg.V, Service = service,
//Encoding = msg.Encoding, MemberId = client.MemberId,
//Client = msg.Client, Encoding = client.Encoding,
Service = service V = client.V
}); });
}
} }
switch (msg.CommandType) switch (msg.CommandType)
@ -201,15 +189,11 @@ namespace OnlineUserPool.ViewModels
} }
if (msg.CommandType == 0) if (msg.CommandType == 0)
{ {
//client.ShowVirtual = msg.ShowVirtual; //client.ShowVirtual = msg.ShowVirtual;
} }
else if (msg.CommandType == 1) else if (msg.CommandType == 1)
{ {
} }
}); });
} }
@ -229,16 +213,16 @@ namespace OnlineUserPool.ViewModels
//对战房间内的人保存处理 //对战房间内的人保存处理
if (msg1.RoomId > 0 && msg1.Saved) if (msg1.RoomId > 0 && msg1.Saved)
{ {
var current = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault(); var current = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
if (current != null) if (current != null)
{ {
//更新房间内的人的保存状态 //更新房间内的人的保存状态
var currentPlayer = current.List.Where(c => c.UserId == msg1.MemberId).FirstOrDefault(); var currentPlayer = current.List.FirstOrDefault(c => c.UserId == msg1.MemberId);
if (currentPlayer != null) if (currentPlayer != null)
{ {
currentPlayer.Saved = true; currentPlayer.Saved = true;
} }
var notAllSaved = current.List.Where(c => !c.Saved).Any(); var notAllSaved = current.List.Any(c => !c.Saved);
if (!notAllSaved && current.Status != 2) if (!notAllSaved && current.Status != 2)
{ {
current.Status = 2; current.Status = 2;
@ -246,7 +230,7 @@ namespace OnlineUserPool.ViewModels
} }
if (!current.Saved) if (!current.Saved)
{ {
notAllSaved = current.List.Where(c => !c.Saved).Any(); notAllSaved = current.List.Any(c => !c.Saved);
if (!notAllSaved && current.Status != 2) if (!notAllSaved && current.Status != 2)
{ {
current.Status = 2; current.Status = 2;
@ -426,7 +410,7 @@ namespace OnlineUserPool.ViewModels
IsLock = !string.IsNullOrEmpty(createMsg.Password), IsLock = !string.IsNullOrEmpty(createMsg.Password),
List = list, List = list,
}; };
var mine = Clients.Where(c => c.MemberId == createMsg.UserId).FirstOrDefault(); var mine = Clients.FirstOrDefault(c => c.MemberId == createMsg.UserId);
if (mine != null) if (mine != null)
{ {
mine.RoomId = room.RoomId; mine.RoomId = room.RoomId;
@ -457,13 +441,13 @@ namespace OnlineUserPool.ViewModels
{ {
var msg1 = msg as JoinGameRoomCommand; var msg1 = msg as JoinGameRoomCommand;
RemoveOtherRoomInfo(msg1.UserId,msg1.RoomId); RemoveOtherRoomInfo(msg1.UserId,msg1.RoomId);
var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault(); var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
var client = Clients.FirstOrDefault(n => n.MemberId.Equals(msg1.UserId)); var client = Clients.FirstOrDefault(n => n.MemberId.Equals(msg1.UserId));
//更新房间信息 //更新房间信息
if (room != null) if (room != null)
{ {
var list = room.List; var list = room.List;
var member = list.Where(c => c.UserId == msg1.UserId).FirstOrDefault(); var member = list.FirstOrDefault(c => c.UserId == msg1.UserId);
if (member != null) if (member != null)
{ {
member.JoinAt = msg1.JoinAt; member.JoinAt = msg1.JoinAt;
@ -514,12 +498,12 @@ namespace OnlineUserPool.ViewModels
private void HandleGameRoomStart(ReceiveModel msg) private void HandleGameRoomStart(ReceiveModel msg)
{ {
var msg1 = msg as GameRoomStartCommand; var msg1 = msg as GameRoomStartCommand;
var client = Clients.Where(c => c.MemberId == msg1.UserId).FirstOrDefault(); var client = Clients.FirstOrDefault(c => c.MemberId == msg1.UserId);
if (client != null) if (client != null)
{ {
client.RoomId = msg1.RoomId; client.RoomId = msg1.RoomId;
} }
var room = RoomList.ToList().Where(c => c.RoomId == msg1.RoomId).FirstOrDefault(); var room = RoomList.ToList().FirstOrDefault(c => c.RoomId == msg1.RoomId);
if (room != null) if (room != null)
{ {
room.Status = 1; room.Status = 1;