Skip to content

Commit

Permalink
Fix the major bug with kills and add API
Browse files Browse the repository at this point in the history
  • Loading branch information
PintTheDragon committed Nov 15, 2020
1 parent 0ed5b67 commit 40083ea
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
15 changes: 12 additions & 3 deletions SCPStats/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static string HmacSha256Digest(string secret, string message)
return BitConverter.ToString(new HMACSHA256(encoding.GetBytes(secret)).ComputeHash(encoding.GetBytes(message))).Replace("-", "").ToLower();
}

private static string HandleId(string id)
internal static string HandleId(string id)
{
return id.Split('@')[0];
}
Expand Down Expand Up @@ -420,11 +420,18 @@ internal static void OnKill(DyingEventArgs ev)
{
if (PauseRound || !ev.IsAllowed || !IsPlayerValid(ev.Target, false) || !IsPlayerValid(ev.Killer, false) || !RoundSummary.RoundInProgress()) return;

if(!ev.Target.DoNotTrack) SendRequest("02", "{\"playerid\": \""+HandleId(ev.Target.RawUserId)+"\", \"killerrole\": \""+((int) ev.Killer.Role).ToString()+"\", \"playerrole\": \""+((int) ev.Target.Role).ToString()+"\", \"damagetype\": \""+DamageTypes.ToIndex(ev.HitInformation.GetDamageType()).ToString()+"\"}");
if (!ev.Target.DoNotTrack)
{
SendRequest("12", "{\"playerid\": \""+HandleId(ev.Target.RawUserId)+"\", \"killerrole\": \""+((int) ev.Killer.Role).ToString()+"\", \"playerrole\": \""+((int) ev.Target.Role).ToString()+"\", \"damagetype\": \""+DamageTypes.ToIndex(ev.HitInformation.GetDamageType()).ToString()+"\"}");
if (!RoundStats.Deaths.ContainsKey(ev.Target.UserId)) RoundStats.Deaths[ev.Target.UserId] = 1;
else RoundStats.Deaths[ev.Target.UserId]++;
}

if (ev.Killer.RawUserId == ev.Target.RawUserId || ev.Killer.DoNotTrack) return;

SendRequest("03", "{\"playerid\": \""+HandleId(ev.Target.RawUserId)+"\", \"targetrole\": \""+((int) ev.Target.Role).ToString()+"\", \"playerrole\": \""+((int) ev.Killer.Role).ToString()+"\", \"damagetype\": \""+DamageTypes.ToIndex(ev.HitInformation.GetDamageType()).ToString()+"\"}");
SendRequest("13", "{\"playerid\": \""+HandleId(ev.Killer.RawUserId)+"\", \"targetrole\": \""+((int) ev.Target.Role).ToString()+"\", \"playerrole\": \""+((int) ev.Killer.Role).ToString()+"\", \"damagetype\": \""+DamageTypes.ToIndex(ev.HitInformation.GetDamageType()).ToString()+"\"}");
if (!RoundStats.Kills.ContainsKey(ev.Target.UserId)) RoundStats.Kills[ev.Killer.UserId] = 1;
else RoundStats.Kills[ev.Killer.UserId]++;
}

internal static void OnRoleChanged(ChangingRoleEventArgs ev)
Expand All @@ -434,6 +441,8 @@ internal static void OnRoleChanged(ChangingRoleEventArgs ev)
if (ev.IsEscaped && !ev.Player.DoNotTrack)
{
SendRequest("07", "{\"playerid\": \""+HandleId(ev.Player.RawUserId)+"\", \"role\": \""+((int) ev.Player.Role).ToString()+"\"}");
if (!RoundStats.Kills.ContainsKey(ev.Player.UserId)) RoundStats.Kills[ev.Player.UserId] = 1;
else RoundStats.Kills[ev.Player.UserId]++;
}

if (ev.NewRole == RoleType.None || ev.NewRole == RoleType.Spectator) return;
Expand Down
4 changes: 2 additions & 2 deletions SCPStats/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.5")]
[assembly: AssemblyFileVersion("1.1.5")]
[assembly: AssemblyVersion("1.1.6")]
[assembly: AssemblyFileVersion("1.1.6")]
2 changes: 1 addition & 1 deletion SCPStats/SCPStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SCPStats : Plugin<Config>
{
public override string Name { get; } = "ScpStats";
public override string Author { get; } = "PintTheDragon";
public override Version Version { get; } = new Version(1, 1, 5);
public override Version Version { get; } = new Version(1, 1, 6);
public override PluginPriority Priority { get; } = PluginPriority.Last;

internal static SCPStats Singleton;
Expand Down
1 change: 1 addition & 0 deletions SCPStats/SCPStats.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
<Compile Include="SCPStats.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerNamePatch.cs" />
<Compile Include="Stats.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
98 changes: 98 additions & 0 deletions SCPStats/Stats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Collections.Generic;
using System.Linq;
using Exiled.API.Features;

namespace SCPStats
{
public static class RoundStats
{
internal static Dictionary<string, int> Kills = new Dictionary<string, int>();
internal static Dictionary<string, int> Deaths = new Dictionary<string, int>();
internal static Dictionary<string, int> Escapes = new Dictionary<string, int>();

internal static void Reset()
{
Kills.Clear();
Deaths.Clear();
Escapes.Clear();
}

private static Player GetPlayer(string id)
{
return Player.UserIdsCache.TryGetValue(id, out var player) ? player : null;
}

public static int GetRoundKills(this Player p) => Kills.TryGetValue(p.UserId, out var kills) ? kills : 0;
public static int GetRoundDeaths(this Player p) => Deaths.TryGetValue(p.UserId, out var deaths) ? deaths : 0;

public static float GetKD(this Player p)
{
var kills = p.GetRoundKills();
var deaths = p.GetRoundDeaths();

return kills == 0 || deaths == 0 ? 0 : (float) kills / deaths;
}

public static Player GetMostKills()
{
if (Kills.Count < 1) return null;
var id = Kills.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;

return GetPlayer(id);
}

public static Player GetLeastKills()
{
if (Kills.Count < 1) return null;
var id = Kills.Aggregate((l, r) => l.Value < r.Value ? l : r).Key;

return GetPlayer(id);
}

public static Player GetMostDeaths()
{
if (Deaths.Count < 1) return null;
var id = Deaths.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;

return GetPlayer(id);
}

public static Player GetLeastDeaths()
{
if (Deaths.Count < 1) return null;
var id = Deaths.Aggregate((l, r) => l.Value < r.Value ? l : r).Key;

return GetPlayer(id);
}

public static Player GetBestKD()
{
return Player.List.OrderBy(p => p.GetKD()).FirstOrDefault();
}

public static Player GetWorstKD()
{
return Player.List.OrderBy(p => p.GetKD()).LastOrDefault();
}

public static Player GetFirstEscape()
{
return Escapes.Count < 1 ? null : GetPlayer(Escapes.Keys.First());
}

public static Player GetFirstKill()
{
return Kills.Count < 1 ? null : GetPlayer(Kills.Keys.First());
}

public static Player GetFirstDeath()
{
return Deaths.Count < 1 ? null : GetPlayer(Deaths.Keys.First());
}

public static List<Player> GetEscapes()
{
return Escapes.Keys.Select(GetPlayer).Where(pl => pl != null).ToList();
}
}
}

0 comments on commit 40083ea

Please sign in to comment.