powerfun-new-net/Hander/MapRecordRankingHander.cs

129 lines
4.9 KiB
C#
Raw Normal View History

using OnlineUserPool.Api;
using OnlineUserPool.Model;
2020-09-17 10:23:26 +08:00
using OnlineUserPool.Unility;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineUserPool.Hander
{
public class MapRecordRankingHander : IHandle
2020-09-17 10:23:26 +08:00
{
private static List<MapRecordRanking> mapRecordRankings = new List<MapRecordRanking>();
private static object locker = new object();//防止服务端响应慢,多次加载
//private int top = ConfigHelp.Top;
2020-09-17 10:23:26 +08:00
public MapRecordRankingHander()
{
Init();
}
private void Init()
2020-09-17 10:23:26 +08:00
{
if (ConfigHelp.Top > 0)
2020-10-30 17:39:19 +08:00
{
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()));
}
2020-10-30 17:39:19 +08:00
}
2020-09-17 10:23:26 +08:00
}
2020-10-30 17:39:19 +08:00
2020-09-17 10:23:26 +08:00
/// <summary>
/// 获取虚拟人物数据
/// </summary>
/// <returns></returns>
public List<MsgModel> GetVirtualUserData()
{
List<MsgModel> msgModels = new List<MsgModel>();
2020-11-23 17:07:40 +08:00
if(ConfigHelp.ShowVirtualUser == false)
{
return msgModels;
}
2020-10-27 10:20:47 +08:00
if (mapRecordRankings != null)
2020-09-17 10:23:26 +08:00
{
for (int i = 0; i < mapRecordRankings.Count; i++)
{
try
{
var item = mapRecordRankings[i];
2020-10-27 10:20:47 +08:00
item.CurrentIndex++;
2020-09-17 10:23:26 +08:00
TargetData targetData = item.GetCurrentTargetData();
2020-10-27 10:20:47 +08:00
//List<string> prop = new List<string>()
//{
// Math.Round(targetData._Speed, 2).ToString(),
// targetData._Power.ToString(),
// targetData._Cadence.ToString()
//};
2020-12-16 13:47:20 +08:00
var weightKg = 0.0D;
if(targetData._Power >= 0)
{
weightKg = Math.Round(targetData._Power / item.Weight, 2);
}
2020-10-27 10:20:47 +08:00
var info = new MsgModel()
2020-09-17 10:23:26 +08:00
{
exit = item.End,
IsCompleted = item.End,
2020-10-27 10:20:47 +08:00
MemberId = item.UserId,//虚拟的人Id变为负数
2020-10-13 08:54:24 +08:00
Point = new double[] { Math.Round(targetData._Lat, 6), Math.Round(targetData._Lon, 6) },
2020-10-27 10:20:47 +08:00
//Prop = string.Join(',', prop),
2020-09-17 10:23:26 +08:00
RouteId = item.RouteId,
2020-10-27 10:20:47 +08:00
EndDistance = Math.Round(targetData._Distance, 6),
//ShowVirtual = true,
2020-09-17 10:23:26 +08:00
CommandType = 1,
//IsVirtual = true,
2020-10-27 10:20:47 +08:00
Speed = targetData._Speed,
2020-12-16 13:47:20 +08:00
WeightKg = weightKg,
2020-10-27 10:20:47 +08:00
PreDistance = item.GetPreDistance()
};
msgModels.Add(info);
}
2020-09-17 10:23:26 +08:00
catch (Exception e)
2020-10-27 10:20:47 +08:00
{
Log.Error("加载虚拟人物错误:" + e.Message + "\r\n" + e.StackTrace);
2020-09-17 10:23:26 +08:00
}
}
2020-10-27 10:20:47 +08:00
}
2020-09-17 10:23:26 +08:00
return msgModels;
}
/// <summary>
/// 删除已经骑行完的人,添加新的人物进去
/// </summary>
public void RemoveEndAndAddNewVirtualUser(int customerCount)
2020-09-17 10:23:26 +08:00
{
lock (locker)
2020-09-17 10:23:26 +08:00
{
mapRecordRankings.RemoveAll(n => n.End);
//var virutalEndCount = mapRecordRankings.Count(d => d.UserId < 0 && d.End);
var routeIds = mapRecordRankings.Select(n => n.RouteId).Distinct().ToList();
var top = ConfigHelp.Top;
if(customerCount > 9)
2020-09-17 10:23:26 +08:00
{
top = 0;
}
if(top - mapRecordRankings.Count(d=>d.UserId<0) > 0)
{
var count = top - mapRecordRankings.Count(d => d.UserId < 0);
var randomUser = WebService.GetMapRouteRandomRecord(count, routeIds);
2020-10-27 10:20:47 +08:00
//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);
2020-09-17 10:23:26 +08:00
}
2020-09-17 10:23:26 +08:00
}
}
}
}