Skip to content

Commit

Permalink
Merge pull request #213 from daffyyyy/main
Browse files Browse the repository at this point in the history
2.3a
  • Loading branch information
daffyyyy committed Mar 12, 2024
2 parents 8cb9563 + a6575e9 commit 5626c32
Show file tree
Hide file tree
Showing 92 changed files with 699 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ jobs:
run: cp website/data/gloves.json ${{ env.OUTPUT_PATH }}/gloves.json
- name: Copy agents.json
run: cp website/data/agents.json ${{ env.OUTPUT_PATH }}/agents.json
- name: Copy music.json
run: cp website/data/music.json ${{ env.OUTPUT_PATH }}/music.json
- name: Zip
run: zip -r "${{ env.PROJECT_NAME }}.zip" "${{ env.OUTPUT_PATH }}" gamedata/
- name: Clean files Website
Expand Down
133 changes: 133 additions & 0 deletions Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{
_ = Task.Run(async () => await weaponSync.GetAgentFromDatabase(playerInfo));
}
if (Config.Additional.MusicEnabled)
{
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
}

RefreshGloves(player);
RefreshWeapons(player);
Expand Down Expand Up @@ -94,6 +98,12 @@ private void OnCommandWS(CCSPlayerController? player, CommandInfo command)
player!.Print(Localizer["wp_info_agent"]);
}

if (Config.Additional.MusicEnabled)
if (!string.IsNullOrEmpty(Localizer["wp_info_music"]))
{
player!.Print(Localizer["wp_info_music"]);
}

if (Config.Additional.KnifeEnabled)
if (!string.IsNullOrEmpty(Localizer["wp_info_knife"]))
{
Expand Down Expand Up @@ -562,5 +572,128 @@ private void SetupAgentsMenu()
}
});
}

private void SetupMusicMenu()
{
var musicSelectionMenu = new ChatMenu(Localizer["wp_music_menu_title"]);
musicSelectionMenu.PostSelectAction = PostSelectAction.Close;

var handleMusicSelection = (CCSPlayerController? player, ChatMenuOption option) =>
{
if (!Utility.IsPlayerValid(player) || player is null) return;
string selectedPaintName = option.Text;
var selectedMusic = musicList.FirstOrDefault(g => g.ContainsKey("name") && g["name"]?.ToString() == selectedPaintName);
if (selectedMusic != null)
{
if (
selectedMusic != null &&
selectedMusic.ContainsKey("id") &&
selectedMusic.ContainsKey("name") &&
int.TryParse(selectedMusic["id"]?.ToString(), out int paint)
)
{
if (Config.Additional.ShowSkinImage)
{
string image = selectedMusic["image"]?.ToString() ?? "";
PlayerWeaponImage[player.Slot] = image;
AddTimer(2.0f, () => PlayerWeaponImage.Remove(player.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Slot = player.Slot,
Index = (int)player.Index,
SteamId = player.SteamID.ToString(),
Name = player.PlayerName,
IpAddress = player.IpAddress?.Split(":")[0]
};
if (paint != 0)
{
g_playersMusic[player.Slot] = (ushort)paint;
}
else
{
g_playersMusic[player.Slot] = 0;
}
if (!string.IsNullOrEmpty(Localizer["wp_music_menu_select"]))
{
player!.Print(Localizer["wp_music_menu_select", selectedPaintName]);
}
if (weaponSync != null)
{
_ = Task.Run(async () =>
{
await weaponSync.SyncMusicToDatabase(playerInfo, (ushort)paint);
});
}
//RefreshGloves(player);
}
}
else
{
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Slot = player.Slot,
Index = (int)player.Index,
SteamId = player.SteamID.ToString(),
Name = player.PlayerName,
IpAddress = player.IpAddress?.Split(":")[0]
};
g_playersMusic[player.Slot] = 0;
if (!string.IsNullOrEmpty(Localizer["wp_music_menu_select"]))
{
player!.Print(Localizer["wp_music_menu_select", Localizer["None"]]);
}
if (weaponSync != null)
{
_ = Task.Run(async () =>
{
await weaponSync.SyncMusicToDatabase(playerInfo, 0);
});
}
}
};

musicSelectionMenu.AddMenuOption(Localizer["None"], handleMusicSelection);
// Add weapon options to the weapon selection menu
foreach (var musicObject in musicList)
{
string paintName = musicObject["name"]?.ToString() ?? "";

if (paintName.Length > 0)
musicSelectionMenu.AddMenuOption(paintName, handleMusicSelection);
}

// Command to open the weapon selection menu for players
AddCommand($"css_{Config.Additional.CommandMusic}", "Music selection menu", (player, info) =>
{
if (!Utility.IsPlayerValid(player) || !g_bCommandsAllowed) return;
if (player == null || player.UserId == null) return;
if (player != null && !commandsCooldown.TryGetValue(player.Slot, out DateTime cooldownEndTime) ||
player != null && DateTime.UtcNow >= (commandsCooldown.TryGetValue(player.Slot, out cooldownEndTime) ? cooldownEndTime : DateTime.UtcNow))
{
commandsCooldown[player.Slot] = DateTime.UtcNow.AddSeconds(Config.CmdRefreshCooldownSeconds);
MenuManager.OpenChatMenu(player, musicSelectionMenu);
return;
}
if (!string.IsNullOrEmpty(Localizer["wp_command_cooldown"]))
{
player!.Print(Localizer["wp_command_cooldown"]);
}
});
}
}
}
6 changes: 6 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class Additional
[JsonPropertyName("GloveEnabled")]
public bool GloveEnabled { get; set; } = true;

