Skip to content
Snippets Groups Projects
Commit 88f25a67 authored by SuperChrisBoy's avatar SuperChrisBoy
Browse files

Merge branch 'main' into chris

parents df139b85 b4fa4b8c
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ initSqlJs().then(function (SQL) { ...@@ -16,6 +16,7 @@ initSqlJs().then(function (SQL) {
const filebuffer = fs.readFileSync(db_path); const filebuffer = fs.readFileSync(db_path);
db = new SQL.Database(filebuffer); 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 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 data = db.export();
const buffer = Buffer.from(data); const buffer = Buffer.from(data);
fs.writeFileSync(env["db_path"], buffer); fs.writeFileSync(env["db_path"], buffer);
...@@ -51,6 +52,13 @@ function writeUser(user, hash) { ...@@ -51,6 +52,13 @@ function writeUser(user, hash) {
fs.writeFileSync(env["db_path"], buffer); 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) { async function signup(user, pass) {
if (user.length < 2 || user.length > 20) if (user.length < 2 || user.length > 20)
return {"code": 400, "error": "Username should be 2-20 characters"}; return {"code": 400, "error": "Username should be 2-20 characters"};
...@@ -79,4 +87,4 @@ async function login(user, pass) { ...@@ -79,4 +87,4 @@ async function login(user, pass) {
} }
} }
module.exports = {login, signup, getUserForToken}; module.exports = {login, signup, getUserForToken, writeRecord};
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
</head> </head>
<body> <body>
<h1>2doku</h1> <h1>2doku</h1>
<div id="error-container"></div>
<div id="player-info-container"> <div id="player-info-container">
<h2 id="score-header">Player Scores</h2> <h2 id="score-header">Player Scores</h2>
</div> </div>
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
BOARD1.updateBoard(args.boardDisplay, args.boardLock, args.boardColors); BOARD1.updateBoard(args.boardDisplay, args.boardLock, args.boardColors);
displayPlayerScores(args.playerScores); displayPlayerScores(args.playerScores);
hideJoinButton(); hideJoinButton();
socket.emit("verify solution", {board: args.boardDisplay, playerScores: args.playerScores});
}); });
socket.on("game stop", () => { socket.on("game stop", () => {
...@@ -41,7 +42,13 @@ ...@@ -41,7 +42,13 @@
}); });
document.querySelector("#join-game").onclick = () => { 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(); hideJoinButton();
} }
......
...@@ -200,6 +200,13 @@ function ensureAuth(req, res, fn) { ...@@ -200,6 +200,13 @@ function ensureAuth(req, res, fn) {
fn(user); 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) => { app.get("/", (req, res) => {
clearBadToken(req, res); clearBadToken(req, res);
return res.sendFile(__dirname + "/semipublic/index.html"); return res.sendFile(__dirname + "/semipublic/index.html");
...@@ -297,10 +304,26 @@ io.on("connection", (socket) => { ...@@ -297,10 +304,26 @@ io.on("connection", (socket) => {
game.sendBoard(); game.sendBoard();
}); });
socket.on("join game", () => { socket.on("join game", (_, fn) => {
if (!game.playerUsers.includes(user)) { if (!game.playerUsers.includes(user)) {
game.userJoin(socketId, 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", () => { socket.on("disconnect", () => {
...@@ -310,6 +333,7 @@ io.on("connection", (socket) => { ...@@ -310,6 +333,7 @@ io.on("connection", (socket) => {
}); });
}); });
httpServer.listen(port, hostname, () => { httpServer.listen(port, hostname, () => {
console.log(`http://${hostname}:${port}`); console.log(`http://${hostname}:${port}`);
}); });
{
"db_path": "../login.sqlite"
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment