mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-02-25 04:42:47 +00:00
1.4.1a
- Minor changes
This commit is contained in:
@@ -11,13 +11,13 @@ public enum PenaltyType
|
||||
|
||||
public class PlayerPenaltyManager
|
||||
{
|
||||
private static ConcurrentDictionary<int, Dictionary<PenaltyType, List<(DateTime EndDateTime, int Duration)>>> penalties =
|
||||
private static readonly ConcurrentDictionary<int, Dictionary<PenaltyType, List<(DateTime EndDateTime, int Duration)>>> Penalties =
|
||||
new ConcurrentDictionary<int, Dictionary<PenaltyType, List<(DateTime, int)>>>();
|
||||
|
||||
// Add a penalty for a player
|
||||
public static void AddPenalty(int slot, PenaltyType penaltyType, DateTime endDateTime, int durationSeconds)
|
||||
{
|
||||
penalties.AddOrUpdate(slot,
|
||||
Penalties.AddOrUpdate(slot,
|
||||
(_) =>
|
||||
{
|
||||
var dict = new Dictionary<PenaltyType, List<(DateTime, int)>>
|
||||
@@ -28,11 +28,13 @@ public class PlayerPenaltyManager
|
||||
},
|
||||
(_, existingDict) =>
|
||||
{
|
||||
if (!existingDict.ContainsKey(penaltyType))
|
||||
if (!existingDict.TryGetValue(penaltyType, out var value))
|
||||
{
|
||||
existingDict[penaltyType] = new List<(DateTime, int)>();
|
||||
value = new List<(DateTime, int)>();
|
||||
existingDict[penaltyType] = value;
|
||||
}
|
||||
existingDict[penaltyType].Add((endDateTime, durationSeconds));
|
||||
|
||||
value.Add((endDateTime, durationSeconds));
|
||||
return existingDict;
|
||||
});
|
||||
}
|
||||
@@ -41,49 +43,47 @@ public class PlayerPenaltyManager
|
||||
{
|
||||
//Console.WriteLine($"Checking penalties for player with slot {slot} and penalty type {penaltyType}");
|
||||
|
||||
if (penalties.TryGetValue(slot, out var penaltyDict) && penaltyDict.TryGetValue(penaltyType, out var penaltiesList))
|
||||
if (!Penalties.TryGetValue(slot, out var penaltyDict) ||
|
||||
!penaltyDict.TryGetValue(penaltyType, out var penaltiesList)) return false;
|
||||
//Console.WriteLine($"Found penalties for player with slot {slot} and penalty type {penaltyType}");
|
||||
|
||||
var now = DateTime.UtcNow.ToLocalTime();
|
||||
|
||||
// Check if any active penalties exist
|
||||
foreach (var penalty in penaltiesList.ToList())
|
||||
{
|
||||
//Console.WriteLine($"Found penalties for player with slot {slot} and penalty type {penaltyType}");
|
||||
|
||||
DateTime now = DateTime.UtcNow.ToLocalTime();
|
||||
|
||||
// Check if any active penalties exist
|
||||
foreach (var penalty in penaltiesList.ToList())
|
||||
// Check if the penalty is still active
|
||||
if (penalty.Duration > 0 && now >= penalty.EndDateTime.AddSeconds(penalty.Duration))
|
||||
{
|
||||
// Check if the penalty is still active
|
||||
if (penalty.Duration > 0 && now >= penalty.EndDateTime.AddSeconds(penalty.Duration))
|
||||
//Console.WriteLine($"Removing expired penalty for player with slot {slot} and penalty type {penaltyType}");
|
||||
penaltiesList.Remove(penalty); // Remove expired penalty
|
||||
if (penaltiesList.Count == 0)
|
||||
{
|
||||
//Console.WriteLine($"Removing expired penalty for player with slot {slot} and penalty type {penaltyType}");
|
||||
penaltiesList.Remove(penalty); // Remove expired penalty
|
||||
if (penaltiesList.Count == 0)
|
||||
{
|
||||
//Console.WriteLine($"No more penalties of type {penaltyType} for player with slot {slot}. Removing penalty type.");
|
||||
penaltyDict.Remove(penaltyType); // Remove penalty type if no more penalties exist
|
||||
}
|
||||
}
|
||||
else if (penalty.Duration == 0 || now < penalty.EndDateTime)
|
||||
{
|
||||
//Console.WriteLine($"Player with slot {slot} is penalized for type {penaltyType}");
|
||||
// Return true if there's an active penalty
|
||||
return true;
|
||||
//Console.WriteLine($"No more penalties of type {penaltyType} for player with slot {slot}. Removing penalty type.");
|
||||
penaltyDict.Remove(penaltyType); // Remove penalty type if no more penalties exist
|
||||
}
|
||||
}
|
||||
|
||||
// Return false if no active penalties are found
|
||||
//Console.WriteLine($"Player with slot {slot} is not penalized for type {penaltyType}");
|
||||
return false;
|
||||
else if (penalty.Duration == 0 || now < penalty.EndDateTime)
|
||||
{
|
||||
//Console.WriteLine($"Player with slot {slot} is penalized for type {penaltyType}");
|
||||
// Return true if there's an active penalty
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Return false if no active penalties are found
|
||||
//Console.WriteLine($"Player with slot {slot} is not penalized for type {penaltyType}");
|
||||
return false;
|
||||
|
||||
// Return false if no penalties of the specified type were found for the player
|
||||
//Console.WriteLine($"No penalties found for player with slot {slot} and penalty type {penaltyType}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the end datetime and duration of penalties for a player and penalty type
|
||||
public static List<(DateTime EndDateTime, int Duration)> GetPlayerPenalties(int slot, PenaltyType penaltyType)
|
||||
{
|
||||
if (penalties.TryGetValue(slot, out Dictionary<PenaltyType, List<(DateTime EndDateTime, int Duration)>>? penaltyDict) &&
|
||||
penaltyDict.TryGetValue(penaltyType, out List<(DateTime EndDateTime, int Duration)>? penaltiesList) && penaltiesList != null)
|
||||
if (Penalties.TryGetValue(slot, out var penaltyDict) &&
|
||||
penaltyDict.TryGetValue(penaltyType, out var penaltiesList))
|
||||
{
|
||||
return penaltiesList;
|
||||
}
|
||||
@@ -92,28 +92,28 @@ public class PlayerPenaltyManager
|
||||
|
||||
public static bool IsSlotInPenalties(int slot)
|
||||
{
|
||||
return penalties.ContainsKey(slot);
|
||||
return Penalties.ContainsKey(slot);
|
||||
}
|
||||
|
||||
// Remove all penalties for a player slot
|
||||
public static void RemoveAllPenalties(int slot)
|
||||
{
|
||||
if (penalties.ContainsKey(slot))
|
||||
if (Penalties.ContainsKey(slot))
|
||||
{
|
||||
penalties.TryRemove(slot, out _);
|
||||
Penalties.TryRemove(slot, out _);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all penalties
|
||||
public static void RemoveAllPenalties()
|
||||
{
|
||||
penalties.Clear();
|
||||
Penalties.Clear();
|
||||
}
|
||||
|
||||
// Remove all penalties of a selected type from a specific player
|
||||
public static void RemovePenaltiesByType(int slot, PenaltyType penaltyType)
|
||||
{
|
||||
if (penalties.TryGetValue(slot, out Dictionary<PenaltyType, List<(DateTime EndDateTime, int Duration)>>? penaltyDict) &&
|
||||
if (Penalties.TryGetValue(slot, out Dictionary<PenaltyType, List<(DateTime EndDateTime, int Duration)>>? penaltyDict) &&
|
||||
penaltyDict.ContainsKey(penaltyType))
|
||||
{
|
||||
penaltyDict.Remove(penaltyType);
|
||||
@@ -123,8 +123,8 @@ public class PlayerPenaltyManager
|
||||
// Remove all expired penalties for all players and penalty types
|
||||
public static void RemoveExpiredPenalties()
|
||||
{
|
||||
DateTime now = DateTime.UtcNow.ToLocalTime();
|
||||
foreach (var kvp in penalties.ToList()) // Use ToList to avoid modification while iterating
|
||||
var now = DateTime.UtcNow.ToLocalTime();
|
||||
foreach (var kvp in Penalties.ToList()) // Use ToList to avoid modification while iterating
|
||||
{
|
||||
var playerSlot = kvp.Key;
|
||||
var penaltyDict = kvp.Value;
|
||||
@@ -138,7 +138,7 @@ public class PlayerPenaltyManager
|
||||
// Remove player slot if no penalties left
|
||||
if (penaltyDict.Count == 0)
|
||||
{
|
||||
penalties.TryRemove(playerSlot, out _);
|
||||
Penalties.TryRemove(playerSlot, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user