powerfun-new-net/Hander/MapRecordRankingHander.cs

172 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
{
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;
}
/// <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);
}
}
}
}
}
}