mirror of
https://github.com/MaciejkaG/statki.git
synced 2024-11-30 07:22:55 +01:00
Compare commits
No commits in common. "8732ff929b3f8ea577a6a01c802e0b99617ecc71" and "9a2d02478c36434aa57babf97afe6367e1fca58a" have entirely different histories.
8732ff929b
...
9a2d02478c
@ -1 +0,0 @@
|
||||
e733201e-f39d-4d91-9e7f-4c2d2c4a81da
|
38
index.js
38
index.js
@ -6,7 +6,6 @@ import { createServer } from 'node:http';
|
||||
import { Server } from 'socket.io';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import fs from 'node:fs';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import session from "express-session";
|
||||
import { engine } from 'express-handlebars';
|
||||
@ -72,28 +71,15 @@ let sessionStore = new SessionRedisStore({
|
||||
prefix: "statkiSession:",
|
||||
});
|
||||
|
||||
var sessionSecret = uuidv4();
|
||||
let secretPath = path.join(__dirname, '.session.secret');
|
||||
|
||||
if (fs.existsSync(secretPath)) {
|
||||
sessionSecret = fs.readFileSync(secretPath);
|
||||
} else {
|
||||
fs.writeFile(secretPath, sessionSecret, function (err) {
|
||||
if (err) {
|
||||
console.log("An error occured while saving a freshly generated session secret.\nSessions may not persist after a restart of the server.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const sessionMiddleware = session({
|
||||
store: sessionStore,
|
||||
secret: sessionSecret,
|
||||
secret: uuidv4(),
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
rolling: true,
|
||||
cookie: {
|
||||
secure: checkFlag("cookie_secure"),
|
||||
maxAge: 7 * 24 * 60 * 60 * 1000,
|
||||
maxAge: 3 * 24 * 60 * 60 * 1000,
|
||||
},
|
||||
});
|
||||
|
||||
@ -477,7 +463,7 @@ io.on('connection', async (socket) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (!await GInfo.isPlayerInGame(socket) && session.nickname != null) {
|
||||
if (!await GInfo.isPlayerInGame(socket) || session.nickname != null) {
|
||||
// if (session.nickname == null) {
|
||||
// socket.disconnect();
|
||||
// return;
|
||||
@ -794,28 +780,16 @@ io.on('connection', async (socket) => {
|
||||
if (hit.gameFinished) {
|
||||
const members = [...roomMemberIterator(playerGame.id)];
|
||||
|
||||
let hostSocket;
|
||||
let guestSocket;
|
||||
|
||||
members.forEach(player => {
|
||||
player = player[0];
|
||||
const playerSocket = io.sockets.sockets.get(player);
|
||||
|
||||
if (playerSocket.session.userId === playerGame.data.hostId) {
|
||||
hostSocket = playerSocket;
|
||||
} else {
|
||||
guestSocket = playerSocket;
|
||||
}
|
||||
});
|
||||
|
||||
let hostSocket = io.sockets.sockets.get(members[0][0]);
|
||||
let hostNickname = hostSocket.session.nickname;
|
||||
let guestSocket = io.sockets.sockets.get(members[1][0]);
|
||||
let guestNickname = guestSocket.session.nickname;
|
||||
|
||||
hostSocket.emit("game finished", !enemyIdx ? 1 : 0, guestNickname);
|
||||
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);
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 59 KiB |
Binary file not shown.
Before Width: | Height: | Size: 95 KiB |
Binary file not shown.
Before Width: | Height: | Size: 104 KiB |
Binary file not shown.
Before Width: | Height: | Size: 58 KiB |
Binary file not shown.
Before Width: | Height: | Size: 67 KiB |
Binary file not shown.
Before Width: | Height: | Size: 2.5 KiB |
@ -114,16 +114,16 @@ $("#createGameButton").on("click", function () {
|
||||
case "ok":
|
||||
console.log("Lobby created");
|
||||
$("#createGameCode").val(response.gameCode);
|
||||
lockUI(false);
|
||||
switchView("pvpCreateView");
|
||||
returnLock = true;
|
||||
lockUI(false);
|
||||
break;
|
||||
|
||||
case "alreadyInLobby":
|
||||
console.log("Lobby creation failed (player is already in a lobby)");
|
||||
$("#createGameCode").val(response.gameCode);
|
||||
lockUI(false);
|
||||
switchView("pvpCreateView");
|
||||
lockUI(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -163,10 +163,16 @@ form.addEventListener('submit', (e) => {
|
||||
case "ok":
|
||||
console.log("Joined a lobby by:", response.oppNickname);
|
||||
$("#oppNameField").html(response.oppNickname);
|
||||
lockUI(false);
|
||||
switchView("preparingGame");
|
||||
lockUI(false);
|
||||
break;
|
||||
|
||||
//case "alreadyInLobby":
|
||||
// $("#createGameCode").val(response.gameCode);
|
||||
// switchView("pvpCreateView");
|
||||
// lockUI(false);
|
||||
// break;
|
||||
|
||||
default:
|
||||
alert(`${window.locale["Unknown error occured"]}\n${window.locale["Status:"]} ${response.status}`);
|
||||
lockUI(false);
|
||||
|
@ -1,8 +1,6 @@
|
||||
if ("serviceWorker" in navigator) {
|
||||
window.addEventListener("load", () => {
|
||||
window.addEventListener("load", function () {
|
||||
navigator.serviceWorker
|
||||
.register("/serviceWorker.js")
|
||||
.register("/assets/js/service-worker.js")
|
||||
.then(res => console.log("Service worker registered"))
|
||||
.catch(err => console.log("Service worker not registered", err));
|
||||
});
|
||||
}
|
||||
});
|
@ -1,6 +1,5 @@
|
||||
{
|
||||
"name": "Statki",
|
||||
"description": "The #1 online multiplayer battleships game\nModern, simple UI, PvP and PvE modes, advanced statistics and more.",
|
||||
"start_url": "/",
|
||||
"background_color": "black",
|
||||
"theme_color": "black",
|
||||
@ -8,48 +7,8 @@
|
||||
"icons": [
|
||||
{
|
||||
"src": "/assets/img/statki-logo-crop.png",
|
||||
"sizes": "1080x1080"
|
||||
},
|
||||
{
|
||||
"src": "/assets/img/statki-logo-crop-144.png",
|
||||
"sizes": "144x144"
|
||||
}
|
||||
],
|
||||
"screenshots": [
|
||||
{
|
||||
"src": "/assets/img/screenshot_game2.png",
|
||||
"sizes": "1920x911",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide",
|
||||
"label": "Middle of the round"
|
||||
},
|
||||
{
|
||||
"src": "/assets/img/screenshot_mainmenu.png",
|
||||
"sizes": "1920x911",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide",
|
||||
"label": "Main menu"
|
||||
},
|
||||
{
|
||||
"src": "/assets/img/screenshot_create.png",
|
||||
"sizes": "1920x911",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide",
|
||||
"label": "Create game screen"
|
||||
},
|
||||
{
|
||||
"src": "/assets/img/screenshot_profile.png",
|
||||
"sizes": "1920x911",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide",
|
||||
"label": "Main menu"
|
||||
},
|
||||
{
|
||||
"src": "/assets/img/screenshot_game1.png",
|
||||
"sizes": "1920x911",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide",
|
||||
"label": "Preparation phase"
|
||||
"sizes": "1080x1080",
|
||||
"type": "image/png"
|
||||
}
|
||||
],
|
||||
"display": "standalone"
|
||||
|
241
test.js
Normal file
241
test.js
Normal file
@ -0,0 +1,241 @@
|
||||
function findEmptyFields(grid, len) {
|
||||
const rowPlacements = [];
|
||||
|
||||
// Helper function to check if a row can be placed horizontally at a given position
|
||||
function canPlaceHorizontally(x, y) {
|
||||
console.log(x, y);
|
||||
// console.log(x + len)
|
||||
// console.log(x + len >= grid.length)
|
||||
if (x + len >= grid[0].length) {
|
||||
return false; // Ship exceeds board boundaries
|
||||
}
|
||||
for (let i = x; i < x + len; i++) {
|
||||
if (grid[i][y]) {
|
||||
return false; // One of ship's fields is already occupied
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper function to check if a row can be placed vertically at a given position
|
||||
function canPlaceVertically(x, y) {
|
||||
// console.log(y + len)
|
||||
// console.log(y + len >= grid.length)
|
||||
if (y + len >= grid.length) {
|
||||
return false; // Ship exceeds board boundaries
|
||||
}
|
||||
for (let i = y; i < y + len; i++) {
|
||||
if (grid[x][i]) {
|
||||
return false; // One of ship's fields is already occupied
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (let i = 0; i < grid.length; i++) {
|
||||
for (let j = 0; j < grid[0].length; j++) {
|
||||
if (grid[j][i] === false) {
|
||||
if (canPlaceHorizontally(j, i)) {
|
||||
rowPlacements.push({ posX: j, posY: i, rot: 0 });
|
||||
}
|
||||
|
||||
if (canPlaceVertically(j, i)) {
|
||||
rowPlacements.push({ posX: j, posY: i, rot: 1 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rowPlacements;
|
||||
}
|
||||
|
||||
let data = {
|
||||
hostId: "123456",
|
||||
state: "action",
|
||||
boards: [
|
||||
{
|
||||
ships: [
|
||||
{ type: 3, posX: 3, posY: 4, rot: 0, hits: [false, false, false] },
|
||||
],
|
||||
shots: [],
|
||||
},
|
||||
{
|
||||
ships: [],
|
||||
shots: [],
|
||||
}
|
||||
],
|
||||
nextPlayer: 0,
|
||||
}
|
||||
|
||||
// checkHit(data, 1, 0, 0);
|
||||
|
||||
// console.log(validateShipPosition(type, posX, posY, rot));
|
||||
|
||||
let boardRender = [];
|
||||
|
||||
for (let i = 0; i < 10; i++) {
|
||||
var array = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
array.push(false);
|
||||
}
|
||||
boardRender.push(array);
|
||||
}
|
||||
|
||||
data.boards[0].ships.forEach(ship => {
|
||||
let multips;
|
||||
|
||||
switch (ship.rot) {
|
||||
case 0:
|
||||
multips = [1, 0];
|
||||
break;
|
||||
|
||||
case 1:
|
||||
multips = [0, 1];
|
||||
break;
|
||||
|
||||
case 2:
|
||||
multips = [-1, 0];
|
||||
break;
|
||||
|
||||
case 3:
|
||||
multips = [0, -1];
|
||||
break;
|
||||
}
|
||||
|
||||
for (let i = 0; i <= ship.type; i++) {
|
||||
boardRender[ship.posX + multips[0] * i][ship.posY + multips[1] * i] = true;
|
||||
}
|
||||
});
|
||||
|
||||
// const rot = 0;
|
||||
const type = 3;
|
||||
|
||||
// let multips;
|
||||
|
||||
// switch (rot) {
|
||||
// case 0:
|
||||
// multips = [1, 0];
|
||||
// break;
|
||||
|
||||
// case 1:
|
||||
// multips = [0, 1];
|
||||
// break;
|
||||
|
||||
// case 2:
|
||||
// multips = [-1, 0];
|
||||
// break;
|
||||
|
||||
// case 3:
|
||||
// multips = [0, -1];
|
||||
// break;
|
||||
// }
|
||||
|
||||
boardRender = [
|
||||
[
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true, false, true,
|
||||
true
|
||||
],
|
||||
[
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true, false, true,
|
||||
true
|
||||
],
|
||||
[
|
||||
false, true, true,
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true
|
||||
],
|
||||
[
|
||||
false, true, true,
|
||||
true, false, false,
|
||||
true, true, true,
|
||||
false
|
||||
],
|
||||
[
|
||||
false, false, false,
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
false
|
||||
],
|
||||
[
|
||||
false, false, false,
|
||||
true, true, true,
|
||||
true, true, false,
|
||||
false
|
||||
],
|
||||
[
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true
|
||||
],
|
||||
[
|
||||
true, true, true,
|
||||
true, false, false,
|
||||
false, true, true,
|
||||
true
|
||||
],
|
||||
[
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true, true, true,
|
||||
true
|
||||
],
|
||||
[
|
||||
false, false, false,
|
||||
true, true, true,
|
||||
true, false, false,
|
||||
false
|
||||
]
|
||||
];
|
||||
|
||||
let search = findEmptyFields(boardRender, 4);
|
||||
for (let y = 0; y < 10; y++) {
|
||||
let row = "";
|
||||
for (let x = 0; x < 10; x++) {
|
||||
row += `${boardRender[x][y] ? "\x1b[31m" : "\x1b[32m"}${boardRender[x][y]}\x1b[0m\t`;
|
||||
}
|
||||
console.log(row);
|
||||
}
|
||||
console.log(search);
|
||||
|
||||
const rPos = search[Math.floor(Math.random() * search.length)];
|
||||
|
||||
switch (rPos.rot) {
|
||||
case 0:
|
||||
multips = [1, 0];
|
||||
break;
|
||||
|
||||
case 1:
|
||||
multips = [0, 1];
|
||||
break;
|
||||
|
||||
case 2:
|
||||
multips = [-1, 0];
|
||||
break;
|
||||
|
||||
case 3:
|
||||
multips = [0, -1];
|
||||
break;
|
||||
}
|
||||
|
||||
for (let i = 0; i <= type; i++) {
|
||||
console.log(`boardRender[${rPos.posX + multips[0] * i}][${rPos.posY + multips[1] * i}]`)
|
||||
boardRender[rPos.posX + multips[0] * i][rPos.posY + multips[1] * i] = true;
|
||||
}
|
||||
|
||||
for (let y = 0; y < 10; y++) {
|
||||
let row = "";
|
||||
for (let x = 0; x < 10; x++) {
|
||||
row += `${boardRender[x][y] ? "\x1b[31m" : "\x1b[32m"}${boardRender[x][y]}\x1b[0m\t`;
|
||||
}
|
||||
console.log(row);
|
||||
}
|
||||
|
||||
// console.log();
|
||||
// console.log(findAllRowsOfXTrueValues(matrix, 3, 1));
|
||||
// console.log(findAllRowsOfXTrueValues(matrix, 3, 2));
|
Loading…
Reference in New Issue
Block a user