|
2 | 2 | using CounterStrikeSharp.API.Core; |
3 | 3 | using CounterStrikeSharp.API.Core.Attributes.Registration; |
4 | 4 | using CounterStrikeSharp.API.Modules.Commands; |
5 | | -using CounterStrikeSharp.API.Modules.Commands.Targeting; |
6 | 5 | using CounterStrikeSharp.API.Modules.Utils; |
7 | | -using Nexd.MySQL; |
8 | 6 |
|
9 | 7 | namespace CoinThrow |
10 | 8 | { |
11 | | - public partial class CoinThrow : BasePlugin, IPluginConfig<ConfigCT> |
| 9 | + public class CoinThrow : BasePlugin, IPluginConfig<Config> |
12 | 10 | { |
13 | | - private Dictionary<string, DateTime> lastCoinThrowTimes = new Dictionary<string, DateTime>(); |
14 | | - private MySqlDb? mySql; |
| 11 | + private readonly Dictionary<string, DateTime> _lastCoinThrowTimes = new(); |
| 12 | + private static readonly Random Random = new(); |
| 13 | + private const int CooldownSeconds = 10; |
| 14 | + |
| 15 | + private Database? _database; |
| 16 | + |
15 | 17 | public override string ModuleAuthor => "TICHOJEBEC"; |
16 | 18 | public override string ModuleName => "CoinThrow"; |
17 | | - public override string ModuleVersion => "v1.0"; |
18 | | - public ConfigCT Config { get; set; } = new ConfigCT(); |
| 19 | + public override string ModuleVersion => "v1.1"; |
| 20 | + |
| 21 | + public Config Config { get; set; } = new(); |
| 22 | + public void OnConfigParsed(Config config) => Config = config; |
19 | 23 |
|
20 | | - public void OnConfigParsed(ConfigCT config) |
21 | | - { |
22 | | - Config = config; |
23 | | - } |
24 | | - private static void GetPlayers(out List<CCSPlayerController> players) |
25 | | - { |
26 | | - players = Utilities.GetPlayers().Where(s => s.IsValid && s.PlayerPawn.Value != null).ToList(); |
27 | | - } |
28 | 24 | public override void Load(bool hotReload) |
29 | 25 | { |
30 | | - InitializeDatabase(); |
31 | | - Console.WriteLine("CoinThrow plugin was successfully loaded!"); |
32 | | - } |
33 | | - private void InitializeDatabase() |
34 | | - { |
35 | | - try |
36 | | - { |
37 | | - mySql = new MySqlDb(Config.DBHost, Config.DBUser, Config.DBPassword, Config.DBDatabase); |
38 | | - mySql.ExecuteNonQueryAsync(@" |
39 | | - CREATE TABLE IF NOT EXISTS `cointhrow` ( |
40 | | - `id` INT AUTO_INCREMENT PRIMARY KEY, |
41 | | - `player_steamid` VARCHAR(32) NOT NULL UNIQUE, |
42 | | - `player_name` VARCHAR(32) NOT NULL, |
43 | | - `counts` INT(11) NOT NULL DEFAULT 0 |
44 | | - ); |
45 | | - "); |
46 | | - Console.WriteLine($"Connection to the database {Config.DBHost} successful."); |
47 | | - } |
48 | | - catch (Exception ex) |
49 | | - { |
50 | | - Console.WriteLine($"Connection to the database was unsuccessful: {ex.Message}."); |
51 | | - } |
52 | | - } |
53 | | - private int GetPlayerThrows(string playerSteamID) |
54 | | - { |
55 | | - if (mySql == null) |
56 | | - throw new InvalidOperationException("Database not initialized."); |
57 | | - var result = mySql.Table("cointhrow") |
58 | | - .Where(MySqlQueryCondition.New("player_steamid", "=", playerSteamID)) |
59 | | - .Select(); |
60 | | - if (result.Rows > 0) |
61 | | - { |
62 | | - return result.Get<int>(0, "counts"); |
63 | | - } |
64 | | - return 0; |
| 26 | + _database = new Database(Config.DbHost, Config.DbPort, Config.DbDatabase, Config.DbUser, Config.DbPassword); |
| 27 | + _database.Initialize(); |
| 28 | + |
| 29 | + Console.WriteLine("[CoinThrow] Plugin loaded successfully."); |
65 | 30 | } |
66 | | - private bool CanPlayerThrowCoin(string steamID) |
| 31 | + |
| 32 | + private bool IsOnCooldown(string steamId, out double remainingSeconds) |
67 | 33 | { |
68 | | - if (!lastCoinThrowTimes.ContainsKey(steamID)) |
| 34 | + if (_lastCoinThrowTimes.TryGetValue(steamId, out var lastThrow)) |
69 | 35 | { |
70 | | - return true; |
| 36 | + var elapsed = DateTime.Now - lastThrow; |
| 37 | + if (elapsed.TotalSeconds < CooldownSeconds) |
| 38 | + { |
| 39 | + remainingSeconds = CooldownSeconds - elapsed.TotalSeconds; |
| 40 | + return true; |
| 41 | + } |
71 | 42 | } |
72 | | - DateTime lastThrowTime = lastCoinThrowTimes[steamID]; |
73 | | - TimeSpan timeSinceLastThrow = DateTime.Now - lastThrowTime; |
74 | | - return timeSinceLastThrow.TotalSeconds >= 10; |
75 | | - } |
76 | | - private void UpdateLastCoinThrowTime(string steamID) |
77 | | - { |
78 | | - lastCoinThrowTimes[steamID] = DateTime.Now; |
| 43 | + |
| 44 | + remainingSeconds = 0; |
| 45 | + return false; |
79 | 46 | } |
80 | | - private void AddOrUpdateCoinThrowInDatabase(string playerSteamID, string playerName) |
81 | | - { |
82 | | - if (mySql == null) |
83 | | - throw new InvalidOperationException("Database not initialized."); |
84 | 47 |
|
85 | | - var result = mySql.Table("cointhrow") |
86 | | - .Where(MySqlQueryCondition.New("player_steamid", "=", playerSteamID)) |
87 | | - .Select(); |
| 48 | + private void UpdateLastCoinThrowTime(string steamId) => |
| 49 | + _lastCoinThrowTimes[steamId] = DateTime.Now; |
88 | 50 |
|
89 | | - if (result.Rows > 0) |
90 | | - { |
91 | | - int currentCount = Convert.ToInt32(result[0]["counts"]); |
92 | | - currentCount++; |
93 | | - mySql.Table("cointhrow") |
94 | | - .Where(MySqlQueryCondition.New("player_steamid", "=", playerSteamID)) |
95 | | - .Update(new MySqlQueryValue |
96 | | - { |
97 | | - ["player_name"] = playerName, |
98 | | - ["counts"] = currentCount.ToString() |
99 | | - }); |
100 | | - } |
101 | | - else |
102 | | - { |
103 | | - mySql.Table("cointhrow").Insert(new MySqlQueryValue |
104 | | - { |
105 | | - ["player_steamid"] = playerSteamID, |
106 | | - ["player_name"] = playerName, |
107 | | - ["counts"] = "1" |
108 | | - }); |
109 | | - } |
110 | | - } |
111 | 51 | [ConsoleCommand("css_cointhrow", "Throw a coin with the result being heads or tails")] |
112 | 52 | public void OnCoinThrowCommand(CCSPlayerController? player, CommandInfo command) |
113 | 53 | { |
114 | | - if (player != null && player.PlayerPawn.Value != null) |
| 54 | + if (player == null || player.PlayerPawn.Value == null || !player.IsValid) |
| 55 | + return; |
| 56 | + |
| 57 | + string steamId = player.SteamID.ToString(); |
| 58 | + |
| 59 | + if (IsOnCooldown(steamId, out var remaining)) |
115 | 60 | { |
116 | | - if (!CanPlayerThrowCoin(player.SteamID.ToString())) |
117 | | - { |
118 | | - player.PrintToChat($"You can throw the coin only once every {ChatColors.DarkRed}10 {ChatColors.Default}seconds!"); |
119 | | - return; |
120 | | - } |
121 | | - |
122 | | - Random random = new Random(); |
123 | | - bool coinResult = random.Next(2) == 0; |
124 | | - |
125 | | - string resultString = coinResult ? "Heads" : "Tails"; |
126 | | - Server.PrintToChatAll($"Player {ChatColors.Green}{player.PlayerName}{ChatColors.Default} threw the coin and the result is {ChatColors.Green}{resultString}{ChatColors.Default}."); |
127 | | - AddOrUpdateCoinThrowInDatabase(player.SteamID.ToString(), player.PlayerName); |
128 | | - int totalThrows = GetPlayerThrows(player.SteamID.ToString()); |
129 | | - Server.PrintToChatAll($"His total number of throws is {ChatColors.Green}{totalThrows}x{ChatColors.Default}."); |
130 | | - UpdateLastCoinThrowTime(player.SteamID.ToString()); |
| 61 | + player.PrintToChat($"You can throw the coin only once every {ChatColors.DarkRed}{CooldownSeconds}{ChatColors.Default} seconds! ({remaining:F0}s left)"); |
| 62 | + return; |
131 | 63 | } |
| 64 | + |
| 65 | + string resultString = Random.Next(2) == 0 ? "Heads" : "Tails"; |
| 66 | + |
| 67 | + Server.PrintToChatAll( |
| 68 | + $"Player {ChatColors.Green}{player.PlayerName}{ChatColors.Default} threw the coin and the result is {ChatColors.Green}{resultString}{ChatColors.Default}." |
| 69 | + ); |
| 70 | + |
| 71 | + int totalThrows = _database?.IncrementPlayerThrows(steamId, player.PlayerName) ?? 0; |
| 72 | + |
| 73 | + Server.PrintToChatAll( |
| 74 | + $"His total number of throws is {ChatColors.Green}{totalThrows}x{ChatColors.Default}." |
| 75 | + ); |
| 76 | + |
| 77 | + UpdateLastCoinThrowTime(steamId); |
132 | 78 | } |
133 | 79 | } |
134 | 80 | } |
0 commit comments