Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
oqyh authored May 12, 2024
1 parent 7846f5d commit 43b674c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Config/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class ConfigData
public string MVP_VipMusicKit { get; set; }
public string MVP_OnlyAllowTheseGroupsCanMVP { get; set; }
public int MVP_AutoRemovePlayerCookieOlderThanXDays { get; set; }
public int MVP_AutoRemovePlayerMySqlOlderThanXDays { get; set; }
public string empty { get; set; }
public string Information_For_You_Dont_Delete_it { get; set; }

Expand All @@ -112,6 +113,7 @@ public ConfigData()
MVP_VipMusicKit = "@css/root,@css/admin,@css/vip,#css/admin,#css/vip";
MVP_OnlyAllowTheseGroupsCanMVP = "";
MVP_AutoRemovePlayerCookieOlderThanXDays = 7;
MVP_AutoRemovePlayerMySqlOlderThanXDays = 7;
empty = "-----------------------------------------------------------------------------------";
Information_For_You_Dont_Delete_it = " Vist [https://github.com/oqyh/cs2-MVP-Sounds-GoldKingZ/tree/main?tab=readme-ov-file#-configuration-] To Understand All Above";
}
Expand Down
51 changes: 39 additions & 12 deletions Config/mysql.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using MVP_Sounds_GoldKingZ.Config;
using MySqlConnector;
using System.Data;

Expand Down Expand Up @@ -47,13 +48,21 @@ DateAndTime DATETIME

