Skip to content

Commit

Permalink
Merge pull request #6 from HuyenThoaiVuaHung/bugfixes
Browse files Browse the repository at this point in the history
[fix] added new listener to update kd data
  • Loading branch information
maik205 committed Aug 21, 2024
2 parents 3e30ecf + a1f7a85 commit 7dab8da
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require("fs");
const { join } = require('path');
const config = require("./utils/config.json");
const compression = require('compression');
const matchDataHelper = require("./utils/matchdata.helper.js");
const app = express();
const os = require('os');
app.use(cors(
Expand Down Expand Up @@ -46,7 +47,14 @@ var matchDataPath = "match_data/123.json";

var socketIDs = ["", "", "", ""];
var adminId = "";
var matchData = JSON.parse(fs.readFileSync(matchDataPath));
var matchData;
if (fs.existsSync(matchDataPath)) {
matchData = JSON.parse(fs.readFileSync(matchDataPath));
}
else {
matchData = matchDataHelper.provideNewMatchData();
fs.writeFileSync(matchDataPath, JSON.stringify(matchData));
}
var lastTurnId = "";
let lastStealingPlayerId = -1;
var kdCurrentMaxQuesNo = 0;
Expand Down Expand Up @@ -275,7 +283,16 @@ io.on("connection", (socket) => {
}

});
socket.on('update-kd-data', (data) => {

fs.writeFileSync(
JSON.parse(fs.readFileSync(matchDataPath)).KDFilePath,
JSON.stringify(data)
);
socket.emit("update-kd-data-admin", fs.readFileSync(JSON.parse(fs.readFileSync(matchDataPath)).KDFilePath));


})
socket.on("edit-player-info", (payload, callback) => {
if (adminId == socket.id) {
matchData.players[payload.index] = payload.player;
Expand Down
24 changes: 24 additions & 0 deletions utils/matchdata.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function provideNewMatchData() {
return {

"matchName": "Legendary",
"matchPos": "H",
"players": [
{ "id": 1, "name": "Lê Nguyễn Huy Hoàng", "score": 40, "isReady": false },
{ "id": 2, "name": "Dương Tiến Hải", "score": 300, "isReady": false },
{ "id": 3, "name": "Trịnh Long Vũ", "score": 0, "isReady": false },
{ "id": 4, "name": "Nguyễn Phúc Sang", "score": 0, "isReady": false }
],
"KDFilePath": "match_data/round_data/kd_1.json",
"VCNVFilePath": "match_data/round_data/vcnv_1.json",
"TangTocFilePath": "match_data/round_data/tt_1.json",
"VedichFilePath": "match_data/round_data/vd_1.json",
"ChpFilePath": "match_data/round_data/chp_1.json",
"pauseTime": 0
}
}


module.exports = {
provideNewMatchData
}

0 comments on commit 7dab8da

Please sign in to comment.