mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-03-10 08:31:42 +00:00
Refactor UtilsClass in utils.php to enhance maintainability by introducing constants for knife defindexes and mappings, improving weapon category organization, and implementing caching for skin and weapon data. Add methods for knife validation and cache clearing to streamline functionality.
This commit is contained in:
@@ -1,95 +1,159 @@
|
|||||||
<?php
|
<?php
|
||||||
class UtilsClass
|
class UtilsClass
|
||||||
{
|
{
|
||||||
|
// Knife defindexes as constants for better maintainability
|
||||||
|
private const KNIFE_DEFINDEXES = [
|
||||||
|
500, 503, 505, 506, 507, 508, 509, 512, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 525, 526
|
||||||
|
];
|
||||||
|
|
||||||
|
// Knife mapping for better maintainability
|
||||||
|
private const KNIFE_MAPPING = [
|
||||||
|
500 => 'weapon_bayonet',
|
||||||
|
503 => 'weapon_knife_css',
|
||||||
|
505 => 'weapon_knife_flip',
|
||||||
|
506 => 'weapon_knife_gut',
|
||||||
|
507 => 'weapon_knife_karambit',
|
||||||
|
508 => 'weapon_knife_m9_bayonet',
|
||||||
|
509 => 'weapon_knife_tactical',
|
||||||
|
512 => 'weapon_knife_falchion',
|
||||||
|
514 => 'weapon_knife_survival_bowie',
|
||||||
|
515 => 'weapon_knife_butterfly',
|
||||||
|
516 => 'weapon_knife_push',
|
||||||
|
517 => 'weapon_knife_cord',
|
||||||
|
518 => 'weapon_knife_canis',
|
||||||
|
519 => 'weapon_knife_ursus',
|
||||||
|
520 => 'weapon_knife_gypsy_jackknife',
|
||||||
|
521 => 'weapon_knife_outdoor',
|
||||||
|
522 => 'weapon_knife_stiletto',
|
||||||
|
523 => 'weapon_knife_widowmaker',
|
||||||
|
525 => 'weapon_knife_skeleton',
|
||||||
|
526 => 'weapon_knife_css'
|
||||||
|
];
|
||||||
|
|
||||||
|
// Weapon categories for better organization
|
||||||
|
private const WEAPON_CATEGORIES = [
|
||||||
|
'Rifles' => [7, 8, 10, 13, 16, 60, 39, 40, 38],
|
||||||
|
'Pistols' => [1, 2, 3, 4, 30, 32, 36, 61, 63, 64],
|
||||||
|
'SMGs' => [17, 19, 24, 26, 33, 34],
|
||||||
|
'Shotguns' => [25, 27, 29, 35],
|
||||||
|
'Snipers' => [9, 11, 38],
|
||||||
|
'Machine Guns' => [14, 28],
|
||||||
|
'Grenades' => [43, 44, 45, 46, 47, 48]
|
||||||
|
];
|
||||||
|
|
||||||
|
private static $skinCache = null;
|
||||||
|
private static $weaponCache = null;
|
||||||
|
private static $knifeCache = null;
|
||||||
|
|
||||||
|
public static function getKnifeDefindexes(): array
|
||||||
|
{
|
||||||
|
return self::KNIFE_DEFINDEXES;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getKnifeMapping(): array
|
||||||
|
{
|
||||||
|
return self::KNIFE_MAPPING;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getWeaponCategories(): array
|
||||||
|
{
|
||||||
|
return self::WEAPON_CATEGORIES;
|
||||||
|
}
|
||||||
|
|
||||||
public static function skinsFromJson(): array
|
public static function skinsFromJson(): array
|
||||||
{
|
{
|
||||||
|
if (self::$skinCache !== null) {
|
||||||
|
return self::$skinCache;
|
||||||
|
}
|
||||||
|
|
||||||
$skins = [];
|
$skins = [];
|
||||||
$json = json_decode(file_get_contents(__DIR__ . "/../data/".SKIN_LANGUAGE.".json"), true);
|
$jsonFile = __DIR__ . "/../data/" . SKIN_LANGUAGE . ".json";
|
||||||
|
|
||||||
|
if (!file_exists($jsonFile)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$json = json_decode(file_get_contents($jsonFile), true);
|
||||||
|
if (!$json) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($json as $skin) {
|
foreach ($json as $skin) {
|
||||||
$skins[(int) $skin['weapon_defindex']][(int) $skin['paint']] = [
|
$defindex = (int) $skin['weapon_defindex'];
|
||||||
|
$paintId = (int) $skin['paint'];
|
||||||
|
|
||||||
|
$skins[$defindex][$paintId] = [
|
||||||
'weapon_name' => $skin['weapon_name'],
|
'weapon_name' => $skin['weapon_name'],
|
||||||
'paint_name' => $skin['paint_name'],
|
'paint_name' => $skin['paint_name'],
|
||||||
'image_url' => $skin['image'],
|
'image_url' => $skin['image'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self::$skinCache = $skins;
|
||||||
return $skins;
|
return $skins;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getWeaponsFromArray()
|
public static function getWeaponsFromArray(): array
|
||||||
{
|
{
|
||||||
$weapons = [];
|
if (self::$weaponCache !== null) {
|
||||||
$temp = self::skinsFromJson();
|
return self::$weaponCache;
|
||||||
|
|
||||||
foreach ($temp as $key => $value) {
|
|
||||||
if (key_exists($key, $weapons))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
$weapons[$key] = [
|
|
||||||
'weapon_name' => $value[0]['weapon_name'],
|
|
||||||
'paint_name' => $value[0]['paint_name'],
|
|
||||||
'image_url' => $value[0]['image_url'],
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$weapons = [];
|
||||||
|
$skins = self::skinsFromJson();
|
||||||
|
|
||||||
|
foreach ($skins as $defindex => $skinList) {
|
||||||
|
if (!isset($weapons[$defindex]) && isset($skinList[0])) {
|
||||||
|
$weapons[$defindex] = [
|
||||||
|
'weapon_name' => $skinList[0]['weapon_name'],
|
||||||
|
'paint_name' => $skinList[0]['paint_name'],
|
||||||
|
'image_url' => $skinList[0]['image_url'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$weaponCache = $weapons;
|
||||||
return $weapons;
|
return $weapons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getKnifeTypes()
|
public static function getKnifeTypes(): array
|
||||||
{
|
{
|
||||||
$knifes = [];
|
if (self::$knifeCache !== null) {
|
||||||
$temp = self::getWeaponsFromArray();
|
return self::$knifeCache;
|
||||||
|
|
||||||
foreach ($temp as $key => $weapon) {
|
|
||||||
if (
|
|
||||||
!in_array($key, [
|
|
||||||
500,
|
|
||||||
503,
|
|
||||||
505,
|
|
||||||
506,
|
|
||||||
507,
|
|
||||||
508,
|
|
||||||
509,
|
|
||||||
512,
|
|
||||||
514,
|
|
||||||
515,
|
|
||||||
516,
|
|
||||||
517,
|
|
||||||
518,
|
|
||||||
519,
|
|
||||||
520,
|
|
||||||
521,
|
|
||||||
522,
|
|
||||||
523,
|
|
||||||
525,
|
|
||||||
526
|
|
||||||
])
|
|
||||||
)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
$knifes[$key] = [
|
|
||||||
'weapon_name' => $weapon['weapon_name'],
|
|
||||||
'paint_name' => rtrim(explode("|", $weapon['paint_name'])[0]),
|
|
||||||
'image_url' => $weapon['image_url'],
|
|
||||||
];
|
|
||||||
$knifes[0] = [
|
|
||||||
'weapon_name' => "weapon_knife",
|
|
||||||
'paint_name' => "Default knife",
|
|
||||||
'image_url' => "https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/website/img/skins/weapon_knife.png",
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$knifes = [];
|
||||||
|
$weapons = self::getWeaponsFromArray();
|
||||||
|
|
||||||
|
foreach (self::KNIFE_DEFINDEXES as $defindex) {
|
||||||
|
if (isset($weapons[$defindex])) {
|
||||||
|
$weapon = $weapons[$defindex];
|
||||||
|
$knifes[$defindex] = [
|
||||||
|
'weapon_name' => $weapon['weapon_name'],
|
||||||
|
'paint_name' => rtrim(explode("|", $weapon['paint_name'])[0]),
|
||||||
|
'image_url' => $weapon['image_url'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add default knife
|
||||||
|
$knifes[0] = [
|
||||||
|
'weapon_name' => "weapon_knife",
|
||||||
|
'paint_name' => "Default knife",
|
||||||
|
'image_url' => "https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/website/img/skins/weapon_knife.png",
|
||||||
|
];
|
||||||
|
|
||||||
ksort($knifes);
|
ksort($knifes);
|
||||||
|
self::$knifeCache = $knifes;
|
||||||
return $knifes;
|
return $knifes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getSelectedSkins(array $temp)
|
public static function getSelectedSkins(array $queryResult): array
|
||||||
{
|
{
|
||||||
$selected = [];
|
$selected = [];
|
||||||
|
|
||||||
foreach ($temp as $weapon) {
|
foreach ($queryResult as $weapon) {
|
||||||
$selected[$weapon['weapon_defindex']] = [
|
$selected[$weapon['weapon_defindex']] = [
|
||||||
'weapon_paint_id' => $weapon['weapon_paint_id'],
|
'weapon_paint_id' => $weapon['weapon_paint_id'],
|
||||||
'weapon_seed' => $weapon['weapon_seed'],
|
'weapon_seed' => $weapon['weapon_seed'],
|
||||||
'weapon_wear' => $weapon['weapon_wear'],
|
'weapon_wear' => $weapon['weapon_wear'],
|
||||||
@@ -98,4 +162,23 @@ class UtilsClass
|
|||||||
|
|
||||||
return $selected;
|
return $selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function isKnifeDefindex(int $defindex): bool
|
||||||
|
{
|
||||||
|
return in_array($defindex, self::KNIFE_DEFINDEXES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isKnifeWeapon(array $weapon): bool
|
||||||
|
{
|
||||||
|
return $weapon['weapon_name'] === 'weapon_knife' ||
|
||||||
|
strpos($weapon['weapon_name'], 'knife') !== false ||
|
||||||
|
strpos($weapon['paint_name'], '★') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function clearCache(): void
|
||||||
|
{
|
||||||
|
self::$skinCache = null;
|
||||||
|
self::$weaponCache = null;
|
||||||
|
self::$knifeCache = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user