Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save the winner and loser in the DB after "Game Ended" event is called #333

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
trying to save to json
ChenemiKen committed Sep 3, 2021

Verified

This commit was signed with the committer’s verified signature.
sandy081 Sandeep Somavarapu
commit 5376f3911310a067ac6514c7c4ec74d2c09b02f7
2,001 changes: 3 additions & 1,998 deletions server/package-lock.json

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion server/public/index.html
Original file line number Diff line number Diff line change
@@ -24,6 +24,9 @@ <h2 id="update"></h2>
<input type="text" id="move-input" />
<button id="send-btn">send</button>
</div>

<br><br>
<button id="end-game">End game</button>

<script src="/socket.io/socket.io.js"></script>
<script>
@@ -42,7 +45,9 @@ <h2 id="update"></h2>
.addEventListener("click", async () => {
//create a new game
const response = await fetch(
"https://chess.zuri.chat/api/createGame"
// "https://chess.zuri.chat/api/createGame"
// TODO : change back to remote
"http://127.0.0.1:5050/api/createGame"
);
const body = await response.json();

@@ -92,7 +97,21 @@ <h2 id="update"></h2>
// update game Id
gameId = joinGameInput.value;
document.getElementById("game-id").innerText = `Game Id: ${gameId}`;

//
});

document.getElementById("end-game").addEventListener('click', ()=>{
stale_mate = false
if(stale_mate){
winner = null;
looser = null;
}else{
winner = "PlayerX";
looser = "PlayerY";
}
socket.emit('game_ended',{game_id:gameId, winner:winner, looser:looser, stale_mate:stale_mate});
})
</script>
</body>
</html>
1 change: 1 addition & 0 deletions server/src/db/mockdb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"game_id":"","winner":"PlayerX","looser":"PlayerY","stale_mate":false}
55 changes: 55 additions & 0 deletions server/src/socket/game_end_handler.socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Node Core Modules
const fs = require('fs');
const Path = require('path')

// Custom Modules
const formatMessage = require("./libs/formatMessage");

const game_end_handler = (socket)=>(msg)=>{
game_id = msg.game_id
var game_details ={
"game_id" : game_id,
"winner" : msg.winner,
"looser" : msg.looser,
"stale_mate" : msg.stale_mate
}
// game_details = JSON.stringify(game_details, null, 4);

// Temporary
save_game(game_details);

}

// Temporary
function save_game(game_data){
const dbpath = Path.join(__dirname, "../db/mockdb.json")
if(fs.existsSync(dbpath)){
fs.readFile(dbpath, 'utf8', (err, data) => {
if (err) {
console.log(`Error reading database file: ${err}`);
} else {
// parse JSON string to JSON object
var db_data = JSON.parse(data);
console.log(db_data)
// db_data = array(db_data)
// add a new record
db_data.push(game_data);
// write new data back to the file
fs.writeFile(dbpath, JSON.stringify(db_data, null, 4), (err) => {
if (err) {
console.log(`Error writing database file: ${err}`);
}
});
}

});
}else{
fs.writeFile(dbpath, JSON.stringify(game_data, null, 4), (err) => {
if (err) {
console.log(`Error writing database file: ${err}`);
}
});
}
}

module.exports = game_end_handler;
3 changes: 3 additions & 0 deletions server/src/socket/index.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
const join_room_handler = require("./join_room_handler.socket.js");
const leave_room_handler = require("./leave_room_handler.socket.js");
const piece_moved_handler = require("./piece_moved_handler.socket.js");
const game_end_handler = require("./game_end_handler.socket.js");

const socket = (socket) => {
console.log("Connected succesfully to the socket ...");
@@ -12,6 +13,8 @@ const socket = (socket) => {
socket.on("leave_room", leave_room_handler(socket));

socket.on("piece_moved", piece_moved_handler(socket));

socket.on("game_ended", game_end_handler(socket));
};

module.exports = socket;