IW4M-Admin/WebfrontCore/ViewComponents/ServerListViewComponent.cs

78 lines
3.1 KiB
C#
Raw Normal View History

2023-05-30 19:12:57 -04:00
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Dtos;
2018-02-21 20:29:23 -05:00
using System.Linq;
2022-06-04 10:58:30 -04:00
using Data.Models;
using Data.Models.Client.Stats;
using IW4MAdmin.Plugins.Stats.Helpers;
using SharedLibraryCore.Configuration;
2018-02-21 20:29:23 -05:00
namespace WebfrontCore.ViewComponents
{
public class ServerListViewComponent : ViewComponent
{
private readonly DefaultSettings _defaultSettings;
2023-05-30 19:12:57 -04:00
public ServerListViewComponent(DefaultSettings defaultSettings)
{
_defaultSettings = defaultSettings;
}
public IViewComponentResult Invoke(Reference.Game? game)
2018-02-21 20:29:23 -05:00
{
if (game.HasValue)
{
ViewBag.Maps = _defaultSettings.Maps?.FirstOrDefault(map => map.Game == (Server.Game)game)?.Maps
?.ToList() ?? new List<Map>();
}
else
{
ViewBag.Maps = _defaultSettings.Maps?.SelectMany(maps => maps.Maps).ToList();
}
var servers = Program.Manager.GetServers()
.Where(server => game is null || server.GameName == (Server.Game)game);
var serverInfo = new List<ServerInfo>();
foreach (var server in servers)
2018-02-21 20:29:23 -05:00
{
serverInfo.Add(new ServerInfo
{
Name = server.Hostname,
ID = server.EndPoint,
Port = server.ListenPort,
Map = server.CurrentMap?.Alias,
2022-06-04 10:58:30 -04:00
Game = (Reference.Game)server.GameName,
ClientCount = server.ClientNum,
MaxClients = server.MaxClients,
PrivateClientSlots = server.PrivateClientSlots,
GameType = server.GametypeName,
2023-05-30 19:12:57 -04:00
ClientHistory = new ClientHistoryInfo(),
Players = server.GetClientsAsList()
.Select(client => new PlayerInfo
{
Name = client.Name,
ClientId = client.ClientId,
Level = client.Level.ToLocalizedLevelName(),
LevelInt = (int)client.Level,
Tag = client.Tag,
ZScore = client.GetAdditionalProperty<EFClientStatistics>(StatManager
.CLIENT_STATS_KEY)?.ZScore
}).ToList(),
ChatHistory = server.ChatHistory.ToList(),
Online = !server.Throttled,
IPAddress = server.ListenAddress,
ExternalIPAddress = server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.ListenAddress,
ConnectProtocolUrl = server.EventParser.URLProtocolFormat.FormatExt(
server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.ListenAddress,
server.ListenPort)
});
}
2018-02-21 20:29:23 -05:00
return View("_List", serverInfo);
}
}
}