mirror of
https://github.com/MaciejkaG/statki.git
synced 2024-11-30 02:52:54 +01:00
Compare commits
4 Commits
955171be36
...
f6f5af9758
Author | SHA1 | Date | |
---|---|---|---|
|
f6f5af9758 | ||
|
1bd84bee09 | ||
|
8969d6242e | ||
|
fd9c4a8ea7 |
17
index.js
17
index.js
@ -444,7 +444,7 @@ io.on('connection', async (socket) => {
|
|||||||
// Teraz utwórz objekt partii w trakcie w bazie Redis
|
// Teraz utwórz objekt partii w trakcie w bazie Redis
|
||||||
const gameId = uuidv4();
|
const gameId = uuidv4();
|
||||||
redis.json.set(`game:${gameId}`, '$', {
|
redis.json.set(`game:${gameId}`, '$', {
|
||||||
hostId: opp.request.session.id,
|
hostId: opp.request.session.userId,
|
||||||
state: "pregame",
|
state: "pregame",
|
||||||
startTs: (new Date()).getTime() / 1000,
|
startTs: (new Date()).getTime() / 1000,
|
||||||
ready: [false, false],
|
ready: [false, false],
|
||||||
@ -522,7 +522,7 @@ io.on('connection', async (socket) => {
|
|||||||
for (let i = 0; i < members.length; i++) {
|
for (let i = 0; i < members.length; i++) {
|
||||||
const sid = members[i][0];
|
const sid = members[i][0];
|
||||||
const socket = io.sockets.sockets.get(sid);
|
const socket = io.sockets.sockets.get(sid);
|
||||||
if (socket.session.id === playerGame.data.hostId) {
|
if (socket.request.session.userId === playerGame.data.hostId) {
|
||||||
io.to(sid).emit('player idx', 0);
|
io.to(sid).emit('player idx', 0);
|
||||||
} else {
|
} else {
|
||||||
io.to(sid).emit('player idx', 1);
|
io.to(sid).emit('player idx', 1);
|
||||||
@ -549,6 +549,7 @@ io.on('connection', async (socket) => {
|
|||||||
if (!(callback && typeof callback === 'function')) {
|
if (!(callback && typeof callback === 'function')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const playerGame = await GInfo.getPlayerGameData(socket);
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
||||||
let timeLeft = await GInfo.timerLeft(playerGame.id);
|
let timeLeft = await GInfo.timerLeft(playerGame.id);
|
||||||
|
|
||||||
@ -593,9 +594,7 @@ io.on('connection', async (socket) => {
|
|||||||
await GInfo.timer(playerGame.id, Math.max(timeLeft / 2.5, 15), async () => {
|
await GInfo.timer(playerGame.id, Math.max(timeLeft / 2.5, 15), async () => {
|
||||||
await finishPrepPhase(socket, playerGame);
|
await finishPrepPhase(socket, playerGame);
|
||||||
});
|
});
|
||||||
} // else if (playerGame.data.ready[1]) {
|
}
|
||||||
// // Guest set ready
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -637,8 +636,8 @@ io.on('connection', async (socket) => {
|
|||||||
let playerGame = await GInfo.getPlayerGameData(socket);
|
let playerGame = await GInfo.getPlayerGameData(socket);
|
||||||
|
|
||||||
if (playerGame && playerGame.data.state === 'action') {
|
if (playerGame && playerGame.data.state === 'action') {
|
||||||
if (bships.checkTurn(playerGame.data, socket.session.id)) {
|
if (bships.checkTurn(playerGame.data, session.userId)) {
|
||||||
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);
|
let hit = await GInfo.shootShip(socket, posX, posY);
|
||||||
|
|
||||||
@ -668,10 +667,10 @@ io.on('connection', async (socket) => {
|
|||||||
guestSocket.emit("game finished", !enemyIdx ? 1 : 0, hostNickname);
|
guestSocket.emit("game finished", !enemyIdx ? 1 : 0, hostNickname);
|
||||||
|
|
||||||
playerGame = await GInfo.getPlayerGameData(socket);
|
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);
|
GInfo.resetTimer(playerGame.id);
|
||||||
endGame(playerGame.id, !enemyIdx ? 1 : 0);
|
endGame(playerGame.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (hit.status === -1) {
|
} else if (hit.status === -1) {
|
||||||
|
@ -8,6 +8,10 @@
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#profileView .matchList .match {
|
||||||
|
height: 7rem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media only screen and (max-width: 820px) {
|
@media only screen and (max-width: 820px) {
|
||||||
@ -31,10 +35,6 @@
|
|||||||
width: 90vw;
|
width: 90vw;
|
||||||
}
|
}
|
||||||
|
|
||||||
#profileView .matchList .match {
|
|
||||||
height: 7rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
#profileView .match div {
|
#profileView .match div {
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,6 @@ import geoip from 'geoip-lite';
|
|||||||
import mysql from 'mysql';
|
import mysql from 'mysql';
|
||||||
import readline from "node:readline";
|
import readline from "node:readline";
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
|
||||||
input: process.stdin,
|
|
||||||
output: process.stdout
|
|
||||||
});
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
@ -203,7 +198,7 @@ export class MailAuth {
|
|||||||
const conn = mysql.createConnection(this.mysqlOptions);
|
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) => {
|
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);
|
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 ? 1 : 0)}), (${conn.escape(matchId)}, ${conn.escape(guestId)}, ${conn.escape(JSON.stringify(boards[1]))}, ${conn.escape(winnerIdx ? 0 : 1)})`, async (error, response) => {
|
||||||
if (error) reject(error);
|
if (error) reject(error);
|
||||||
else resolve();
|
else resolve();
|
||||||
});
|
});
|
||||||
|
@ -61,14 +61,14 @@ export class GameInfo {
|
|||||||
|
|
||||||
async incrStat(socket, statKey, by = 1) {
|
async incrStat(socket, statKey, by = 1) {
|
||||||
const game = await this.redis.json.get(`game:${socket.session.activeGame}`);
|
const game = await this.redis.json.get(`game:${socket.session.activeGame}`);
|
||||||
const idx = socket.request.session.id === game.hostId ? 0 : 1;
|
const idx = socket.request.session.userId === game.hostId ? 0 : 1;
|
||||||
|
|
||||||
this.redis.json.numIncrBy(`game:${socket.session.activeGame}`, `.boards[${idx}].stats.${statKey}`, by);
|
this.redis.json.numIncrBy(`game:${socket.session.activeGame}`, `.boards[${idx}].stats.${statKey}`, by);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPlayerShips(socket) {
|
async getPlayerShips(socket) {
|
||||||
const game = await this.redis.json.get(`game:${socket.session.activeGame}`);
|
const game = await this.redis.json.get(`game:${socket.session.activeGame}`);
|
||||||
const idx = socket.request.session.id === game.hostId ? 0 : 1;
|
const idx = socket.request.session.userId === game.hostId ? 0 : 1;
|
||||||
return game.boards[idx].ships;
|
return game.boards[idx].ships;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ export class GameInfo {
|
|||||||
const key = `game:${gameId}`;
|
const key = `game:${gameId}`;
|
||||||
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
||||||
|
|
||||||
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
|
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
|
||||||
await this.redis.json.arrAppend(key, `.boards[${playerIdx}].ships`, shipData);
|
await this.redis.json.arrAppend(key, `.boards[${playerIdx}].ships`, shipData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ export class GameInfo {
|
|||||||
const key = `game:${gameId}`;
|
const key = `game:${gameId}`;
|
||||||
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
||||||
|
|
||||||
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
|
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
|
||||||
|
|
||||||
var playerShips = (await this.redis.json.get(key, { path: `.boards[${playerIdx}].ships` }));
|
var playerShips = (await this.redis.json.get(key, { path: `.boards[${playerIdx}].ships` }));
|
||||||
|
|
||||||
@ -221,7 +221,7 @@ export class GameInfo {
|
|||||||
const key = `game:${gameId}`;
|
const key = `game:${gameId}`;
|
||||||
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
||||||
|
|
||||||
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
|
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
|
||||||
let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`});
|
let playerShips = await this.redis.json.get(key, {path: `.boards[${playerIdx}].ships`});
|
||||||
|
|
||||||
var deletedShip;
|
var deletedShip;
|
||||||
@ -242,7 +242,7 @@ export class GameInfo {
|
|||||||
const key = `game:${gameId}`;
|
const key = `game:${gameId}`;
|
||||||
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
||||||
|
|
||||||
const enemyIdx = socket.request.session.id === hostId ? 1 : 0;
|
const enemyIdx = socket.request.session.userId === hostId ? 1 : 0;
|
||||||
// const playerIdx = enemyIdx ? 0 : 1;
|
// const playerIdx = enemyIdx ? 0 : 1;
|
||||||
|
|
||||||
let playerBoard = await this.redis.json.get(key, { path: `.boards[${enemyIdx}]` });
|
let playerBoard = await this.redis.json.get(key, { path: `.boards[${enemyIdx}]` });
|
||||||
@ -291,7 +291,7 @@ export class GameInfo {
|
|||||||
const key = `game:${gameId}`;
|
const key = `game:${gameId}`;
|
||||||
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
const hostId = (await this.redis.json.get(key, { path: '.hostId' }));
|
||||||
|
|
||||||
const playerIdx = socket.request.session.id === hostId ? 0 : 1;
|
const playerIdx = socket.request.session.userId === hostId ? 0 : 1;
|
||||||
|
|
||||||
await this.redis.json.set(key, `.ready[${playerIdx}]`, true);
|
await this.redis.json.set(key, `.ready[${playerIdx}]`, true);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user