diff --git a/Store_Modules/Store_Daily/MySqlConnector.dll b/Store_Modules/Store_Daily/MySqlConnector.dll new file mode 100644 index 0000000..ea3237d Binary files /dev/null and b/Store_Modules/Store_Daily/MySqlConnector.dll differ diff --git a/Store_Modules/Store_Daily/Newtonsoft.Json.dll b/Store_Modules/Store_Daily/Newtonsoft.Json.dll new file mode 100644 index 0000000..d035c38 Binary files /dev/null and b/Store_Modules/Store_Daily/Newtonsoft.Json.dll differ diff --git a/Store_Modules/Store_Daily/cs2-store-daily.cs b/Store_Modules/Store_Daily/cs2-store-daily.cs new file mode 100644 index 0000000..3405a98 --- /dev/null +++ b/Store_Modules/Store_Daily/cs2-store-daily.cs @@ -0,0 +1,205 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core.Attributes.Registration; +using CounterStrikeSharp.API.Modules.Commands; +using StoreApi; +using MySqlConnector; +using System.Collections.Concurrent; +using System.Text.Json.Serialization; + +namespace Store_DailyRewards; + +public class Store_DailyRewardsConfig : BasePluginConfig +{ + [JsonPropertyName("daily_rewards")] + public Dictionary DailyRewards { get; set; } = new() { { 1, 1000 }, { 2, 2000 }, { 3, 3000 }, { 4, 4000 }, { 5, 5000 }, { 6, 6000 }, { 7, 7000 } }; + + [JsonPropertyName("daily_commands")] + public List ClaimDailyCommands { get; set; } = ["daily", "claimdaily"]; + + [JsonPropertyName("daily_message_type")] + public int DailyMessageType { get; set; } = 1; + + [JsonPropertyName("database_host")] + public string DatabaseHost { get; set; } = "localhost"; + + [JsonPropertyName("database_port")] + public int DatabasePort { get; set; } = 3306; + + [JsonPropertyName("database_name")] + public string DatabaseName { get; set; } = "name"; + + [JsonPropertyName("database_user")] + public string DatabaseUser { get; set; } = "root"; + + [JsonPropertyName("database_password")] + public string DatabasePassword { get; set; } = "password"; +} + +public class Store_DailyRewards : BasePlugin, IPluginConfig +{ + public override string ModuleName => "Store Module [Daily Rewards]"; + public override string ModuleVersion => "0.0.1"; + public override string ModuleAuthor => "Nathy"; + + public IStoreApi? StoreApi { get; set; } + public Store_DailyRewardsConfig Config { get; set; } = new(); + + public override void OnAllPluginsLoaded(bool hotReload) + { + StoreApi = IStoreApi.Capability.Get() ?? throw new Exception("StoreApi could not be located."); + InitializeDatabase(); + CreateCommands(); + } + + public void OnConfigParsed(Store_DailyRewardsConfig config) + { + Config = config; + } + + private void CreateCommands() + { + foreach (var cmd in Config.ClaimDailyCommands) + { + AddCommand($"css_{cmd}", "Claims daily reward", Command_ClaimReward); + } + } + + public void Command_ClaimReward(CCSPlayerController? player, CommandInfo info) + { + if (player == null) return; + + if (StoreApi == null) throw new Exception("StoreApi could not be located."); + + using (var connection = new MySqlConnection(GetConnectionString())) + { + connection.Open(); + + string query = "SELECT LastLogin, ConsecutiveDays FROM store_daily WHERE SteamID = @SteamID"; + using (var command = new MySqlCommand(query, connection)) + { + command.Parameters.AddWithValue("@SteamID", player.SteamID.ToString()); + + using (var reader = command.ExecuteReader()) + { + if (reader.Read()) + { + DateTime lastLogin = reader.GetDateTime("LastLogin"); + int consecutiveDays = reader.GetInt32("ConsecutiveDays"); + DateTime today = DateTime.Now.Date; + + if (lastLogin.Date == today) + { + DateTime nextClaimTime = lastLogin.AddDays(1); + TimeSpan timeUntilNextClaim = nextClaimTime - DateTime.Now; + + player.PrintToChat(Localizer["Prefix"] + Localizer["Already claimed todays reward", timeUntilNextClaim.Hours, timeUntilNextClaim.Minutes]); + } + else + { + if (lastLogin.Date == today.AddDays(-1)) + { + consecutiveDays++; + } + else + { + consecutiveDays = 1; + } + + int reward = Config.DailyRewards.ContainsKey(consecutiveDays) ? Config.DailyRewards[consecutiveDays] : Config.DailyRewards[1]; + StoreApi.GivePlayerCredits(player, reward); + + reader.Close(); + + string updateQuery = "UPDATE store_daily SET LastLogin = @LastLogin, ConsecutiveDays = @ConsecutiveDays WHERE SteamID = @SteamID"; + using (var updateCommand = new MySqlCommand(updateQuery, connection)) + { + updateCommand.Parameters.AddWithValue("@LastLogin", today); + updateCommand.Parameters.AddWithValue("@ConsecutiveDays", consecutiveDays); + updateCommand.Parameters.AddWithValue("@SteamID", player.SteamID.ToString()); + updateCommand.ExecuteNonQuery(); + } + + if (Config.DailyMessageType == 1) + { + player.PrintToChat(Localizer["Prefix"] + Localizer["You received your daily reward", reward, consecutiveDays]); + } + else if (Config.DailyMessageType == 2) + { + PrintDailyRewards(player, consecutiveDays); + } + } + } + else + { + reader.Close(); + + string insertQuery = "INSERT INTO store_daily (SteamID, LastLogin, ConsecutiveDays) VALUES (@SteamID, @LastLogin, @ConsecutiveDays)"; + using (var insertCommand = new MySqlCommand(insertQuery, connection)) + { + insertCommand.Parameters.AddWithValue("@SteamID", player.SteamID.ToString()); + insertCommand.Parameters.AddWithValue("@LastLogin", DateTime.Now.Date); + insertCommand.Parameters.AddWithValue("@ConsecutiveDays", 1); + insertCommand.ExecuteNonQuery(); + } + + int reward = Config.DailyRewards[1]; + StoreApi.GivePlayerCredits(player, reward); + + if (Config.DailyMessageType == 1) + { + player.PrintToChat(Localizer["Prefix"] + Localizer["You received your first daily reward", reward]); + } + else if (Config.DailyMessageType == 2) + { + PrintDailyRewards(player, 1); + } + } + } + } + } + } + + private void PrintDailyRewards(CCSPlayerController player, int consecutiveDays) + { + int startDay = ((consecutiveDays - 1) / 7) * 7 + 1; + int endDay = startDay + 6; + + player.PrintToChat(Localizer["Detailed daily reward first line"]); + + for (int day = startDay; day <= endDay; day++) + { + string messageKey = (day <= consecutiveDays) ? "Detailed daily reward claimed" : "Detailed daily reward not claimed"; + int reward = Config.DailyRewards.ContainsKey(day) ? Config.DailyRewards[day] : 0; + player.PrintToChat(Localizer["Prefix"] + Localizer[messageKey, day, reward]); + } + + player.PrintToChat(Localizer["Detailed daily reward last line"]); + } + + private void InitializeDatabase() + { + using (var connection = new MySqlConnection(GetConnectionString())) + { + connection.Open(); + + string createTableQuery = @" + CREATE TABLE IF NOT EXISTS store_daily ( + id INT AUTO_INCREMENT PRIMARY KEY, + SteamID VARCHAR(255) NOT NULL, + LastLogin DATETIME NOT NULL, + ConsecutiveDays INT NOT NULL + )"; + + using (var command = new MySqlCommand(createTableQuery, connection)) + { + command.ExecuteNonQuery(); + } + } + } + + private string GetConnectionString() + { + return $"Server={Config.DatabaseHost};Port={Config.DatabasePort};Database={Config.DatabaseName};Uid={Config.DatabaseUser};Pwd={Config.DatabasePassword};"; + } +} diff --git a/Store_Modules/Store_Daily/cs2-store-daily.csproj b/Store_Modules/Store_Daily/cs2-store-daily.csproj new file mode 100644 index 0000000..894649f --- /dev/null +++ b/Store_Modules/Store_Daily/cs2-store-daily.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/Store_Modules/Store_Daily/lang/en.json b/Store_Modules/Store_Daily/lang/en.json new file mode 100644 index 0000000..816d157 --- /dev/null +++ b/Store_Modules/Store_Daily/lang/en.json @@ -0,0 +1,12 @@ +{ + "Prefix": "{red}[Daily] {white}", + + "Already claimed todays reward": "You have already claimed today's reward. You can claim your next reward in {green}{0} {white}hours and {green}{1} {white}minutes", + "You received your daily reward": "You received your daily reward of {green}{0} {white}credits for {green}{1} {white}consecutive days", + "You received your first daily reward": "You received your daily reward of {green}{0} {white}credits for your first day. Login daily to receive more!", + + "Detailed daily reward first line": "=============================", + "Detailed daily reward claimed": "Day {blue}{0} {white}- {gold}{1} {white}credits ({green}✔{white})", + "Detailed daily reward not claimed": "Day {blue}{0} {white}- {gold}{1} {white}credits ({red}✘{white})", + "Detailed daily reward last line": "=============================" +} \ No newline at end of file diff --git a/Store_Modules/Store_Daily/lang/pt-br.json b/Store_Modules/Store_Daily/lang/pt-br.json new file mode 100644 index 0000000..c2b21c1 --- /dev/null +++ b/Store_Modules/Store_Daily/lang/pt-br.json @@ -0,0 +1,12 @@ +{ + "Prefix": "{red}[Daily] {white}", + + "Already claimed todays reward": "Você já pegou a recompensa de hoje. Você pode pegar sua próxima recompensa em {green}{0} {white}horas e {green}{1} {white}minutos", + "You received your daily reward": "Você recebeu {green}{0} {white}créditos por entrar {green}{1} {white}dias consecutivos", + "You received your first daily reward": "Você recebeu {green}{0} {white}créditos por entrar no seu primeiro dia. Logue diariamente para receber mais!", + + "Detailed daily reward first line": "=============================", + "Detailed daily reward claimed": "Dia {blue}{0} {white}- {gold}{1} {white}créditos ({green}✔{white})", + "Detailed daily reward not claimed": "dia {blue}{0} {white}- {gold}{1} {white}créditos ({red}✘{white})", + "Detailed daily reward last line": "=============================" +} \ No newline at end of file