import * as Loader from "./loader"; export function getuid() { console.error("Only share this uid with Miodec and nobody else!"); console.log(firebase.auth().currentUser.uid); console.error("Only share this uid with Miodec and nobody else!"); } function hexToHSL(H) { // Convert hex to RGB first let r = 0, g = 0, b = 0; if (H.length == 4) { r = "0x" + H[1] + H[1]; g = "0x" + H[2] + H[2]; b = "0x" + H[3] + H[3]; } else if (H.length == 7) { r = "0x" + H[1] + H[2]; g = "0x" + H[3] + H[4]; b = "0x" + H[5] + H[6]; } // Then to HSL r /= 255; g /= 255; b /= 255; let cmin = Math.min(r, g, b), cmax = Math.max(r, g, b), delta = cmax - cmin, h = 0, s = 0, l = 0; if (delta == 0) h = 0; else if (cmax == r) h = ((g - b) / delta) % 6; else if (cmax == g) h = (b - r) / delta + 2; else h = (r - g) / delta + 4; h = Math.round(h * 60); if (h < 0) h += 360; l = (cmax + cmin) / 2; s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1)); s = +(s * 100).toFixed(1); l = +(l * 100).toFixed(1); return { hue: h, sat: s, lgt: l, string: "hsl(" + h + "," + s + "%," + l + "%)", }; } let themesList = null; export async function getThemesList() { if (themesList == null) { return $.getJSON("themes/_list.json", function (data) { const list = data.sort(function (a, b) { const nameA = a.name.toLowerCase(); const nameB = b.name.toLowerCase(); if (nameA < nameB) return -1; if (nameA > nameB) return 1; return 0; }); themesList = list; return themesList; }); } else { return themesList; } } let sortedThemesList = null; export async function getSortedThemesList() { if (sortedThemesList == null) { if (themesList == null) { await getThemesList(); } const sorted = themesList.sort((a, b) => { let b1 = hexToHSL(a.bgColor); let b2 = hexToHSL(b.bgColor); return b2.lgt - b1.lgt; }); sortedThemesList = sorted; return sortedThemesList; } else { return sortedThemesList; } } let funboxList = null; export async function getFunboxList() { if (funboxList == null) { return $.getJSON("funbox/_list.json", function (data) { funboxList = data.sort(function (a, b) { const nameA = a.name.toLowerCase(); const nameB = b.name.toLowerCase(); if (nameA < nameB) return -1; if (nameA > nameB) return 1; return 0; }); return funboxList; }); } else { return funboxList; } } let quotes = null; export async function getQuotes(language) { if (quotes === null || quotes.language !== language.replace(/_\d*k$/g, "")) { Loader.show(); try { let data = await $.getJSON(`quotes/${language}.json`); Loader.hide(); if (data.quotes === undefined || data.quotes.length === 0) { quotes = { quotes: [], length: 0, }; return quotes; } quotes = data; quotes.length = data.quotes.length; quotes.groups.forEach((qg, i) => { let lower = qg[0]; let upper = qg[1]; quotes.groups[i] = quotes.quotes.filter((q) => { if (q.length >= lower && q.length <= upper) { q.group = i; return true; } else { return false; } }); }); return quotes; } catch { Loader.hide(); quotes = { quotes: [], length: 0, }; return quotes; } // error: (e) => { // Notifications.add( // `Error while loading ${language.replace( // /_\d*k$/g, // "" // )} quotes: ${e}`, // -1 // ); // quotes = []; // return quotes; // }, } else { return quotes; } } let fontsList = null; export async function getFontsList() { if (fontsList == null) { return $.getJSON("fonts/_list.json", function (data) { fontsList = data.sort(function (a, b) { const nameA = a.name.toLowerCase(); const nameB = b.name.toLowerCase(); if (nameA < nameB) return -1; if (nameA > nameB) return 1; return 0; }); return fontsList; }); } else { return fontsList; } } let languageList = null; export async function getLanguageList() { if (languageList == null) { return $.getJSON("languages/_list.json", function (data) { languageList = data; return languageList; }); } else { return languageList; } } let languageGroupList = null; export async function getLanguageGroups() { if (languageGroupList == null) { return $.getJSON("languages/_groups.json", function (data) { languageGroupList = data; return languageGroupList; }); } else { return languageGroupList; } } export async function findCurrentGroup(language) { let retgroup = undefined; let groups = await getLanguageGroups(); groups.forEach((group) => { if (retgroup === undefined) { if (group.languages.includes(language)) { retgroup = group; } } }); return retgroup; } let challengeList = null; export async function getChallengeList() { if (challengeList == null) { return $.getJSON("challenges/_list.json", function (data) { challengeList = data; return challengeList; }); } else { return challengeList; } } export function showNotification(text, time) { let noti = $(".notification"); noti.text(text); noti.css("top", `-${noti.outerHeight()}px`); noti.stop(true, false).animate( { top: "1rem", }, 250, "swing", () => { noti.stop(true, false).animate( { opacity: 1, }, time, () => { noti.stop(true, false).animate( { top: `-${noti.outerHeight()}px`, }, 250, "swing", () => { noti.text(""); } ); } ); } ); } let currentLanguage = null; export function getCurrentLanguage() { return currentLanguage; } export async function getLanguage(lang) { try { if (currentLanguage == null || currentLanguage.name !== lang) { console.log("getting language json"); await $.getJSON(`languages/${lang}.json`, function (data) { currentLanguage = data; }); } return currentLanguage; } catch (e) { console.error(`error getting language`); console.error(e); showNotification(`Error getting language: ${e.message}`, 4000); await $.getJSON(`languages/english.json`, function (data) { currentLanguage = data; }); return currentLanguage; } } export function migrateFromCookies() { ["resultFilters", "config", "merchbannerclosed", "activeTags"].forEach( function (name) { let decodedCookie = decodeURIComponent(document.cookie).split(";"); let value = null; for (var i = 0; i < decodedCookie.length; i++) { var c = decodedCookie[i]; while (c.charAt(0) == " ") { c = c.substring(1); } if (c.indexOf(name + "=") == 0) { value = c.substring(name.length + 1, c.length); } } if (value) { window.localStorage.setItem(name, value); $.removeCookie(name, { path: "/" }); } } ); } export function sendVerificationEmail() { Loader.show(); let cu = firebase.auth().currentUser; cu.sendEmailVerification() .then(() => { Loader.hide(); showNotification("Email sent to " + cu.email, 4000); }) .catch((e) => { Loader.hide(); showNotification("Error: " + e.message, 3000); console.error(e.message); }); } export function smooth(arr, windowSize, getter = (value) => value, setter) { const get = getter; const result = []; for (let i = 0; i < arr.length; i += 1) { const leftOffeset = i - windowSize; const from = leftOffeset >= 0 ? leftOffeset : 0; const to = i + windowSize + 1; let count = 0; let sum = 0; for (let j = from; j < to && j < arr.length; j += 1) { sum += get(arr[j]); count += 1; } result[i] = setter ? setter(arr[i], sum / count) : sum / count; } return result; } export function stdDev(array) { try { const n = array.length; const mean = array.reduce((a, b) => a + b) / n; return Math.sqrt( array.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n ); } catch (e) { return 0; } } export function mean(array) { try { return ( array.reduce((previous, current) => (current += previous)) / array.length ); } catch (e) { return 0; } } export function getReleasesFromGitHub() { $.getJSON( "https://api.github.com/repos/Miodec/monkeytype/releases", (data) => { $("#bottom .version").text(data[0].name).css("opacity", 1); $("#versionHistory .releases").empty(); data.forEach((release) => { if (!release.draft && !release.prerelease) { $("#versionHistory .releases").append(`