UNPKG

csgo-fade-percentage-calculator

Version:

Calculate the Fade percentage of a CS2 skin using its paint seed.

262 lines (253 loc) 9.39 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FadeCalculator = {})); })(this, (function (exports) { 'use strict'; class RandomNumberGenerator { constructor() { this.mIdum = 0; this.mIy = 0; this.mIv = []; this.NTAB = 32; this.IA = 16807; this.IM = 2147483647; this.IQ = 127773; this.IR = 2836; this.NDIV = 1 + (this.IM - 1) / this.NTAB; this.AM = 1.0 / this.IM; this.RNMX = 1.0 - 1.2e-7; } setSeed(seed) { this.mIdum = seed; if (seed >= 0) { this.mIdum = -seed; } this.mIy = 0; } generateRandomNumber() { let k; let j; if (this.mIdum <= 0 || this.mIy === 0) { if (-this.mIdum < 1) { this.mIdum = 1; } else { this.mIdum = -this.mIdum; } for (j = this.NTAB + 7; j >= 0; j -= 1) { k = Math.floor(this.mIdum / this.IQ); this.mIdum = Math.floor(this.IA * (this.mIdum - k * this.IQ) - this.IR * k); if (this.mIdum < 0) { this.mIdum += this.IM; } if (j < this.NTAB) { this.mIv[j] = this.mIdum; } } [this.mIy] = this.mIv; } k = Math.floor(this.mIdum / this.IQ); this.mIdum = Math.floor(this.IA * (this.mIdum - k * this.IQ) - this.IR * k); if (this.mIdum < 0) { this.mIdum += this.IM; } j = Math.floor(this.mIy / this.NDIV); this.mIy = Math.floor(this.mIv[j]); this.mIv[j] = this.mIdum; return this.mIy; } randomFloat(low, high) { let float = this.AM * this.generateRandomNumber(); if (float > this.RNMX) { float = this.RNMX; } return (float * (high - low)) + low; } } class BaseCalculator { constructor() { this.minPercentage = 80; } getSupportedWeapons() { return this.weapons; } getFadePercentage(weapon, seed) { const percentages = this.getFadePercentages(weapon); return percentages[seed]; } getAllFadePercentages() { return this.weapons.map((weapon) => ({ weapon, percentages: this.getFadePercentages(weapon), })); } getFadePercentages(weapon) { if (!this.weapons.includes(weapon)) { throw new Error(`The weapon "${weapon}" is currently not supported.`); } const config = this.configs[weapon] || this.configs.default; const rawResults = []; const maxSeed = this.tradeUpWeapons.includes(weapon) ? 1000 : 999; for (let i = 0; i <= maxSeed; i += 1) { const randomNumberGenerator = new RandomNumberGenerator(); randomNumberGenerator.setSeed(i); const xOffset = randomNumberGenerator.randomFloat(config.pattern_offset_x_start, config.pattern_offset_x_end); randomNumberGenerator.randomFloat(config.pattern_offset_y_start, config.pattern_offset_y_end); const rotation = randomNumberGenerator.randomFloat(config.pattern_rotate_start, config.pattern_rotate_end); let rawResult; if (config.pattern_offset_x_start !== config.pattern_offset_x_end) { rawResult = rotation * xOffset; } else { rawResult = rotation; } rawResults.push(Math.abs(rawResult)); } const isReversed = this.reversedWeapons.includes(weapon); let bestResult; let worstResult; if (isReversed) { bestResult = Math.max(...rawResults); worstResult = Math.min(...rawResults); } else { bestResult = Math.min(...rawResults); worstResult = Math.max(...rawResults); } const resultRange = worstResult - bestResult; const percentageResults = rawResults.map((rawResult) => (worstResult - rawResult) / resultRange); const sortedPercentageResults = [...percentageResults].sort((a, b) => a - b); return percentageResults.map((percentageResult, i) => ({ seed: i, percentage: this.minPercentage + (percentageResult * (100 - this.minPercentage)), ranking: Math.min(sortedPercentageResults.indexOf(percentageResult) + 1, sortedPercentageResults.length - sortedPercentageResults.indexOf(percentageResult)), })); } } let FadeCalculator$1 = class FadeCalculator extends BaseCalculator { constructor() { super(...arguments); this.weapons = [ 'AWP', 'Bayonet', 'Bowie Knife', 'Butterfly Knife', 'Classic Knife', 'Falchion Knife', 'Flip Knife', 'Glock-18', 'Gut Knife', 'Huntsman Knife', 'Karambit', 'Kukri Knife', 'M9 Bayonet', 'MAC-10', 'MP7', 'Navaja Knife', 'Nomad Knife', 'Paracord Knife', 'R8 Revolver', 'Shadow Daggers', 'Skeleton Knife', 'Stiletto Knife', 'Survival Knife', 'Talon Knife', 'UMP-45', 'Ursus Knife', ]; this.reversedWeapons = [ 'AWP', 'Karambit', 'Talon Knife', ]; this.tradeUpWeapons = [ 'AWP', 'Glock-18', 'MAC-10', 'MP7', 'R8 Revolver', 'UMP-45', ]; this.configs = { default: { pattern_offset_x_start: -0.7, pattern_offset_x_end: -0.7, pattern_offset_y_start: -0.7, pattern_offset_y_end: -0.7, pattern_rotate_start: -55, pattern_rotate_end: -65, }, MP7: { pattern_offset_x_start: -0.9, pattern_offset_x_end: -0.3, pattern_offset_y_start: -0.7, pattern_offset_y_end: -0.5, pattern_rotate_start: -55, pattern_rotate_end: -65, }, }; } }; let AmberFadeCalculator$1 = class AmberFadeCalculator extends BaseCalculator { constructor() { super(...arguments); this.weapons = [ 'AUG', 'Galil AR', 'MAC-10', 'P2000', 'R8 Revolver', 'Sawed-Off', ]; this.reversedWeapons = []; this.tradeUpWeapons = [ 'AUG', 'Galil AR', 'MAC-10', 'P2000', 'R8 Revolver', 'Sawed-Off', ]; this.configs = { default: { pattern_offset_x_start: -0.7, pattern_offset_x_end: -0.7, pattern_offset_y_start: -0.7, pattern_offset_y_end: -0.7, pattern_rotate_start: -55, pattern_rotate_end: -65, }, }; } }; let AcidFadeCalculator$1 = class AcidFadeCalculator extends BaseCalculator { constructor() { super(...arguments); this.weapons = [ 'SSG 08', ]; this.reversedWeapons = []; this.tradeUpWeapons = [ 'SSG 08', ]; this.configs = { default: { pattern_offset_x_start: -2.4, pattern_offset_x_end: -2.1, pattern_offset_y_start: 0.0, pattern_offset_y_end: 0.0, pattern_rotate_start: -55, pattern_rotate_end: -65, }, }; } }; const FadeCalculator = new FadeCalculator$1(); const AmberFadeCalculator = new AmberFadeCalculator$1(); const AcidFadeCalculator = new AcidFadeCalculator$1(); exports.AcidFadeCalculator = AcidFadeCalculator; exports.AmberFadeCalculator = AmberFadeCalculator; exports.FadeCalculator = FadeCalculator; }));