Skip to content
Snippets Groups Projects
Commit 888e17c9 authored by SuperChrisBoy's avatar SuperChrisBoy
Browse files

Some changes from main

parents 85de6f85 5c06f34e
Branches
No related tags found
No related merge requests found
...@@ -53,7 +53,7 @@ function writeUser(user, hash) { ...@@ -53,7 +53,7 @@ function writeUser(user, hash) {
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-16 characters"}; return {"code": 400, "error": "Username should be 2-20 characters"};
if (pass.length < 8 || pass.length > 32) if (pass.length < 8 || pass.length > 32)
return {"code": 400, "error": "Password should be 8-32 characters"}; return {"code": 400, "error": "Password should be 8-32 characters"};
if (userExists(user)) if (userExists(user))
......
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
<script src="/socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>
<script src="/game_render.js"></script> <script src="/game_render.js"></script>
<script> <script>
function hideJoinButton() {
document.querySelector("#join-game").style.display = "none";
}
const socket = io({ const socket = io({
query: { query: {
gameId: window.location.href.split("/").filter(e => e.length > 0).pop() gameId: window.location.href.split("/").filter(e => e.length > 0).pop()
...@@ -28,12 +33,18 @@ ...@@ -28,12 +33,18 @@
BOARD1 = new SudokuBoard("board-container", updateToServer); BOARD1 = new SudokuBoard("board-container", updateToServer);
BOARD1.updateBoard(args.boardDisplay, args.boardLock, args.boardColors); BOARD1.updateBoard(args.boardDisplay, args.boardLock, args.boardColors);
displayPlayerScores(args.playerScores); displayPlayerScores(args.playerScores);
hideJoinButton();
}); });
socket.on("game stop", () => { socket.on("game stop", () => {
window.location.href = "/"; window.location.href = "/";
}); });
document.querySelector("#join-game").onclick = () => {
socket.emit("join game");
hideJoinButton();
}
function updateToServer(x, y, num) { function updateToServer(x, y, num) {
socket.emit("cell input", { "x": x, "y": y, "num": num }); socket.emit("cell input", { "x": x, "y": y, "num": num });
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<a href="/"><- Back</a><br> <a href="/"><- Back</a><br>
<a href="/login">Login</a> <a href="/login">Login</a>
<h1>Create Account</h1> <h1>Create Account</h1>
<label for="username">Username (2-16 characters):</label> <label for="username">Username (2-20 characters):</label>
<input type="text" id="username" name="username" /><br><br> <input type="text" id="username" name="username" /><br><br>
<label for="password">Password (8-32 characters):</label> <label for="password">Password (8-32 characters):</label>
<input type="password" id="password" name="password"/><br><br> <input type="password" id="password" name="password"/><br><br>
......
...@@ -61,15 +61,19 @@ class Game { ...@@ -61,15 +61,19 @@ class Game {
this.sendBoard(); this.sendBoard();
} }
sendBoard() { packageBoardState() {
// do not send board state if game is not in progress return {
if (this.state === ROOM_PLAYING) {
this.sendAll("board state", {
boardDisplay: this.boardDisplay, boardDisplay: this.boardDisplay,
boardLock: this.boardLock, boardLock: this.boardLock,
boardColors: this.boardColors, boardColors: this.boardColors,
playerScores: this.playerScores playerScores: this.playerScores
}); };
}
sendBoard() {
// do not send board state if game is not in progress
if (this.state === ROOM_PLAYING) {
this.sendAll("board state", this.packageBoardState());
} }
} }
...@@ -94,10 +98,6 @@ class Game { ...@@ -94,10 +98,6 @@ class Game {
return true; return true;
} }
validateCell(args) {
return this.boardSolution[args.y][args.x] === args.num;
}
userJoin(clientId, user) { userJoin(clientId, user) {
if (this.state === ROOM_OPEN && this.playerUsers.length < 2) { if (this.state === ROOM_OPEN && this.playerUsers.length < 2) {
this.playerUsers.push(user); this.playerUsers.push(user);
...@@ -131,15 +131,13 @@ class Game { ...@@ -131,15 +131,13 @@ class Game {
userLeave(client, user) { userLeave(client, user) {
this.usersToClients.delete(user); this.usersToClients.delete(user);
if (this.playerUsers.includes(user)) { if (this.connectedClients.includes(client)) {
this.kill();
} else if (this.connectedClients.includes(client)) {
this.connectedClients.splice(this.connectedClients.indexOf(client), 1); this.connectedClients.splice(this.connectedClients.indexOf(client), 1);
} }
} }
sendAll(name, message = null) { send(name, clients, message = null) {
for (let clientId of this.connectedClients) { for (let clientId of clients) {
io.in(clientId).fetchSockets().then(sockets => { io.in(clientId).fetchSockets().then(sockets => {
sockets.forEach(socket => { sockets.forEach(socket => {
socket.emit(name, message) socket.emit(name, message)
...@@ -148,6 +146,10 @@ class Game { ...@@ -148,6 +146,10 @@ class Game {
} }
} }
sendAll(name, message = null) {
this.send(name, this.connectedClients, message);
}
constructor(id, difficulty) { constructor(id, difficulty) {
this.id = id; this.id = id;
this.difficulty = difficulty; this.difficulty = difficulty;
...@@ -156,9 +158,9 @@ class Game { ...@@ -156,9 +158,9 @@ class Game {
this.boardLock = []; this.boardLock = [];
this.boardColors = []; this.boardColors = [];
this.connectedClients = []; this.connectedClients = [];
this.usersToClients = new Map();
this.playerUsers = []; this.playerUsers = [];
this.playerScores = {}; this.playerScores = {};
this.usersToClients = new Map();
this.state = ROOM_OPEN; this.state = ROOM_OPEN;
} }
} }
...@@ -284,6 +286,10 @@ io.on("connection", (socket) => { ...@@ -284,6 +286,10 @@ io.on("connection", (socket) => {
game.clientJoin(socketId); game.clientJoin(socketId);
console.log(`Client connected: ${socketId} to game ${gameId}`); console.log(`Client connected: ${socketId} to game ${gameId}`);
if (game.playerUsers.includes(user)) {
socket.emit("board state", game.packageBoardState());
}
socket.on("cell input", args => { socket.on("cell input", args => {
if (game.playerUsers.includes(user)) { if (game.playerUsers.includes(user)) {
game.input(socketId, user, args.x, args.y, args.num); game.input(socketId, user, args.x, args.y, args.num);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment