wynnbuilder-idk/js/utils.js

513 lines
15 KiB
JavaScript
Raw Normal View History

let getUrl = window.location;
const url_base = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
2021-06-20 23:44:04 +00:00
const zip = (a, b) => a.map((k, i) => [k, b[i]]);
2022-01-14 05:52:54 +00:00
//updates all the OGP tags for a webpage. Should be called when build changes
function updateOGP() {
2022-01-14 06:18:06 +00:00
//update the embed URL
2022-01-14 05:52:54 +00:00
let url_elem = document.getElementById("ogp-url");
if (url_elem) {
2022-01-14 06:18:06 +00:00
url_elem.content = url_base+location.hash;
}
//update the embed text content
let build_elem = document.getElementById("ogp-build-list");
if (build_elem && player_build) {
2022-01-14 06:21:20 +00:00
let text = "WynnBuilder build:\n"+
2022-01-14 06:18:06 +00:00
"> "+player_build.helmet.get("displayName")+"\n"+
"> "+player_build.chestplate.get("displayName")+"\n"+
"> "+player_build.leggings.get("displayName")+"\n"+
"> "+player_build.boots.get("displayName")+"\n"+
"> "+player_build.ring1.get("displayName")+"\n"+
"> "+player_build.ring2.get("displayName")+"\n"+
"> "+player_build.bracelet.get("displayName")+"\n"+
"> "+player_build.necklace.get("displayName")+"\n"+
"> "+player_build.weapon.get("displayName")+" ["+player_build.weapon.get("powders").map(x => powderNames.get(x)).join("")+"]";
build_elem.content = text;
2022-01-14 05:52:54 +00:00
}
}
2021-02-12 17:00:06 +00:00
function clamp(num, low, high){
return Math.min(Math.max(num, low), high);
}
2021-06-19 07:06:50 +00:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
2021-01-07 06:41:41 +00:00
// Permutations in js reference (also cool algorithm):
// https://stackoverflow.com/a/41068709
function perm(a){
if (a.length == 0) return [[]];
var r = [[a[0]]],
t = [],
s = [];
if (a.length == 1) return r;
for (var i = 1, la = a.length; i < la; i++){
for (var j = 0, lr = r.length; j < lr; j++){
r[j].push(a[i]);
t.push(r[j]);
for(var k = 1, lrj = r[j].length; k < lrj; k++){
for (var l = 0; l < lrj; l++) s[l] = r[j][(k+l)%lrj];
t[t.length] = s;
s = [];
}
}
r = t;
t = [];
}
return r;
}
2021-02-17 18:08:47 +00:00
function round_near(value) {
let eps = 0.00000001;
if (Math.abs(value - Math.round(value)) < eps) {
return Math.round(value);
}
return value;
}
function setText(id, text) {
document.getElementById(id).textContent = text;
}
2021-01-07 06:41:41 +00:00
function setHTML(id, html) {
document.getElementById(id).innerHTML = html;
}
function setValue(id, value) {
2021-01-07 10:23:54 +00:00
let el = document.getElementById(id);
if (el == null) {
console.log("WARN tried to set text value of id {"+id+"} to ["+value+"] but did not exist!");
return;
}
2021-01-07 10:23:54 +00:00
el.value = value;
el.dispatchEvent(new Event("change"));
2021-01-07 06:41:41 +00:00
}
2021-01-07 08:34:31 +00:00
2021-01-08 20:17:37 +00:00
function getValue(id) {
return document.getElementById(id).value;
}
function log(b, n) {
return Math.log(n) / Math.log(b);
}
2021-01-07 08:34:31 +00:00
// Base 64 encoding tools
// https://stackoverflow.com/a/27696695
2021-01-07 10:23:54 +00:00
// Modified for fixed precision
2021-01-07 08:34:31 +00:00
2022-06-19 19:05:01 +00:00
// Base64.fromInt(-2147483648); // gives "200000"
// Base64.toInt("200000"); // gives -2147483648
2021-01-07 08:34:31 +00:00
Base64 = (function () {
var digitsStr =
// 0 8 16 24 32 40 48 56 63
// v v v v v v v v v
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-";
var digits = digitsStr.split('');
var digitsMap = {};
for (var i = 0; i < digits.length; i++) {
digitsMap[digits[i]] = i;
}
return {
2021-01-07 10:23:54 +00:00
fromIntV: function(int32) {
2021-01-07 08:34:31 +00:00
var result = '';
while (true) {
result = digits[int32 & 0x3f] + result;
int32 >>>= 6;
if (int32 === 0)
break;
}
return result;
},
2021-01-07 10:23:54 +00:00
fromIntN: function(int32, n) {
var result = '';
for (let i = 0; i < n; ++i) {
result = digits[int32 & 0x3f] + result;
2021-01-09 09:09:47 +00:00
int32 >>= 6;
2021-01-07 10:23:54 +00:00
}
return result;
},
2021-01-07 08:34:31 +00:00
toInt: function(digitsStr) {
var result = 0;
var digits = digitsStr.split('');
for (var i = 0; i < digits.length; i++) {
result = (result << 6) + digitsMap[digits[i]];
}
return result;
2021-01-07 10:23:54 +00:00
},
2021-01-09 09:09:47 +00:00
toIntSigned: function(digitsStr) {
var result = 0;
var digits = digitsStr.split('');
if (digits[0] && (digitsMap[digits[0]] & 0x20)) {
result = -1;
}
for (var i = 0; i < digits.length; i++) {
result = (result << 6) + digitsMap[digits[i]];
}
return result;
}
2021-01-07 08:34:31 +00:00
};
})();
2022-06-19 19:05:01 +00:00
/** A class used to represent an arbitrary length bit vector. Very useful for encoding and decoding.
*
*/
class BitVector {
/** Constructs an arbitrary-length bit vector.
* @class
2022-06-22 04:51:26 +00:00
* @param {String | Number} data
* @param {Number} length - a set length for the data. Must be in the range [0, 32] if data is a Number.
*
* The structure of the Uint32Array should be [[last, ..., first], ..., [last, ..., first], [empty space, last, ..., first]]
2022-06-19 19:05:01 +00:00
*/
constructor(data, length) {
2022-06-22 04:51:26 +00:00
let bit_vec = [];
if (length < 0) {
throw new RangeError("BitVector must have nonnegative length.");
}
if (typeof data === "string") {
//string in B64
let str_len = data.length;
let curr = 0;
let curr_bits = 0;
} else if (typeof data === "number") {
//convert to int just in case
data = Math.round(data);
//range of numbers that won't fit in a uint32
if (data > 2**32 - 1 || data < -(2 ** 32 - 1)) {
throw new RangeError("Numerical data has to fit within a 32-bit integer range to instantiate a BitVector.");
}
bit_vec.push(data);
} else if (data instanceof Array) [
2022-06-19 19:05:01 +00:00
2022-06-22 04:51:26 +00:00
]
2022-06-19 19:05:01 +00:00
2022-06-22 04:51:26 +00:00
this.length = length;
this.bits = new Uint32Array(bit_vec);
2022-06-19 19:05:01 +00:00
}
2021-01-10 10:02:23 +00:00
2022-06-22 04:51:26 +00:00
readBitsSigned() {
}
readBitsUnsigned() {
}
setBits() {
}
clearBits() {
}
append(data, length) {
if (length < 0) {
throw new RangeError("BitVector length must increase by a nonnegative number.");
}
this.length += length;
}
/** Creates a string version of the bit vector
*
* @returns A bit vector in string format
*/
toString() {
if (this.length == 0) {
return "";
}
let bitstr = "";
//extract bits from first uint32 - may not be all 32 bits
let length_first = this.length % 32;
let curr = this.bits[0];
for (let i = 0; i < length_first; ++i) {
bitstr = (curr % 2 == 0 ? '0' : '1') + bitstr;
curr >>= 1;
}
//extract bits from rest of uint32s - always all 32 bits
for (let i = 1; i < this.bits.length; ++i) {
curr = this.bits[i];
for (let j = 0; j < 32; ++j) {
bitstr = (curr % 2 == 0 ? '0' : '1') + bitstr;
curr >>= 1;
}
}
//return the formed bitstring
return bitstr;
}
};
2022-06-20 19:07:25 +00:00
2021-01-10 10:02:23 +00:00
/*
2021-01-15 00:30:49 +00:00
Turns a raw stat and a % stat into a final stat on the basis that - raw and >= 100% becomes 0 and + raw and <=-100% becomes negative.
2021-01-10 10:02:23 +00:00
Pct would be 0.80 for 80%, -1.20 for 120%, etc
Example Outputs:
raw: -100
pct: +0.20, output = -80
pct: +1.20, output = 0
pct: -0.20, output = -120
pct: -1.20, output = -220
raw: +100
pct: +0.20, output = 120
pct: +1.20, output = 220
pct: -0.20, output = 80
2021-01-15 00:30:49 +00:00
pct: -1.20, output = -20
2021-01-10 10:02:23 +00:00
*/
function rawToPct(raw, pct){
final = 0;
if (raw < 0){
final = (Math.min(0, raw - (raw * pct) ));
}else if(raw > 0){
final = raw + (raw * pct);
2021-01-10 10:02:23 +00:00
}else{ //do nothing - final's already 0
}
return final;
}
/*
* Clipboard utilities
* From: https://stackoverflow.com/a/30810322
*/
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
//
// *** This styling is an extra step which is likely not required. ***
//
// Why is it here? To ensure:
// 1. the element is able to have focus and selection.
// 2. if the element was to flash render it has minimal visual impact.
// 3. less flakyness with selection and copying which **might** occur if
// the textarea element is not visible.
//
// The likelihood is the element won't even render, not even a
// flash, so some of these are just precautions. However in
// Internet Explorer the element is visible whilst the popup
// box asking the user for permission for the web page to
// copy to the clipboard.
//
// Place in the top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;
// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
// Avoid flash of the white box if rendered for any reason.
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
2021-02-17 18:29:41 +00:00
/**
* Generates a random color using the #(R)(G)(B) format.
2021-03-16 05:40:05 +00:00
*/
function randomColor() {
return '#' + Math.round(Math.random() * 0xFFFFFF).toString(16);
2021-03-16 05:40:05 +00:00
}
/**
* Generates a random color, but lightning must be relatively high (>0.5).
2021-03-16 05:40:05 +00:00
*
* @returns a random color in RGB 6-bit form.
*/
function randomColorLight() {
return randomColorHSL([0,1],[0,1],[0.5,1]);
}
/** Generates a random color given HSL restrictions.
*
* @returns a random color in RGB 6-bit form.
*/
function randomColorHSL(h,s,l) {
var letters = '0123456789abcdef';
let h_var = h[0] + (h[1]-h[0])*Math.random(); //hue
let s_var = s[0] + (s[1]-s[0])*Math.random(); //saturation
let l_var = l[0] + (l[1]-l[0])*Math.random(); //lightness
let rgb = hslToRgb(h_var,s_var,l_var);
let color = "#";
for (const c of rgb) {
color += letters[Math.floor(c/16)] + letters[c%16];
}
return color;
}
/**
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255]. Not written by wynnbuilder devs.
*
* @param {number} h The hue
* @param {number} s The saturation
* @param {number} l The lightness
* @return {Array} The RGB representation
*/
function hslToRgb(h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
var hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
/** Creates a tooltip.
*
* @param {DOM Element} elem - the element to make a tooltip
* @param {String} element_type - the HTML element type that the tooltiptext should be.
* @param {String} tooltiptext - the text to display in the tooltip.
* @param {DOM Element} parent - the parent elem. optional.
* @param {String[]} classList - a list of classes to add to the element.
*/
function createTooltip(elem, element_type, tooltiptext, parent, classList) {
elem = document.createElement(element_type);
elem.classList.add("tooltiptext");
if (tooltiptext.includes("\n")) {
let texts = tooltiptext.split("\n");
for (const t of texts) {
let child = document.createElement(element_type);
child.textContent = t;
elem.appendChild(child);
}
} else {
elem.textContent = tooltiptext;
}
for (const c of classList) {
elem.classList.add(c);
}
if (parent) {
parent.classList.add("tooltip");
parent.appendChild(elem);
}
return elem;
}
/** A generic function that toggles the on and off state of a button.
*
* @param {String} button_id - the id name of the button.
*/
function toggleButton(button_id) {
let elem = document.getElementById(button_id);
if (elem.tagName === "BUTTON") {
if (elem.classList.contains("toggleOn")) { //toggle the pressed button off
elem.classList.remove("toggleOn");
} else {
elem.classList.add("toggleOn");
}
}
2021-04-30 00:03:50 +00:00
}
/**
* If the input object is undefined, make it "match" the target type
* with default value (0 or empty str).
*/
function matchType(object, target) {
if (typeof object === 'undefined') {
switch (target) {
case 'string':
return "";
case 'number':
return 0;
case 'undefined':
return undefined;
default:
throw new Error(`Incomparable type ${target}`);
}
}
return object;
}
/**
* Add multiple classes to a html element
*/
function addClasses(elem, classes) {
for (let _class of classes) {
elem.classList.add(_class);
}
return elem;
}
/** A utility function that reloads the page forcefully.
*
*/
async function hardReload() {
//https://gist.github.com/rmehner/b9a41d9f659c9b1c3340
const dbs = await window.indexedDB.databases();
await dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) });
location.reload(true);
2021-06-19 07:06:50 +00:00
}
function capitalizeFirst(str) {
2022-05-21 07:43:03 +00:00
return str[0].toUpperCase() + str.substring(1);
}