Refactor skill lookup logic and vote system, update language files

Refactored command handling to improve player and skill lookup logic, replaced some list operations with direct queries, and updated method signatures for clarity and null safety. The vote system was reworked to support more robust vote tracking, cooldowns, and localized chat messages. Language files were updated to add new vote-related translations. Various code style and nullability improvements were made throughout the plugin.
This commit is contained in:
Juzlus 2025-09-04 04:53:23 +02:00
parent 54b265d43d
commit e33975fe8b
117 changed files with 1532 additions and 1674 deletions

View file

@ -11,14 +11,13 @@ namespace jRandomSkills
public static class Config
{
private static readonly string configPath = Path.Combine(Instance.ModuleDirectory, "Config.json");
public static ConfigModel config;
private static FileSystemWatcher fileWatcher;
private static ConfigModel config = LoadConfig();
private static FileSystemWatcher? fileWatcher;
public static ConfigModel LoadedConfig => config;
public static void Initialize()
{
config = LoadConfig();
SetupFileWatcher();
}
@ -38,21 +37,24 @@ namespace jRandomSkills
try
{
var root = JsonConvert.DeserializeObject<JObject>(json);
if (root == null) return config;
var settings = root["Settings"];
if (settings != null) JsonConvert.PopulateObject(settings.ToString(), config.Settings);
var skillsArray = (JArray)root["SkillsInfo"];
foreach (var skillObj in skillsArray)
{
string name = skillObj["Name"]?.ToString();
var instance = config.SkillsInfo.FirstOrDefault(x => x.Name == name);
if (instance != null) JsonConvert.PopulateObject(skillObj.ToString(), instance);
}
var skillsArray = (JArray?)root["SkillsInfo"];
if (skillsArray != null)
foreach (var skillObj in skillsArray)
{
var name = skillObj["Name"];
if (name == null) continue;
var instance = config.SkillsInfo.FirstOrDefault(x => x.Name == name.ToString());
if (instance != null) JsonConvert.PopulateObject(skillObj.ToString(), instance);
}
}
catch (Exception ex)
catch
{
Instance.Logger.LogError($"Error when loading the config file: {ex.Message}");
Instance.Logger.LogError("Error when loading the config file.");
}
return config;
@ -65,15 +67,17 @@ namespace jRandomSkills
string json = JsonConvert.SerializeObject(config, Formatting.Indented);
File.WriteAllText(configPath, json);
}
catch (Exception ex)
catch
{
Instance.Logger.LogError($"Error when saving the config file: {ex.Message}");
Instance.Logger.LogError("Error when saving the config file.");
}
}
private static void SetupFileWatcher()
{
fileWatcher = new FileSystemWatcher(Path.GetDirectoryName(configPath))
string? path = Path.GetDirectoryName(configPath);
if (string.IsNullOrEmpty(path)) return;
fileWatcher = new FileSystemWatcher(path)
{
Filter = Path.GetFileName(configPath),
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size
@ -86,6 +90,7 @@ namespace jRandomSkills
public static T GetValue<T>(object skill, string key)
{
var skillConfig = config.SkillsInfo.FirstOrDefault(s => s.Name == skill.ToString());
if (skillConfig == null) return default!;
var prop = skillConfig.GetType().GetProperty(key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (prop != null)
@ -146,6 +151,7 @@ namespace jRandomSkills
public string PauseCommands { get; set; }
public string HealCommands { get; set; }
public string SetScoreCommands { get; set; }
public VotingCommand[] VotingCommand { get; set; }
public Settings()
{
@ -159,34 +165,41 @@ namespace jRandomSkills
SetSkillCommands = "ustawskill, setskill, definirhabilidade, configurarhabilidade, 设置技能, 配置技能";
SkillsListCommands = "supermoc, skille, listamocy, supermoce, skills, listaHabilidades, habilidades, 技能列表, 超能力列表";
UseSkillCommands = "t, useSkill, usarHabilidade, 技能使用, 使用技能";
ChangeMapCommands = "map, mapa, changemap, zmienmape, zmienmape, mudarMapa, trocarMapa, 更换地图, 更改地图";
StartGameCommands = "start, go, começar, iniciar, 开始, 启动";
ConsoleCommands = "console, sv, 控制台, 服务器";
SwapCommands = "swap, zmiana, trocar, 交换, 切换";
ShuffleCommands = "shuffle, embaralhar, 随机排序, 洗牌";
PauseCommands = "pause, unpause, pausar, despausar, 暂停, 恢复";
HealCommands = "heal, ulecz, curar, tratar, 治疗, 治愈";
SetScoreCommands = "setscore, wynik, definirPontuacao, configurarPontos, 设置分数, 调整分数";
ConsoleCommands = "console, sv, 控制台, 服务器";
VotingCommand = [
new VotingCommand("StartGameCommands", true, "start, go, começar, iniciar, 开始, 启动", "@jRandmosSkills/admin", 15, 60, 15, 500, 2),
new VotingCommand("ChangeMapCommands", true, "map, mapa, changemap, zmienmape, zmienmape, mudarMapa, trocarMapa, 更换地图, 更改地图", "@jRandmosSkills/admin", 25, 90, 15, 500, 2),
new VotingCommand("SwapCommands", true, "swap, zmiana, trocar, 交换, 切换", "@jRandmosSkills/admin", 15, 90, 15, 20, 2),
new VotingCommand("ShuffleCommands", true, "shuffle, embaralhar, 随机排序, 洗牌", "@jRandmosSkills/admin", 15, 90, 15, 90, 2),
new VotingCommand("PauseCommands", true, "pause, unpause, pausar, despausar, 暂停, 恢复", "@jRandmosSkills/admin", 15, 60, 15, 90, 2),
new VotingCommand("SetScoreCommands", true, "setscore, wynik, definirPontuacao, configurarPontos, 设置分数, 调整分数", "@jRandmosSkills/root", 15, 90, 15, 90, 2),
];
}
}
public class DefaultSkillInfo
public class VotingCommand(string name, bool enableVoting, string alias, string permissions, float timeToVote, float percentagesToSuccess, float timeToNextVoting, float timeToNextSameVoting, int minimumPlayersToStartVoting)
{
public bool NeedsTeammates { get; set; }
public int OnlyTeam { get; set; }
public string Color { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
public string Name { get; set; } = name;
public bool EnableVoting { get; set; } = enableVoting;
public string Alias { get; set; } = alias;
public string Permissions { get; set; } = permissions;
public float TimeToVote { get; set; } = timeToVote;
public float PercentagesToSuccess { get; set; } = percentagesToSuccess;
public float TimeToNextVoting { get; set; } = timeToNextVoting;
public float TimeToNextSameVoting { get; set; } = timeToNextSameVoting;
public int MinimumPlayersToStartVoting { get; set; } = minimumPlayersToStartVoting;
}
public DefaultSkillInfo(Skills skill, bool active = true, string color = "#ffffff", CsTeam onlyTeam = CsTeam.None, bool needsTeammates = false)
{
Name = skill.ToString();
Active = active;
Color = color;
OnlyTeam = (int)onlyTeam;
NeedsTeammates = needsTeammates;
}
public class DefaultSkillInfo(Skills skill, bool active = true, string color = "#ffffff", CsTeam onlyTeam = CsTeam.None, bool needsTeammates = false)
{
public bool NeedsTeammates { get; set; } = needsTeammates;
public int OnlyTeam { get; set; } = (int)onlyTeam;
public string Color { get; set; } = color;
public bool Active { get; set; } = active;
public string Name { get; set; } = skill.ToString();
}
public enum GameModes

View file

@ -6,7 +6,7 @@ namespace jRandomSkills.src.utils
public static class Localization
{
private static readonly string path = Path.Combine(jRandomSkills.Instance.ModuleDirectory, "lang");
private static readonly Dictionary<string, Dictionary<string, string>> _translations = new Dictionary<string, Dictionary<string, string>>();
private static readonly Dictionary<string, Dictionary<string, string>> _translations = [];
private static string langCode = "pl";
public static void Load()
@ -18,7 +18,7 @@ namespace jRandomSkills.src.utils
private static void SetLangCode()
{
langCode = Config.config.Settings.LangCode;
langCode = Config.LoadedConfig.Settings.LangCode;
}
private static void LoadAllLanguages()
@ -46,7 +46,7 @@ namespace jRandomSkills.src.utils
public static string GetTranslation(string key, params object[] args)
{
if (_translations.TryGetValue(langCode, out var langDict) && langDict.TryGetValue(key, out var translation))
if (args.Length != 0 && args[0] == "welcome")
if (args.Length != 0 && args[0].ToString() == "welcome")
return translation;
else
return string.Format(translation, args).Replace("CHATCOLORS.RED", ChatColors.Red.ToString());

View file

@ -12,7 +12,7 @@ namespace jRandomSkills
{
public static class SkillUtils
{
private static MemoryFunctionVoid<nint, float, RoundEndReason, nint, nint> TerminateRoundFunc = new(GameData.GetSignature("CCSGameRules_TerminateRound"));
private static readonly MemoryFunctionVoid<nint, float, RoundEndReason, nint, nint> TerminateRoundFunc = new(GameData.GetSignature("CCSGameRules_TerminateRound"));
public static void PrintToChat(CCSPlayerController player, string msg, bool isError)
{
@ -31,7 +31,11 @@ namespace jRandomSkills
string? itemString = EnumUtils.GetEnumMemberAttributeValue(item);
if (string.IsNullOrWhiteSpace(itemString)) return;
var exists = player.PlayerPawn.Value.WeaponServices.MyWeapons.FirstOrDefault(w => w.Value.DesignerName == itemString);
if (player == null || !player.IsValid || player.PlayerPawn.Value == null || !player.PlayerPawn.Value.IsValid) return;
if (player.PlayerPawn.Value.WeaponServices == null) return;
var exists = player.PlayerPawn.Value.WeaponServices.MyWeapons
.FirstOrDefault(w => w != null && w.IsValid && w.Value != null && w.Value.IsValid && w.Value.DesignerName == itemString);
if (exists == null)
for (int i = 0; i < count; i++)
player.GiveNamedItem(item);
@ -62,9 +66,9 @@ namespace jRandomSkills
return new Vector(x, y, z);
}
public static void TakeHealth(CCSPlayerPawn pawn, int damage)
public static void TakeHealth(CCSPlayerPawn? pawn, int damage)
{
if (pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE)
if (pawn == null || !pawn.IsValid || pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE)
return;
int newHealth = (int)(pawn.Health - damage);
@ -78,9 +82,9 @@ namespace jRandomSkills
});
}
public static void AddHealth(CCSPlayerPawn pawn, int extraHealth, int maxHealth = 100)
public static void AddHealth(CCSPlayerPawn? pawn, int extraHealth, int maxHealth = 100)
{
if (pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE)
if (pawn == null || !pawn.IsValid || pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE)
return;
int newHealth = (int)(pawn.Health + extraHealth);
@ -110,12 +114,14 @@ namespace jRandomSkills
public static void SetTeamScores(short ctScore, short tScore, RoundEndReason roundEndReason)
{
if (jRandomSkills.Instance == null || jRandomSkills.Instance.GameRules == null) return;
UpdateServerTeamScores(ctScore, tScore);
TerminateRoundFunc.Invoke(jRandomSkills.Instance.GameRules.Handle, 5f, roundEndReason, 0, 0);
}
public static void TerminateRound(CsTeam winnerTeam)
{
if (jRandomSkills.Instance == null || jRandomSkills.Instance.GameRules == null) return;
var teams = Utilities.FindAllEntitiesByDesignerName<CCSTeam>("cs_team_manager");
var ctTeam = teams.First(t => t.IsValid && (CsTeam)t.TeamNum == CsTeam.CounterTerrorist);
var tTeams = teams.First(t => t.IsValid && (CsTeam)t.TeamNum == CsTeam.Terrorist);
@ -130,6 +136,7 @@ namespace jRandomSkills
private static void UpdateServerTeamScores(short ctScore, short tScore)
{
if (jRandomSkills.Instance == null || jRandomSkills.Instance.GameRules == null) return;
int totalRoundsPlayed = ctScore + tScore;
int maxRounds = ConVar.Find("mp_maxrounds")?.GetPrimitiveValue<int>() ?? 24;
int halfRounds = maxRounds / 2;