sgrelo.js (3664B)
1 module.exports = { 2 name: "sgrelo", 3 description: "Calculate reload time for shotgun", 4 options: [ 5 { 6 type: 7, 7 name: "gun", 8 description: "Gun name", 9 required: true, 10 }, 11 ], 12 run: async (client, interaction, args) => { 13 let err = "", 14 wrn = "", 15 interpretedAs = []; 16 function mather(inp) { 17 const inpArr = interpreter(inp), 18 outArr = []; 19 inpArr.map((x, i, a) => { 20 for (let j = i + 1; j < a.length; j++) { 21 outArr.push(worker(x, a[j])); 22 } 23 }); 24 const aArr = outArr.map(x => x.a), 25 iArr = outArr.map(x => x.i); 26 return ( 27 "Input: `" + 28 interpretedAs.join("`, `") + 29 "`" + 30 (aArr.length == 1 31 ? "\nOpening and Closing Animation Time: " + 32 beautifier(aArr[0]) + 33 "\nReload Time: " + 34 beautifier(iArr[0]) 35 : "\nAll Opening and Closing Animation Time: " + 36 aArr.map(x => beautifier(x)).join(", ") + 37 "\nAll Reload Time: " + 38 iArr.map(x => beautifier(x)).join(", ") + 39 "\nAverage Opening and Closing Animation Time: " + 40 beautifier(aArr.reduce((t, x) => t + x, 0) / aArr.length) + 41 "\nAverage Reload Time: " + 42 beautifier(iArr.reduce((t, x) => t + x, 0) / iArr.length)) 43 ); 44 } 45 46 function interpreter(inp) { 47 let out = [ 48 ...new Set( 49 inp 50 .split(/\n|, |,/g) 51 .filter(x => x) 52 .map(x => x.toLowerCase().replace(/[^0-9a-z]/g, "")) 53 ), 54 ]; 55 out = out 56 .map(x => { 57 interpretedAs.push(x); 58 const obj = { 59 b: 0, 60 f: 0, 61 }; 62 if ( 63 x.split(/b/g).length > 2 || 64 x.split(/f/g).length > 2 || 65 x.split(/[a-z]/g).length > 3 66 ) { 67 err += "Unknown identifier `" + x + "`\n"; 68 } else if (x.indexOf("b") === -1 || x.indexOf("f") === -1) { 69 err += "Missing identifier `" + x + "`\n"; 70 } else if (x.indexOf("b") === x.length - 1) { 71 obj.b = parseFloat(x.split("f")[1].trim()); 72 obj.f = parseFloat(x.split("f")[0].trim()); 73 } else if (x.indexOf("f") === x.length - 1) { 74 obj.b = parseFloat(x.split("b")[0].trim()); 75 obj.f = parseFloat(x.split("b")[1].trim()); 76 } else { 77 err += "Unknown identifier `" + x + "`\n"; 78 } 79 if (!obj.b && obj.f) { 80 err += "Couldn't interpret `" + x + "`\n"; 81 } else if (!Number.isInteger(obj.b)) { 82 err += "Decimal bullet count found `" + x + "`\n"; 83 } else if (!Number.isInteger(obj.f)) { 84 wrn += "Decimal value found `" + x + "`\n"; 85 } 86 return obj; 87 }) 88 .filter(x => JSON.stringify(x).length && x); 89 if (out.length === 1) { 90 err += "Single equation found\n"; 91 } 92 return err || !out.length ? [] : out; 93 } 94 95 function worker(o1, o2) { 96 const out = { 97 i: 0, 98 a: 0, 99 }; 100 out.i = (o1.f - o2.f) / (o1.b - o2.b); 101 out.a = o1.f - out.i * o1.b; 102 return out; 103 } 104 105 function beautifier(num) { 106 return parseFloat(num.toFixed(2)).toString() + "s"; 107 } 108 109 const msg = mather(args[0]); 110 111 if (err !== "") { 112 interaction.followUp(err); 113 } else if (wrn !== "") { 114 interaction.followUp({ content: wrn + "\n" + msg }); 115 } else { 116 interaction.followUp({ content: msg }); 117 } 118 }, 119 };