mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
moved stats to a plugin!
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using SharedLibrary;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
|
||||
namespace SamplePlugin
|
||||
{
|
||||
#if SAMPLE_CODE
|
||||
public class SampleCommand : Command
|
||||
{
|
||||
public SampleCommand() : base("testplugin", "sample plugin command. syntax !testplugin", "tp", Player.Permission.User, 0, false) { }
|
||||
@ -35,11 +39,17 @@ namespace SamplePlugin
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
originatingServer.Broadcast(String.Format("^7{0}: ^{1}{2}^7", Sender, i, Message));
|
||||
|
||||
originatingServer.Log.Write("This line is coming from the plugin " + this.Name, Log.Level.Production);
|
||||
}
|
||||
}
|
||||
|
||||
public class SampleEvent : EventNotify
|
||||
{
|
||||
public override void onLoad()
|
||||
{
|
||||
Console.WriteLine("The sample event plugin was loaded!");
|
||||
}
|
||||
public override void onEvent(Event E)
|
||||
{
|
||||
E.Owner.Broadcast("An event occured of type: ^1" + E.Type);
|
||||
@ -48,9 +58,142 @@ namespace SamplePlugin
|
||||
E.Origin.Tell(E.Data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class InvalidCommandExample
|
||||
{
|
||||
private void doNotDoThis() { }
|
||||
}
|
||||
#endif
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public class Stats : EventNotify
|
||||
{
|
||||
public static StatsDB playerStats { get; private set; }
|
||||
|
||||
public override void onEvent(Event E)
|
||||
{
|
||||
Console.WriteLine("event type is " + E.Type);
|
||||
if (E.Type == Event.GType.Kill)
|
||||
{
|
||||
Player Killer = E.Origin;
|
||||
PlayerStats killerStats = playerStats.getStats(Killer);
|
||||
|
||||
if (Killer != E.Target)
|
||||
{
|
||||
killerStats.Kills++;
|
||||
|
||||
if (killerStats.Deaths == 0)
|
||||
killerStats.KDR = killerStats.Kills;
|
||||
else
|
||||
killerStats.KDR = killerStats.Kills / killerStats.Deaths;
|
||||
|
||||
playerStats.updateStats(Killer, killerStats);
|
||||
}
|
||||
}
|
||||
|
||||
if (E.Type == Event.GType.Death)
|
||||
{
|
||||
Player Victim = E.Origin;
|
||||
PlayerStats victimStats = playerStats.getStats(Victim);
|
||||
|
||||
victimStats.Deaths++;
|
||||
victimStats.KDR = victimStats.Kills / victimStats.Deaths;
|
||||
playerStats.updateStats(Victim, victimStats);
|
||||
}
|
||||
}
|
||||
|
||||
public override void onLoad()
|
||||
{
|
||||
playerStats = new StatsDB("stats.rm");
|
||||
}
|
||||
}
|
||||
|
||||
public class StatsDB : Database
|
||||
{
|
||||
public StatsDB(String FN) : base(FN) { }
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
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 );";
|
||||
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);
|
||||
newPlayer.Add("KDR", 0);
|
||||
newPlayer.Add("SKILL", 0);
|
||||
|
||||
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"]),
|
||||
Convert.ToDouble(ResponseRow["SKILL"])
|
||||
);
|
||||
}
|
||||
|
||||
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));
|
||||
updatedPlayer.Add("SKILL", S.Skill);
|
||||
|
||||
Update("STATS", updatedPlayer, String.Format("npID = '{0}'", P.npID));
|
||||
}
|
||||
}
|
||||
|
||||
public struct PlayerStats
|
||||
{
|
||||
public PlayerStats(int K, int D, double DR, double S)
|
||||
{
|
||||
Kills = K;
|
||||
Deaths = D;
|
||||
KDR = DR;
|
||||
Skill = S;
|
||||
}
|
||||
|
||||
public int Kills;
|
||||
public int Deaths;
|
||||
public double KDR;
|
||||
public double Skill;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user