mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 23:31:13 -05:00
Stats plugin modifications
This commit is contained in:
@ -80,14 +80,53 @@ namespace SamplePlugin
|
||||
public class Stats : Plugin
|
||||
{
|
||||
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 override void onEvent(Event E)
|
||||
{
|
||||
playerStats = new StatsDB("stats_" + E.Owner.getPort() + ".rm");
|
||||
|
||||
if (E.Type == Event.GType.Connect)
|
||||
{
|
||||
resetCounters(E.Origin.clientID);
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.MapEnd)
|
||||
{
|
||||
foreach (Player P in E.Owner.getPlayers())
|
||||
{
|
||||
calculateAndSaveSkill(P);
|
||||
resetCounters(P.clientID);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Disconnect)
|
||||
{
|
||||
calculateAndSaveSkill(E.Origin);
|
||||
E.Owner.Log.Write("Updated skill for disconnecting client #" + E.Origin.databaseID, Log.Level.Debug);
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Kill)
|
||||
{
|
||||
Player Killer = E.Origin;
|
||||
PlayerStats killerStats = 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]++;
|
||||
}
|
||||
|
||||
if (Killer != E.Target)
|
||||
{
|
||||
killerStats.Kills++;
|
||||
@ -95,15 +134,15 @@ namespace SamplePlugin
|
||||
if (killerStats.Deaths == 0)
|
||||
killerStats.KDR = killerStats.Kills;
|
||||
else
|
||||
killerStats.KDR = killerStats.Kills / killerStats.Deaths;
|
||||
killerStats.KDR = (double)killerStats.Kills / (double)killerStats.Deaths;
|
||||
|
||||
playerStats.updateStats(Killer, killerStats);
|
||||
|
||||
killerStats.killStreak++;
|
||||
killerStats.deathStreak = 0;
|
||||
killStreaks[E.Origin.clientID]++;
|
||||
deathStreaks[E.Origin.clientID] = 0;
|
||||
}
|
||||
|
||||
Killer.Tell(messageOnStreak(killerStats.killStreak, killerStats.deathStreak));
|
||||
Killer.Tell(messageOnStreak(killStreaks[E.Origin.clientID], deathStreaks[E.Origin.clientID]));
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Death)
|
||||
@ -112,20 +151,71 @@ namespace SamplePlugin
|
||||
PlayerStats victimStats = playerStats.getStats(Victim);
|
||||
|
||||
victimStats.Deaths++;
|
||||
victimStats.KDR = victimStats.Kills / victimStats.Deaths;
|
||||
victimStats.KDR = (double)victimStats.Kills / (double)victimStats.Deaths;
|
||||
|
||||
playerStats.updateStats(Victim, victimStats);
|
||||
|
||||
victimStats.deathStreak++;
|
||||
victimStats.killStreak = 0;
|
||||
deathStreaks[E.Origin.clientID]++;
|
||||
killStreaks[E.Origin.clientID] = 0;
|
||||
|
||||
Victim.Tell(messageOnStreak(victimStats.killStreak, victimStats.deathStreak));
|
||||
Victim.Tell(messageOnStreak(killStreaks[E.Origin.clientID], deathStreaks[E.Origin.clientID]));
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateAndSaveSkill(Player P)
|
||||
{
|
||||
PlayerStats disconnectStats = playerStats.getStats(P);
|
||||
if (Kills[P.clientID] == 0)
|
||||
return;
|
||||
|
||||
else if (lastKill[P.clientID] > connectionTime[P.clientID])
|
||||
inactiveMinutes[P.clientID] += (int)(DateTime.Now - lastKill[P.clientID]).TotalMinutes;
|
||||
|
||||
int newPlayTime = (int)(DateTime.Now - connectionTime[P.clientID]).TotalMinutes - inactiveMinutes[P.clientID];
|
||||
double newSPM = Kills[P.clientID] * 50 / Math.Max(newPlayTime, 1);
|
||||
|
||||
double kdrWeight = Math.Round(Math.Pow(disconnectStats.KDR, 2 / Math.E), 3);
|
||||
double Multiplier;
|
||||
|
||||
if (disconnectStats.scorePerMinute == 1)
|
||||
Multiplier = 1;
|
||||
else
|
||||
Multiplier = newSPM / disconnectStats.scorePerMinute;
|
||||
|
||||
double scoreWeight = (newSPM * (newPlayTime / disconnectStats.playTime));
|
||||
double newSkillFactor = Multiplier * scoreWeight;
|
||||
|
||||
if (Multiplier >= 1)
|
||||
disconnectStats.scorePerMinute += newSkillFactor;
|
||||
else
|
||||
disconnectStats.scorePerMinute -= (scoreWeight - newSkillFactor);
|
||||
|
||||
disconnectStats.Skill = disconnectStats.scorePerMinute * kdrWeight / 10;
|
||||
disconnectStats.playTime += newPlayTime;
|
||||
|
||||
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()
|
||||
{
|
||||
playerStats = new StatsDB("stats.rm");
|
||||
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()
|
||||
@ -142,17 +232,17 @@ namespace SamplePlugin
|
||||
Message = "Great job! You're on a ^55 killstreak!";
|
||||
break;
|
||||
case 10:
|
||||
Message = "Amazing! ^510 ^7kills without dying!";
|
||||
Message = "Amazing! ^510 kills ^7without dying!";
|
||||
break;
|
||||
}
|
||||
|
||||
switch (deathStreak)
|
||||
{
|
||||
case 5:
|
||||
Message = "Pick it up soldier, you've died 5 times in a row...";
|
||||
Message = "Pick it up soldier, you've died ^55 times ^7in a row...";
|
||||
break;
|
||||
case 10:
|
||||
Message = "Seriously? ^510 ^7deaths without getting a kill?";
|
||||
Message = "Seriously? ^510 deaths ^7without getting a kill?";
|
||||
break;
|
||||
}
|
||||
|
||||
@ -171,7 +261,7 @@ namespace SamplePlugin
|
||||
{
|
||||
get
|
||||
{
|
||||
return 0.1f;
|
||||
return 0.2f;
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,7 +282,7 @@ namespace SamplePlugin
|
||||
{
|
||||
if (!File.Exists(FileName))
|
||||
{
|
||||
String Create = "CREATE TABLE [STATS] ( [npID] TEXT, [KILLS] INTEGER DEFAULT 0, [DEATHS] INTEGER DEFAULT 0, [KDR] REAL DEFAULT 0, [SKILL] REAL DEFAULT 0, [MEAN] REAL DEFAULT 0, [DEV] REAL DEFAULT 0 );";
|
||||
String Create = "CREATE TABLE [STATS] ( [npID] TEXT, [KILLS] INTEGER DEFAULT 0, [DEATHS] INTEGER DEFAULT 0, [KDR] TEXT DEFAULT 0, [SKILL] TEXT DEFAULT 0, [MEAN] REAL DEFAULT 0, [DEV] REAL DEFAULT 0, [SPM] TEXT DEFAULT 0, [PLAYTIME] INTEGER DEFAULT 0);";
|
||||
ExecuteNonQuery(Create);
|
||||
}
|
||||
}
|
||||
@ -204,8 +294,10 @@ namespace SamplePlugin
|
||||
newPlayer.Add("npID", P.npID);
|
||||
newPlayer.Add("KILLS", 0);
|
||||
newPlayer.Add("DEATHS", 0);
|
||||
newPlayer.Add("KDR", 0);
|
||||
newPlayer.Add("SKILL", 0);
|
||||
newPlayer.Add("KDR", 0.0);
|
||||
newPlayer.Add("SKILL", 1.0);
|
||||
newPlayer.Add("SPM", 1.0);
|
||||
newPlayer.Add("PLAYTIME", 1.0);
|
||||
|
||||
Insert("STATS", newPlayer);
|
||||
}
|
||||
@ -222,7 +314,9 @@ namespace SamplePlugin
|
||||
Convert.ToInt32(ResponseRow["KILLS"]),
|
||||
Convert.ToInt32(ResponseRow["DEATHS"]),
|
||||
Convert.ToDouble(ResponseRow["KDR"]),
|
||||
Convert.ToDouble(ResponseRow["SKILL"])
|
||||
Convert.ToDouble(ResponseRow["SKILL"]),
|
||||
Convert.ToDouble(ResponseRow["SPM"]),
|
||||
Convert.ToInt32(ResponseRow["PLAYTIME"])
|
||||
);
|
||||
}
|
||||
|
||||
@ -240,7 +334,9 @@ namespace SamplePlugin
|
||||
updatedPlayer.Add("KILLS", S.Kills);
|
||||
updatedPlayer.Add("DEATHS", S.Deaths);
|
||||
updatedPlayer.Add("KDR", Math.Round(S.KDR, 2));
|
||||
updatedPlayer.Add("SKILL", S.Skill);
|
||||
updatedPlayer.Add("SKILL", Math.Round(S.Skill, 1));
|
||||
updatedPlayer.Add("SPM", Math.Round(S.scorePerMinute, 0));
|
||||
updatedPlayer.Add("PLAYTIME", S.playTime);
|
||||
|
||||
Update("STATS", updatedPlayer, String.Format("npID = '{0}'", P.npID));
|
||||
}
|
||||
@ -248,21 +344,21 @@ namespace SamplePlugin
|
||||
|
||||
public struct PlayerStats
|
||||
{
|
||||
public PlayerStats(int K, int D, double DR, double S)
|
||||
public PlayerStats(int K, int D, double DR, double S, double sc, int P)
|
||||
{
|
||||
Kills = K;
|
||||
Deaths = D;
|
||||
KDR = DR;
|
||||
Skill = S;
|
||||
deathStreak = 0;
|
||||
killStreak = 0;
|
||||
scorePerMinute = sc;
|
||||
playTime = P;
|
||||
}
|
||||
|
||||
public int Kills;
|
||||
public int Deaths;
|
||||
public double KDR;
|
||||
public double Skill;
|
||||
public int deathStreak;
|
||||
public int killStreak;
|
||||
public double scorePerMinute;
|
||||
public int playTime;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user