diff --git a/index.js b/index.js index f34f9e2..5e92e98 100644 --- a/index.js +++ b/index.js @@ -638,7 +638,7 @@ io.on('connection', async (socket) => { if (playerGame && playerGame.data.state === 'action') { if (bships.checkTurn(playerGame.data, socket.session.id)) { - const enemyIdx = socket.session.id === playerGame.data.hostId ? 1 : 0; + const enemyIdx = session.userId === playerGame.data.hostId ? 1 : 0; let hit = await GInfo.shootShip(socket, posX, posY); @@ -668,10 +668,10 @@ io.on('connection', async (socket) => { guestSocket.emit("game finished", !enemyIdx ? 1 : 0, hostNickname); playerGame = await GInfo.getPlayerGameData(socket); - auth.saveMatch(playerGame.id, (new Date).getTime() / 1000 - playerGame.data.startTs, "pvp", hostSocket.session.userId, guestSocket.session.userId, playerGame.data.boards, !enemyIdx ? 1 : 0); + auth.saveMatch(playerGame.id, (new Date).getTime() / 1000 - playerGame.data.startTs, "pvp", hostSocket.session.userId, guestSocket.session.userId, playerGame.data.boards, enemyIdx ? 0 : 1); GInfo.resetTimer(playerGame.id); - endGame(playerGame.id, !enemyIdx ? 1 : 0); + endGame(playerGame.id); return; } } else if (hit.status === -1) { diff --git a/utils/auth.js b/utils/auth.js index 821149a..87bf844 100644 --- a/utils/auth.js +++ b/utils/auth.js @@ -7,11 +7,6 @@ import geoip from 'geoip-lite'; import mysql from 'mysql'; import readline from "node:readline"; -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout -}); - const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -203,7 +198,7 @@ export class MailAuth { const conn = mysql.createConnection(this.mysqlOptions); conn.query(`INSERT INTO matches(match_id, match_type, host_id, guest_id, duration) VALUES (${conn.escape(matchId)}, ${conn.escape(type)}, ${conn.escape(hostId)}, ${conn.escape(guestId)}, ${conn.escape(duration)})`, async (error) => { if (error) reject(error); - else conn.query(`INSERT INTO statistics(match_id, user_id, board, won) VALUES (${conn.escape(matchId)}, ${conn.escape(hostId)}, ${conn.escape(JSON.stringify(boards[0]))}, ${conn.escape(!winnerIdx ? 1 : 0)}), (${conn.escape(matchId)}, ${conn.escape(guestId)}, ${conn.escape(JSON.stringify(boards[1]))}, ${conn.escape(winnerIdx ? 1 : 0)})`, async (error, response) => { + else conn.query(`INSERT INTO statistics(match_id, user_id, board, won) VALUES (${conn.escape(matchId)}, ${conn.escape(hostId)}, ${conn.escape(JSON.stringify(boards[0]))}, ${conn.escape(winnerIdx ? 0 : 1)}), (${conn.escape(matchId)}, ${conn.escape(guestId)}, ${conn.escape(JSON.stringify(boards[1]))}, ${conn.escape(winnerIdx ? 1 : 0)})`, async (error, response) => { if (error) reject(error); else resolve(); });