added tag pbs

This commit is contained in:
Jack 2020-12-13 22:37:19 +00:00
parent adbccb498f
commit f904f6e32b
5 changed files with 149 additions and 11 deletions

View file

@ -452,6 +452,67 @@ function checkIfPB(uid, obj, userdata) {
}
}
async function checkIfTagPB(uid, obj) {
if (obj.tags.length === 0) {
return [];
}
let dbtags = [];
let restags = obj.tags;
try {
let snap = await db.collection(`users/${uid}/tags`).get();
snap.forEach((doc) => {
if (restags.includes(doc.id)) {
let data = doc.data();
data.id = doc.id;
dbtags.push(data);
}
});
} catch (e) {
return [];
}
let wpm = obj.wpm;
let ret = [];
for (let i = 0; i < dbtags.length; i++) {
let dbtag = dbtags[i];
if (dbtag.pb === undefined || dbtag.pb < wpm) {
//no pb found, meaning this one is a pb
await db.collection(`users/${uid}/tags`).doc(dbtag.id).update({
pb: wpm,
});
ret.push(dbtag.id);
}
}
return ret;
}
exports.clearTagPb = functions.https.onCall((request, response) => {
try {
return db
.collection(`users/${request.uid}/tags`)
.doc(request.tagid)
.update({
pb: 0,
})
.then((e) => {
return {
resultCode: 1,
};
})
.catch((e) => {
console.error(
`error deleting tag pb for user ${request.uid}: ${e.message}`
);
return {
resultCode: -999,
message: e.message,
};
});
} catch (e) {
console.error(`error deleting tag pb for ${request.uid} - ${e}`);
return { resultCode: -999 };
}
});
function stdDev(array) {
const n = array.length;
const mean = array.reduce((a, b) => a + b) / n;
@ -1040,11 +1101,13 @@ exports.testCompleted = functions.https.onRequest(async (request, response) => {
emailVerified
),
checkIfPB(request.uid, request.obj, userdata),
checkIfTagPB(request.uid, request.obj),
])
.then(async (values) => {
let globallb = values[0].insertedAt;
let dailylb = values[1].insertedAt;
let ispb = values[2];
let tagPbs = values[3];
// console.log(values);
if (obj.mode === "time" && String(obj.mode2) === "60") {
@ -1096,6 +1159,7 @@ exports.testCompleted = functions.https.onRequest(async (request, response) => {
createdId: createdDocId,
needsToVerify: values[0].needsToVerify,
needsToVerifyEmail: values[0].needsToVerifyEmail,
tagPbs: tagPbs,
};
if (ispb) {

View file

@ -26,3 +26,4 @@ export const namecheck = firebase
export const getLeaderboard = firebase
.functions()
.httpsCallable("getLeaderboard");
export const clearTagPb = firebase.functions().httpsCallable("clearTagPb");

View file

@ -569,6 +569,11 @@ function refreshTagsSettingsSection() {
if (firebase.auth().currentUser !== null && db_getSnapshot() !== null) {
let tagsEl = $(".pageSettings .section.tags .tagsList").empty();
db_getSnapshot().tags.forEach((tag) => {
let tagPbString = "No PB found";
let balloon = "";
if (tag.pb != undefined && tag.pb > 0) {
tagPbString = `PB: ${tag.pb}`;
}
if (tag.active === true) {
tagsEl.append(`
@ -578,6 +583,7 @@ function refreshTagsSettingsSection() {
</div>
<div class="title">${tag.name}</div>
<div class="editButton"><i class="fas fa-pen"></i></div>
<div class="clearPbButton" aria-label="${tagPbString}" data-balloon-pos="up"><i class="fas fa-crown"></i></div>
<div class="removeButton"><i class="fas fa-trash"></i></div>
</div>
@ -591,6 +597,7 @@ function refreshTagsSettingsSection() {
</div>
<div class="title">${tag.name}</div>
<div class="editButton"><i class="fas fa-pen"></i></div>
<div class="clearPbButton" aria-label="${tagPbString}" data-balloon-pos="up"><i class="fas fa-crown"></i></div>
<div class="removeButton"><i class="fas fa-trash"></i></div>
</div>
@ -812,6 +819,17 @@ $(document).on("click", ".pageSettings .section.tags .addTagButton", (e) => {
showEditTags("add");
});
$(document).on(
"click",
".pageSettings .section.tags .tagsList .tag .clearPbButton",
(e) => {
let target = e.currentTarget;
let tagid = $(target).parent(".tag").attr("id");
let tagname = $(target).siblings(".title")[0].innerHTML;
simplePopups.clearTagPb.show([tagid, tagname]);
}
);
$(document).on(
"click",
".pageSettings .section.tags .tagsList .tag .editButton",

View file

@ -7,8 +7,10 @@ class SimplePopup {
inputs = [],
text = "",
buttonText = "Confirm",
execFn
execFn,
beforeShowFn
) {
this.parameters = [];
this.id = id;
this.type = type;
this.execFn = execFn;
@ -18,6 +20,7 @@ class SimplePopup {
this.wrapper = $("#simplePopupWrapper");
this.element = $("#simplePopup");
this.buttonText = buttonText;
this.beforeShowFn = beforeShowFn;
}
reset() {
this.element.html(`
@ -30,16 +33,16 @@ class SimplePopup {
init() {
let el = this.element;
el.find("input").val("");
if (el.attr("popupId") !== this.id) {
this.reset();
el.attr("popupId", this.id);
el.find(".title").text(this.title);
el.find(".text").text(this.text);
// if (el.attr("popupId") !== this.id) {
this.reset();
el.attr("popupId", this.id);
el.find(".title").text(this.title);
el.find(".text").text(this.text);
this.initInputs();
this.initInputs();
el.find(".button").text(this.buttonText);
}
el.find(".button").text(this.buttonText);
// }
}
initInputs() {
@ -73,7 +76,9 @@ class SimplePopup {
this.hide();
}
show() {
show(parameters) {
this.parameters = parameters;
this.beforeShowFn();
this.init();
this.wrapper
.stop(true, true)
@ -161,5 +166,54 @@ simplePopups.updateEmail = new SimplePopup(
} catch (e) {
Misc.showNotification("Something went wrong: " + e, 5000);
}
},
() => {}
);
simplePopups.clearTagPb = new SimplePopup(
"clearTagPb",
"text",
"Clear Tag PB",
[],
`Are you sure you want to clear this tags PB?`,
"Clear",
() => {
let tagid = eval("this.parameters[0]");
showBackgroundLoader();
CloudFunctions.clearTagPb({
uid: firebase.auth().currentUser.uid,
tagid: tagid,
})
.then((res) => {
hideBackgroundLoader();
if (res.data.resultCode === 1) {
let tag = db_getSnapshot().tags.filter((t) => t.id === tagid)[0];
tag.pb = 0;
$(
`.pageSettings .section.tags .tagsList .tag[id="${tagid}"] .clearPbButton`
).attr("aria-label", "No PB found");
Misc.showNotification("Tag PB cleared.", 1000);
} else {
console.error(res.data.message);
Misc.showNotification(
"Something went wrong: " + res.data.message,
5000
);
}
})
.catch((e) => {
hideBackgroundLoader();
console.error(e);
Misc.showNotification(
"Something went wrong while clearing tag pb " + e,
5000
);
});
// console.log(`clearing for ${eval("this.parameters[0]")} ${eval("this.parameters[1]")}`);
},
() => {
eval(
"this.text = `Are you sure you want to clear PB for tag ${eval('this.parameters[1]')}?`"
);
}
);

View file

@ -2250,7 +2250,7 @@ key {
.tag {
display: grid;
display: grid;
grid-template-columns: auto 1fr auto auto;
grid-template-columns: auto 1fr auto auto auto;
color: var(--text-color);
.title {
@ -2258,6 +2258,7 @@ key {
}
.editButton,
.clearPbButton,
.removeButton,
.active {
display: grid;