UNPKG

csgo-fade-percentage-calculator

Version:

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

65 lines 2.78 kB
import RandomNumberGenerator from './RandomNumberGenerator.js'; 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)), })); } } export default BaseCalculator; //# sourceMappingURL=BaseCalculator.js.map