public static async Task SaveToMySqlAsync(ulong PlayerSteamID, string MusicKit, DateTime DateAndTime, MySqlConnection connection, MySqlConnectionSettings connectionSettings)
{
string query = @"INSERT INTO PersonData (PlayerSteamID, MusicKit, DateAndTime)
int days = Configs.GetConfigData().MVP_AutoRemovePlayerMySqlOlderThanXDays;
string deleteOldRecordsQuery = $"DELETE FROM PersonData WHERE DateAndTime < NOW() - INTERVAL {days} DAY";

string insertOrUpdateQuery = @"INSERT INTO PersonData (PlayerSteamID, MusicKit, DateAndTime)
VALUES (@PlayerSteamID, @MusicKit, @DateAndTime)
ON DUPLICATE KEY UPDATE MusicKit = VALUES(MusicKit), DateAndTime = VALUES(DateAndTime)";

try
{
using (var command = new MySqlCommand(query, connection))
using (var deleteCommand = new MySqlCommand(deleteOldRecordsQuery, connection))
{
await deleteCommand.ExecuteNonQueryAsync();
}

using (var command = new MySqlCommand(insertOrUpdateQuery, connection))
{
command.Parameters.AddWithValue("@PlayerSteamID", PlayerSteamID);
command.Parameters.AddWithValue("@MusicKit", MusicKit);
Expand Down Expand Up @@ -99,26 +108,44 @@ public static async Task RemoveFromMySqlAsync(ulong PlayerSteamID, MySqlConnecti

public static async Task<PersonData> RetrievePersonDataByIdAsync(ulong targetId, MySqlConnection connection)
{
string query = "SELECT * FROM PersonData WHERE PlayerSteamID = @PlayerSteamID";
int days = Configs.GetConfigData().MVP_AutoRemovePlayerMySqlOlderThanXDays;
string deleteOldRecordsQuery = $"DELETE FROM PersonData WHERE DateAndTime < NOW() - INTERVAL {days} DAY";

string retrieveQuery = "SELECT * FROM PersonData WHERE PlayerSteamID = @PlayerSteamID";
var personData = new PersonData();

using (var command = new MySqlCommand(query, connection))
try
{
command.Parameters.AddWithValue("@PlayerSteamID", targetId);
using (var deleteCommand = new MySqlCommand(deleteOldRecordsQuery, connection))
{
await deleteCommand.ExecuteNonQueryAsync();
}

using (var reader = await command.ExecuteReaderAsync())
using (var command = new MySqlCommand(retrieveQuery, connection))
{
if (await reader.ReadAsync())
command.Parameters.AddWithValue("@PlayerSteamID", targetId);

using (var reader = await command.ExecuteReaderAsync())
{
personData = new PersonData
if (await reader.ReadAsync())
{
PlayerSteamID = Convert.ToUInt64(reader["PlayerSteamID"]),
MusicKit = Convert.ToString(reader["MusicKit"]),
DateAndTime = Convert.ToDateTime(reader["DateAndTime"])
};
personData = new PersonData
{
PlayerSteamID = Convert.ToUInt64(reader["PlayerSteamID"]),
MusicKit = Convert.ToString(reader["MusicKit"]),
DateAndTime = Convert.ToDateTime(reader["DateAndTime"])
};
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"======================== ERROR =============================");
Console.WriteLine($"Error retrieving data from MySQL: {ex.Message}");
Console.WriteLine($"======================== ERROR =============================");
throw;
}
return personData;
}
}
58 changes: 47 additions & 11 deletions MVP-Sounds-GoldKingZ.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
using System.Text;
using MySqlConnector;
using Newtonsoft.Json;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using System.Text.Json.Serialization;
using MVP_Sounds_GoldKingZ.Config;
using Microsoft.Extensions.Localization;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Menu;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Memory;
using Newtonsoft.Json;
using MVP_Sounds_GoldKingZ.Config;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Menu;
using MySqlConnector;

namespace MVP_Sounds_GoldKingZ;


public class MVPSoundsGoldKingZ : BasePlugin
{
public override string ModuleName => "Custom MVP Sounds (Custom MVP Sounds + Vips)";
public override string ModuleVersion => "1.0.1";
public override string ModuleVersion => "1.0.2";
public override string ModuleAuthor => "Gold KingZ";
public override string ModuleDescription => "https://github.com/oqyh";
internal static IStringLocalizer? Stringlocalizer;
Expand Down Expand Up @@ -286,6 +280,7 @@ private void HandleMenu(CCSPlayerController Player, ChatMenuOption option, strin
{
VoteGameModeMenu.AddMenuOption(answer, (Player, option) => HandleMenu2(Player, option, ChoosenKitKey, isVIP, isPreviewAble, choosedkit));
}
VoteGameModeMenu.AddMenuOption(Localizer["menu.back"], (Player, option) => HandleMenu2(Player, option, ChoosenKitKey, isVIP, isPreviewAble, choosedkit));
if (Configs.GetConfigData().MVP_ChangeMVPMenuFromChatToCentre)
{
if (VoteGameModeMenu is CenterHtmlMenu centerHtmlMenu)
Expand Down Expand Up @@ -399,6 +394,47 @@ private void HandleMenu2(CCSPlayerController Player, ChatMenuOption option, stri
}
}
catch { }
}else if(option.Text == Localizer["menu.back"])
{
try
{
string jsonFilePath = Path.Combine(ModuleDirectory, "../../plugins/MVP-Sounds-GoldKingZ/config/MVP_Settings.json");
string jsonData = File.ReadAllText(jsonFilePath);
var data = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(jsonData);
if (data == null) return;
IMenu VoteGameModeMenu;
if (Configs.GetConfigData().MVP_ChangeMVPMenuFromChatToCentre)
{
VoteGameModeMenu = new CenterHtmlMenu(Localizer["menu.music"]);
}
else
{
VoteGameModeMenu = new ChatMenu(Localizer["menu.music"]);
}
VoteGameModeMenu.AddMenuOption(Localizer["menu.disabled"], (Player, option) => HandleMenu(Player, option, string.Empty, false, false));
foreach (var key in data.Keys)
{
string ChoosenKitKeyy = data[key]["MVP_Kit_Name"];
bool isVIPs = data[key].ContainsKey("VIP") && bool.TryParse(data[key]["VIP"].ToString(), out bool vipValue) ? vipValue : false;
bool isPreviewAblee = data[key].ContainsKey("CanBePreview") && bool.TryParse(data[key]["CanBePreview"].ToString(), out bool PreviewValue) ? PreviewValue : false;
VoteGameModeMenu.AddMenuOption(ChoosenKitKeyy, (Player, option) => HandleMenu(Player, option, key, isVIPs, isPreviewAblee));
}
if (Configs.GetConfigData().MVP_ChangeMVPMenuFromChatToCentre)
{
if (VoteGameModeMenu is CenterHtmlMenu centerHtmlMenu)
{
MenuManager.OpenCenterHtmlMenu(this, Player, centerHtmlMenu);
}
}
else
{
if (VoteGameModeMenu is ChatMenu chatMenu)
{
MenuManager.OpenChatMenu(Player, chatMenu);
}
}

}catch{}
}
}

Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"menu.are.you.sure": "You Want [ {0} ] ?",
"menu.answer.yes": "Yes",
"menu.answer.no": "No, Preview It First",
"menu.back": "-> Back",
"menu.disabled": "Disabled/Remove MVP"
}

0 comments on commit 43b674c

Please sign in to comment.