forked from powerfun/udpservice
165 lines
6.1 KiB
C#
165 lines
6.1 KiB
C#
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<MapRecordRanking> mapRecordRankings = new List<MapRecordRanking>();
|
||
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<MsgModel> msgModels = new List<MsgModel>();
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取虚拟人物数据
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public List<MsgModel> GetVirtualUserData()
|
||
{
|
||
msgModels.Clear();
|
||
|
||
if (ConfigHelp.ShowVirtualUser == false)
|
||
{
|
||
return msgModels;
|
||
}
|
||
|
||
if (mapRecordRankings == null) return msgModels;
|
||
|
||
for (int i = 0; i < mapRecordRankings.Count; i++)
|
||
{
|
||
try
|
||
{
|
||
var item = mapRecordRankings[i];
|
||
item.CurrentIndex++;
|
||
var 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 },
|
||
RouteId = item.RouteId,
|
||
EndDistance = Math.Round(targetData._Distance, 6),
|
||
CommandType = 1,
|
||
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) },
|
||
RouteId = item.RouteId,
|
||
EndDistance = Math.Round(targetData._Distance, 6),
|
||
CommandType = 1,
|
||
Speed = targetData._Speed,
|
||
WeightKg = weightKg,
|
||
PreDistance = item.GetPreDistance()
|
||
};
|
||
msgModels.Add(info);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Log.Error("加载虚拟人物错误:" + e.Message + "\r\n" + e.StackTrace);
|
||
}
|
||
}
|
||
|
||
return msgModels;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除已经骑行完的人,添加新的人物进去
|
||
/// </summary>
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|