针对unity版本比赛功能的调整

This commit is contained in:
suntao committed 2021-07-07 09:49:36 +08:00
1 parent b9bb485a2d
commit c380654041
10 files changed
+393 -74

No files matched your search

+178 -34
View File
@@ -16,6 +16,8 @@ using System.Diagnostics;
using OnlineUserPool.Services;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.IO.Compression;
using System.IO;
namespace OnlineUserPool.ViewModels
{
@@ -61,11 +63,11 @@ namespace OnlineUserPool.ViewModels
//Customers.Add("suntao");
WriteLine(DateTime.Now.ToShortDateString());
LogHelper.Init();
mapRecordRankingHander = new MultiUserHandle();//new MapRecordRankingHander();
mapRecordRankingHander = new MapRecordRankingHander();//new MultiUserHandle();//
//mapRecordRankingHander = new MultiUserHandle();
Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}");
new TcpService1().RunServer(ReceivedData);
new TcpService1().RunServer(ReceivedData, CientDisconnected);
//var tet = new System.Collections.Concurrent.ConcurrentBag<object>();
var _udpService = new UdpService();
@@ -81,7 +83,7 @@ namespace OnlineUserPool.ViewModels
//Console.ReadKey();
}
private void ReceivedData(IPEndPoint remoteIpEndPoint, MsgModel msg, IService service)
private void ReceivedData(IPEndPoint remoteIpEndPoint, ReceiveModel msg, IService service)
{
dispatcher.Invoke(() =>
{
@@ -91,24 +93,76 @@ namespace OnlineUserPool.ViewModels
{
IPEndPoint = remoteIpEndPoint,
LastActiveTime = DateTime.Now,
RouteId = msg.RouteId,
MemberId = msg.MemberId,
//ShowVirtual = msg.ShowVirtual
V = msg.V,
//RouteId = msg.RouteId,
//MemberId = msg.MemberId,
//V = msg.V,
//Encoding = msg.Encoding,
//Client = msg.Client,
Service = service
});
}
switch (msg.CommandType)
{
//ping命令
case 0:
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
client.LastActiveTime = DateTime.Now;
if(msg.V > 0)
{
client.V = msg.V;
}
}
break;
//消息
case 1:
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
var msg1 = (msg as MsgModel);
client.Competitionid = msg1.Competitionid;
if (msg1.V > 0)
{
client.V = msg1.V;
}
receiveMes.Add(msg1);
}
break;
//设置客户端
case 2:
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
var msg1 = (msg as SetClientCommand);
client.Encoding = msg1.Encoding;
client.Client = msg1.Client;
client.V = msg1.V;
client.MemberId = msg1.MemberId;
client.Competitionid = msg1.Competitionid;
client.IsWatch = msg1.IsWatch;
}
break;
//case 3:
// {
// var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
// var msg1 = (msg as SetWatchCommand);
// client.Competitionid = msg1.Competitionid;
// client.IsWatch = true;
// }
// break;
default:
break;
}
if (msg.CommandType == 0)
{
var client = Clients.FirstOrDefault(n => n.Equals(remoteIpEndPoint));
client.LastActiveTime = DateTime.Now;
//client.ShowVirtual = msg.ShowVirtual;
}
else if (msg.CommandType == 1)
{
receiveMes.Add(msg);
}
});
}
@@ -125,7 +179,7 @@ namespace OnlineUserPool.ViewModels
{
dispatcher.Invoke(() =>
{
Customers.Clear();
//Customers.Clear();
//删除已经发送的数据
//receiveMes.Clear();
@@ -142,7 +196,7 @@ namespace OnlineUserPool.ViewModels
}
lock (locker)
{
var list = CloneJson(receiveMes).ToList();
var msgs = CloneJson(receiveMes).ToList();
receiveMes.Clear();
//加入虚拟人物消息
@@ -150,17 +204,17 @@ namespace OnlineUserPool.ViewModels
WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-当前在线人数:" + Clients.Count + "-当前虚拟人数:" + virtualData.Count);
#if DEBUG
WriteLine($"在线人:{Newtonsoft.Json.JsonConvert.SerializeObject(list)}");
WriteLine($"在线人:{Newtonsoft.Json.JsonConvert.SerializeObject(msgs)}");
//\r\n虚拟人:{Newtonsoft.Json.JsonConvert.SerializeObject(virtualData)}
#endif
list.AddRange(virtualData);
msgs.AddRange(virtualData);
SendMessage(Clients, list);
SendMessage(Clients, msgs);
dispatcher.Invoke(() =>
{
Customers.Clear();
foreach (var item in list)
foreach (var item in msgs)
{
if (Customers.Any(c => c.MemberId == item.MemberId))
{
@@ -170,12 +224,12 @@ namespace OnlineUserPool.ViewModels
}
//移除下线的客户端
for (int i = 0; i < list.Count; i++)
for (int i = 0; i < msgs.Count; i++)
{
if (list[i].exit && list[i].MemberId >0)//客户端退出,并且不是虚拟的人物
if (msgs[i].exit && msgs[i].MemberId >0)//客户端退出,并且不是虚拟的人物
{
//这个地方有严重的逻辑错误(虚拟的人物不能和真实的人用同一个名字)
var info = Clients.FirstOrDefault(n => n.MemberId == list[i].MemberId);
var info = Clients.FirstOrDefault(n => n.MemberId == msgs[i].MemberId);
if (info != null)
{
Clients.Remove(info);
@@ -218,7 +272,7 @@ namespace OnlineUserPool.ViewModels
item.EndDistance = Math.Round(item.EndDistance, 5);
item.WeightKg = Math.Round(item.WeightKg, 2);
}
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(list.Select(m=> new {
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(list.Select(m=> new {
m.RouteId,
m.MemberId,
m.Point,
@@ -240,13 +294,29 @@ namespace OnlineUserPool.ViewModels
{
strV1 += "|";
}
//strV1 = "abcdefghijklmnopqrstuvwxyz";
//System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "要发送的数据.txt", strV1.Trim().Replace("\0", "") + "\r\n", Encoding.UTF8);
WriteLine(strV1);
WriteLine(strV1.Length.ToString());
var dataV1 = Encoding.ASCII.GetBytes(strV1);
var dataV1ForUdp = strV1.Last() == '|' ? Encoding.ASCII.GetBytes(strV1.Substring(0, strV1.Length - 1)) : dataV1;
SendDataSize = $"\t单客户端数据包V1:{ (strV1.Length/1000D) }KB,一共占用带宽:{ strV1.Length / 1000D * clients1.Count }KB";
//var dataV1 = Encoding.UTF8.GetBytes(strV1);
//var dataV1ForUdp = strV1.Last() == '|' ? Encoding.ASCII.GetBytes(strV1.Substring(0, strV1.Length - 1)) : dataV1;
//var dataCompressV1 = Encoding.UTF8.GetBytes($"*{ Convert.ToBase64String(CommonHelper.Compress(strV1)) }#");
var data1 = new Data1(strV1);
var data1ForUdp = new Data1(strV1.Last() == '|' ? strV1.Substring(0, strV1.Length - 1) : strV1);
//var watchList = string.Join('|', clients1.Where(c => c.IsWatch).Select(c => c.MemberId));
//var strV2 = $"*l{{{ strV1 }}};w{{{ watchList }}}#";
////var dataCompressV2 = Encoding.UTF8.GetBytes($"{ Convert.ToBase64String(CommonHelper.Compress(strV2)) }");
//var data2 = new Data1(strV2);
SendDataSize = $"\t单客户端数据包V1:{ data1.GetBytes().Length/1000D }KB,一共占用带宽:{ data1.GetBytes().Length / 1000D * clients1.Count }KB,压缩以后{ data1.GetBytes(true).Length/1000D * clients1.Count }KB";
#region udp发送数据
foreach (var item in clients1)
{
try
@@ -255,19 +325,44 @@ namespace OnlineUserPool.ViewModels
{
continue;
}
if((item.Service is UdpService) ==false)
//if((item.Service is UdpService) ==false)
//{
// continue;
//}
if (item.V != 1 && item.V != 2)
{
item.Service.Send(data, data.Length, item.IPEndPoint);
continue;
}
if (item.V == 1)
if (item.V == 1 && item.Service is UdpService)
{
item.Service.Send(dataV1ForUdp, dataV1ForUdp.Length, item.IPEndPoint);
item.Service.Send(data1ForUdp.GetBytes(), data1ForUdp.GetBytes().Length, item.IPEndPoint);
}
else
{
item.Service.Send(data, data.Length, item.IPEndPoint);
}
Data1 ddd = null;// = item.V == 1 ? ;
if (item.V == 1)
{
ddd = data1;
}
else if (item.V == 2)
{
//ddd = data2;
var temp = string.Join("|", list.Where(m => m.Competitionid == item.Competitionid).Select(m => m.ToString(1))) + "|";
var watchList1 = string.Join('|', clients1.Where(c => c.IsWatch && c.Competitionid == item.Competitionid).Select(c => c.MemberId));
var strV21 = $"*l{{{ temp }}};w{{{ watchList1 }}}#";
ddd = new Data1(strV21);
}
else
{
continue;
}
bool isZip = item.Encoding == "gzip";
item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint);
}
}
catch (Exception e)
{
@@ -275,11 +370,15 @@ namespace OnlineUserPool.ViewModels
Log.Error($"{ item.IPEndPoint.ToString() }:{ e.Message }\r\n{ e.StackTrace }");
}
}
var tcpClient = clients1.Where(c => c.Service is TcpService1).FirstOrDefault();
if(tcpClient != null)
{
tcpClient.Service.Send(dataV1, dataV1.Length, null);
}
#endregion
//#region tcp发送数据
//var tcpClient = clients1.Where(c => c.Service is TcpService1).FirstOrDefault();
//if(tcpClient != null)
//{
// tcpClient.Service.Send(dataV1, dataV1.Length, null);
//}
//#endregion
}
@@ -304,5 +403,50 @@ namespace OnlineUserPool.ViewModels
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
}
private void CientDisconnected(EndPoint point)
{
var client = Clients.FirstOrDefault(f => f.IPEndPoint.ToString() == point.ToString());
if (client != null)
{
dispatcher.Invoke(() =>
{
Clients.Remove(client);
});
}
}
}
public class Data1
{
private string _txt = "";
public Data1(string txt)
{
this._txt = txt;
}
public byte[] GetBytes(bool compress = false)
{
if (compress)
{
return GetCompressBytes();
}
return Encoding.UTF8.GetBytes(this._txt);
}
private byte[] GetCompressBytes()
{
var temp = this._txt;
if(temp[0] == '*')
{
temp = temp.Substring(1);
}
if(temp.Last() == '#')
{
temp = temp.Substring(0, temp.Length - 1);
}
return Encoding.UTF8.GetBytes($"*{ Convert.ToBase64String(CommonHelper.Compress(temp)) }#");
}
}
}