[JsonPropertyName("MusicEnabled")]
public bool MusicEnabled { get; set; } = true;

[JsonPropertyName("AgentEnabled")]
public bool AgentEnabled { get; set; } = true;

Expand All @@ -26,6 +29,9 @@ public class Additional
[JsonPropertyName("CommandKnife")]
public string CommandKnife { get; set; } = "knife";

[JsonPropertyName("CommandMusic")]
public string CommandMusic { get; set; } = "music";

[JsonPropertyName("CommandGlove")]
public string CommandGlove { get; set; } = "gloves";

Expand Down
31 changes: 22 additions & 9 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public HookResult OnClientFullConnect(EventPlayerConnectFull @event, GameEventIn
{
_ = Task.Run(async () => await weaponSync.GetAgentFromDatabase(playerInfo));
}
if (Config.Additional.MusicEnabled)
{
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
}
}
catch (Exception)
{
Expand Down Expand Up @@ -85,6 +89,10 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
{
g_playersAgent.TryRemove(player.Slot, out _);
}
if (Config.Additional.MusicEnabled)
{
g_playersMusic.TryRemove(player.Slot, out _);
}

commandsCooldown.Remove(player.Slot);

Expand Down Expand Up @@ -204,6 +212,7 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)

g_knifePickupCount[player.Slot] = 0;

