2015-08-20 01:06:44 -04:00
|
|
|
|
using System;
|
|
|
|
|
using SharedLibrary;
|
|
|
|
|
using System.Text;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
|
|
|
|
namespace SamplePlugin
|
|
|
|
|
{
|
2015-08-20 17:54:38 -04:00
|
|
|
|
#if SAMPLE_CODE
|
2015-08-20 01:06:44 -04:00
|
|
|
|
public class SampleCommand : Command
|
|
|
|
|
{
|
|
|
|
|
public SampleCommand() : base("testplugin", "sample plugin command. syntax !testplugin", "tp", Player.Permission.User, 0, false) { }
|
|
|
|
|
|
|
|
|
|
public override void Execute(Event E)
|
|
|
|
|
{
|
|
|
|
|
Player clientWhoSent = E.Origin;
|
|
|
|
|
Server originatingServer = E.Owner;
|
|
|
|
|
|
|
|
|
|
String[] messageToClient = {
|
|
|
|
|
String.Format("The command {0} was requested by ^3{1}", Name, clientWhoSent.Name),
|
|
|
|
|
String.Format("The command was request on server ^1{0}", originatingServer.getName())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (String Line in messageToClient)
|
|
|
|
|
clientWhoSent.Tell(Line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class AnotherSampleCommand : Command
|
|
|
|
|
{
|
|
|
|
|
public AnotherSampleCommand() : base("scream", "broadcast your message. syntax !scream <message>", "s", Player.Permission.Moderator, 1, false) { }
|
|
|
|
|
|
|
|
|
|
public override void Execute(Event E)
|
|
|
|
|
{
|
|
|
|
|
Server originatingServer = E.Owner;
|
|
|
|
|
String Message = E.Data;
|
|
|
|
|
String Sender = E.Origin.Name;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
2015-08-20 13:52:30 -04:00
|
|
|
|
originatingServer.Broadcast(String.Format("^7{0}: ^{1}{2}^7", Sender, i, Message));
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
|
|
|
|
originatingServer.Log.Write("This line is coming from the plugin " + this.Name, Log.Level.Production);
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 15:23:13 -04:00
|
|
|
|
public class SampleEvent : EventNotify
|
|
|
|
|
{
|
2015-08-20 17:54:38 -04:00
|
|
|
|
public override void onLoad()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("The sample event plugin was loaded!");
|
|
|
|
|
}
|
2015-08-20 15:23:13 -04:00
|
|
|
|
public override void onEvent(Event E)
|
|
|
|
|
{
|
|
|
|
|
E.Owner.Broadcast("An event occured of type: ^1" + E.Type);
|
|
|
|
|
|
|
|
|
|
if (E.Data != null)
|
|
|
|
|
E.Origin.Tell(E.Data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
2015-08-20 01:06:44 -04:00
|
|
|
|
public class InvalidCommandExample
|
|
|
|
|
{
|
|
|
|
|
private void doNotDoThis() { }
|
|
|
|
|
}
|
2015-08-20 17:54:38 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-08-21 21:11:35 -04:00
|
|
|
|
public class StatCommand : Command
|
2015-08-20 17:54:38 -04:00
|
|
|
|
{
|
|
|
|
|
public StatCommand() : base("stats", "view your stats. syntax !stats", "xlrstats", Player.Permission.User, 0, false) { }
|
|
|
|
|
|
|
|
|
|
public override void Execute(Event E)
|
|
|
|
|
{
|
|
|
|
|
PlayerStats pStats = Stats.playerStats.getStats(E.Origin);
|
|
|
|
|
String 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 02:04:30 -04:00
|
|
|
|
public class Stats : Plugin
|
2015-08-20 17:54:38 -04:00
|
|
|
|
{
|
|
|
|
|
public static StatsDB playerStats { get; private set; }
|
2015-08-26 01:49:47 -04:00
|
|
|
|
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];
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
|
|
|
|
public override void onEvent(Event E)
|
|
|
|
|
{
|
2015-08-26 01:49:47 -04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 17:54:38 -04:00
|
|
|
|
if (E.Type == Event.GType.Kill)
|
|
|
|
|
{
|
|
|
|
|
Player Killer = E.Origin;
|
|
|
|
|
PlayerStats killerStats = playerStats.getStats(Killer);
|
|
|
|
|
|
2015-08-26 01:49:47 -04:00
|
|
|
|
lastKill[E.Origin.clientID] = DateTime.Now;
|
|
|
|
|
Kills[E.Origin.clientID]++;
|
|
|
|
|
|
|
|
|
|
if ((lastKill[E.Origin.clientID] - DateTime.Now).TotalSeconds > 60)
|
|
|
|
|
{
|
|
|
|
|
inactiveMinutes[E.Origin.clientID]++;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 17:54:38 -04:00
|
|
|
|
if (Killer != E.Target)
|
|
|
|
|
{
|
|
|
|
|
killerStats.Kills++;
|
|
|
|
|
|
|
|
|
|
if (killerStats.Deaths == 0)
|
|
|
|
|
killerStats.KDR = killerStats.Kills;
|
|
|
|
|
else
|
2015-08-26 01:49:47 -04:00
|
|
|
|
killerStats.KDR = (double)killerStats.Kills / (double)killerStats.Deaths;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
|
|
|
|
playerStats.updateStats(Killer, killerStats);
|
2015-08-23 17:58:48 -04:00
|
|
|
|
|
2015-08-26 01:49:47 -04:00
|
|
|
|
killStreaks[E.Origin.clientID]++;
|
|
|
|
|
deathStreaks[E.Origin.clientID] = 0;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
2015-08-23 17:58:48 -04:00
|
|
|
|
|
2015-08-26 01:49:47 -04:00
|
|
|
|
Killer.Tell(messageOnStreak(killStreaks[E.Origin.clientID], deathStreaks[E.Origin.clientID]));
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Type == Event.GType.Death)
|
|
|
|
|
{
|
|
|
|
|
Player Victim = E.Origin;
|
|
|
|
|
PlayerStats victimStats = playerStats.getStats(Victim);
|
|
|
|
|
|
|
|
|
|
victimStats.Deaths++;
|
2015-08-26 01:49:47 -04:00
|
|
|
|
victimStats.KDR = (double)victimStats.Kills / (double)victimStats.Deaths;
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2015-08-20 17:54:38 -04:00
|
|
|
|
playerStats.updateStats(Victim, victimStats);
|
2015-08-23 17:58:48 -04:00
|
|
|
|
|
2015-08-26 01:49:47 -04:00
|
|
|
|
deathStreaks[E.Origin.clientID]++;
|
|
|
|
|
killStreaks[E.Origin.clientID] = 0;
|
2015-08-23 17:58:48 -04:00
|
|
|
|
|
2015-08-26 01:49:47 -04:00
|
|
|
|
Victim.Tell(messageOnStreak(killStreaks[E.Origin.clientID], deathStreaks[E.Origin.clientID]));
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 01:49:47 -04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-08-20 17:54:38 -04:00
|
|
|
|
public override void onLoad()
|
|
|
|
|
{
|
2015-08-26 01:49:47 -04:00
|
|
|
|
for (int i = 0; i < 18; i++)
|
|
|
|
|
{
|
|
|
|
|
Kills[i] = 0;
|
|
|
|
|
connectionTime[i] = DateTime.Now;
|
|
|
|
|
inactiveMinutes[i] = 0;
|
|
|
|
|
deathStreaks[i] = 0;
|
|
|
|
|
killStreaks[i] = 0;
|
|
|
|
|
}
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
2015-08-22 02:04:30 -04:00
|
|
|
|
|
2015-08-22 12:41:14 -04:00
|
|
|
|
public override void onUnload()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
private String messageOnStreak(int killStreak, int deathStreak)
|
|
|
|
|
{
|
|
|
|
|
String Message = "";
|
|
|
|
|
switch (killStreak)
|
|
|
|
|
{
|
|
|
|
|
case 5:
|
|
|
|
|
Message = "Great job! You're on a ^55 killstreak!";
|
|
|
|
|
break;
|
|
|
|
|
case 10:
|
2015-08-26 01:49:47 -04:00
|
|
|
|
Message = "Amazing! ^510 kills ^7without dying!";
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (deathStreak)
|
|
|
|
|
{
|
|
|
|
|
case 5:
|
2015-08-26 01:49:47 -04:00
|
|
|
|
Message = "Pick it up soldier, you've died ^55 times ^7in a row...";
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
|
|
|
|
case 10:
|
2015-08-26 01:49:47 -04:00
|
|
|
|
Message = "Seriously? ^510 deaths ^7without getting a kill?";
|
2015-08-23 17:58:48 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Message;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 02:04:30 -04:00
|
|
|
|
public override string Name
|
|
|
|
|
{
|
2015-08-23 23:35:04 -04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "Basic Stats";
|
|
|
|
|
}
|
2015-08-22 02:04:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override float Version
|
|
|
|
|
{
|
2015-08-23 23:35:04 -04:00
|
|
|
|
get
|
|
|
|
|
{
|
2015-08-26 01:49:47 -04:00
|
|
|
|
return 0.2f;
|
2015-08-23 23:35:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Author
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "RaidMax";
|
|
|
|
|
}
|
2015-08-22 02:04:30 -04:00
|
|
|
|
}
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StatsDB : Database
|
|
|
|
|
{
|
|
|
|
|
public StatsDB(String FN) : base(FN) { }
|
|
|
|
|
|
|
|
|
|
public override void Init()
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(FileName))
|
|
|
|
|
{
|
2015-08-26 01:49:47 -04:00
|
|
|
|
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);";
|
2015-08-20 17:54:38 -04:00
|
|
|
|
ExecuteNonQuery(Create);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addPlayer(Player P)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<String, object> newPlayer = new Dictionary<String, object>();
|
|
|
|
|
|
|
|
|
|
newPlayer.Add("npID", P.npID);
|
|
|
|
|
newPlayer.Add("KILLS", 0);
|
|
|
|
|
newPlayer.Add("DEATHS", 0);
|
2015-08-26 01:49:47 -04:00
|
|
|
|
newPlayer.Add("KDR", 0.0);
|
|
|
|
|
newPlayer.Add("SKILL", 1.0);
|
|
|
|
|
newPlayer.Add("SPM", 1.0);
|
|
|
|
|
newPlayer.Add("PLAYTIME", 1.0);
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
|
|
|
|
Insert("STATS", newPlayer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlayerStats getStats(Player P)
|
|
|
|
|
{
|
|
|
|
|
String Query = String.Format("SELECT * FROM STATS WHERE npID = '{0}'", P.npID);
|
|
|
|
|
DataTable Result = GetDataTable(Query);
|
|
|
|
|
|
|
|
|
|
if (Result != null && Result.Rows.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
DataRow ResponseRow = Result.Rows[0];
|
|
|
|
|
return new PlayerStats(
|
|
|
|
|
Convert.ToInt32(ResponseRow["KILLS"]),
|
|
|
|
|
Convert.ToInt32(ResponseRow["DEATHS"]),
|
|
|
|
|
Convert.ToDouble(ResponseRow["KDR"]),
|
2015-08-26 01:49:47 -04:00
|
|
|
|
Convert.ToDouble(ResponseRow["SKILL"]),
|
|
|
|
|
Convert.ToDouble(ResponseRow["SPM"]),
|
|
|
|
|
Convert.ToInt32(ResponseRow["PLAYTIME"])
|
2015-08-20 17:54:38 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
addPlayer(P);
|
|
|
|
|
return getStats(P);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateStats(Player P, PlayerStats S)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<String, object> updatedPlayer = new Dictionary<String, object>();
|
|
|
|
|
|
|
|
|
|
updatedPlayer.Add("KILLS", S.Kills);
|
|
|
|
|
updatedPlayer.Add("DEATHS", S.Deaths);
|
|
|
|
|
updatedPlayer.Add("KDR", Math.Round(S.KDR, 2));
|
2015-08-26 01:49:47 -04:00
|
|
|
|
updatedPlayer.Add("SKILL", Math.Round(S.Skill, 1));
|
|
|
|
|
updatedPlayer.Add("SPM", Math.Round(S.scorePerMinute, 0));
|
|
|
|
|
updatedPlayer.Add("PLAYTIME", S.playTime);
|
2015-08-20 17:54:38 -04:00
|
|
|
|
|
|
|
|
|
Update("STATS", updatedPlayer, String.Format("npID = '{0}'", P.npID));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct PlayerStats
|
|
|
|
|
{
|
2015-08-26 01:49:47 -04:00
|
|
|
|
public PlayerStats(int K, int D, double DR, double S, double sc, int P)
|
2015-08-20 17:54:38 -04:00
|
|
|
|
{
|
|
|
|
|
Kills = K;
|
|
|
|
|
Deaths = D;
|
|
|
|
|
KDR = DR;
|
|
|
|
|
Skill = S;
|
2015-08-26 01:49:47 -04:00
|
|
|
|
scorePerMinute = sc;
|
|
|
|
|
playTime = P;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Kills;
|
|
|
|
|
public int Deaths;
|
|
|
|
|
public double KDR;
|
|
|
|
|
public double Skill;
|
2015-08-26 01:49:47 -04:00
|
|
|
|
public double scorePerMinute;
|
|
|
|
|
public int playTime;
|
2015-08-20 17:54:38 -04:00
|
|
|
|
}
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|