let currentLeaderboard = { mode: "words", words: "10", time: "15", }; function showLeaderboards() { if ($("#leaderboardsWrapper").hasClass("hidden")) { $("#leaderboardsWrapper") .stop(true, true) .css("opacity", 0) .removeClass("hidden") .animate( { opacity: 1, }, 125, () => { updateLeaderboards(); } ); } } function hideLeaderboards() { $("#leaderboardsWrapper") .stop(true, true) .css("opacity", 1) .animate( { opacity: 0, }, 100, () => { $("#leaderboardsWrapper").addClass("hidden"); } ); focusWords(); } function updateLeaderboards() { $("#leaderboardsWrapper .leaderboardMode .button").removeClass("active"); $( `#leaderboardsWrapper .leaderboardMode .button[mode=${currentLeaderboard.mode}]` ).addClass("active"); $("#leaderboardsWrapper .leaderboardWords .button").removeClass("active"); $( `#leaderboardsWrapper .leaderboardWords .button[words=${currentLeaderboard.words}]` ).addClass("active"); $("#leaderboardsWrapper .leaderboardTime .button").removeClass("active"); $( `#leaderboardsWrapper .leaderboardTime .button[time=${currentLeaderboard.time}]` ).addClass("active"); if (currentLeaderboard.mode === "time") { $("#leaderboardsWrapper .leaderboardWords").addClass("hidden"); $("#leaderboardsWrapper .leaderboardTime").removeClass("hidden"); } else if (currentLeaderboard.mode === "words") { $("#leaderboardsWrapper .leaderboardWords").removeClass("hidden"); $("#leaderboardsWrapper .leaderboardTime").addClass("hidden"); } let mode2; if (currentLeaderboard.mode === "words") { mode2 = currentLeaderboard.words; } else if (currentLeaderboard.mode === "time") { mode2 = currentLeaderboard.time; } showBackgroundLoader(); Promise.all([ firebase.functions().httpsCallable("getLeaderboard")({ mode: currentLeaderboard.mode, mode2: mode2, type: "daily", }), firebase.functions().httpsCallable("getLeaderboard")({ mode: currentLeaderboard.mode, mode2: mode2, type: "global", }), ]).then((lbdata) => { hideBackgroundLoader(); let dailyData = lbdata[0].data; let globalData = lbdata[1].data; $("#leaderboardsWrapper table.daily tbody").empty(); if (dailyData.board !== undefined) { dailyData.board.forEach((entry, index) => { $("#leaderboardsWrapper table.daily tbody").append(` ${index + 1} ${entry.name} ${entry.wpm} ${entry.raw} ${entry.acc}% ${entry.mode} ${entry.mode2} ${moment(entry.timestamp).format("DD MMM YYYY
HH:mm")} `); }); } $("#leaderboardsWrapper table.global tbody").empty(); if (globalData.board !== undefined) { globalData.board.forEach((entry, index) => { $("#leaderboardsWrapper table.global tbody").append(` ${index + 1} ${entry.name} ${entry.wpm} ${entry.raw} ${entry.acc}% ${entry.mode} ${entry.mode2} ${moment(entry.timestamp).format("DD MMM YYYY
HH:mm")} `); }); } }); firebase .functions() .httpsCallable("getLeaderboard")({ mode: currentLeaderboard.mode, mode2: mode2, type: "global", }) .then((data) => { // console.log(data); $("#leaderboardsWrapper table.global tbody").empty(); if (data.data.board !== undefined) { data.data.board.forEach((entry, index) => { $("#leaderboardsWrapper table.global tbody").append(` ${index + 1} ${entry.name} ${entry.wpm} ${entry.raw} ${entry.acc} ${entry.mode} ${entry.mode2} ${moment(entry.timestamp).format("DD MMM YYYY
HH:mm")} `); }); } }); } $("#leaderboardsWrapper").click((e) => { if ($(e.target).attr("id") === "leaderboardsWrapper") { hideLeaderboards(); } }); $("#leaderboardsWrapper .leaderboardMode .button").click((e) => { currentLeaderboard.mode = $(e.target).attr("mode"); updateLeaderboards(); }); $("#leaderboardsWrapper .leaderboardWords .button").click((e) => { currentLeaderboard.words = $(e.target).attr("words"); updateLeaderboards(); }); $("#leaderboardsWrapper .leaderboardTime .button").click((e) => { currentLeaderboard.time = $(e.target).attr("time"); updateLeaderboards(); });