153 lines
No EOL
6.3 KiB
C#
153 lines
No EOL
6.3 KiB
C#
using CounterStrikeSharp.API;
|
|
using CounterStrikeSharp.API.Core;
|
|
using CounterStrikeSharp.API.Modules.Utils;
|
|
using jRandomSkills.src.player;
|
|
using jRandomSkills.src.utils;
|
|
using System.Collections.Immutable;
|
|
using static jRandomSkills.jRandomSkills;
|
|
|
|
namespace jRandomSkills
|
|
{
|
|
public class RandomWeapon : ISkill
|
|
{
|
|
private const Skills skillName = Skills.RandomWeapon;
|
|
private static readonly float timerCooldown = Config.GetValue<float>(skillName, "cooldown");
|
|
private static readonly Dictionary<ulong, PlayerSkillInfo> SkillPlayerInfo = [];
|
|
|
|
private static readonly string[] pistols = [ "weapon_deagle", "weapon_revolver", "weapon_glock", "weapon_usp_silencer",
|
|
"weapon_cz75a", "weapon_fiveseven", "weapon_p250", "weapon_tec9", "weapon_elite", "weapon_hkp2000" ];
|
|
|
|
private static readonly string[] rifles = [ "weapon_mp9", "weapon_mac10", "weapon_bizon", "weapon_mp7", "weapon_ump45", "weapon_p90",
|
|
"weapon_mp5sd", "weapon_famas", "weapon_galilar", "weapon_m4a1", "weapon_m4a1_silencer", "weapon_ak47",
|
|
"weapon_aug", "weapon_sg553", "weapon_ssg08", "weapon_awp", "weapon_scar20", "weapon_g3sg1",
|
|
"weapon_nova", "weapon_xm1014", "weapon_mag7", "weapon_sawedoff", "weapon_m249", "weapon_negev" ];
|
|
|
|
public static void LoadSkill()
|
|
{
|
|
SkillUtils.RegisterSkill(skillName, Config.GetValue<string>(skillName, "color"));
|
|
}
|
|
|
|
public static void NewRound()
|
|
{
|
|
SkillPlayerInfo.Clear();
|
|
}
|
|
|
|
public static void OnTick()
|
|
{
|
|
foreach (var player in Utilities.GetPlayers())
|
|
{
|
|
var playerInfo = Instance.SkillPlayer.FirstOrDefault(p => p.SteamID == player.SteamID);
|
|
if (playerInfo?.Skill == skillName)
|
|
if (SkillPlayerInfo.TryGetValue(player.SteamID, out var skillInfo))
|
|
UpdateHUD(player, skillInfo);
|
|
}
|
|
}
|
|
|
|
public static void EnableSkill(CCSPlayerController player)
|
|
{
|
|
SkillPlayerInfo[player.SteamID] = new PlayerSkillInfo
|
|
{
|
|
SteamID = player.SteamID,
|
|
CanUse = true,
|
|
Cooldown = DateTime.MinValue,
|
|
};
|
|
}
|
|
|
|
public static void DisableSkill(CCSPlayerController player)
|
|
{
|
|
SkillPlayerInfo.Remove(player.SteamID);
|
|
}
|
|
|
|
private static void UpdateHUD(CCSPlayerController player, PlayerSkillInfo skillInfo)
|
|
{
|
|
float cooldown = 0;
|
|
if (skillInfo != null)
|
|
{
|
|
float time = (int)(skillInfo.Cooldown.AddSeconds(timerCooldown) - DateTime.Now).TotalSeconds;
|
|
cooldown = Math.Max(time, 0);
|
|
|
|
if (cooldown == 0 && skillInfo?.CanUse == false)
|
|
skillInfo.CanUse = true;
|
|
}
|
|
|
|
var skillData = SkillData.Skills.FirstOrDefault(s => s.Skill == skillName);
|
|
if (skillData == null) return;
|
|
|
|
string infoLine = $"<font class='fontSize-l' class='fontWeight-Bold' color='#FFFFFF'>{Localization.GetTranslation("your_skill")}:</font> <br>";
|
|
string skillLine = $"<font class='fontSize-l' class='fontWeight-Bold' color='{skillData.Color}'>{skillData.Name}</font> <br>";
|
|
string remainingLine = cooldown != 0 ? $"<font class='fontSize-m' color='#FFFFFF'>{Localization.GetTranslation("hud_info", $"<font color='#FF0000'>{cooldown}</font>")}</font> <br>" : "";
|
|
|
|
var hudContent = infoLine + skillLine + remainingLine;
|
|
player.PrintToCenterHtml(hudContent);
|
|
}
|
|
|
|
public static void UseSkill(CCSPlayerController player)
|
|
{
|
|
var playerPawn = player.PlayerPawn.Value;
|
|
if (playerPawn?.CBodyComponent == null) return;
|
|
|
|
if (SkillPlayerInfo.TryGetValue(player.SteamID, out var skillInfo))
|
|
{
|
|
if (!player.IsValid || !player.PawnIsAlive) return;
|
|
if (skillInfo.CanUse)
|
|
{
|
|
skillInfo.CanUse = false;
|
|
skillInfo.Cooldown = DateTime.Now;
|
|
RemoveAndGiveWeapon(player);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void RemoveAndGiveWeapon(CCSPlayerController player)
|
|
{
|
|
var pawn = player.PlayerPawn.Value;
|
|
if (pawn == null || !pawn.IsValid || pawn.WeaponServices == null) return;
|
|
|
|
List<string> playerWeapons = [];
|
|
foreach (var item in pawn.WeaponServices.MyWeapons)
|
|
if (item != null && item.IsValid && item.Value != null && item.Value.IsValid && !string.IsNullOrEmpty(item.Value.DesignerName))
|
|
playerWeapons.Add(item.Value.DesignerName);
|
|
|
|
if (playerWeapons.Count == 0)
|
|
return;
|
|
|
|
List<string> weaponList = new(pistols.Concat(rifles));
|
|
weaponList.RemoveAll(w => playerWeapons.Contains(w));
|
|
|
|
if (weaponList.Count == 0)
|
|
return;
|
|
|
|
string weapon = weaponList[Instance.Random.Next(weaponList.Count)];
|
|
bool isPistol = pistols.Contains(weapon);
|
|
|
|
string? weaponToRemove = playerWeapons.FirstOrDefault(itemName =>
|
|
(isPistol && pistols.Contains(itemName)) || (!isPistol && rifles.Contains(itemName)));
|
|
|
|
if (!string.IsNullOrEmpty(weaponToRemove))
|
|
{
|
|
foreach (var item in pawn.WeaponServices.MyWeapons)
|
|
{
|
|
if (item != null && item.IsValid && item.Value != null && item.Value.IsValid && item.Value.DesignerName == weaponToRemove)
|
|
item.Value.AcceptInput("Kill");
|
|
}
|
|
}
|
|
|
|
Instance.AddTimer(.1f, () =>
|
|
{
|
|
player.GiveNamedItem(weapon);
|
|
});
|
|
}
|
|
|
|
public class PlayerSkillInfo
|
|
{
|
|
public ulong SteamID { get; set; }
|
|
public bool CanUse { get; set; }
|
|
public DateTime Cooldown { get; set; }
|
|
}
|
|
|
|
public class SkillConfig(Skills skill = skillName, bool active = true, string color = "#e0873a", CsTeam onlyTeam = CsTeam.None, bool needsTeammates = false, float cooldown = 15f) : Config.DefaultSkillInfo(skill, active, color, onlyTeam, needsTeammates)
|
|
{
|
|
public float Cooldown { get; set; } = cooldown;
|
|
}
|
|
}
|
|
} |