mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-06 13:40:16 +08:00
removed unecessary createBlankLb function, removed mongob package
This commit is contained in:
parent
82ca581f6d
commit
61ae1084da
4 changed files with 17 additions and 632 deletions
|
@ -25,25 +25,24 @@ app.use(express.static(mtRootDir + "/dist"));
|
|||
app.use(bodyParser.json());
|
||||
|
||||
// Initialize database leaderboards if no leaderboards exist
|
||||
function createBlankLeaderboards() {
|
||||
let lb = {
|
||||
size: 999,
|
||||
board: [],
|
||||
mode: "time",
|
||||
mode2: 15,
|
||||
type: "global",
|
||||
};
|
||||
Leaderboard.create(lb);
|
||||
lb.mode2 = 60;
|
||||
Leaderboard.create(lb);
|
||||
lb.type = "daily";
|
||||
lb.size = 100;
|
||||
Leaderboard.create(lb);
|
||||
lb.mode2 = 15;
|
||||
Leaderboard.create(lb);
|
||||
}
|
||||
Leaderboard.findOne((err, lb) => {
|
||||
if (lb === null) createBlankLeaderboards();
|
||||
if (lb === null) {
|
||||
let lb = {
|
||||
size: 999,
|
||||
board: [],
|
||||
mode: "time",
|
||||
mode2: 15,
|
||||
type: "global",
|
||||
};
|
||||
Leaderboard.create(lb);
|
||||
lb.mode2 = 60;
|
||||
Leaderboard.create(lb);
|
||||
lb.type = "daily";
|
||||
lb.size = 100;
|
||||
Leaderboard.create(lb);
|
||||
lb.mode2 = 15;
|
||||
Leaderboard.create(lb);
|
||||
}
|
||||
});
|
||||
|
||||
function authenticateToken(req, res, next) {
|
||||
|
|
|
@ -233,213 +233,6 @@ exports.updateResultTags = functions.https.onCall((request, response) => {
|
|||
}
|
||||
});
|
||||
|
||||
class Leaderboard {
|
||||
constructor(size, mode, mode2, type, starting) {
|
||||
this.size = size;
|
||||
this.board = [];
|
||||
this.mode = mode;
|
||||
this.mode2 = parseInt(mode2);
|
||||
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;
|
||||
this.board.push({
|
||||
uid: entry.uid,
|
||||
name: entry.name,
|
||||
wpm: parseFloat(entry.wpm),
|
||||
raw: parseFloat(entry.raw),
|
||||
acc: parseFloat(entry.acc),
|
||||
consistency: isNaN(parseInt(entry.consistency))
|
||||
? "-"
|
||||
: parseInt(entry.consistency),
|
||||
mode: entry.mode,
|
||||
mode2: parseInt(entry.mode2),
|
||||
timestamp: entry.timestamp,
|
||||
hidden: hid,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
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);
|
||||
}
|
||||
getMinWpm() {
|
||||
return this.board[this.board.length - 1].wpm;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
// console.log(ret);
|
||||
// return ret;
|
||||
return foundAt;
|
||||
}
|
||||
insert(a) {
|
||||
let insertedAt = -1;
|
||||
if (a.mode == this.mode && parseInt(a.mode2) === parseInt(this.mode2)) {
|
||||
if (
|
||||
this.board.length !== this.size ||
|
||||
(this.board.length === this.size && a.wpm > this.getMinWpm())
|
||||
) {
|
||||
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,
|
||||
wpm: parseFloat(a.wpm),
|
||||
raw: parseFloat(a.rawWpm),
|
||||
acc: parseFloat(a.acc),
|
||||
consistency: isNaN(parseInt(a.consistency))
|
||||
? "-"
|
||||
: parseInt(a.consistency),
|
||||
mode: a.mode,
|
||||
mode2: parseInt(a.mode2),
|
||||
timestamp: a.timestamp,
|
||||
hidden: a.hidden === undefined ? false : a.hidden,
|
||||
});
|
||||
insertedAt = index;
|
||||
}
|
||||
} else {
|
||||
if (a.acc > b.acc) {
|
||||
this.board.splice(index, 0, {
|
||||
uid: a.uid,
|
||||
name: a.name,
|
||||
wpm: parseFloat(a.wpm),
|
||||
raw: parseFloat(a.rawWpm),
|
||||
acc: parseFloat(a.acc),
|
||||
consistency: isNaN(parseInt(a.consistency))
|
||||
? "-"
|
||||
: parseInt(a.consistency),
|
||||
mode: a.mode,
|
||||
mode2: parseInt(a.mode2),
|
||||
timestamp: a.timestamp,
|
||||
hidden: a.hidden === undefined ? false : a.hidden,
|
||||
});
|
||||
insertedAt = index;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (a.wpm > b.wpm) {
|
||||
this.board.splice(index, 0, {
|
||||
uid: a.uid,
|
||||
name: a.name,
|
||||
wpm: parseFloat(a.wpm),
|
||||
raw: parseFloat(a.rawWpm),
|
||||
acc: parseFloat(a.acc),
|
||||
consistency: isNaN(parseInt(a.consistency))
|
||||
? "-"
|
||||
: parseInt(a.consistency),
|
||||
mode: a.mode,
|
||||
mode2: parseInt(a.mode2),
|
||||
timestamp: a.timestamp,
|
||||
hidden: a.hidden === undefined ? false : a.hidden,
|
||||
});
|
||||
insertedAt = index;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (this.board.length < this.size && insertedAt === -1) {
|
||||
this.board.push({
|
||||
uid: a.uid,
|
||||
name: a.name,
|
||||
wpm: parseFloat(a.wpm),
|
||||
raw: parseFloat(a.rawWpm),
|
||||
acc: parseFloat(a.acc),
|
||||
consistency: isNaN(parseInt(a.consistency))
|
||||
? "-"
|
||||
: parseInt(a.consistency),
|
||||
mode: a.mode,
|
||||
mode2: parseInt(a.mode2),
|
||||
timestamp: a.timestamp,
|
||||
hidden: a.hidden === undefined ? false : a.hidden,
|
||||
});
|
||||
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);
|
||||
this.clipBoard();
|
||||
return {
|
||||
insertedAt: insertedAt,
|
||||
newBest: newBest,
|
||||
foundAt: foundAt,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
insertedAt: -999,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
insertedAt: -999,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.unlinkDiscord = functions.https.onRequest((request, response) => {
|
||||
response.set("Access-Control-Allow-Origin", origin);
|
||||
if (request.method === "OPTIONS") {
|
||||
|
@ -493,411 +286,6 @@ exports.unlinkDiscord = functions.https.onRequest((request, response) => {
|
|||
}
|
||||
});
|
||||
|
||||
exports.checkLeaderboards = functions.https.onRequest(
|
||||
async (request, response) => {
|
||||
response.set("Access-Control-Allow-Origin", 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;
|
||||
|
||||
function verifyValue(val) {
|
||||
let errCount = 0;
|
||||
if (val === null || val === undefined) {
|
||||
} else if (Array.isArray(val)) {
|
||||
//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]);
|
||||
});
|
||||
} else {
|
||||
if (!/^[0-9a-zA-Z._\-\+]+$/.test(val)) errCount++;
|
||||
}
|
||||
return errCount;
|
||||
}
|
||||
let errCount = verifyValue(request);
|
||||
if (errCount > 0) {
|
||||
console.error(
|
||||
`error checking leaderboard for ${
|
||||
request.uid
|
||||
} error count ${errCount} - bad input - ${JSON.stringify(request.obj)}`
|
||||
);
|
||||
response.status(200).send({
|
||||
data: {
|
||||
status: -999,
|
||||
message: "Bad input",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let emailVerified = await admin
|
||||
.auth()
|
||||
.getUser(request.uid)
|
||||
.then((user) => {
|
||||
return user.emailVerified;
|
||||
});
|
||||
|
||||
try {
|
||||
if (emailVerified === false) {
|
||||
response.status(200).send({
|
||||
data: {
|
||||
needsToVerifyEmail: true,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (request.name === undefined) {
|
||||
response.status(200).send({
|
||||
data: {
|
||||
noName: true,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (request.banned) {
|
||||
response.status(200).send({
|
||||
data: {
|
||||
banned: true,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (request.verified === false) {
|
||||
response.status(200).send({
|
||||
data: {
|
||||
needsToVerify: true,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
request.result.name = request.name;
|
||||
|
||||
if (
|
||||
request.result.mode === "time" &&
|
||||
["15", "60"].includes(String(request.result.mode2)) &&
|
||||
request.result.language === "english" &&
|
||||
request.result.funbox === "none"
|
||||
) {
|
||||
let global = await db
|
||||
.runTransaction(async (t) => {
|
||||
const lbdoc = await t.get(
|
||||
db
|
||||
.collection("leaderboards")
|
||||
.where("mode", "==", String(request.result.mode))
|
||||
.where("mode2", "==", String(request.result.mode2))
|
||||
.where("type", "==", "global")
|
||||
);
|
||||
// let lbData;
|
||||
let docid = `${String(request.result.mode)}_${String(
|
||||
request.result.mode2
|
||||
)}_${"global"}`;
|
||||
// if (lbdoc.docs.length === 0) {
|
||||
// console.log(
|
||||
// `no ${request.mode} ${request.mode2} ${type} leaderboard found - creating`
|
||||
// );
|
||||
// let toAdd = {
|
||||
// size: 20,
|
||||
// mode: String(request.mode),
|
||||
// mode2: String(request.mode2),
|
||||
// type: type,
|
||||
// };
|
||||
// t.set(
|
||||
// db
|
||||
// .collection("leaderboards")
|
||||
// .doc(
|
||||
// `${String(request.mode)}_${String(request.mode2)}_${type}`
|
||||
// ),
|
||||
// toAdd
|
||||
// );
|
||||
// lbData = toAdd;
|
||||
// } else {
|
||||
// lbData = lbdoc.docs[0].data();
|
||||
// }
|
||||
let boardInfo = lbdoc.docs[0].data();
|
||||
if (
|
||||
boardInfo.minWpm === undefined ||
|
||||
boardInfo.board.length !== boardInfo.size ||
|
||||
(boardInfo.minWpm !== undefined &&
|
||||
request.result.wpm > boardInfo.minWpm &&
|
||||
boardInfo.board.length === boardInfo.size)
|
||||
) {
|
||||
let boardData = boardInfo.board;
|
||||
let lb = new Leaderboard(
|
||||
boardInfo.size,
|
||||
request.result.mode,
|
||||
request.result.mode2,
|
||||
boardInfo.type,
|
||||
boardData
|
||||
);
|
||||
let insertResult = lb.insert(request.result);
|
||||
if (insertResult.insertedAt >= 0) {
|
||||
t.update(db.collection("leaderboards").doc(docid), {
|
||||
size: lb.size,
|
||||
type: lb.type,
|
||||
board: lb.board,
|
||||
minWpm: lb.getMinWpm(),
|
||||
});
|
||||
}
|
||||
return insertResult;
|
||||
} else {
|
||||
//not above leaderboard minwpm
|
||||
return {
|
||||
insertedAt: -1,
|
||||
};
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
`error in transaction checking leaderboards - ${error}`
|
||||
);
|
||||
response.status(200).send({
|
||||
data: {
|
||||
status: -999,
|
||||
message: error,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
let daily = await db
|
||||
.runTransaction(async (t) => {
|
||||
const lbdoc = await t.get(
|
||||
db
|
||||
.collection("leaderboards")
|
||||
.where("mode", "==", String(request.result.mode))
|
||||
.where("mode2", "==", String(request.result.mode2))
|
||||
.where("type", "==", "daily")
|
||||
);
|
||||
// let lbData;
|
||||
let docid = `${String(request.result.mode)}_${String(
|
||||
request.result.mode2
|
||||
)}_${"daily"}`;
|
||||
// if (lbdoc.docs.length === 0) {
|
||||
// console.log(
|
||||
// `no ${request.mode} ${request.mode2} ${type} leaderboard found - creating`
|
||||
// );
|
||||
// let toAdd = {
|
||||
// size: 20,
|
||||
// mode: String(request.mode),
|
||||
// mode2: String(request.mode2),
|
||||
// type: type,
|
||||
// };
|
||||
// t.set(
|
||||
// db
|
||||
// .collection("leaderboards")
|
||||
// .doc(
|
||||
// `${String(request.mode)}_${String(request.mode2)}_${type}`
|
||||
// ),
|
||||
// toAdd
|
||||
// );
|
||||
// lbData = toAdd;
|
||||
// } else {
|
||||
// lbData = lbdoc.docs[0].data();
|
||||
// }
|
||||
let boardInfo = lbdoc.docs[0].data();
|
||||
if (
|
||||
boardInfo.minWpm === undefined ||
|
||||
boardInfo.board.length !== boardInfo.size ||
|
||||
(boardInfo.minWpm !== undefined &&
|
||||
request.result.wpm > boardInfo.minWpm &&
|
||||
boardInfo.board.length === boardInfo.size)
|
||||
) {
|
||||
let boardData = boardInfo.board;
|
||||
let lb = new Leaderboard(
|
||||
boardInfo.size,
|
||||
request.result.mode,
|
||||
request.result.mode2,
|
||||
boardInfo.type,
|
||||
boardData
|
||||
);
|
||||
let insertResult = lb.insert(request.result);
|
||||
if (insertResult.insertedAt >= 0) {
|
||||
t.update(db.collection("leaderboards").doc(docid), {
|
||||
size: lb.size,
|
||||
type: lb.type,
|
||||
board: lb.board,
|
||||
minWpm: lb.getMinWpm(),
|
||||
});
|
||||
}
|
||||
return insertResult;
|
||||
} else {
|
||||
//not above leaderboard minwpm
|
||||
return {
|
||||
insertedAt: -1,
|
||||
};
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
`error in transaction checking leaderboards - ${error}`
|
||||
);
|
||||
response.status(200).send({
|
||||
data: {
|
||||
status: -999,
|
||||
message: error,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
//send discord update
|
||||
let usr =
|
||||
request.discordId != undefined ? request.discordId : request.name;
|
||||
|
||||
if (
|
||||
global !== null &&
|
||||
global.insertedAt >= 0 &&
|
||||
global.insertedAt <= 9 &&
|
||||
global.newBest
|
||||
) {
|
||||
let lbstring = `${request.result.mode} ${request.result.mode2} global`;
|
||||
console.log(
|
||||
`sending command to the bot to announce lb update ${usr} ${
|
||||
global.insertedAt + 1
|
||||
} ${lbstring} ${request.result.wpm}`
|
||||
);
|
||||
|
||||
announceLbUpdate(
|
||||
usr,
|
||||
global.insertedAt + 1,
|
||||
lbstring,
|
||||
request.result.wpm,
|
||||
request.result.rawWpm,
|
||||
request.result.acc,
|
||||
request.result.consistency
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
if (
|
||||
// obj.mode === "time" &&
|
||||
// (obj.mode2 == "15" || obj.mode2 == "60") &&
|
||||
// obj.language === "english"
|
||||
global !== null ||
|
||||
daily !== null
|
||||
) {
|
||||
let updatedLbMemory = await getUpdatedLbMemory(
|
||||
request.lbMemory,
|
||||
request.result.mode,
|
||||
request.result.mode2,
|
||||
global,
|
||||
daily
|
||||
);
|
||||
db.collection("users").doc(request.uid).update({
|
||||
lbMemory: updatedLbMemory,
|
||||
});
|
||||
}
|
||||
|
||||
response.status(200).send({
|
||||
data: {
|
||||
status: 2,
|
||||
daily: daily,
|
||||
global: global,
|
||||
},
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
response.status(200).send({
|
||||
data: {
|
||||
status: 1,
|
||||
daily: {
|
||||
insertedAt: null,
|
||||
},
|
||||
global: {
|
||||
insertedAt: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
response.status(200).send({
|
||||
data: {
|
||||
status: -999,
|
||||
message: e,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
exports.getLeaderboard = functions.https.onCall((request, response) => {
|
||||
return db
|
||||
.collection("leaderboards")
|
||||
.where("mode", "==", String(request.mode))
|
||||
.where("mode2", "==", String(request.mode2))
|
||||
.where("type", "==", String(request.type))
|
||||
.get()
|
||||
.then(async (data) => {
|
||||
// console.log("got data");
|
||||
if (data.docs.length === 0) return null;
|
||||
let lbdata = data.docs[0].data();
|
||||
if (lbdata.board !== undefined) {
|
||||
// console.log("replacing users");
|
||||
|
||||
// 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") {
|
||||
let resetTime = new Date(Date.now());
|
||||
resetTime.setHours(0, 0, 0, 0);
|
||||
resetTime.setDate(resetTime.getUTCDate() + 1);
|
||||
resetTime = resetTime.valueOf();
|
||||
lbdata.resetTime = resetTime;
|
||||
}
|
||||
|
||||
return lbdata;
|
||||
} else {
|
||||
if (
|
||||
lbdata.board === undefined ||
|
||||
lbdata.board === [] ||
|
||||
lbdata.board.length === 0
|
||||
) {
|
||||
return lbdata;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
async function announceLbUpdate(discordId, pos, lb, wpm, raw, acc, con) {
|
||||
db.collection("bot-commands").add({
|
||||
command: "sayLbUpdate",
|
||||
|
|
1
package-lock.json
generated
1
package-lock.json
generated
|
@ -20,7 +20,6 @@
|
|||
"express": "^4.17.1",
|
||||
"js-cookie": "^2.2.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongodb": "^3.6.6",
|
||||
"mongoose": "^5.12.8",
|
||||
"tinycolor2": "^1.4.2"
|
||||
},
|
||||
|
|
|
@ -53,7 +53,6 @@
|
|||
"express": "^4.17.1",
|
||||
"js-cookie": "^2.2.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongodb": "^3.6.6",
|
||||
"mongoose": "^5.12.8",
|
||||
"tinycolor2": "^1.4.2"
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue