using OnlineUserPool.Api; using OnlineUserPool.Model; using OnlineUserPool.Unility; using Serilog; using System; using System.Collections.Generic; using System.Linq; namespace OnlineUserPool.Hander { public class MapRecordRankingHander : IHandle { private static List mapRecordRankings = new List(); private static object locker = new object();//防止服务端响应慢,多次加载 private const int START = 120; private const int SMALL = 100; private const int MEDIUM = 150; private const int LARGE = 180; private const int OFFSET = 15; const int offset = 1; const int timeInterval = 15; List msgModels = new List(); private DateTime UpdateVirtualTime { get; set; } public MapRecordRankingHander() { Init(); } private void Init() { if (ConfigHelp.Top > 0) { var records = WebService.GetMapRouteRandomRecord(START, null); mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Select(n => n.Id).ToList())); UpdateVirtualTime = DateTime.Now; } } private Random rand = new Random(); //根据当前时段计算虚拟总人数 private int ComputeTop() { var hour = DateTime.Now.Hour;//当前点数 int top = 1;//少:1 中:3 多:10 if (hour <= 6) { top = rand.Next((int)(0.8 * START), (int)(1.2 * START)); } if (hour > 6 && hour <= 18) { top = rand.Next((int)(0.8 * OFFSET), (int)(1.2 * MEDIUM)); } if (hour >= 18 && hour <= 22) { top = rand.Next((int)(0.8 * LARGE), (int)(1.2 * LARGE)); } if (hour > 22 && hour <= 24) { top = rand.Next((int)(0.8 * SMALL), (int)(1.2 * SMALL)); } return top; } /// /// 获取虚拟人物数据 /// /// public List GetVirtualUserData() { msgModels.Clear(); if (ConfigHelp.ShowVirtualUser == false) { return msgModels; } if (mapRecordRankings != null) { for (int i = 0; i < mapRecordRankings.Count; i++) { try { var item = mapRecordRankings[i]; item.CurrentIndex++; TargetData targetData = item.GetCurrentTargetData(); var weightKg = 0.0D; if (targetData._Power >= 0) { 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, IsCompleted = item.End, MemberId = item.UserId, //虚拟的人Id变为负数 Point = new double[] { Math.Round(targetData._Lat, 6), Math.Round(targetData._Lon, 6) }, //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(info); } catch (Exception e) { Log.Error("加载虚拟人物错误:" + e.Message + "\r\n" + e.StackTrace); } } } return msgModels; } /// /// 删除已经骑行完的人,添加新的人物进去 /// public void RemoveEndAndAddNewVirtualUser(int customerCount) { lock (locker) { var top = ComputeTop();//理论上当前时间段虚拟用户的总数 mapRecordRankings.RemoveAll(n => n.End);//移除骑行结束的人 var routeIds = mapRecordRankings.Select(n => n.RouteId).Distinct().ToList(); var currentVirtualCount = mapRecordRankings.Count(d => d.UserId < 0);//实际虚拟用户数 if (top - currentVirtualCount > 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); } } } } } }