This code is supposed to send a message with a list of 5 players with the most barrel kills, however it sends the message but with only ONE person in the message and its the one person who got added the 'cachedPlayerStats' first. How do I fix this?
static Dictionary<ulong, PLRStats> cachedPlayerStats = new Dictionary<ulong, PLRStats>();
void GetTopBarrels(BasePlayer player)
{
StringBuilder result = new StringBuilder("Top Player Barrels Hit\n\n");
Dictionary<BasePlayer, float> TopBarrels = new Dictionary<BasePlayer, float>();
//What I want: For every player which is in cached player stats, do the following
//What ACTUALLY happens: It only picks one player (the same one every time) out of the cached player stats and not a list of 5
foreach (KeyValuePair<ulong, PLRStats> individualStats in cachedPlayerStats)
{
Puts(individualStats.Value.PlayerID.ToString());
int barrels = individualStats.Value.BarrelsHit;
if (barrels > 0)
{
Puts("Barrels Hit over 0");
BasePlayer findPlayer = BasePlayer.FindByID(individualStats.Value.PlayerID);
if (!TopBarrels.ContainsKey(findPlayer))
{
TopBarrels.Add(findPlayer, barrels);
Puts("Adding player to the top Barrels list");
}
foreach (KeyValuePair<BasePlayer, float> topFivePlayer in TopBarrels.OrderByDescending(x => x.Value).Take(5))
{
Puts("Getting all the top players");
PLRStats.TryLoad(topFivePlayer.Key.userID);
result.Append($" {topFivePlayer.Key.displayName} with {TopBarrels[findPlayer]} Barrels Hit");
}
SendMessage(player, result.ToString());
Puts("Sent Message");
return;
}
else
{
SendMessage(player, "Nobody on the server has top amount of Barrels Hit!");
return;
}