diff --git a/Hander/MapRecordRankingHander.cs b/Hander/MapRecordRankingHander.cs index f9d60f4..ab73e9d 100644 --- a/Hander/MapRecordRankingHander.cs +++ b/Hander/MapRecordRankingHander.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Timers; namespace OnlineUserPool.Hander { @@ -20,22 +21,58 @@ namespace OnlineUserPool.Hander public MapRecordRankingHander() { Init(); + //var timer = new Timer(); + //timer } private void Init() { if (ConfigHelp.Top > 0) { - var records = WebService.GetMapRouteRandomRecord(ConfigHelp.Top, null); - var pageSize = 10; - var pageCount = (int)Math.Ceiling(records.Count / (double)pageSize); - for (int i = 0; i < pageCount; i++) - { - mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Skip(i * pageSize).Take(pageSize).Select(n => n.Id).ToList())); - } + var records = WebService.GetMapRouteRandomRecord(START, null); + mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Select(n => n.Id).ToList())); + UpdateVirtualTime = DateTime.Now; + //var pageSize = 10; + //var pageCount = (int)Math.Ceiling(records.Count / (double)pageSize); + //for (int i = 0; i < pageCount; i++) + //{ + // mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Skip(i * pageSize).Take(pageSize).Select(n => n.Id).ToList())); + //} } } + private const int START = 15; + private const int SMALL = 50; + private const int MEDIUM = 100; + private const int LARGE = 200; + private const int OFFSET = 15; + + //根据当前时段计算虚拟总人数 + private int ComputeTop() + { + var rand = new Random(); + + var hour = DateTime.Now.Hour;//当前点数 + int offset = 1;//少:1 中:3 多:10 + if (hour <= 6) + { + offset = rand.Next(START- OFFSET, START+ OFFSET); + } + if (hour > 6 && hour <= 18) + { + offset = rand.Next(MEDIUM - OFFSET, MEDIUM + OFFSET); + } + if (hour >= 18 && hour <= 22) + { + offset = rand.Next(LARGE - OFFSET, LARGE + OFFSET); + } + if (hour > 22 && hour <= 24) + { + offset = rand.Next(SMALL - OFFSET, SMALL + OFFSET); + } + return offset; + } + /// /// 获取虚拟人物数据 /// @@ -67,6 +104,28 @@ namespace OnlineUserPool.Hander { weightKg = Math.Round(targetData._Power / item.Weight, 2); } + + if (item.CurrentIndex == 0) + { + //机器人上线 + var loginInfo = new MsgModel() + { + exit = item.End, + IsCompleted = item.End, + MemberId = item.UserId,//虚拟的人Id变为负数 + Point = new double[] { -1d, -1d }, + //Prop = string.Join(',', prop), + RouteId = item.RouteId, + EndDistance = Math.Round(targetData._Distance, 6), + //ShowVirtual = true, + CommandType = 1, + //IsVirtual = true, + Speed = targetData._Speed, + WeightKg = weightKg, + PreDistance = item.GetPreDistance() + }; + msgModels.Add(loginInfo); + } var info = new MsgModel() { exit = item.End, @@ -93,7 +152,9 @@ namespace OnlineUserPool.Hander } return msgModels; } - + const int offset = 1; + const int timeInterval = 15; + private DateTime UpdateVirtualTime { get; set; } /// /// 删除已经骑行完的人,添加新的人物进去 /// @@ -101,27 +162,24 @@ namespace OnlineUserPool.Hander { lock (locker) { - mapRecordRankings.RemoveAll(n => n.End); - //var virutalEndCount = mapRecordRankings.Count(d => d.UserId < 0 && d.End); + //var offlineCount = mapRecordRankings.Where(c => c.End).Count();//下线人数 + var top = ComputeTop();//理论上当前时间段虚拟用户的总数 + mapRecordRankings.RemoveAll(n => n.End);//移除骑行结束的人 var routeIds = mapRecordRankings.Select(n => n.RouteId).Distinct().ToList(); - var top = ConfigHelp.Top; - if(customerCount > 9) + var currentVirtualCount = mapRecordRankings.Count(d => d.UserId < 0);//实际虚拟用户数 + if (top - currentVirtualCount > 0) { - top = 0; + var interval = DateTime.Now - UpdateVirtualTime; + if (interval.TotalSeconds > timeInterval) + { + UpdateVirtualTime = DateTime.Now; + var needUpdateCount = offset;//这一秒需要上线的人数 + var randomUser = WebService.GetMapRouteRandomRecord(needUpdateCount, routeIds); + var addRankings = WebService.GetRecordFileFromServer(randomUser.Select(n => n.Id).ToList()); + mapRecordRankings.AddRange(addRankings); + } } - if(top - mapRecordRankings.Count(d=>d.UserId<0) > 0) - { - var count = top - mapRecordRankings.Count(d => d.UserId < 0); - var randomUser = WebService.GetMapRouteRandomRecord(count, routeIds); - - //var str = "参数:" + (ConfigHelp.Top - virutalEndCount) + "," + string.Join(",",routeIds) + "\r\n"; - //str += "服务端返回:" + Newtonsoft.Json.JsonConvert.SerializeObject(randomUser) +"\r\n"; - //Log.Information(str); - var addRankings = WebService.GetRecordFileFromServer(randomUser.Select(n => n.Id).ToList()); - mapRecordRankings.AddRange(addRankings); - } - } } } diff --git a/Hander/MultiUserHandle.cs b/Hander/MultiUserHandle.cs index fa7f6b4..b6bf421 100644 --- a/Hander/MultiUserHandle.cs +++ b/Hander/MultiUserHandle.cs @@ -15,8 +15,8 @@ namespace OnlineUserPool.Hander private int _index = 0; //3600*3; private MultiUserModel model; private TurfHelper _turfHelper; - private int _routeId = 1255; //5593; //1660; - private int _size = 150; + private int _routeId = 1215; //5593; //1660; + private int _size = 100; private int _competitionid = 0;//35; public MultiUserHandle() { @@ -33,15 +33,15 @@ namespace OnlineUserPool.Hander #region sis - _routeId = 3386; - _competitionid = 1048; + _routeId = 1215; + _competitionid = 0; #endregion model = await BaseApi.GetAsync($"/Map/sss?routeId={ _routeId }&size={ _size }"); double i = 0; foreach (var item in model.users) { - i += 0.0002; + i += 0.0012; item.Distance = i; } _turfHelper = new TurfHelper(model.route.List.Select(d => d.Point)); diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index ae687f7..c286a47 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -18,6 +18,7 @@ using Newtonsoft.Json; using System.Collections.Concurrent; using System.IO.Compression; using System.IO; +using System.Windows; namespace OnlineUserPool.ViewModels { @@ -52,8 +53,28 @@ namespace OnlineUserPool.ViewModels SetProperty(ref _SendDataSize, value); } } + private string _VirtualData = ""; + public string VirtualData + { + get { return _VirtualData; } + set + { + SetProperty(ref _VirtualData, value); + } + } + + private bool _closeVirtualUser = false; + public bool CloseVirtualUser + { + get { return _closeVirtualUser; } + set + { + SetProperty(ref _closeVirtualUser, value); + } + } //private IService _udpService; + public MainWindowViewModel() { Title = $"{ IPAddress.Any }:{ ConfigHelp.UdpPort }"; @@ -63,7 +84,7 @@ namespace OnlineUserPool.ViewModels //Customers.Add("suntao"); WriteLine(DateTime.Now.ToShortDateString()); LogHelper.Init(); - mapRecordRankingHander = new MapRecordRankingHander();//new MultiUserHandle();// + mapRecordRankingHander = new MapRecordRankingHander();// //mapRecordRankingHander = new MultiUserHandle(); Log.Information($"初始化连接,当前地址:{ IPAddress.Any }:{ConfigHelp.UdpPort}"); @@ -199,15 +220,19 @@ namespace OnlineUserPool.ViewModels var msgs = CloneJson(receiveMes).ToList(); receiveMes.Clear(); - //加入虚拟人物消息 - var virtualData = mapRecordRankingHander.GetVirtualUserData(); - WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-当前在线人数:" + Clients.Count + "-当前虚拟人数:" + virtualData.Count); + if (!CloseVirtualUser) + { + //加入虚拟人物消息 + var virtualData = mapRecordRankingHander.GetVirtualUserData(); + VirtualData = $"{virtualData.Count}"; + WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-当前在线人数:" + Clients.Count + "-当前虚拟人数:" + virtualData.Count); #if DEBUG - WriteLine($"在线人:{Newtonsoft.Json.JsonConvert.SerializeObject(msgs)}"); - //\r\n虚拟人:{Newtonsoft.Json.JsonConvert.SerializeObject(virtualData)} -#endif - msgs.AddRange(virtualData); + WriteLine($"在线人:{Newtonsoft.Json.JsonConvert.SerializeObject(msgs)}"); + //\r\n虚拟人:{Newtonsoft.Json.JsonConvert.SerializeObject(virtualData)} +#endif + msgs.AddRange(virtualData); + } SendMessage(Clients, msgs); diff --git a/ViewModels/OnlineUserPool.sln b/ViewModels/OnlineUserPool.sln new file mode 100644 index 0000000..1893932 --- /dev/null +++ b/ViewModels/OnlineUserPool.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30320.27 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OnlineUserPool", "OnlineUserPool.csproj", "{5E6C3E3A-13CE-4DBE-8352-E45D01E9F743}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5E6C3E3A-13CE-4DBE-8352-E45D01E9F743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E6C3E3A-13CE-4DBE-8352-E45D01E9F743}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E6C3E3A-13CE-4DBE-8352-E45D01E9F743}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E6C3E3A-13CE-4DBE-8352-E45D01E9F743}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {21B354CA-A2B2-4F3E-B971-C742C9BEAA53} + EndGlobalSection +EndGlobal diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index 2a1d2d5..492ab33 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -29,11 +29,16 @@ --> + + + + + diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index 2d59ce2..7a76e2f 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using OnlineUserPool.Hander; using OnlineUserPool.Model; using OnlineUserPool.Unility; +using OnlineUserPool.ViewModels; using Serilog; using System; using System.Collections.Generic; @@ -39,6 +40,16 @@ namespace OnlineUserPool.Views } - + private void CloseVirtualUser_Click(object sender, RoutedEventArgs e) + { + MainWindowViewModel data = DataContext as MainWindowViewModel; + data.CloseVirtualUser = true; + } + + private void OpenVirtualUser_Click1(object sender, RoutedEventArgs e) + { + MainWindowViewModel data = DataContext as MainWindowViewModel; + data.CloseVirtualUser = false; + } } }