GivePlayerMusicKit(player);
GivePlayerAgent(player);
Server.NextFrame(() =>
{
Expand Down Expand Up @@ -259,12 +268,14 @@ public void OnEntityCreated(CEntityInstance entity)
var weapon = new CBasePlayerWeapon(entity.Handle);
if (weapon == null || !weapon.IsValid || weapon.OwnerEntity.Value == null) return;
SteamID? _steamid = (SteamID)weapon.OriginalOwnerXuidLow;
CCSWeaponBaseGun gun = weapon.As<CCSWeaponBaseGun>();
CCSPlayerController? player = null;
try
{
SteamID? _steamid = null;
if (weapon.OriginalOwnerXuidLow > 0)
_steamid = new(weapon.OriginalOwnerXuidLow);
CCSPlayerController? player = null;
if (_steamid != null && _steamid.IsValid())
{
Expand All @@ -274,18 +285,20 @@ public void OnEntityCreated(CEntityInstance entity)
player = Utilities.GetPlayerFromSteamId(weapon.OriginalOwnerXuidLow);
}
else
{
CCSWeaponBaseGun gun = weapon.As<CCSWeaponBaseGun>();
player = Utilities.GetPlayerFromIndex((int)weapon.OwnerEntity.Index) ?? Utilities.GetPlayerFromIndex((int)gun.OwnerEntity.Value!.Index);
}
if (player == null || string.IsNullOrEmpty(player?.PlayerName)) return;
if (string.IsNullOrEmpty(player?.PlayerName)) return;
if (player is null || !Utility.IsPlayerValid(player)) return;
GivePlayerWeaponSkin(player, weapon);
}
catch (Exception)
{
return;
}
if (player is null || !player.IsValid || !Utility.IsPlayerValid(player)) return;
GivePlayerWeaponSkin(player, weapon);
});
}
}
Expand Down
25 changes: 22 additions & 3 deletions Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ internal static async Task CheckDatabaseTables()
`weapon_defindex` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB",
@"CREATE TABLE `wp_player_agents` (
@"CREATE TABLE IF NOT EXISTS `wp_player_agents` (
`steamid` varchar(18) NOT NULL,
`agent_ct` varchar(64) DEFAULT NULL,
`agent_t` varchar(64) DEFAULT NULL,
UNIQUE KEY `steamid` (`steamid`)
) ENGINE=InnoDB"
UNIQUE (`steamid`)
) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_music` (
`steamid` varchar(64) NOT NULL,
`music_id` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB",
};

foreach (var query in createTableQueries)
Expand Down Expand Up @@ -136,6 +141,20 @@ internal static void LoadAgentsFromFile(string filePath)
}
}

internal static void LoadMusicFromFile(string filePath)
{
try
{
string json = File.ReadAllText(filePath);
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
WeaponPaints.musicList = deserializedSkins ?? new List<JObject>();
}
catch (FileNotFoundException)
{
throw;
}
}

internal static void Log(string message)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2e
2.3a
10 changes: 10 additions & 0 deletions WeaponAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ public static void GivePlayerAgent(CCSPlayerController player)
}
}

public static void GivePlayerMusicKit(CCSPlayerController player)
{
if (!g_playersMusic.ContainsKey(player.Slot)) return;
if (player.InventoryServices == null) return;

Console.WriteLine(g_playersMusic[player.Slot]);

player.InventoryServices.MusicID = g_playersMusic[player.Slot];
}

public static CCSPlayerController? GetPlayerFromItemServices(CCSPlayer_ItemServices itemServices)
{
var pawn = itemServices.Pawn.Value;
Expand Down
11 changes: 10 additions & 1 deletion WeaponPaints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
internal static Dictionary<int, int> g_knifePickupCount = new Dictionary<int, int>();
internal static ConcurrentDictionary<int, string> g_playersKnife = new ConcurrentDictionary<int, string>();
internal static ConcurrentDictionary<int, ushort> g_playersGlove = new ConcurrentDictionary<int, ushort>();
internal static ConcurrentDictionary<int, ushort> g_playersMusic = new ConcurrentDictionary<int, ushort>();
internal static ConcurrentDictionary<int, (string? CT, string? T)> g_playersAgent = new ConcurrentDictionary<int, (string?, string?)>();
internal static ConcurrentDictionary<int, ConcurrentDictionary<int, WeaponInfo>> gPlayerWeaponsInfo = new ConcurrentDictionary<int, ConcurrentDictionary<int, WeaponInfo>>();
internal static List<JObject> skinsList = new List<JObject>();
internal static List<JObject> glovesList = new List<JObject>();
internal static List<JObject> agentsList = new List<JObject>();
internal static List<JObject> musicList = new List<JObject>();
internal static WeaponSynchronization? weaponSync;
public static bool g_bCommandsAllowed = true;
internal Dictionary<int, string> PlayerWeaponImage = new();
Expand Down Expand Up @@ -158,7 +160,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleDescription => "Skin, gloves, agents and knife selector, standalone and web-based";
public override string ModuleName => "WeaponPaints";
public override string ModuleVersion => "2.2e";
public override string ModuleVersion => "2.3a";

public static WeaponPaintsConfig GetWeaponPaintsConfig()
{
Expand Down Expand Up @@ -211,12 +213,17 @@ public override void Load(bool hotReload)
{
_ = Task.Run(async () => await weaponSync.GetAgentFromDatabase(playerInfo));
}
if (Config.Additional.MusicEnabled)
{
_ = Task.Run(async () => await weaponSync.GetMusicFromDatabase(playerInfo));
}
}
}

Utility.LoadSkinsFromFile(ModuleDirectory + "/skins.json");
Utility.LoadGlovesFromFile(ModuleDirectory + "/gloves.json");
Utility.LoadAgentsFromFile(ModuleDirectory + "/agents.json");
Utility.LoadMusicFromFile(ModuleDirectory + "/music.json");

if (Config.Additional.KnifeEnabled)
SetupKnifeMenu();
Expand All @@ -226,6 +233,8 @@ public override void Load(bool hotReload)
SetupGlovesMenu();
if (Config.Additional.AgentEnabled)
SetupAgentsMenu();
if (Config.Additional.MusicEnabled)
SetupMusicMenu();

RegisterListeners();
RegisterCommands();
Expand Down
Loading

0 comments on commit 5626c32

Please sign in to comment.