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

Refactored end of game logic

parent fa0a300c
No related branches found
No related tags found
No related merge requests found
...@@ -53,6 +53,7 @@ function writeUser(user, hash) { ...@@ -53,6 +53,7 @@ function writeUser(user, hash) {
} }
function writeRecord(user, score) { function writeRecord(user, score) {
console.log(user, score)
db.run("INSERT INTO leaderboard (username, score) VALUES (?, ?);", [user, score]); db.run("INSERT INTO leaderboard (username, score) VALUES (?, ?);", [user, score]);
const data = db.export(); const data = db.export();
const buffer = Buffer.from(data); const buffer = Buffer.from(data);
...@@ -76,8 +77,8 @@ async function signup(user, pass) { ...@@ -76,8 +77,8 @@ async function signup(user, pass) {
async function login(user, pass) { async function login(user, pass) {
const row = queryUser(user); const row = queryUser(user);
if (!row) if (!row['username'])
return {"code": 404, "error": "Username does not exist"}; return {"code": 400, "error": "Username/password combination is incorrect"};
try { try {
if (!await argon2.verify(row['pwhash'], pass)) if (!await argon2.verify(row['pwhash'], pass))
return {"code": 400, "error": "Username/password combination is incorrect"}; return {"code": 400, "error": "Username/password combination is incorrect"};
......
...@@ -45,13 +45,19 @@ ...@@ -45,13 +45,19 @@
BOARD1.editable = args.editable; BOARD1.editable = args.editable;
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", () => {
window.location.href = "/"; window.location.href = "/";
}); });
socket.on("game complete", args => {
const bestScore = Object.values(args.playerScores).reduce((a, b) => a > b ? a : b);
const winners = Object.keys(args.playerScores).filter(u => args.playerScores[u] === bestScore).reduce((a,b)=>`${a},${b}`);
window.alert(`Game complete!\nWinner: ${winners} with ${bestScore} points`);
window.location.href = "/";
})
socket.on("please log in", () => { socket.on("please log in", () => {
window.alert("Please log in to join a game."); window.alert("Please log in to join a game.");
window.location.href = "/login"; window.location.href = "/login";
......
...@@ -18,6 +18,8 @@ const hostname = "localhost"; ...@@ -18,6 +18,8 @@ const hostname = "localhost";
const GAMES = new Map(); // maps game id -> game const GAMES = new Map(); // maps game id -> game
const DEBUG_FLAG = false;
const ROOM_OPEN = 0; const ROOM_OPEN = 0;
const ROOM_PLAYING = 1; const ROOM_PLAYING = 1;
const ROOM_TERMINATED = -1; const ROOM_TERMINATED = -1;
...@@ -53,6 +55,10 @@ class Game { ...@@ -53,6 +55,10 @@ class Game {
initGame() { initGame() {
const gen = generateBoard(this.difficulty); const gen = generateBoard(this.difficulty);
this.boardDisplay = gen[0]; this.boardDisplay = gen[0];
if (DEBUG_FLAG) {
this.boardDisplay = JSON.parse(JSON.stringify(gen[1]));
this.boardDisplay[0][0] = 0;
}
this.boardSolution = gen[1]; this.boardSolution = gen[1];
this.boardLock = ARR9.map(y => ARR9.map(x => this.boardDisplay[y][x] ? 1 : 0)); this.boardLock = ARR9.map(y => ARR9.map(x => this.boardDisplay[y][x] ? 1 : 0));
this.boardColors = ARR9.map(y => ARR9.map(x => this.boardDisplay[y][x] ? 1 : 0)); this.boardColors = ARR9.map(y => ARR9.map(x => this.boardDisplay[y][x] ? 1 : 0));
...@@ -79,6 +85,24 @@ class Game { ...@@ -79,6 +85,24 @@ class Game {
} }
} }
isSolved() {
for (let y = 0; y < 9; y++) {
for (let x = 0; x < 9; x++) {
if (this.boardDisplay[y][x] !== this.boardSolution[y][x]) {
return false;
}
}
}
return true;
}
endGame() {
this.sendAll("game complete", this.packageBoardState(false));
for (let user in this.playerScores) {
login.writeRecord(user, this.playerScores[user]);
}
}
input(client, user, x, y, val) { input(client, user, x, y, val) {
let dPoints = 0; let dPoints = 0;
if (!(typeof x === "number" && typeof y === "number" && typeof val === "number" && 0 <= x && x <= 8 && 0 <= y && y <= 8 && 1 <= val && val <= 9)) if (!(typeof x === "number" && typeof y === "number" && typeof val === "number" && 0 <= x && x <= 8 && 0 <= y && y <= 8 && 1 <= val && val <= 9))
...@@ -97,6 +121,11 @@ class Game { ...@@ -97,6 +121,11 @@ class Game {
} }
this.boardDisplay[y][x] = val; this.boardDisplay[y][x] = val;
this.playerScores[user] += dPoints; this.playerScores[user] += dPoints;
if (this.isSolved()) {
this.endGame();
}
return true; return true;
} }
...@@ -327,20 +356,6 @@ io.on("connection", (socket) => { ...@@ -327,20 +356,6 @@ io.on("connection", (socket) => {
} }
}); });
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", () => {
if (game) { if (game) {
game.leave(socketId, user); game.leave(socketId, user);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment