Skip to content

Commit

Permalink
Implement error message system
Browse files Browse the repository at this point in the history
* Add validation checks for multiplayer
* Add error messages for multiplayer
* Add checkInputTaskTwo()
  • Loading branch information
IDIliev18 authored and IDIliev18 committed Jan 10, 2021
1 parent f72ed0c commit 86bba11
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 231 deletions.
59 changes: 35 additions & 24 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ app.get("/contact", function (req, res) {
res.sendFile(__dirname + "/public/contact.html");
});

app.get("/project", function (req, res) {
res.sendFile(__dirname + "/public/aboutProject.html");
});

app.get(process.env.er1, function (req, res) {
res.sendFile(__dirname + process.env.ee1);
});
Expand Down Expand Up @@ -188,6 +184,13 @@ let GAME_PROGRESS = {
FINISH: 4,
};

let ERROR = {
NO_NAME_P1: 0,
NO_NAME_P2: 1,
INVALID_CODE_GERMAN: 2,
INVALID_CODE_BRITISH: 3,
};

io.on("connection", function (socket) {
console.log("New Connection!");

Expand Down Expand Up @@ -276,8 +279,9 @@ io.on("connection", function (socket) {
const roomId = uuid.v4();
socket.join(roomId);

if (nickname == undefined || role == undefined) {
if (nickname == "" || role == "") {
console.log("User tried to join with no name or role");
io.to(socket.id).emit("error", ERROR.NO_NAME_P1);
return {};
}

Expand All @@ -300,15 +304,14 @@ io.on("connection", function (socket) {

socket.on("playerTwoJoin", function (nickname, roomId) {
// TODO: check if room exist, Stoyane!
if (nickname == undefined) {
if (nickname == "") {
socket.emit("error", ERROR.NO_NAME_P2);
return {};
}
if (roomId != undefined) {
if (multiGameState.hasOwnProperty(roomId)) {
let players = multiGameState.getUsersByRoles();
socket.join(roomId);
let size = Object.keys(multiGameState).length;
console.log(size);

if (
multiGameState[roomId].firstPlayer != undefined &&
multiGameState[roomId].firstPlayerRole != undefined
Expand Down Expand Up @@ -409,15 +412,15 @@ io.on("connection", function (socket) {
if (socket.id == players.britishPlayerId) {
return {};
}
cntCodeSetup++;
if (cntCodeSetup > 2) {
console.log("Game terminated due to cheater in the game");
socket.emit(
"cheaterDetected",
"Game terminated due to cheater in the game"
);
return {};
}
// cntCodeSetup++;
// if (cntCodeSetup > 2) {
// console.log("Game terminated due to cheater in the game");
// socket.emit(
// "cheaterDetected",
// "Game terminated due to cheater in the game"
// );
// return {};
// }
// this.counter++;
// console.log(`Codes entered: ${this.counter}`);
// if (this.counter > 2) {
Expand All @@ -427,16 +430,23 @@ io.on("connection", function (socket) {

console.log(multiGameState[roomId].progress);

if (game.checkInput(code)) {
if (multiGameState[roomId].progress + 1 == 2) {
if (multiGameState[roomId].progress + 1 == 2) {
if (game.checkInput(code)) {
multiGameState[roomId].code = code;
socket.emit("codeGenerated", "Qsha si", code);
} else if (multiGameState[roomId].progress == 3) {
socket.emit("codeGenerated");
} else {
socket.emit("error", ERROR.INVALID_CODE_GERMAN);
}
} else if (multiGameState[roomId].progress == 3) {
if (game.checkInputTaskTwo(code)) {
multiGameState[roomId].code = code;
io.to(players.britishPlayerId).emit(`nextLevelBritish`);
} else {
io.to(players.germanPlayerId).emit(
"error",
ERROR.INVALID_CODE_GERMAN
);
}
} else {
socket.emit("incorrectInput", "Kaval");
}
});

Expand All @@ -456,6 +466,7 @@ io.on("connection", function (socket) {

if (!britishCode || britishCode.length != 4) {
console.log(`Invalid request: !${britishCode}!`);
socket.emit("error", ERROR.INVALID_CODE_BRITISH);
return {};
}

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"body-parser": "^1.19.0",
"bootstrap": "^4.5.3",
"clipboard-copy": "^4.0.1",
"dotenv": "^8.2.0",
"ejs": "^3.1.5",
"express": "^4.17.1",
Expand Down
160 changes: 0 additions & 160 deletions public/aboutProject.html

This file was deleted.

6 changes: 0 additions & 6 deletions public/aboutTeam.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@
data-lang="nav-about-team"
>About Team</a
>
<a
class="dropdown-item"
href="/project"
data-lang="nav-about-project"
>About Project</a
>
<a
class="dropdown-item"
href="/documentation"
Expand Down
6 changes: 0 additions & 6 deletions public/contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@
data-lang="nav-about-team"
>About Team</a
>
<a
class="dropdown-item"
href="/project"
data-lang="nav-about-project"
>About Project</a
>
<a
class="dropdown-item"
href="/documentation"
Expand Down
8 changes: 1 addition & 7 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Crypto Zargan</title>
<link rel="stylesheet" href="css/style.css" />

<link rel="icon" type="image/png" href="img/furdu.png">
<link rel="icon" type="image/png" href="img/furdu.png" />

<!-- Bootstrap -->
<link
Expand Down Expand Up @@ -95,12 +95,6 @@
data-lang="nav-about-team"
>About Team</a
>
<a
class="dropdown-item"
href="/project"
data-lang="nav-about-project"
>About Project</a
>
<a
class="dropdown-item"
href="/documentation"
Expand Down
Loading

0 comments on commit 86bba11

Please sign in to comment.