PixelPiggy/CS_float
0
1const utils = require('./utils');2 3class InspectURL {4 constructor() {5 this.requiredParams = ['s', 'a', 'd', 'm'];6 7 if (arguments.length === 1 && typeof arguments[0] === 'string') {8 // parse the inspect link9 this.parseLink(arguments[0]);10 }11 else if (arguments.length === 1 && typeof arguments[0] === 'object') {12 // parse object with the requiredParams13 14 for (let param of this.requiredParams) {15 if (arguments[0][param] && typeof arguments[0][param] === 'string' && arguments[0][param].length > 0) {16 this[param] = arguments[0][param];17 }18 else this[param] = '0';19 }20 }21 else if (arguments.length === 4) {22 // parse each arg23 24 // Ensure each arg is a string25 for (let param in this.requiredParams) {26 if (typeof arguments[param] === 'string') {27 this[this.requiredParams[param]] = arguments[param];28 }29 else return;30 }31 }32 }33 34 get valid() {35 // Ensure each param exists and only contains digits36 for (let param of this.requiredParams) {37 if (!this[param] || !utils.isOnlyDigits(this[param])) return false;38 }39 40 return true;41 }42 43 parseLink(link) {44 try {45 link = decodeURI(link);46 } catch (e) {47 // Catch URI Malformed exceptions48 return;49 }50 51 let groups = /^steam:\/\/rungame\/730\/\d+\/[+ ]csgo_econ_action_preview ([SM])(\d+)A(\d+)D(\d+)$/.exec(link);52 53 if (groups) {54 if (groups[1] === 'S') {55 this.s = groups[2];56 this.m = '0';57 }58 else if (groups[1] === 'M') {59 this.m = groups[2];60 this.s = '0';61 }62 63 this.a = groups[3];64 this.d = groups[4];65 }66 }67 68 getParams() {69 if (this.valid) return {s: this.s, a: this.a, d: this.d, m: this.m};70 }71 72 isMarketLink() {73 return this.valid && this.m !== '0';74 }75 76 getLink() {77 if (!this.valid) return;78 79 if (this.s === '0' && this.m) {80 return `steam://rungame/730/76561202255233023/+csgo_econ_action_preview M${this.m}A${this.a}D${this.d}`;81 }82 else {83 return `steam://rungame/730/76561202255233023/+csgo_econ_action_preview S${this.s}A${this.a}D${this.d}`;84 }85 }86}87 88module.exports = InspectURL;89 