2021-08-29 14:10:10 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-08-03 22:11:58 -04:00
|
|
|
|
using SharedLibraryCore;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using System.Linq;
|
2019-04-08 13:29:48 -04:00
|
|
|
|
using System.Net;
|
2021-08-29 14:10:10 -04:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-30 10:57:07 -04:00
|
|
|
|
using Data.Models.Client.Stats;
|
2021-08-29 14:10:10 -04:00
|
|
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-04-13 21:26:13 -04:00
|
|
|
|
using static SharedLibraryCore.Server;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.ViewComponents
|
|
|
|
|
{
|
|
|
|
|
public class ServerListViewComponent : ViewComponent
|
|
|
|
|
{
|
2021-08-29 14:10:10 -04:00
|
|
|
|
private readonly IServerDataViewer _serverDataViewer;
|
|
|
|
|
private readonly ApplicationConfiguration _appConfig;
|
2022-03-29 17:42:53 -04:00
|
|
|
|
private readonly DefaultSettings _defaultSettings;
|
2021-08-29 14:10:10 -04:00
|
|
|
|
|
|
|
|
|
public ServerListViewComponent(IServerDataViewer serverDataViewer,
|
2022-03-29 17:42:53 -04:00
|
|
|
|
ApplicationConfiguration applicationConfiguration, DefaultSettings defaultSettings)
|
2021-08-29 14:10:10 -04:00
|
|
|
|
{
|
|
|
|
|
_serverDataViewer = serverDataViewer;
|
|
|
|
|
_appConfig = applicationConfiguration;
|
2022-03-29 17:42:53 -04:00
|
|
|
|
_defaultSettings = defaultSettings;
|
2021-08-29 14:10:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 21:26:13 -04:00
|
|
|
|
public IViewComponentResult Invoke(Game? game)
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2022-03-29 17:42:53 -04:00
|
|
|
|
if (game.HasValue)
|
|
|
|
|
{
|
2022-03-30 16:44:05 -04:00
|
|
|
|
ViewBag.Maps = _defaultSettings.Maps.FirstOrDefault(map => map.Game == game)?.Maps.ToList() ??
|
|
|
|
|
new List<Map>();
|
2022-03-29 17:42:53 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ViewBag.Maps = _defaultSettings.Maps.SelectMany(maps => maps.Maps).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:10:10 -04:00
|
|
|
|
var servers = Program.Manager.GetServers().Where(server => !game.HasValue || server.GameName == game);
|
|
|
|
|
|
|
|
|
|
var serverInfo = new List<ServerInfo>();
|
2019-04-08 13:29:48 -04:00
|
|
|
|
|
2021-08-29 14:10:10 -04:00
|
|
|
|
foreach (var server in servers)
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2021-08-29 14:10:10 -04:00
|
|
|
|
var serverId = server.GetIdForServer().Result;
|
|
|
|
|
var clientHistory = _serverDataViewer.ClientHistoryAsync(_appConfig.MaxClientHistoryTime,
|
2021-08-31 09:44:15 -04:00
|
|
|
|
CancellationToken.None).Result?
|
2021-08-29 14:10:10 -04:00
|
|
|
|
.FirstOrDefault(history => history.ServerId == serverId) ??
|
|
|
|
|
new ClientHistoryInfo
|
|
|
|
|
{
|
2021-09-17 12:23:57 -04:00
|
|
|
|
ServerId = serverId,
|
|
|
|
|
ClientCounts = new List<ClientCountSnapshot>()
|
2021-08-29 14:10:10 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var counts = clientHistory.ClientCounts?.AsEnumerable() ?? Enumerable.Empty<ClientCountSnapshot>();
|
|
|
|
|
|
2022-03-29 17:42:53 -04:00
|
|
|
|
if (server.ClientHistory.ClientCounts.Any())
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2022-03-29 17:42:53 -04:00
|
|
|
|
counts = counts.Union(server.ClientHistory.ClientCounts.Where(history =>
|
|
|
|
|
history.Time > (clientHistory.ClientCounts?.LastOrDefault()?.Time ?? DateTime.MinValue)))
|
|
|
|
|
.Where(history => history.Time >= DateTime.UtcNow - _appConfig.MaxClientHistoryTime);
|
2021-08-29 14:10:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 17:42:53 -04:00
|
|
|
|
serverInfo.Add(new ServerInfo
|
2021-08-29 14:10:10 -04:00
|
|
|
|
{
|
|
|
|
|
Name = server.Hostname,
|
|
|
|
|
ID = server.EndPoint,
|
|
|
|
|
Port = server.Port,
|
|
|
|
|
Map = server.CurrentMap.Alias,
|
2021-08-31 19:21:40 -04:00
|
|
|
|
ClientCount = server.Clients.Count(client => client != null),
|
2021-08-29 14:10:10 -04:00
|
|
|
|
MaxClients = server.MaxClients,
|
2021-11-28 11:17:56 -05:00
|
|
|
|
GameType = server.GametypeName,
|
2022-03-29 17:42:53 -04:00
|
|
|
|
ClientHistory = new ClientHistoryInfo
|
|
|
|
|
{
|
|
|
|
|
ServerId = server.EndPoint,
|
|
|
|
|
ClientCounts = counts.ToList()
|
|
|
|
|
},
|
2021-08-29 14:10:10 -04:00
|
|
|
|
Players = server.GetClientsAsList()
|
|
|
|
|
.Select(p => new PlayerInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = p.Name,
|
|
|
|
|
ClientId = p.ClientId,
|
|
|
|
|
Level = p.Level.ToLocalizedLevelName(),
|
|
|
|
|
LevelInt = (int) p.Level,
|
|
|
|
|
Tag = p.Tag,
|
|
|
|
|
ZScore = p.GetAdditionalProperty<EFClientStatistics>(IW4MAdmin.Plugins.Stats.Helpers
|
|
|
|
|
.StatManager
|
|
|
|
|
.CLIENT_STATS_KEY)?.ZScore
|
|
|
|
|
}).ToList(),
|
|
|
|
|
ChatHistory = server.ChatHistory.ToList(),
|
|
|
|
|
Online = !server.Throttled,
|
|
|
|
|
IPAddress =
|
|
|
|
|
$"{(server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.IP)}:{server.Port}",
|
|
|
|
|
ConnectProtocolUrl = server.EventParser.URLProtocolFormat.FormatExt(
|
|
|
|
|
server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.IP,
|
|
|
|
|
server.Port)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
return View("_List", serverInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-28 11:17:56 -05:00
|
|
|
|
}
|