Compare commits

..

No commits in common. "f6f5af9758dc53d4b021d197eae928e12c5311c5" and "955171be364c28e965d2f850ba3c681e4a83a78c" have entirely different histories.

4 changed files with 26 additions and 20 deletions

View File

@ -444,7 +444,7 @@ io.on('connection', async (socket) => {
// Teraz utwórz objekt partii w trakcie w bazie Redis
const gameId = uuidv4();
redis.json.set(`game:${gameId}`, '$', {
hostId: opp.request.session.userId,
hostId: opp.request.session.id,
state: "pregame",
startTs: (new Date()).getTime() / 1000,
ready: [false, false],
@ -522,7 +522,7 @@ io.on('connection', async (socket) => {
for (let i = 0; i < members.length; i++) {
const sid = members[i][0];
const socket = io.sockets.sockets.get(sid);
if (socket.request.session.userId === playerGame.data.hostId) {
if (socket.session.id === playerGame.data.hostId) {
io.to(sid).emit('player idx', 0);
} else {
io.to(sid).emit('player idx', 1);
@ -549,7 +549,6 @@ io.on('connection', async (socket) => {
if (!(callback && typeof callback === 'function')) {
return;
}
const playerGame = await GInfo.getPlayerGameData(socket);
let timeLeft = await GInfo.timerLeft(playerGame.id);
@ -594,7 +593,9 @@ io.on('connection', async (socket) => {
await GInfo.timer(playerGame.id, Math.max(timeLeft / 2.5, 15), async () => {
await finishPrepPhase(socket, playerGame);
});
}
} // else if (playerGame.data.ready[1]) {
// // Guest set ready
// }
}
});
@ -636,8 +637,8 @@ io.on('connection', async (socket) => {
let playerGame = await GInfo.getPlayerGameData(socket);
if (playerGame && playerGame.data.state === 'action') {
if (bships.checkTurn(playerGame.data, session.userId)) {
const enemyIdx = session.userId === playerGame.data.hostId ? 1 : 0;
if (bships.checkTurn(playerGame.data, socket.session.id)) {
const enemyIdx = socket.session.id === playerGame.data.hostId ? 1 : 0;
let hit = await GInfo.shootShip(socket, posX, posY);
@ -667,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 ? 0 : 1);
auth.saveMatch(playerGame.id, (new Date).getTime() / 1000 - playerGame.data.startTs, "pvp", hostSocket.session.userId, guestSocket.session.userId, playerGame.data.boards, !enemyIdx ? 1 : 0);
GInfo.resetTimer(playerGame.id);
endGame(playerGame.id);
endGame(playerGame.id, !enemyIdx ? 1 : 0);
return;
}
} else if (hit.status === -1) {

View File

@ -8,10 +8,6 @@
justify-content: center;
text-align: center;
}
#profileView .matchList .match {
height: 7rem;
}
}
@media only screen and (max-width: 820px) {
@ -35,6 +31,10 @@
width: 90vw;
}
#profileView .matchList .match {
height: 7rem;
}
#profileView .match div {
flex-direction: column;
}

View File

@ -7,6 +7,11 @@ 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);
@ -198,7 +203,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 ? 0 : 1)})`, 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 ? 1 : 0)}), (${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();
});

View File

@ -61,14 +61,14 @@ export class GameInfo {
async incrStat(socket, statKey, by = 1) {
const game = await this.redis.json.get(`game:${socket.session.activeGame}`);
const idx = socket.request.session.userId === game.hostId ? 0 : 1;
const idx = socket.request.session.id === game.hostId ? 0 : 1;
this.redis.json.numIncrBy(`game:${socket.session.activeGame}`, `.boards[${idx}].stats.${statKey}`, by);
}
async getPlayerShips(socket) {
const game = await this.redis.json.get(`game:${socket.session.activeGame}`);
const idx = socket.request.session.userId === game.hostId ? 0 : 1;
const idx = socket.request.session.id === game.hostId ? 0 : 1;
return game.boards[idx].ships;
}
@ -101,7 +101,7 @@ export class GameInfo {
const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
await this.redis.json.arrAppend(key, `.boards[${playerIdx}].ships`, shipData);
}
@ -110,7 +110,7 @@ export class GameInfo {
const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
var playerShips = (await this.redis.json.get(key, { path: `.boards[${playerIdx}].ships` }));
@ -221,7 +221,7 @@ export class GameInfo {
const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`});
var deletedShip;
@ -242,7 +242,7 @@ export class GameInfo {
const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
const enemyIdx = socket.request.session.userId === hostId ? 1 : 0;
const enemyIdx = socket.request.session.id === hostId ? 1 : 0;
// const playerIdx = enemyIdx ? 0 : 1;
let playerBoard = await this.redis.json.get(key, { path: `.boards[${enemyIdx}]` });
@ -291,7 +291,7 @@ export class GameInfo {
const key = `game:${gameId}`;
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
await this.redis.json.set(key, `.ready[${playerIdx}]`, true);
}