monkeytype/functions/index.js

1600 lines
45 KiB
JavaScript
Raw Normal View History

2020-07-03 08:35:45 +08:00
const functions = require("firebase-functions");
const admin = require("firebase-admin");
let key = "./serviceAccountKey.json";
2020-07-03 08:35:45 +08:00
if (process.env.GCLOUD_PROJECT === "monkey-type") {
key = "./serviceAccountKey_live.json";
}
var serviceAccount = require(key);
admin.initializeApp({
2020-07-03 08:35:45 +08:00
credential: admin.credential.cert(serviceAccount),
});
2020-07-07 20:28:01 +08:00
const db = admin.firestore();
2020-07-03 08:35:45 +08:00
exports.moveResults = functions
.runWith({ timeoutSeconds: 540, memory: "2GB" })
.https.onCall((request, response) => {
return db
2020-07-03 08:35:45 +08:00
.collection("results")
.orderBy("timestamp", "desc")
.limit(2000)
.get()
.then((data) => {
data.docs.forEach((doc) => {
let result = doc.data();
if (result.moved === undefined || result.moved === false) {
db.collection(`results`).doc(doc.id).update({ moved: true });
db.collection(`users/${result.uid}/results`).add(result);
2020-07-03 08:35:45 +08:00
console.log(`moving doc ${doc.id}`);
}
});
return;
});
});
function getAllNames() {
return admin
.auth()
.listUsers()
.then((data) => {
let names = [];
data.users.forEach((user) => {
names.push(user.displayName);
});
return names;
});
}
2020-07-03 08:35:45 +08:00
function getAllUsers() {
return admin
.auth()
.listUsers()
.then((data) => {
return data.users;
});
}
2020-07-03 08:35:45 +08:00
function isUsernameValid(name) {
if (name === null || name === undefined || name === "") return false;
if (/miodec/.test(name.toLowerCase())) return false;
2020-07-22 21:42:15 +08:00
if (/bitly/.test(name.toLowerCase())) return false;
if (name.length > 14) return false;
2020-07-03 08:35:45 +08:00
return /^[0-9a-zA-Z_.-]+$/.test(name);
}
2020-07-03 08:35:45 +08:00
exports.checkNameAvailability = functions.https.onCall((request, response) => {
// 1 - available
// -1 - unavailable (taken)
// -2 - not valid name
// -999 - unknown error
try {
if (!isUsernameValid(request.name)) return -2;
return getAllNames().then((data) => {
let available = 1;
data.forEach((name) => {
try {
if (name.toLowerCase() === request.name.toLowerCase()) available = -1;
} catch (e) {
//
}
2020-07-03 08:35:45 +08:00
});
return available;
});
} catch (e) {
return -999;
}
});
exports.changeName = functions.https.onCall((request, response) => {
try {
if (!isUsernameValid(request.name)) {
console.warn(
`${request.uid} tried to change their name to ${request.name} - not valid`
);
return 0;
}
2020-07-03 08:35:45 +08:00
return getAllNames().then((data) => {
let available = 1;
data.forEach((name) => {
try {
if (name.toLowerCase() === request.name.toLowerCase()) available = 0;
} catch (e) {
//
}
});
if (available === 1) {
return admin
.auth()
.updateUser(request.uid, {
displayName: request.name,
})
.then((d) => {
console.log(
`${request.uid} changed their name to ${request.name} - done`
);
return 1;
})
.catch((e) => {
console.error(
`${request.uid} tried to change their name to ${request.name} - ${e.message}`
);
return -1;
});
} else {
console.warn(
`${request.uid} tried to change their name to ${request.name} - already taken`
);
return 0;
}
});
} catch (e) {
console.error(
`${request.uid} tried to change their name to ${request.name} - ${e}`
);
return -1;
}
});
2020-07-03 08:35:45 +08:00
exports.checkIfNeedsToChangeName = functions.https.onCall(
(request, response) => {
try {
return db
2020-07-04 05:09:29 +08:00
.collection("users")
.doc(request.uid)
.get()
.then((doc) => {
if (
doc.data().name === undefined ||
doc.data().name === null ||
doc.data().name === ""
) {
2020-07-04 05:09:29 +08:00
return admin
.auth()
.getUser(request.uid)
.then((requestUser) => {
if (!isUsernameValid(requestUser.displayName)) {
//invalid name, needs to change
2020-07-03 08:35:45 +08:00
console.log(
`user ${requestUser.uid} ${requestUser.displayName} needs to change name`
);
2020-07-04 05:09:29 +08:00
return 1;
2020-07-03 08:35:45 +08:00
} else {
2020-07-04 05:09:29 +08:00
//valid name, but need to change if not duplicate
return getAllUsers()
.then((users) => {
let sameName = [];
//look for name names
users.forEach((user) => {
if (user.uid !== requestUser.uid) {
try {
if (
user.displayName.toLowerCase() ===
requestUser.displayName.toLowerCase()
) {
sameName.push(user);
}
} catch (e) {
//
2020-07-04 05:09:29 +08:00
}
}
});
if (sameName.length === 0) {
db.collection("users")
2020-07-04 05:09:29 +08:00
.doc(request.uid)
.update({ name: requestUser.displayName })
.then(() => {
return 0;
});
} else {
//check when the request user made the account compared to others
let earliestTimestamp = 999999999999999;
sameName.forEach((sn) => {
let ts =
new Date(sn.metadata.creationTime).getTime() / 1000;
if (ts <= earliestTimestamp) {
earliestTimestamp = ts;
}
});
if (
new Date(
requestUser.metadata.creationTime
).getTime() /
1000 >
earliestTimestamp
) {
console.log(
`user ${requestUser.uid} ${requestUser.displayName} needs to change name`
);
return 2;
} else {
db.collection("users")
.doc(request.uid)
.update({ name: requestUser.displayName })
.then(() => {
return 0;
});
}
2020-07-04 05:09:29 +08:00
}
})
.catch((e) => {
console.error(`error getting all users - ${e}`);
});
2020-07-03 08:35:45 +08:00
}
2020-07-04 05:09:29 +08:00
});
} else {
console.log("name is good");
return 0;
2020-07-03 08:35:45 +08:00
}
});
2020-07-03 08:35:45 +08:00
} catch (e) {
return -1;
}
2020-07-03 08:35:45 +08:00
}
);
function checkIfPB(uid, obj) {
return db
2020-07-03 08:35:45 +08:00
.collection(`users`)
.doc(uid)
.get()
.then((data) => {
let pbs = null;
try {
pbs = data.data().personalBests;
if (pbs === undefined) {
throw new Error("pb is undefined");
2020-06-08 09:24:24 +08:00
}
2020-07-03 08:35:45 +08:00
} catch (e) {
return db
2020-07-03 08:35:45 +08:00
.collection("users")
.doc(uid)
.update({
personalBests: {
[obj.mode]: {
[obj.mode2]: [
{
2020-06-08 09:24:24 +08:00
language: obj.language,
difficulty: obj.difficulty,
punctuation: obj.punctuation,
2020-07-03 08:35:45 +08:00
wpm: obj.wpm,
2020-08-15 23:09:52 +08:00
acc: obj.acc,
raw: obj.rawWpm,
2020-07-03 08:35:45 +08:00
},
],
},
},
})
.then((e) => {
return true;
})
.catch((e) => {
return db
2020-07-03 08:35:45 +08:00
.collection("users")
.doc(uid)
.set({
personalBests: {
[obj.mode]: {
[obj.mode2]: [
{
language: obj.language,
difficulty: obj.difficulty,
punctuation: obj.punctuation,
wpm: obj.wpm,
2020-08-15 23:09:52 +08:00
acc: obj.acc,
raw: obj.rawWpm,
2020-07-03 08:35:45 +08:00
},
],
},
},
})
.then((e) => {
2020-06-08 09:24:24 +08:00
return true;
2020-07-03 08:35:45 +08:00
});
});
}
2020-09-04 02:45:01 +08:00
// //check mode, mode2, punctuation, language and difficulty
2020-07-03 08:35:45 +08:00
let toUpdate = false;
let found = false;
try {
if (pbs[obj.mode][obj.mode2] === undefined) {
pbs[obj.mode][obj.mode2] = [];
}
2020-07-03 08:35:45 +08:00
pbs[obj.mode][obj.mode2].forEach((pb) => {
if (
pb.punctuation === obj.punctuation &&
pb.difficulty === obj.difficulty &&
pb.language === obj.language
) {
//entry like this already exists, compare wpm
found = true;
if (pb.wpm < obj.wpm) {
//new pb
pb.wpm = obj.wpm;
2020-08-15 23:09:52 +08:00
pb.acc = obj.acc;
pb.raw = obj.rawWpm;
2020-07-03 08:35:45 +08:00
toUpdate = true;
} else {
//no pb
return false;
}
}
});
//checked all pbs, nothing found - meaning this is a new pb
if (!found) {
pbs[obj.mode][obj.mode2].push({
language: obj.language,
difficulty: obj.difficulty,
2020-09-04 02:45:01 +08:00
punctuation: obj.punctuation,
2020-07-03 08:35:45 +08:00
wpm: obj.wpm,
2020-08-15 23:09:52 +08:00
acc: obj.acc,
raw: obj.rawWpm,
2020-07-03 08:35:45 +08:00
});
toUpdate = true;
2020-06-08 09:24:24 +08:00
}
2020-07-03 08:35:45 +08:00
} catch (e) {
// console.log(e);
pbs[obj.mode] = {};
pbs[obj.mode][obj.mode2] = [
{
language: obj.language,
difficulty: obj.difficulty,
punctuation: obj.punctuation,
wpm: obj.wpm,
2020-08-15 23:09:52 +08:00
acc: obj.acc,
raw: obj.rawWpm,
2020-07-03 08:35:45 +08:00
},
];
toUpdate = true;
}
if (toUpdate) {
return db
2020-07-03 08:35:45 +08:00
.collection("users")
.doc(uid)
.update({ personalBests: pbs })
.then((e) => {
return true;
});
} else {
return false;
}
});
}
2020-07-07 02:40:52 +08:00
function stdDev(array) {
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
);
}
2020-08-09 03:35:28 +08:00
function roundTo2(num) {
return Math.round((num + Number.EPSILON) * 100) / 100;
}
function validateResult(result) {
if (result.wpm > result.rawWpm) {
console.error(
`Could not validate result for ${result.uid}. ${result.wpm} > ${result.rawWpm}`
);
return false;
}
let wpm = roundTo2((result.correctChars * (60 / result.testDuration)) / 5);
if (
wpm < result.wpm - result.wpm * 0.01 ||
wpm > result.wpm + result.wpm * 0.01
) {
console.error(
2020-08-11 22:16:19 +08:00
`Could not validate result for ${result.uid}. wpm ${wpm} != ${result.wpm}`
2020-08-09 03:35:28 +08:00
);
return false;
}
if (result.allChars != undefined) {
let raw = roundTo2((result.allChars * (60 / result.testDuration)) / 5);
if (
raw < result.rawWpm - result.rawWpm * 0.01 ||
raw > result.rawWpm + result.rawWpm * 0.01
) {
console.error(
2020-08-11 22:16:19 +08:00
`Could not validate result for ${result.uid}. raw ${raw} != ${result.rawWpm}`
);
return false;
}
2020-08-09 03:35:28 +08:00
}
2020-08-10 10:41:19 +08:00
if (result.mode === "time" && (result.mode2 === 15 || result.mode2 === 60)) {
let keyPressTimeSum =
result.keySpacing.reduce((total, val) => {
return total + val;
}) / 1000;
if (
keyPressTimeSum < result.testDuration - 8 ||
keyPressTimeSum > result.testDuration + 1
) {
console.error(
`Could not validate key spacing sum for ${result.uid}. ${keyPressTimeSum} !~ ${result.testDuration}`
);
return false;
}
if (
result.testDuration < result.mode2 - 1 ||
result.testDuration > result.mode2 + 1
) {
console.error(
`Could not validate test duration for ${result.uid}. ${result.testDuration} !~ ${result.mode2}`
);
return false;
}
}
2020-08-09 03:35:28 +08:00
return true;
}
2020-08-22 01:25:30 +08:00
exports.requestTest = functions.https.onRequest((request, response) => {
response.set("Access-Control-Allow-Origin", "*");
response.set("Access-Control-Allow-Headers", "*");
response.set("Access-Control-Allow-Credentials", "true");
response.status(200).send({ data: "test" });
});
2020-08-24 06:27:21 +08:00
exports.getPatreons = functions.https.onRequest(async (request, response) => {
response.set("Access-Control-Allow-Origin", "*");
response.set("Access-Control-Allow-Headers", "*");
response.set("Access-Control-Allow-Credentials", "true");
let patreon = await db.collection("patreon").orderBy("value", "desc").get();
let ret = [];
patreon.docs.forEach((pdoc) => {
ret.push(pdoc.data().name);
});
// console.log(ret);
response.status(200).send({ data: ret });
});
2020-08-22 07:04:23 +08:00
async function incrementTestCounter(uid) {
let userDoc = await db.collection("users").doc(uid).get();
let userData = userDoc.data();
if (userData.completedTests === undefined) {
let results = await db.collection(`users/${uid}/results`).get();
let count = results.docs.length;
db.collection("users")
.doc(uid)
.update({
completedTests: admin.firestore.FieldValue.increment(count),
});
db.collection("public")
.doc("stats")
.update({
completedTests: admin.firestore.FieldValue.increment(count),
});
} else {
db.collection("users")
.doc(uid)
.update({ completedTests: admin.firestore.FieldValue.increment(1) });
db.collection("public")
.doc("stats")
.update({ completedTests: admin.firestore.FieldValue.increment(1) });
}
}
2020-08-22 01:25:30 +08:00
exports.testCompleted = functions.https.onRequest(async (request, response) => {
response.set("Access-Control-Allow-Origin", "*");
if (request.method === "OPTIONS") {
// Send response to OPTIONS requests
response.set("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
response.set("Access-Control-Allow-Headers", "Authorization,Content-Type");
response.set("Access-Control-Max-Age", "3600");
response.status(204).send("");
return;
}
request = request.body.data;
2020-07-03 08:35:45 +08:00
try {
if (request.uid === undefined || request.obj === undefined) {
console.error(`error saving result for ${request.uid} - missing input`);
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { resultCode: -999 } });
return;
2020-07-03 08:35:45 +08:00
}
2020-07-03 08:35:45 +08:00
let obj = request.obj;
2020-08-25 09:18:01 +08:00
function verifyValue(val) {
let errCount = 0;
2020-07-03 08:35:45 +08:00
if (Array.isArray(val)) {
2020-08-25 09:18:01 +08:00
//array
val.forEach((val2) => {
errCount += verifyValue(val2);
});
} else if (typeof val === "object" && !Array.isArray(val)) {
//object
Object.keys(val).forEach((valkey) => {
errCount += verifyValue(val[valkey]);
2020-07-03 08:35:45 +08:00
});
} else {
2020-08-25 09:18:01 +08:00
if (!/^[0-9a-zA-Z._]+$/.test(val)) errCount++;
2020-07-03 08:35:45 +08:00
}
2020-08-25 09:18:01 +08:00
return errCount;
}
let errCount = verifyValue(obj);
2020-08-25 09:19:25 +08:00
// console.log(errCount);
2020-08-25 09:18:01 +08:00
if (errCount > 0) {
2020-07-03 08:35:45 +08:00
console.error(
2020-08-25 09:19:25 +08:00
`error saving result for ${
request.uid
} error count ${errCount} - bad input - ${JSON.stringify(request.obj)}`
2020-07-03 08:35:45 +08:00
);
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { resultCode: -1 } });
return;
2020-07-03 08:35:45 +08:00
}
2020-07-03 08:35:45 +08:00
if (obj.wpm <= 0 || obj.wpm > 350 || obj.acc < 50 || obj.acc > 100) {
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { resultCode: -1 } });
return;
2020-07-03 08:35:45 +08:00
}
2020-08-09 03:35:28 +08:00
if (!validateResult(obj)) {
if (
obj.bailedOut &&
((obj.mode === "time" && obj.mode2 >= 3600) ||
(obj.mode === "words" && obj.mode2 >= 5000) ||
obj.mode === "custom")
) {
//dont give an error
} else {
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { resultCode: -4 } });
return;
}
2020-08-09 03:35:28 +08:00
}
let keySpacing = null;
let keyDuration = null;
2020-07-09 23:22:00 +08:00
try {
keySpacing = {
average:
obj.keySpacing.reduce((previous, current) => (current += previous)) /
obj.keySpacing.length,
sd: stdDev(obj.keySpacing),
};
2020-07-09 23:22:00 +08:00
keyDuration = {
average:
obj.keyDuration.reduce((previous, current) => (current += previous)) /
obj.keyDuration.length,
sd: stdDev(obj.keyDuration),
};
2020-07-09 23:22:00 +08:00
} catch (e) {
console.error(
2020-07-18 06:55:45 +08:00
`cant verify key spacing or duration for user ${request.uid}! - ${e} - ${obj.keySpacing} ${obj.keyDuration}`
2020-07-09 23:22:00 +08:00
);
}
2020-07-07 02:40:52 +08:00
2020-07-21 04:46:38 +08:00
obj.keySpacingStats = keySpacing;
obj.keyDurationStats = keyDuration;
2020-07-21 04:46:38 +08:00
if (obj.mode == "time" && (obj.mode2 == 15 || obj.mode2 == 60)) {
} else {
obj.keySpacing = "removed";
obj.keyDuration = "removed";
}
emailVerified = await admin
.auth()
.getUser(request.uid)
.then((user) => {
return user.emailVerified;
});
return db
.collection("users")
2020-07-08 04:44:03 +08:00
.doc(request.uid)
.get()
.then((ret) => {
let userdata = ret.data();
2020-07-09 22:02:19 +08:00
let name = userdata.name === undefined ? false : userdata.name;
let banned = userdata.banned === undefined ? false : userdata.banned;
let verified = userdata.verified;
request.obj.name = name;
2020-07-07 02:40:52 +08:00
//check keyspacing and duration here
2020-08-09 03:35:28 +08:00
if (obj.mode === "time" && obj.wpm > 130) {
if (verified === false || verified === undefined) {
2020-07-10 01:32:54 +08:00
if (keySpacing !== null && keyDuration !== null) {
2020-07-10 01:31:25 +08:00
if (
2020-08-19 21:29:08 +08:00
keySpacing.sd <= 15 ||
keyDuration.sd <= 10 ||
keyDuration.average < 15 ||
(obj.wpm > 200 && obj.consistency < 60)
2020-07-10 01:31:25 +08:00
) {
console.error(
`possible bot detected by user (${obj.wpm} ${obj.rawWpm} ${
obj.acc
}) ${request.uid} ${name} - spacing ${JSON.stringify(
keySpacing
)} duration ${JSON.stringify(keyDuration)}`
);
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { resultCode: -2 } });
return;
2020-07-10 01:31:25 +08:00
}
2020-07-15 00:13:10 +08:00
if (
(keySpacing.sd > 15 && keySpacing.sd <= 25) ||
(keyDuration.sd > 10 && keyDuration.sd <= 15) ||
(keyDuration.average > 15 && keyDuration.average <= 20)
) {
console.error(
`very close to bot threshold by user (${obj.wpm} ${
obj.rawWpm
} ${obj.acc}) ${
request.uid
} ${name} - spacing ${JSON.stringify(
keySpacing
)} duration ${JSON.stringify(keyDuration)}`
);
}
2020-07-10 01:31:25 +08:00
} else {
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { resultCode: -3 } });
return;
}
}
2020-07-07 02:40:52 +08:00
}
2020-07-08 04:44:03 +08:00
return db
.collection(`users/${request.uid}/results`)
.add(obj)
.then((e) => {
let createdDocId = e.id;
2020-07-07 02:40:52 +08:00
return Promise.all([
checkLeaderboards(
request.obj,
"global",
banned,
name,
verified,
emailVerified
),
checkLeaderboards(
request.obj,
"daily",
banned,
name,
verified,
emailVerified
),
2020-07-07 02:40:52 +08:00
checkIfPB(request.uid, request.obj),
2020-07-12 21:37:26 +08:00
])
.then((values) => {
let globallb = values[0].insertedAt;
let dailylb = values[1].insertedAt;
let ispb = values[2];
// console.log(values);
2020-08-22 07:04:23 +08:00
incrementTestCounter(request.uid);
2020-07-12 21:37:26 +08:00
let usr =
userdata.discordId !== undefined
? userdata.discordId
: userdata.name;
2020-07-09 22:55:02 +08:00
2020-07-08 04:47:51 +08:00
if (
2020-07-12 21:37:26 +08:00
globallb !== null &&
2020-07-13 21:23:40 +08:00
globallb.insertedAt >= 0 &&
globallb.insertedAt <= 9 &&
2020-07-12 21:37:26 +08:00
globallb.newBest
2020-07-08 04:47:51 +08:00
) {
2020-07-12 21:37:26 +08:00
let lbstring = `${obj.mode} ${obj.mode2} global`;
console.log(
`sending command to the bot to announce lb update ${
userdata.discordId
} ${globallb + 1} ${lbstring} ${obj.wpm}`
);
announceLbUpdate(
usr,
globallb.insertedAt + 1,
lbstring,
obj.wpm
);
}
let returnobj = {
resultCode: null,
globalLeaderboard: globallb,
dailyLeaderboard: dailylb,
lbBanned: banned,
name: name,
createdId: createdDocId,
needsToVerify: values[0].needsToVerify,
needsToVerifyEmail: values[0].needsToVerifyEmail,
2020-07-12 21:37:26 +08:00
};
2020-07-12 21:37:26 +08:00
if (ispb) {
2020-08-20 02:39:39 +08:00
let logobj = request.obj;
logobj.keySpacing = "removed";
logobj.keyDuration = "removed";
2020-07-12 21:37:26 +08:00
console.log(
`saved result for ${
request.uid
2020-08-21 08:41:41 +08:00
} (new PB) - ${JSON.stringify(logobj)}`
2020-07-12 21:37:26 +08:00
);
if (
obj.mode === "time" &&
String(obj.mode2) === "60" &&
userdata.discordId !== null &&
userdata.discordId !== undefined
) {
2020-07-24 22:01:53 +08:00
if (verified !== false) {
console.log(
`sending command to the bot to update the role for user ${request.uid} with wpm ${obj.wpm}`
);
updateDiscordRole(
userdata.discordId,
Math.round(obj.wpm)
);
}
2020-07-12 21:37:26 +08:00
}
returnobj.resultCode = 2;
} else {
2020-08-20 02:39:39 +08:00
let logobj = request.obj;
logobj.keySpacing = "removed";
logobj.keyDuration = "removed";
2020-07-09 19:14:00 +08:00
console.log(
2020-07-12 21:37:26 +08:00
`saved result for ${request.uid} - ${JSON.stringify(
2020-08-20 02:39:39 +08:00
logobj
2020-07-12 21:37:26 +08:00
)}`
2020-07-09 19:14:00 +08:00
);
2020-07-12 21:37:26 +08:00
returnobj.resultCode = 1;
2020-07-08 04:44:03 +08:00
}
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: returnobj });
return;
2020-07-12 21:37:26 +08:00
})
.catch((e) => {
console.error(
`error saving result when checking for PB / checking leaderboards for ${request.uid} - ${e.message}`
2020-07-08 04:44:03 +08:00
);
2020-08-22 01:25:30 +08:00
response
.status(200)
.send({ data: { resultCode: -999, message: e.message } });
return;
2020-07-12 21:37:26 +08:00
});
2020-07-08 04:44:03 +08:00
})
.catch((e) => {
console.error(
2020-07-12 21:37:26 +08:00
`error saving result when adding result to the db for ${request.uid} - ${e.message}`
);
2020-08-22 01:25:30 +08:00
response
.status(200)
.send({ data: { resultCode: -999, message: e.message } });
return;
2020-07-08 04:44:03 +08:00
});
2020-07-03 08:35:45 +08:00
})
.catch((e) => {
console.error(
2020-07-07 02:40:52 +08:00
`error saving result when getting user data for ${request.uid} - ${e.message}`
2020-07-03 08:35:45 +08:00
);
2020-08-22 01:25:30 +08:00
response
.status(200)
.send({ data: { resultCode: -999, message: e.message } });
return;
2020-07-03 08:35:45 +08:00
});
} catch (e) {
console.error(
`error saving result for ${request.uid} - ${JSON.stringify(
request.obj
)} - ${e}`
);
2020-08-22 01:25:30 +08:00
response
.status(200)
.send({ data: { resultCode: -999, message: e.message } });
return;
2020-07-03 08:35:45 +08:00
}
});
2020-06-12 01:48:15 +08:00
function updateDiscordRole(discordId, wpm) {
db.collection("bot-commands").add({
command: "updateRole",
arguments: [discordId, wpm],
executed: false,
requestTimestamp: Date.now(),
});
}
2020-07-03 08:35:45 +08:00
function isTagValid(name) {
if (name === null || name === undefined || name === "") return false;
if (name.length > 16) return false;
return /^[0-9a-zA-Z_.-]+$/.test(name);
2020-06-12 01:48:15 +08:00
}
2020-07-03 08:35:45 +08:00
exports.addTag = functions.https.onCall((request, response) => {
try {
if (!isTagValid(request.name)) {
return { resultCode: -1 };
} else {
return db
2020-07-03 08:35:45 +08:00
.collection(`users/${request.uid}/tags`)
.add({
name: request.name,
})
.then((e) => {
console.log(`user ${request.uid} created a tag: ${request.name}`);
return {
resultCode: 1,
id: e.id,
};
})
.catch((e) => {
console.error(
`error while creating tag for user ${request.uid}: ${e.message}`
);
return { resultCode: -999 };
});
2020-06-12 01:48:15 +08:00
}
2020-07-03 08:35:45 +08:00
} catch (e) {
console.error(`error adding tag for ${request.uid} - ${e}`);
return { resultCode: -999 };
}
});
2020-07-03 08:35:45 +08:00
exports.editTag = functions.https.onCall((request, response) => {
try {
if (!isTagValid(request.name)) {
return { resultCode: -1 };
} else {
return db
2020-07-03 08:35:45 +08:00
.collection(`users/${request.uid}/tags`)
.doc(request.tagid)
.update({
name: request.name,
})
.then((e) => {
console.log(`user ${request.uid} updated a tag: ${request.name}`);
return {
resultCode: 1,
};
})
.catch((e) => {
console.error(
`error while updating tag for user ${request.uid}: ${e.message}`
);
return { resultCode: -999 };
});
}
2020-07-03 08:35:45 +08:00
} catch (e) {
console.error(`error updating tag for ${request.uid} - ${e}`);
return { resultCode: -999 };
}
});
2020-06-24 07:54:32 +08:00
2020-07-03 08:35:45 +08:00
exports.removeTag = functions.https.onCall((request, response) => {
try {
return db
2020-07-03 08:35:45 +08:00
.collection(`users/${request.uid}/tags`)
.doc(request.tagid)
.delete()
.then((e) => {
console.log(`user ${request.uid} deleted a tag`);
return {
resultCode: 1,
};
})
.catch((e) => {
console.error(
`error deleting tag for user ${request.uid}: ${e.message}`
);
return { resultCode: -999 };
});
} catch (e) {
console.error(`error deleting tag for ${request.uid} - ${e}`);
return { resultCode: -999 };
}
});
exports.updateResultTags = functions.https.onCall((request, response) => {
try {
let validTags = true;
request.tags.forEach((tag) => {
if (!/^[0-9a-zA-Z]+$/.test(tag)) validTags = false;
});
if (validTags) {
return db
2020-07-03 08:35:45 +08:00
.collection(`users/${request.uid}/results`)
.doc(request.resultid)
.update({
tags: request.tags,
2020-06-24 07:54:32 +08:00
})
2020-07-03 08:35:45 +08:00
.then((e) => {
console.log(
`user ${request.uid} updated tags for result ${request.resultid}`
);
return {
resultCode: 1,
};
})
.catch((e) => {
console.error(
`error while updating tags for result by user ${request.uid}: ${e.message}`
);
return { resultCode: -999 };
});
} else {
console.error(`invalid tags for user ${request.uid}: ${request.tags}`);
return { resultCode: -1 };
2020-06-24 07:54:32 +08:00
}
2020-07-03 08:35:45 +08:00
} catch (e) {
console.error(`error updating tags by ${request.uid} - ${e}`);
return { resultCode: -999 };
}
});
2020-06-28 06:45:24 +08:00
2020-07-03 08:35:45 +08:00
function isConfigKeyValid(name) {
if (name === null || name === undefined || name === "") return false;
if (name.length > 30) return false;
return /^[0-9a-zA-Z_.\-#]+$/.test(name);
2020-06-28 06:45:24 +08:00
}
2020-07-03 08:35:45 +08:00
exports.saveConfig = functions.https.onCall((request, response) => {
try {
if (request.uid === undefined || request.obj === undefined) {
console.error(`error saving config for ${request.uid} - missing input`);
return {
returnCode: -1,
message: "Missing input",
};
2020-07-03 08:35:45 +08:00
}
2020-06-28 06:45:24 +08:00
2020-07-03 08:35:45 +08:00
let obj = request.obj;
let errorMessage = "";
2020-07-03 08:35:45 +08:00
let err = false;
Object.keys(obj).forEach((key) => {
2020-08-10 02:52:00 +08:00
if (err) return;
if (!isConfigKeyValid(key)) {
err = true;
console.error(`${key} failed regex check`);
errorMessage = `${key} failed regex check`;
2020-08-10 02:52:00 +08:00
}
if (err) return;
if (key === "resultFilters") return;
2020-07-03 08:35:45 +08:00
let val = obj[key];
if (Array.isArray(val)) {
val.forEach((valarr) => {
if (!isConfigKeyValid(valarr)) {
err = true;
console.error(`${key}: ${valarr} failed regex check`);
errorMessage = `${key}: ${valarr} failed regex check`;
2020-07-03 08:35:45 +08:00
}
2020-06-28 06:45:24 +08:00
});
2020-07-03 08:35:45 +08:00
} else {
if (!isConfigKeyValid(val)) {
err = true;
console.error(`${key}: ${val} failed regex check`);
errorMessage = `${key}: ${val} failed regex check`;
2020-07-03 08:35:45 +08:00
}
}
});
if (err) {
console.error(
`error saving config for ${request.uid} - bad input - ${JSON.stringify(
request.obj
)}`
);
return {
returnCode: -1,
message: "Bad input. " + errorMessage,
};
2020-06-28 06:45:24 +08:00
}
2020-07-03 08:35:45 +08:00
return db
2020-07-03 08:35:45 +08:00
.collection(`users`)
.doc(request.uid)
.set(
{
config: obj,
},
{ merge: true }
)
.then((e) => {
2020-08-14 06:11:53 +08:00
return {
returnCode: 1,
message: "Saved",
};
2020-07-03 08:35:45 +08:00
})
.catch((e) => {
console.error(
`error saving config to DB for ${request.uid} - ${e.message}`
);
return {
returnCode: -1,
message: e.message,
};
2020-07-03 08:35:45 +08:00
});
} catch (e) {
console.error(`error saving config for ${request.uid} - ${e}`);
return {
resultCode: -999,
message: e,
};
2020-07-03 08:35:45 +08:00
}
});
2020-06-28 06:45:24 +08:00
function generate(n) {
var add = 1,
max = 12 - add;
if (n > max) {
return generate(max) + generate(n - max);
}
max = Math.pow(10, n + add);
var min = max / 10; // Math.pow(10, n) basically
var number = Math.floor(Math.random() * (max - min + 1)) + min;
return ("" + number).substring(add);
}
2020-07-04 02:15:46 +08:00
class Leaderboard {
constructor(size, mode, mode2, type, starting) {
this.size = size;
this.board = [];
this.mode = mode;
this.mode2 = parseInt(mode2);
2020-07-04 02:15:46 +08:00
this.type = type;
if (starting !== undefined && starting !== null) {
starting.forEach((entry) => {
if (
entry.mode == this.mode &&
parseInt(entry.mode2) === parseInt(this.mode2)
) {
let hid = entry.hidden === undefined ? false : entry.hidden;
2020-07-04 02:15:46 +08:00
this.board.push({
uid: entry.uid,
name: entry.name,
2020-07-04 02:15:46 +08:00
wpm: parseFloat(entry.wpm),
2020-07-04 02:29:48 +08:00
raw: parseFloat(entry.raw),
2020-07-04 02:15:46 +08:00
acc: parseFloat(entry.acc),
mode: entry.mode,
mode2: parseInt(entry.mode2),
2020-07-04 02:15:46 +08:00
timestamp: entry.timestamp,
hidden: hid,
2020-07-04 02:15:46 +08:00
});
}
});
}
this.sortBoard();
this.clipBoard();
}
sortBoard() {
this.board.sort((a, b) => {
if (a.wpm === b.wpm) {
if (a.acc === b.acc) {
return a.timestamp - b.timestamp;
} else {
return b.acc - a.acc;
}
} else {
return b.wpm - a.wpm;
}
});
}
clipBoard() {
let boardLength = this.board.length;
if (boardLength > this.size) {
while (this.board.length !== this.size) {
this.board.pop();
}
}
}
logBoard() {
console.log(this.board);
}
removeDuplicates(insertedAt, uid) {
//return true if a better result is found
let found = false;
// let ret;
let foundAt = null;
if (this.board !== undefined) {
this.board.forEach((entry, index) => {
if (entry.uid === uid) {
if (found) {
this.board.splice(index, 1);
// if (index > insertedAt) {
// //removed old result
// ret = false;
// } else {
// ret = true;
// }
} else {
found = true;
foundAt = index;
}
}
});
}
2020-07-04 06:41:42 +08:00
// console.log(ret);
// return ret;
return foundAt;
}
2020-07-04 02:15:46 +08:00
insert(a) {
let insertedAt = -1;
if (a.mode == this.mode && parseInt(a.mode2) === parseInt(this.mode2)) {
2020-07-04 02:15:46 +08:00
this.board.forEach((b, index) => {
if (insertedAt !== -1) return;
if (a.wpm === b.wpm) {
if (a.acc === b.acc) {
if (a.timestamp < b.timestamp) {
this.board.splice(index, 0, {
uid: a.uid,
name: a.name,
2020-07-04 02:15:46 +08:00
wpm: parseFloat(a.wpm),
2020-07-04 03:10:22 +08:00
raw: parseFloat(a.rawWpm),
2020-07-04 02:15:46 +08:00
acc: parseFloat(a.acc),
mode: a.mode,
mode2: parseInt(a.mode2),
2020-07-04 02:15:46 +08:00
timestamp: a.timestamp,
hidden: a.hidden === undefined ? false : a.hidden,
2020-07-04 02:15:46 +08:00
});
insertedAt = index;
}
} else {
if (a.acc > b.acc) {
this.board.splice(index, 0, {
uid: a.uid,
name: a.name,
2020-07-04 02:15:46 +08:00
wpm: parseFloat(a.wpm),
2020-07-04 03:10:22 +08:00
raw: parseFloat(a.rawWpm),
2020-07-04 02:15:46 +08:00
acc: parseFloat(a.acc),
mode: a.mode,
mode2: parseInt(a.mode2),
2020-07-04 02:15:46 +08:00
timestamp: a.timestamp,
hidden: a.hidden === undefined ? false : a.hidden,
2020-07-04 02:15:46 +08:00
});
insertedAt = index;
}
}
} else {
if (a.wpm > b.wpm) {
this.board.splice(index, 0, {
uid: a.uid,
name: a.name,
2020-07-04 02:15:46 +08:00
wpm: parseFloat(a.wpm),
2020-07-04 03:10:22 +08:00
raw: parseFloat(a.rawWpm),
2020-07-04 02:15:46 +08:00
acc: parseFloat(a.acc),
mode: a.mode,
mode2: parseInt(a.mode2),
2020-07-04 02:15:46 +08:00
timestamp: a.timestamp,
hidden: a.hidden === undefined ? false : a.hidden,
2020-07-04 02:15:46 +08:00
});
insertedAt = index;
}
}
});
if (this.board.length < this.size && insertedAt === -1) {
this.board.push({
uid: a.uid,
name: a.name,
2020-07-04 02:15:46 +08:00
wpm: parseFloat(a.wpm),
2020-07-04 03:10:22 +08:00
raw: parseFloat(a.rawWpm),
2020-07-04 02:15:46 +08:00
acc: parseFloat(a.acc),
mode: a.mode,
mode2: parseInt(a.mode2),
2020-07-04 02:15:46 +08:00
timestamp: a.timestamp,
hidden: a.hidden === undefined ? false : a.hidden,
2020-07-04 02:15:46 +08:00
});
insertedAt = this.board.length - 1;
}
// console.log("before duplicate remove");
// console.log(this.board);
let newBest = false;
let foundAt = null;
if (insertedAt >= 0) {
// if (this.removeDuplicates(insertedAt, a.uid)) {
// insertedAt = -2;
// }
foundAt = this.removeDuplicates(insertedAt, a.uid);
if (foundAt >= insertedAt) {
//new better result
newBest = true;
}
}
// console.log(this.board);
2020-07-04 02:15:46 +08:00
this.clipBoard();
return {
insertedAt: insertedAt,
newBest: newBest,
foundAt: foundAt,
};
2020-07-04 02:15:46 +08:00
} else {
return {
insertedAt: -999,
};
2020-07-04 02:15:46 +08:00
}
}
}
2020-08-18 07:31:51 +08:00
exports.generatePairingCode = functions
.runWith({
timeoutSeconds: 100,
memory: "2GB",
2020-08-18 07:31:51 +08:00
})
2020-08-22 01:30:33 +08:00
.https.onRequest((request, response) => {
2020-08-22 01:25:30 +08:00
response.set("Access-Control-Allow-Origin", "*");
if (request.method === "OPTIONS") {
// Send response to OPTIONS requests
response.set("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
response.set(
"Access-Control-Allow-Headers",
"Authorization,Content-Type"
);
response.set("Access-Control-Max-Age", "3600");
response.status(204).send("");
return;
}
request = request.body.data;
2020-08-18 07:31:51 +08:00
try {
if (request === null) {
console.error(
`error while trying to generate discord pairing code - no input`
);
2020-08-22 01:25:30 +08:00
response.status(200).send({ data: { status: -999 } });
return;
2020-08-18 07:31:51 +08:00
}
return db
.collection("users")
.doc(request.uid)
.get()
.then((userDoc) => {
userDocData = userDoc.data();
if (userDocData.discordPairingCode !== undefined) {
console.log(
`user ${request.uid} already has code ${userDocData.discordPairingCode}`
);
return {
status: 2,
pairingCode: userDocData.discordPairingCode,
};
} else {
return db
.collection("users")
.get()
.then((res) => {
let existingCodes = [];
res.docs.forEach((doc) => {
let docData = doc.data();
if (docData.discordPairingCode !== undefined) {
existingCodes.push(docData.discordPairingCode);
}
});
// console.log(`existing codes ${JSON.stringify(existingCodes)}`);
let randomCode = generate(9);
while (existingCodes.includes(randomCode)) {
randomCode = generate(9);
}
return db
.collection("users")
.doc(request.uid)
.update(
{
discordPairingCode: randomCode,
},
{ merge: true }
)
.then((res) => {
console.log(
`generated ${randomCode} for user ${request.uid}`
);
2020-08-22 01:25:30 +08:00
response.status(200).send({
data: {
status: 1,
pairingCode: randomCode,
},
});
return;
2020-08-18 07:31:51 +08:00
})
.catch((e) => {
console.error(
`error while trying to set discord pairing code ${randomCode} for user ${request.uid} - ${e}`
);
2020-08-22 01:25:30 +08:00
response.status(200).send({
data: {
status: -999,
},
});
return;
2020-08-18 07:31:51 +08:00
});
});
}
});
} catch (e) {
console.error(
2020-08-18 07:31:51 +08:00
`error while trying to generate discord pairing code for user ${request.uid} - ${e}`
);
2020-08-22 01:25:30 +08:00
response.status(200).send({
data: {
status: -999,
},
});
return;
}
2020-08-18 07:31:51 +08:00
});
async function checkLeaderboards(
resultObj,
type,
banned,
name,
verified,
emailVerified
) {
2020-07-23 06:27:16 +08:00
//lb disable
2020-07-09 23:52:37 +08:00
// return {
// insertedAt: null,
// };
2020-07-23 06:27:16 +08:00
//
2020-07-04 07:32:45 +08:00
try {
if (emailVerified === false)
return {
insertedAt: null,
needsToVerifyEmail: true,
};
if (!name)
return {
insertedAt: null,
noName: true,
};
2020-07-07 02:40:52 +08:00
if (banned)
return {
insertedAt: null,
banned: true,
};
if (verified === false)
return {
insertedAt: null,
needsToVerify: true,
};
2020-07-04 08:39:29 +08:00
if (
resultObj.mode === "time" &&
["15", "60"].includes(String(resultObj.mode2)) &&
2020-07-05 23:29:57 +08:00
resultObj.language === "english"
2020-07-04 08:39:29 +08:00
) {
return await db.runTransaction(async (t) => {
const lbdoc = await t.get(
db
.collection("leaderboards")
.where("mode", "==", String(resultObj.mode))
.where("mode2", "==", String(resultObj.mode2))
.where("type", "==", type)
);
let lbData;
let docid = `${String(resultObj.mode)}_${String(
resultObj.mode2
)}_${type}`;
if (lbdoc.docs.length === 0) {
console.log(
`no ${resultObj.mode} ${resultObj.mode2} ${type} leaderboard found - creating`
);
let toAdd = {
size: 20,
mode: String(resultObj.mode),
mode2: String(resultObj.mode2),
type: type,
};
await t.set(
db
2020-07-04 08:39:29 +08:00
.collection("leaderboards")
.doc(
`${String(resultObj.mode)}_${String(resultObj.mode2)}_${type}`
),
toAdd
);
lbData = toAdd;
} else {
lbData = lbdoc.docs[0].data();
}
let boardInfo = lbData;
let boardData = lbData.board;
2020-07-04 07:32:45 +08:00
let lb = new Leaderboard(
boardInfo.size,
resultObj.mode,
resultObj.mode2,
boardInfo.type,
boardData
);
let insertResult = lb.insert(resultObj);
2020-07-04 02:15:46 +08:00
if (insertResult.insertedAt >= 0) {
2020-07-04 07:32:45 +08:00
//update the database here
console.log(
2020-07-04 07:53:29 +08:00
`leaderboard changed ${resultObj.mode} ${
resultObj.mode2
} ${type} - ${JSON.stringify(lb.board)}`
2020-07-04 07:32:45 +08:00
);
await t.update(db.collection("leaderboards").doc(docid), {
size: lb.size,
type: lb.type,
board: lb.board,
});
2020-07-04 07:32:45 +08:00
}
2020-07-04 02:15:46 +08:00
2020-07-07 02:40:52 +08:00
return {
insertedAt: insertResult,
};
});
} else {
return {
insertedAt: null,
};
2020-07-04 08:39:29 +08:00
}
2020-07-04 07:32:45 +08:00
} catch (e) {
console.error(
`error while checking leaderboards - ${e} - ${type} ${resultObj}`
);
return {
insertedAt: null,
};
2020-07-04 07:32:45 +08:00
}
2020-07-04 02:15:46 +08:00
}
exports.getLeaderboard = functions.https.onCall((request, response) => {
return db
2020-07-04 02:15:46 +08:00
.collection("leaderboards")
.where("mode", "==", String(request.mode))
.where("mode2", "==", String(request.mode2))
2020-07-04 03:10:22 +08:00
.where("type", "==", String(request.type))
2020-07-04 02:15:46 +08:00
.get()
.then(async (data) => {
2020-07-04 07:58:49 +08:00
// console.log("got data");
2020-07-04 03:10:22 +08:00
if (data.docs.length === 0) return null;
2020-07-04 02:15:46 +08:00
let lbdata = data.docs[0].data();
2020-07-04 03:10:22 +08:00
if (lbdata.board !== undefined) {
2020-07-04 07:58:49 +08:00
// console.log("replacing users");
2020-07-04 05:09:29 +08:00
// for (let i = 0; i < lbdata.board.length; i++) {
// await db
// .collection("users")
// .doc(lbdata.board[i].uid)
// .get()
// .then((doc) => {
// if (
// lbdata.board[i].uid !== null &&
// lbdata.board[i].uid === request.uid
// ) {
// lbdata.board[i].currentUser = true;
// }
// lbdata.board[i].name = doc.data().name;
// lbdata.board[i].uid = null;
// });
// }
lbdata.board.forEach((boardentry) => {
if (boardentry.uid !== null && boardentry.uid === request.uid) {
boardentry.currentUser = true;
}
boardentry.uid = null;
});
// console.log(lbdata);
if (request.type === "daily") {
2020-07-06 00:56:16 +08:00
let resetTime = new Date(Date.now());
resetTime.setHours(0, 0, 0, 0);
2020-07-06 00:27:18 +08:00
resetTime.setDate(resetTime.getUTCDate() + 1);
resetTime = resetTime.valueOf();
lbdata.resetTime = resetTime;
}
2020-07-04 05:09:29 +08:00
2020-07-04 03:10:22 +08:00
return lbdata;
} else {
if (
lbdata.board === undefined ||
lbdata.board === [] ||
lbdata.board.length === 0
) {
return lbdata;
} else {
return [];
}
2020-07-04 02:15:46 +08:00
}
});
});
exports.scheduledFunctionCrontab = functions.pubsub
2020-07-05 23:38:08 +08:00
.schedule("00 00 * * *")
2020-07-06 08:09:17 +08:00
.timeZone("Africa/Abidjan")
.onRun((context) => {
2020-07-04 07:32:45 +08:00
try {
console.log("moving daily leaderboards to history");
db.collection("leaderboards")
2020-07-04 07:32:45 +08:00
.where("type", "==", "daily")
.get()
.then(async (res) => {
for (let i = 0; i < res.docs.length; i++) {
let doc = res.docs[i];
2020-07-04 07:32:45 +08:00
let lbdata = doc.data();
let winnerUid = lbdata.board[0].uid;
await db
.collection("users")
.doc(winnerUid)
.get()
.then(async (userDoc) => {
let userData = userDoc.data();
let lbwins = userData.dailyLbWins;
let lbname = lbdata.mode + lbdata.mode2;
if (lbwins === undefined) {
//first win ever
lbwins = {
[lbname]: 1,
};
} else {
//object already exists
if (lbwins[lbname] === undefined) {
lbwins[lbname] = 1;
} else {
lbwins[lbname] = lbwins[lbname] + 1;
}
}
await db.collection("users").doc(winnerUid).update({
dailyLbWins: lbwins,
});
});
2020-07-11 17:53:04 +08:00
announceDailyLbResult(lbdata);
2020-07-04 07:32:45 +08:00
t = new Date();
db.collection("leaderboards_history")
.doc(
2020-07-06 00:27:18 +08:00
`${t.getUTCDate()}_${t.getUTCMonth()}_${t.getUTCFullYear()}_${
lbdata.mode
}_${lbdata.mode2}`
)
2020-07-04 07:32:45 +08:00
.set(lbdata);
db.collection("leaderboards").doc(doc.id).set(
2020-07-04 07:32:45 +08:00
{
board: [],
},
{ merge: true }
);
}
});
2020-07-04 07:32:45 +08:00
return null;
} catch (e) {
console.error(`error while moving daily leaderboards to history - ${e}`);
}
});
async function announceLbUpdate(discordId, pos, lb, wpm) {
db.collection("bot-commands").add({
2020-07-09 22:55:02 +08:00
command: "sayLbUpdate",
arguments: [discordId, pos, lb, wpm],
executed: false,
requestTimestamp: Date.now(),
});
}
2020-07-11 17:53:04 +08:00
async function announceDailyLbResult(lbdata) {
db.collection("bot-commands").add({
command: "announceDailyLbResult",
arguments: [lbdata],
executed: false,
requestTimestamp: Date.now(),
});
}