2018-02-07 00:19:06 -05:00
|
|
|
|
using System;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
using System.Linq;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-08 17:50:58 -04:00
|
|
|
|
using System.Reflection;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Services;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Config;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Helpers;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Models;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
namespace IW4MAdmin.Plugins.Stats
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2018-02-07 00:19:06 -05:00
|
|
|
|
class Plugin : IPlugin
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2018-02-07 00:19:06 -05:00
|
|
|
|
public string Name => "Simple Stats";
|
2017-06-01 13:42:28 -04:00
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
public float Version => Assembly.GetExecutingAssembly().GetName().Version.Major + Assembly.GetExecutingAssembly().GetName().Version.Minor / 10.0f;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2017-11-15 16:04:13 -05:00
|
|
|
|
public string Author => "RaidMax";
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
public static StatManager Manager { get; private set; }
|
2018-02-10 01:26:38 -05:00
|
|
|
|
private IManager ServerManager;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
public static BaseConfigurationHandler<StatsConfiguration> Config { get; private set; }
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public async Task OnEventAsync(GameEvent E, Server S)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2018-02-07 00:19:06 -05:00
|
|
|
|
switch (E.Type)
|
2015-08-23 17:58:48 -04:00
|
|
|
|
{
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Start:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
Manager.AddServer(S);
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Stop:
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Connect:
|
2018-03-06 02:22:19 -05:00
|
|
|
|
await Manager.AddPlayer(E.Origin);
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Disconnect:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
await Manager.RemovePlayer(E.Origin);
|
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Say:
|
2018-03-06 02:22:19 -05:00
|
|
|
|
if (E.Data != string.Empty && E.Data.Trim().Length > 0 && E.Message.Trim()[0] != '!' && E.Origin.ClientId > 1)
|
2018-02-10 01:26:38 -05:00
|
|
|
|
await Manager.AddMessageAsync(E.Origin.ClientId, E.Owner.GetHashCode(), E.Data);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.MapChange:
|
2018-03-06 02:22:19 -05:00
|
|
|
|
Manager.ResetKillstreaks(S.GetHashCode());
|
|
|
|
|
await Manager.Sync(S);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.MapEnd:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Broadcast:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Tell:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Kick:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Ban:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Remote:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Unknown:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Report:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Flag:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Script:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Kill:
|
2018-02-07 00:19:06 -05:00
|
|
|
|
string[] killInfo = (E.Data != null) ? E.Data.Split(';') : new string[0];
|
2018-02-26 23:24:19 -05:00
|
|
|
|
if (killInfo.Length >= 9 && killInfo[0].Contains("ScriptKill") && E.Owner.CustomCallback)
|
2018-05-03 01:25:49 -04:00
|
|
|
|
await Manager.AddScriptKill(E.Time, E.Origin, E.Target, S.GetHashCode(), S.CurrentMap.Name, killInfo[7], killInfo[8],
|
|
|
|
|
killInfo[5], killInfo[6], killInfo[3], killInfo[4], killInfo[9], killInfo[10], killInfo[11], killInfo[12], killInfo[13]);
|
2018-02-26 23:24:19 -05:00
|
|
|
|
else if (!E.Owner.CustomCallback)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
await Manager.AddStandardKill(E.Origin, E.Target);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
break;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
case GameEvent.EventType.Death:
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
case GameEvent.EventType.Damage:
|
|
|
|
|
Manager.AddDamageEvent(E.Data, E.Origin.ClientId, E.Owner.GetHashCode());
|
|
|
|
|
break;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-18 22:25:11 -04:00
|
|
|
|
public async Task OnLoadAsync(IManager manager)
|
2015-08-20 17:54:38 -04:00
|
|
|
|
{
|
2018-03-18 22:25:11 -04:00
|
|
|
|
// load custom configuration
|
|
|
|
|
Config = new BaseConfigurationHandler<StatsConfiguration>("StatsPluginSettings");
|
2018-03-22 14:50:09 -04:00
|
|
|
|
if (Config.Configuration() == null)
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
|
|
|
|
Config.Set((StatsConfiguration)new StatsConfiguration().Generate());
|
|
|
|
|
await Config.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 14:01:26 -05:00
|
|
|
|
// meta data info
|
|
|
|
|
async Task<List<ProfileMeta>> getStats(int clientId)
|
|
|
|
|
{
|
|
|
|
|
var statsSvc = new GenericRepository<EFClientStatistics>();
|
|
|
|
|
var clientStats = await statsSvc.FindAsync(c => c.ClientId == clientId);
|
|
|
|
|
|
|
|
|
|
int kills = clientStats.Sum(c => c.Kills);
|
|
|
|
|
int deaths = clientStats.Sum(c => c.Deaths);
|
2018-02-16 23:24:03 -05:00
|
|
|
|
double kdr = Math.Round(kills / (double)deaths, 2);
|
2018-04-28 17:39:45 -04:00
|
|
|
|
double skill = Math.Round(clientStats.Sum(c => c.Skill) / clientStats.Where(c => c.Skill > 0).Count(), 2);
|
|
|
|
|
double spm = Math.Round(clientStats.Sum(c => c.SPM) / clientStats.Where(c => c.SPM > 0).Count(), 1);
|
2018-02-14 14:01:26 -05:00
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
return new List<ProfileMeta>()
|
|
|
|
|
{
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Kills",
|
|
|
|
|
Value = kills
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Deaths",
|
|
|
|
|
Value = deaths
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "KDR",
|
|
|
|
|
Value = kdr
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Skill",
|
|
|
|
|
Value = skill
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Score Per Minute",
|
|
|
|
|
Value = spm
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task<List<ProfileMeta>> getAnticheatInfo(int clientId)
|
|
|
|
|
{
|
|
|
|
|
var statsSvc = new GenericRepository<EFClientStatistics>();
|
|
|
|
|
var clientStats = await statsSvc.FindAsync(c => c.ClientId == clientId);
|
|
|
|
|
|
2018-03-22 14:50:09 -04:00
|
|
|
|
double headRatio = 0;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
double chestRatio = 0;
|
|
|
|
|
double abdomenRatio = 0;
|
|
|
|
|
double chestAbdomenRatio = 0;
|
2018-03-27 20:27:01 -04:00
|
|
|
|
double hitOffsetAverage = 0;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
double maxStrain = clientStats.Count(c=> c.MaxStrain > 0) == 0 ? 0 : clientStats.Max(cs => cs.MaxStrain);
|
|
|
|
|
//double maxAngle = clientStats.Max(cs => cs.HitLocations.Max(hl => hl.MaxAngleDistance));
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-03-09 03:01:12 -05:00
|
|
|
|
if (clientStats.Where(cs => cs.HitLocations.Count > 0).FirstOrDefault() != null)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
|
|
|
|
chestRatio = Math.Round(clientStats.Where(c => c.HitLocations.Count > 0).Sum(c =>
|
|
|
|
|
c.HitLocations.First(hl => hl.Location == IW4Info.HitLocation.torso_upper).HitCount) /
|
|
|
|
|
(double)clientStats.Where(c => c.HitLocations.Count > 0)
|
|
|
|
|
.Sum(c => c.HitLocations.Where(hl => hl.Location != IW4Info.HitLocation.none).Sum(f => f.HitCount)), 2);
|
|
|
|
|
|
|
|
|
|
abdomenRatio = Math.Round(clientStats.Where(c => c.HitLocations.Count > 0).Sum(c =>
|
|
|
|
|
c.HitLocations.First(hl => hl.Location == IW4Info.HitLocation.torso_lower).HitCount) /
|
|
|
|
|
(double)clientStats.Where(c => c.HitLocations.Count > 0).Sum(c => c.HitLocations.Where(hl => hl.Location != IW4Info.HitLocation.none).Sum(f => f.HitCount)), 2);
|
|
|
|
|
|
|
|
|
|
chestAbdomenRatio = Math.Round(clientStats.Where(c => c.HitLocations.Count > 0).Sum(cs => cs.HitLocations.First(hl => hl.Location == IW4Info.HitLocation.torso_upper).HitCount) /
|
|
|
|
|
(double)clientStats.Where(c => c.HitLocations.Count > 0).Sum(cs => cs.HitLocations.First(hl => hl.Location == IW4Info.HitLocation.torso_lower).HitCount), 2);
|
2018-03-22 14:50:09 -04:00
|
|
|
|
|
|
|
|
|
headRatio = Math.Round(clientStats.Where(c => c.HitLocations.Count > 0).Sum(cs => cs.HitLocations.First(hl => hl.Location == IW4Info.HitLocation.head).HitCount) /
|
|
|
|
|
(double)clientStats.Where(c => c.HitLocations.Count > 0)
|
|
|
|
|
.Sum(c => c.HitLocations.Where(hl => hl.Location != IW4Info.HitLocation.none).Sum(f => f.HitCount)), 2);
|
2018-03-27 20:27:01 -04:00
|
|
|
|
|
2018-03-28 23:01:09 -04:00
|
|
|
|
hitOffsetAverage = clientStats.Sum(c => c.AverageHitOffset) / Math.Max(1, clientStats.Where(c => c.AverageHitOffset > 0).Count());
|
2018-03-06 02:22:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 14:01:26 -05:00
|
|
|
|
return new List<ProfileMeta>()
|
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Key = "Chest Ratio",
|
|
|
|
|
Value = chestRatio,
|
|
|
|
|
Sensitive = true
|
2018-04-05 00:38:45 -04:00
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Abdomen Ratio",
|
|
|
|
|
Value = abdomenRatio,
|
|
|
|
|
Sensitive = true
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Chest To Abdomen Ratio",
|
|
|
|
|
Value = chestAbdomenRatio,
|
|
|
|
|
Sensitive = true
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Headshot Ratio",
|
|
|
|
|
Value = headRatio,
|
|
|
|
|
Sensitive = true
|
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Hit Offset Average",
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Value = $"{Math.Round(((float)hitOffsetAverage), 4)}°",
|
2018-04-05 00:38:45 -04:00
|
|
|
|
Sensitive = true
|
2018-05-03 01:25:49 -04:00
|
|
|
|
},
|
|
|
|
|
new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Max Strain",
|
|
|
|
|
Value = Math.Round(maxStrain, 3),
|
|
|
|
|
Sensitive = true
|
|
|
|
|
},
|
|
|
|
|
/*new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Max Angle Distance",
|
|
|
|
|
Value = Math.Round(maxAngle, 1),
|
|
|
|
|
Sensitive = true
|
|
|
|
|
}*/
|
2018-02-14 14:01:26 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task<List<ProfileMeta>> getMessages(int clientId)
|
|
|
|
|
{
|
|
|
|
|
var messageSvc = new GenericRepository<EFClientMessage>();
|
|
|
|
|
var messages = await messageSvc.FindAsync(m => m.ClientId == clientId);
|
|
|
|
|
var messageMeta = messages.Select(m => new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "EventMessage",
|
|
|
|
|
Value = m.Message,
|
|
|
|
|
When = m.TimeSent
|
|
|
|
|
}).ToList();
|
|
|
|
|
messageMeta.Add(new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Messages",
|
|
|
|
|
Value = messages.Count
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return messageMeta;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
MetaService.AddMeta(getStats);
|
|
|
|
|
|
2018-03-22 14:50:09 -04:00
|
|
|
|
if (Config.Configuration().EnableAntiCheat)
|
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
MetaService.AddMeta(getAnticheatInfo);
|
2018-03-22 14:50:09 -04:00
|
|
|
|
}
|
2018-04-05 00:38:45 -04:00
|
|
|
|
|
2018-02-14 14:01:26 -05:00
|
|
|
|
MetaService.AddMeta(getMessages);
|
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
string totalKills()
|
|
|
|
|
{
|
|
|
|
|
var serverStats = new GenericRepository<EFServerStatistics>();
|
2018-02-10 01:26:38 -05:00
|
|
|
|
return serverStats.Find(s => s.Active)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
.Sum(c => c.TotalKills).ToString("#,##0");
|
2018-02-09 02:21:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string totalPlayTime()
|
|
|
|
|
{
|
|
|
|
|
var serverStats = new GenericRepository<EFServerStatistics>();
|
2018-02-10 23:33:42 -05:00
|
|
|
|
return Math.Ceiling((serverStats.GetQuery(s => s.Active)
|
|
|
|
|
.Sum(c => c.TotalPlayTime) / 3600.0)).ToString("#,##0");
|
2018-02-09 02:21:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
manager.GetMessageTokens().Add(new MessageToken("TOTALKILLS", totalKills));
|
|
|
|
|
manager.GetMessageTokens().Add(new MessageToken("TOTALPLAYTIME", totalPlayTime));
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
|
|
|
|
ServerManager = manager;
|
|
|
|
|
|
2018-03-18 22:25:11 -04:00
|
|
|
|
Manager = new StatManager(manager);
|
2017-05-31 01:31:56 -04:00
|
|
|
|
}
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public Task OnTickAsync(Server S) => Task.CompletedTask;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
public async Task OnUnloadAsync()
|
2015-08-20 17:54:38 -04:00
|
|
|
|
{
|
2018-02-10 01:26:38 -05:00
|
|
|
|
foreach (var sv in ServerManager.GetServers())
|
|
|
|
|
await Manager.Sync(sv);
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|