Skip to content
Snippets Groups Projects
Commit 176c5a18 authored by AceZephyr's avatar AceZephyr
Browse files

Merge remote-tracking branch 'origin/main' into main

parents c5b32f11 b4fa4b8c
Branches
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ initSqlJs().then(function (SQL) {
const filebuffer = fs.readFileSync(db_path);
db = new SQL.Database(filebuffer);
db.run("CREATE TABLE IF NOT EXISTS users(username VARCHAR(20), pwhash VARCHAR(100));");
db.run("CREATE TABLE IF NOT EXISTS leaderboard(username VARCHAR(20), score INT);");
const data = db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(env["db_path"], buffer);
......@@ -51,6 +52,13 @@ function writeUser(user, hash) {
fs.writeFileSync(env["db_path"], buffer);
}
function writeRecord(user, score) {
db.run("INSERT INTO leaderboard (username, score) VALUES (?, ?);", [user, score]);
const data = db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(env["db_path"], buffer);
}
async function signup(user, pass) {
if (user.length < 2 || user.length > 20)
return {"code": 400, "error": "Username should be 2-20 characters"};
......@@ -79,4 +87,4 @@ async function login(user, pass) {
}
}
module.exports = {login, signup, getUserForToken};
module.exports = {login, signup, getUserForToken, writeRecord};
......@@ -7,7 +7,7 @@
</head>
<body>
<h1>2doku</h1>
<div id="error-container"></div>
<div id="player-info-container">
<h2 id="score-header">Player Scores</h2>
</div>
......@@ -43,6 +43,7 @@
document.querySelector("#join-game").style.display = "none";
displayPlayerScores(args.playerScores);
hideJoinButton();
socket.emit("verify solution", {board: args.boardDisplay, playerScores: args.playerScores});
});
socket.on("game stop", () => {
......@@ -50,7 +51,13 @@
});
document.querySelector("#join-game").onclick = () => {
socket.emit("join game");
socket.emit("join game", null, (duplicate) => {
if (duplicate) {
document.querySelector("#error-container").textContent = "Already joined game.";
document.querySelector("#player-info-container").style.display = "none";
document.querySelector("#board-container").style.display = "none";
}
});
hideJoinButton();
}
......
......@@ -200,6 +200,13 @@ function ensureAuth(req, res, fn) {
fn(user);
}
function writeUser(user, hash) {
db.run("INSERT INTO users (username, pwhash) VALUES (?, ?);", [user, hash]);
const data = db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(env["db_path"], buffer);
}
app.get("/", (req, res) => {
clearBadToken(req, res);
return res.sendFile(__dirname + "/semipublic/index.html");
......@@ -303,10 +310,26 @@ io.on("connection", (socket) => {
game.sendBoard();
});
socket.on("join game", () => {
socket.on("join game", (_, fn) => {
if (!game.playerUsers.includes(user)) {
game.userJoin(socketId, user);
} else {
fn("duplicate");
}
});
socket.on("verify solution", args => {
const { board, playerScores } = args;
// Check for missing/incorrect answers
for (let y = 0; y < 9; y++) {
for (let x = 0; x < 9; x++) {
if (board[y][x] !== game.boardSolution[y][x]) {
return;
}
}
}
// We solved the sudoku!
login.writeRecord(user, playerScores[user]);
});
socket.on("disconnect", () => {
......@@ -316,6 +339,7 @@ io.on("connection", (socket) => {
});
});
httpServer.listen(port, hostname, () => {
console.log(`http://${hostname}:${port}`);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment