mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-11 15:52:25 -05:00
Branch for IW4X practically everything refactored
This commit is contained in:
@ -5,92 +5,152 @@ using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
|
||||
using SharedLibrary.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StatsPlugin
|
||||
{
|
||||
public class StatCommand : Command
|
||||
{
|
||||
public StatCommand() : base("stats", "view your stats. syntax !stats", "xlrstats", Player.Permission.User, 0, false) { }
|
||||
|
||||
public override void Execute(Event E)
|
||||
public override async Task ExecuteAsync(Event E)
|
||||
{
|
||||
String statLine;
|
||||
PlayerStats pStats;
|
||||
|
||||
if (E.Target != null)
|
||||
{
|
||||
pStats = Stats.playerStats.getStats(E.Target);
|
||||
pStats = Stats.statLists.Find(x => x.Port == E.Owner.getPort()).playerStats.getStats(E.Target);
|
||||
statLine = String.Format("^5{0} ^7KILLS | ^5{1} ^7DEATHS | ^5{2} ^7KDR | ^5{3} ^7SKILL", pStats.Kills, pStats.Deaths, pStats.KDR, pStats.Skill);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pStats = Stats.playerStats.getStats(E.Origin);
|
||||
pStats = Stats.statLists.Find(x => x.Port == E.Owner.getPort()).playerStats.getStats(E.Origin);
|
||||
statLine = String.Format("^5{0} ^7KILLS | ^5{1} ^7DEATHS | ^5{2} ^7KDR | ^5{3} ^7SKILL", pStats.Kills, pStats.Deaths, pStats.KDR, pStats.Skill);
|
||||
}
|
||||
|
||||
E.Origin.Tell(statLine);
|
||||
await E.Origin.Tell(statLine);
|
||||
}
|
||||
}
|
||||
|
||||
public class topStats : Command
|
||||
public class TopStats : Command
|
||||
{
|
||||
public topStats() : base("topstats", "view the top 5 players on this server. syntax !topstats", "!ts", Player.Permission.User, 0, false) { }
|
||||
public TopStats() : base("topstats", "view the top 5 players on this server. syntax !topstats", "!ts", Player.Permission.User, 0, false) { }
|
||||
|
||||
public override void Execute(Event E)
|
||||
public override async Task ExecuteAsync(Event E)
|
||||
{
|
||||
List<KeyValuePair<String, PlayerStats>> pStats = Stats.playerStats.topStats();
|
||||
List<KeyValuePair<String, PlayerStats>> pStats = Stats.statLists.Find(x => x.Port == E.Owner.getPort()).playerStats.topStats();
|
||||
StringBuilder msgBlder = new StringBuilder();
|
||||
|
||||
E.Origin.Tell("^5--Top Players--");
|
||||
await E.Origin.Tell("^5--Top Players--");
|
||||
foreach (KeyValuePair<String, PlayerStats> pStat in pStats)
|
||||
{
|
||||
Player P = E.Owner.clientDB.getPlayer(pStat.Key, -1);
|
||||
if (P == null)
|
||||
continue;
|
||||
E.Origin.Tell(String.Format("^3{0}^7 - ^5{1} ^7KDR | ^5{2} ^7SKILL", P.Name, pStat.Value.KDR, pStat.Value.Skill));
|
||||
await E.Origin.Tell(String.Format("^3{0}^7 - ^5{1} ^7KDR | ^5{2} ^7SKILL", P.Name, pStat.Value.KDR, pStat.Value.Skill));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class Stats : Plugin
|
||||
/// <summary>
|
||||
/// Each server runs from the same plugin ( for easier reloading and reduced memory usage ).
|
||||
/// So, to have multiple stat tracking, we must store a stat struct for each server
|
||||
/// </summary>
|
||||
public class Stats : IPlugin
|
||||
{
|
||||
public static StatsDB playerStats { get; private set; }
|
||||
private DateTime[] lastKill = new DateTime[18];
|
||||
private DateTime[] connectionTime = new DateTime[18];
|
||||
private int[] inactiveMinutes = new int[18];
|
||||
private int[] Kills = new int[18];
|
||||
private int[] deathStreaks = new int[18];
|
||||
private int[] killStreaks = new int[18];
|
||||
public static List<StatTracking> statLists;
|
||||
|
||||
public override void onEvent(Event E)
|
||||
public struct StatTracking
|
||||
{
|
||||
playerStats = new StatsDB("stats_" + E.Owner.getPort() + ".rm");
|
||||
public StatsDB playerStats;
|
||||
public DateTime[] lastKill, connectionTime;
|
||||
public int[] inactiveMinutes, Kills, deathStreaks, killStreaks;
|
||||
public int Port;
|
||||
|
||||
public StatTracking(int port)
|
||||
{
|
||||
playerStats = new StatsDB("Database/stats_" + port + ".rm");
|
||||
inactiveMinutes = new int[18];
|
||||
Kills = new int[18];
|
||||
deathStreaks = new int[18];
|
||||
killStreaks = new int[18];
|
||||
lastKill = new DateTime[18];
|
||||
connectionTime = new DateTime[18];
|
||||
Port = port;
|
||||
}
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return "Basic Stats"; }
|
||||
}
|
||||
|
||||
public float Version
|
||||
{
|
||||
get { return 1f; }
|
||||
}
|
||||
|
||||
public string Author
|
||||
{
|
||||
get { return "RaidMax"; }
|
||||
}
|
||||
|
||||
public async Task OnLoad()
|
||||
{
|
||||
statLists = new List<StatTracking>();
|
||||
await Task.Delay(0);
|
||||
}
|
||||
|
||||
public async Task OnUnload()
|
||||
{
|
||||
statLists.Clear();
|
||||
await Task.Delay(0);
|
||||
}
|
||||
|
||||
public async Task OnTick(Server S)
|
||||
{
|
||||
await Task.Delay(0);
|
||||
}
|
||||
|
||||
public async Task OnEvent(Event E, Server S)
|
||||
{
|
||||
if (E.Type == Event.GType.Start)
|
||||
{
|
||||
statLists.Add(new StatTracking(S.getPort()));
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Stop)
|
||||
{
|
||||
statLists.RemoveAll(x => x.Port == S.getPort());
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Connect)
|
||||
{
|
||||
resetCounters(E.Origin.clientID);
|
||||
resetCounters(E.Origin.clientID, S.getPort());
|
||||
|
||||
PlayerStats checkForTrusted = playerStats.getStats(E.Origin);
|
||||
PlayerStats checkForTrusted = statLists.Find(x => x.Port == S.getPort()).playerStats.getStats(E.Origin);
|
||||
if (checkForTrusted.playTime >= 4320 && E.Origin.Level < Player.Permission.Trusted)
|
||||
{
|
||||
E.Origin.setLevel(Player.Permission.Trusted);
|
||||
E.Owner.clientDB.updatePlayer(E.Origin);
|
||||
E.Origin.Tell("Congratulations, you are now a ^5trusted ^7player! Type ^5!help ^7to view new commands.");
|
||||
E.Origin.Tell("You earned this by playing for ^53 ^7full days!");
|
||||
await E.Origin.Tell("Congratulations, you are now a ^5trusted ^7player! Type ^5!help ^7to view new commands.");
|
||||
await E.Origin.Tell("You earned this by playing for ^53 ^7full days!");
|
||||
}
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.MapEnd)
|
||||
if (E.Type == Event.GType.MapEnd || E.Type == Event.GType.Stop)
|
||||
{
|
||||
foreach (Player P in E.Owner.getPlayers())
|
||||
foreach (Player P in S.getPlayers())
|
||||
{
|
||||
|
||||
if (P == null)
|
||||
continue;
|
||||
|
||||
calculateAndSaveSkill(P);
|
||||
resetCounters(P.clientID);
|
||||
calculateAndSaveSkill(P, statLists.Find(x =>x.Port == S.getPort()));
|
||||
resetCounters(P.clientID, S.getPort());
|
||||
|
||||
E.Owner.Log.Write("Updated skill for client #" + P.databaseID, Log.Level.Debug);
|
||||
//E.Owner.Log.Write(String.Format("\r\nJoin: {0}\r\nInactive Minutes: {1}\r\nnewPlayTime: {2}\r\nnewSPM: {3}\r\nkdrWeight: {4}\r\nMultiplier: {5}\r\nscoreWeight: {6}\r\nnewSkillFactor: {7}\r\nprojectedNewSkill: {8}\r\nKills: {9}\r\nDeaths: {10}", connectionTime[P.clientID].ToShortTimeString(), inactiveMinutes[P.clientID], newPlayTime, newSPM, kdrWeight, Multiplier, scoreWeight, newSkillFactor, disconnectStats.Skill, disconnectStats.Kills, disconnectStats.Deaths));
|
||||
@ -99,8 +159,8 @@ namespace StatsPlugin
|
||||
|
||||
if (E.Type == Event.GType.Disconnect)
|
||||
{
|
||||
calculateAndSaveSkill(E.Origin);
|
||||
resetCounters(E.Origin.clientID);
|
||||
calculateAndSaveSkill(E.Origin, statLists.Find(x=>x.Port == S.getPort()));
|
||||
resetCounters(E.Origin.clientID, S.getPort());
|
||||
E.Owner.Log.Write("Updated skill for disconnecting client #" + E.Origin.databaseID, Log.Level.Debug);
|
||||
}
|
||||
|
||||
@ -110,27 +170,29 @@ namespace StatsPlugin
|
||||
return;
|
||||
|
||||
Player Killer = E.Origin;
|
||||
PlayerStats killerStats = playerStats.getStats(Killer);
|
||||
StatTracking curServer = statLists.Find(x => x.Port == S.getPort());
|
||||
PlayerStats killerStats = curServer.playerStats.getStats(Killer);
|
||||
|
||||
lastKill[E.Origin.clientID] = DateTime.Now;
|
||||
Kills[E.Origin.clientID]++;
|
||||
|
||||
if ((lastKill[E.Origin.clientID] - DateTime.Now).TotalSeconds > 60)
|
||||
inactiveMinutes[E.Origin.clientID]++;
|
||||
curServer.lastKill[E.Origin.clientID] = DateTime.Now;
|
||||
curServer.Kills[E.Origin.clientID]++;
|
||||
|
||||
if ((curServer.lastKill[E.Origin.clientID] - DateTime.Now).TotalSeconds > 60)
|
||||
curServer.inactiveMinutes[E.Origin.clientID]++;
|
||||
|
||||
killerStats.Kills++;
|
||||
|
||||
if (killerStats.Deaths == 0)
|
||||
killerStats.KDR = killerStats.Kills;
|
||||
else
|
||||
killerStats.KDR = killerStats.Kills / killerStats.Deaths;
|
||||
killerStats.KDR = Math.Round((double)killerStats.Kills / (double)killerStats.Deaths, 2);
|
||||
|
||||
playerStats.updateStats(Killer, killerStats);
|
||||
curServer.playerStats.updateStats(Killer, killerStats);
|
||||
|
||||
killStreaks[Killer.clientID] += 1;
|
||||
deathStreaks[Killer.clientID] = 0;
|
||||
curServer.killStreaks[Killer.clientID] += 1;
|
||||
curServer.deathStreaks[Killer.clientID] = 0;
|
||||
|
||||
Killer.Tell(messageOnStreak(killStreaks[Killer.clientID], deathStreaks[Killer.clientID]));
|
||||
await Killer.Tell(messageOnStreak(curServer.killStreaks[Killer.clientID], curServer.deathStreaks[Killer.clientID]));
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Death)
|
||||
@ -139,38 +201,39 @@ namespace StatsPlugin
|
||||
return;
|
||||
|
||||
Player Victim = E.Origin;
|
||||
PlayerStats victimStats = playerStats.getStats(Victim);
|
||||
|
||||
StatTracking curServer = statLists.Find(x => x.Port == S.getPort());
|
||||
PlayerStats victimStats = curServer.playerStats.getStats(Victim);
|
||||
|
||||
victimStats.Deaths++;
|
||||
victimStats.KDR = victimStats.Kills / victimStats.Deaths;
|
||||
victimStats.KDR = Math.Round((double)victimStats.Kills / (double)victimStats.Deaths, 2);
|
||||
|
||||
playerStats.updateStats(Victim, victimStats);
|
||||
curServer.playerStats.updateStats(Victim, victimStats);
|
||||
|
||||
deathStreaks[Victim.clientID] += 1;
|
||||
killStreaks[Victim.clientID] = 0;
|
||||
curServer.deathStreaks[Victim.clientID] += 1;
|
||||
curServer.killStreaks[Victim.clientID] = 0;
|
||||
|
||||
Victim.Tell(messageOnStreak(killStreaks[Victim.clientID], deathStreaks[Victim.clientID]));
|
||||
await Victim.Tell(messageOnStreak(curServer.killStreaks[Victim.clientID], curServer.deathStreaks[Victim.clientID]));
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateAndSaveSkill(Player P)
|
||||
private void calculateAndSaveSkill(Player P, StatTracking curServer)
|
||||
{
|
||||
if (P == null)
|
||||
return;
|
||||
|
||||
PlayerStats disconnectStats = playerStats.getStats(P);
|
||||
if (Kills[P.clientID] == 0)
|
||||
PlayerStats disconnectStats = curServer.playerStats.getStats(P);
|
||||
if (curServer.Kills[P.clientID] == 0)
|
||||
return;
|
||||
|
||||
else if (lastKill[P.clientID] > connectionTime[P.clientID])
|
||||
inactiveMinutes[P.clientID] += (int)(DateTime.Now - lastKill[P.clientID]).TotalMinutes;
|
||||
else if (curServer.lastKill[P.clientID] > curServer.connectionTime[P.clientID])
|
||||
curServer.inactiveMinutes[P.clientID] += (int)(DateTime.Now - curServer.lastKill[P.clientID]).TotalMinutes;
|
||||
|
||||
int newPlayTime = (int)(DateTime.Now - connectionTime[P.clientID]).TotalMinutes - inactiveMinutes[P.clientID];
|
||||
int newPlayTime = (int)(DateTime.Now - curServer.connectionTime[P.clientID]).TotalMinutes - curServer.inactiveMinutes[P.clientID];
|
||||
|
||||
if (newPlayTime < 2)
|
||||
return;
|
||||
|
||||
double newSPM = Kills[P.clientID] * 50 / newPlayTime;
|
||||
double newSPM = curServer.Kills[P.clientID] * 50 / Math.Max(1, newPlayTime);
|
||||
double kdrWeight = Math.Round(Math.Pow(disconnectStats.KDR, 2 / Math.E), 3);
|
||||
double Multiplier;
|
||||
|
||||
@ -190,34 +253,18 @@ namespace StatsPlugin
|
||||
disconnectStats.Skill = disconnectStats.scorePerMinute * kdrWeight / 10;
|
||||
disconnectStats.playTime += newPlayTime;
|
||||
|
||||
playerStats.updateStats(P, disconnectStats);
|
||||
curServer.playerStats.updateStats(P, disconnectStats);
|
||||
}
|
||||
|
||||
private void resetCounters(int cID)
|
||||
{
|
||||
Kills[cID] = 0;
|
||||
connectionTime[cID] = DateTime.Now;
|
||||
inactiveMinutes[cID] = 0;
|
||||
deathStreaks[cID] = 0;
|
||||
killStreaks[cID] = 0;
|
||||
}
|
||||
|
||||
|
||||
public override void onLoad()
|
||||
{
|
||||
for (int i = 0; i < 18; i++)
|
||||
{
|
||||
Kills[i] = 0;
|
||||
connectionTime[i] = DateTime.Now;
|
||||
inactiveMinutes[i] = 0;
|
||||
deathStreaks[i] = 0;
|
||||
killStreaks[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override void onUnload()
|
||||
private void resetCounters(int cID, int serverPort)
|
||||
{
|
||||
StatTracking selectedPlayers = statLists.Find(x => x.Port == serverPort);
|
||||
|
||||
selectedPlayers.Kills[cID] = 0;
|
||||
selectedPlayers.connectionTime[cID] = DateTime.Now;
|
||||
selectedPlayers.inactiveMinutes[cID] = 0;
|
||||
selectedPlayers.deathStreaks[cID] = 0;
|
||||
selectedPlayers.killStreaks[cID] = 0;
|
||||
}
|
||||
|
||||
private String messageOnStreak(int killStreak, int deathStreak)
|
||||
@ -245,30 +292,6 @@ namespace StatsPlugin
|
||||
|
||||
return Message;
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Basic Stats";
|
||||
}
|
||||
}
|
||||
|
||||
public override float Version
|
||||
{
|
||||
get
|
||||
{
|
||||
return 0.3f;
|
||||
}
|
||||
}
|
||||
|
||||
public override string Author
|
||||
{
|
||||
get
|
||||
{
|
||||
return "RaidMax";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class StatsDB : Database
|
||||
@ -332,15 +355,15 @@ namespace StatsPlugin
|
||||
updatedPlayer.Add("DEATHS", S.Deaths);
|
||||
updatedPlayer.Add("KDR", Math.Round(S.KDR, 2));
|
||||
updatedPlayer.Add("SKILL", Math.Round(S.Skill, 1));
|
||||
updatedPlayer.Add("SPM", Math.Round(S.scorePerMinute, 0));
|
||||
updatedPlayer.Add("SPM", Math.Round(S.scorePerMinute, 1));
|
||||
updatedPlayer.Add("PLAYTIME", S.playTime);
|
||||
|
||||
Update("STATS", updatedPlayer, String.Format("npID = '{0}'", P.npID));
|
||||
Update("STATS", updatedPlayer, new KeyValuePair<string, object>("npID", P.npID));
|
||||
}
|
||||
|
||||
public List<KeyValuePair<String, PlayerStats>> topStats()
|
||||
{
|
||||
String Query = String.Format("SELECT * FROM STATS WHERE KDR < '{0}' AND KILLS > '{1}' AND PLAYTIME > '{2}' ORDER BY SKILL DESC LIMIT '{3}'", 10, 150, 60, 5);
|
||||
String Query = String.Format("SELECT * FROM STATS WHERE SKILL > 0 AND KDR < '{0}' AND KILLS > '{1}' AND PLAYTIME > '{2}' ORDER BY SKILL DESC LIMIT '{3}'", 10, 150, 60, 5);
|
||||
DataTable Result = GetDataTable(Query);
|
||||
List<KeyValuePair<String, PlayerStats>> pStats = new List<KeyValuePair<String, PlayerStats>>();
|
||||
|
@ -9,8 +9,9 @@
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>StatsPlugin</RootNamespace>
|
||||
<AssemblyName>StatsPlugin</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@ -20,6 +21,7 @@
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -28,12 +30,9 @@
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="SharedLibrary, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\Admin\lib\SharedLibrary.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@ -43,12 +42,19 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="Plugin.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SharedLibrary\SharedLibrary.csproj">
|
||||
<Project>{d51eeceb-438a-47da-870f-7d7b41bc24d6}</Project>
|
||||
<Name>SharedLibrary</Name>
|
||||
<Private>False</Private>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PostBuildEvent>copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)Admin\plugins\SimpleStatsPlugin.dll"</PostBuildEvent>
|
||||
<PostBuildEvent>copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)BUILD\plugins\"</PostBuildEvent>
|
||||
</PropertyGroup>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
Reference in New Issue
Block a user