forked from powerfun/udpservice
虚拟人物
This commit is contained in:
parent
12d1a01543
commit
81409dca9e
@ -8,6 +8,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Timers;
|
||||
|
||||
namespace OnlineUserPool.Hander
|
||||
{
|
||||
@ -20,20 +21,56 @@ 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++)
|
||||
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()
|
||||
{
|
||||
mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Skip(i * pageSize).Take(pageSize).Select(n => n.Id).ToList()));
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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; }
|
||||
/// <summary>
|
||||
/// 删除已经骑行完的人,添加新的人物进去
|
||||
/// </summary>
|
||||
@ -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;
|
||||
}
|
||||
if(top - mapRecordRankings.Count(d=>d.UserId<0) > 0)
|
||||
var interval = DateTime.Now - UpdateVirtualTime;
|
||||
if (interval.TotalSeconds > timeInterval)
|
||||
{
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<MultiUserModel>($"/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));
|
||||
|
||||
@ -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,8 +220,11 @@ namespace OnlineUserPool.ViewModels
|
||||
var msgs = CloneJson(receiveMes).ToList();
|
||||
receiveMes.Clear();
|
||||
|
||||
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
|
||||
@ -208,6 +232,7 @@ namespace OnlineUserPool.ViewModels
|
||||
//\r\n虚拟人:{Newtonsoft.Json.JsonConvert.SerializeObject(virtualData)}
|
||||
#endif
|
||||
msgs.AddRange(virtualData);
|
||||
}
|
||||
|
||||
SendMessage(Clients, msgs);
|
||||
|
||||
|
||||
25
ViewModels/OnlineUserPool.sln
Normal file
25
ViewModels/OnlineUserPool.sln
Normal file
@ -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
|
||||
@ -29,11 +29,16 @@
|
||||
</DataTemplate>
|
||||
</ListView.ItemTemplate>
|
||||
</ListView>-->
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="在线客户端:"></Run>
|
||||
<Run Text="{ Binding Clients.Count, Mode=OneWay}"></Run>
|
||||
<Run Text="虚拟人数:"></Run>
|
||||
<Run Text="{ Binding VirtualData, Mode=OneWay}"></Run>
|
||||
<!--<Run Text="发送单个客户端数据包大小:"></Run>-->
|
||||
<Run Text="{Binding SendDataSize,Mode=OneWay}"></Run>
|
||||
<Button x:Name="btn1" Click="CloseVirtualUser_Click">关闭虚拟人物</Button>
|
||||
<Button x:Name="btn2" Click="OpenVirtualUser_Click1">开启虚拟人物</Button>
|
||||
</TextBlock>
|
||||
<DataGrid ItemsSource="{ Binding Clients }" Grid.Row="1" IsReadOnly="True"></DataGrid>
|
||||
<DataGrid ItemsSource="{ Binding Customers }" Grid.Row="2" IsReadOnly="True"></DataGrid>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user