2024-03-04 20:34:45 +01:00
|
|
|
import 'dotenv/config';
|
2024-03-04 12:23:47 +01:00
|
|
|
|
2024-03-04 20:34:45 +01:00
|
|
|
const PORT = parseInt(process.env.port);
|
2024-01-05 23:35:11 +01:00
|
|
|
import express from 'express';
|
|
|
|
import { createServer } from 'node:http';
|
|
|
|
import { Server } from 'socket.io';
|
|
|
|
import path from 'node:path';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
2024-04-14 00:36:51 +02:00
|
|
|
import fs from 'node:fs';
|
2024-03-27 15:51:33 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2024-01-05 23:35:11 +01:00
|
|
|
import session from "express-session";
|
|
|
|
import { engine } from 'express-handlebars';
|
2024-03-01 22:24:30 +01:00
|
|
|
import { createClient } from 'redis';
|
2024-03-13 21:40:24 +01:00
|
|
|
import * as bships from './utils/battleships.js';
|
|
|
|
import { MailAuth } from './utils/auth.js';
|
2024-03-27 15:51:33 +01:00
|
|
|
import { Lang } from './utils/localisation.js';
|
2024-03-18 18:43:13 +01:00
|
|
|
import { rateLimit } from 'express-rate-limit';
|
|
|
|
import { RedisStore as LimiterRedisStore } from 'rate-limit-redis';
|
|
|
|
import SessionRedisStore from 'connect-redis';
|
2024-04-06 01:48:49 +02:00
|
|
|
import mysql from 'mysql';
|
2024-01-05 23:35:11 +01:00
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
var packageJSON;
|
|
|
|
|
|
|
|
fs.readFile(path.join(__dirname, 'package.json'), function (err, data) {
|
|
|
|
if (err) throw err;
|
|
|
|
packageJSON = JSON.parse(data);
|
|
|
|
});
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
const app = express();
|
|
|
|
|
2024-03-28 16:47:48 +01:00
|
|
|
const flags = process.env.flags ? process.env.flags.split(",") : null;
|
2024-03-26 12:41:28 +01:00
|
|
|
|
2024-04-06 01:48:49 +02:00
|
|
|
const langs = [{ id: "en", name: "English" }, { id: "pl", name: "Polish" }];
|
|
|
|
|
2024-01-08 16:53:12 +01:00
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
app.engine('handlebars', engine());
|
|
|
|
app.set('view engine', 'handlebars');
|
|
|
|
app.set('views', './views');
|
|
|
|
|
|
|
|
const server = createServer(app);
|
|
|
|
const io = new Server(server);
|
2024-03-01 22:24:30 +01:00
|
|
|
const redis = createClient();
|
|
|
|
redis.on('error', err => console.log('Redis Client Error', err));
|
|
|
|
await redis.connect();
|
2024-01-05 23:35:11 +01:00
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
const prefixes = ["game:*", "timer:*", "loginTimer:*"];
|
|
|
|
|
|
|
|
prefixes.forEach(prefix => {
|
|
|
|
redis.eval(`for _,k in ipairs(redis.call('keys', '${prefix}')) do redis.call('del', k) end`, 0);
|
|
|
|
});
|
|
|
|
|
2024-03-18 18:43:13 +01:00
|
|
|
const limiter = rateLimit({
|
|
|
|
windowMs: 40 * 1000,
|
2024-03-25 17:40:07 +01:00
|
|
|
limit: 500,
|
2024-03-18 18:43:13 +01:00
|
|
|
standardHeaders: 'draft-7',
|
|
|
|
legacyHeaders: false,
|
|
|
|
store: new LimiterRedisStore({
|
|
|
|
sendCommand: (...args) => redis.sendCommand(args),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
app.use(limiter);
|
|
|
|
|
2024-03-02 18:28:33 +01:00
|
|
|
const GInfo = new bships.GameInfo(redis, io);
|
2024-03-13 21:40:24 +01:00
|
|
|
const auth = new MailAuth(redis, {
|
|
|
|
host: process.env.mail_host,
|
|
|
|
user: process.env.mail_user,
|
|
|
|
pass: process.env.mail_pass
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: process.env.db_host,
|
|
|
|
user: process.env.db_user,
|
|
|
|
password: process.env.db_pass,
|
|
|
|
database: 'statki'
|
|
|
|
});
|
2024-03-02 18:28:33 +01:00
|
|
|
|
2024-01-07 00:59:08 +01:00
|
|
|
app.set('trust proxy', 1);
|
2024-03-18 18:43:13 +01:00
|
|
|
|
|
|
|
let sessionStore = new SessionRedisStore({
|
|
|
|
client: redis,
|
|
|
|
prefix: "statkiSession:",
|
|
|
|
});
|
|
|
|
|
2024-04-14 00:36:51 +02:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
const sessionMiddleware = session({
|
2024-03-18 18:43:13 +01:00
|
|
|
store: sessionStore,
|
2024-04-14 00:36:51 +02:00
|
|
|
secret: sessionSecret,
|
2024-01-05 23:35:11 +01:00
|
|
|
resave: true,
|
|
|
|
saveUninitialized: true,
|
2024-03-18 12:41:50 +01:00
|
|
|
rolling: true,
|
|
|
|
cookie: {
|
2024-03-26 12:41:28 +01:00
|
|
|
secure: checkFlag("cookie_secure"),
|
2024-04-14 00:36:51 +02:00
|
|
|
maxAge: 7 * 24 * 60 * 60 * 1000,
|
2024-03-18 12:41:50 +01:00
|
|
|
},
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
app.use(sessionMiddleware);
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
|
|
io.engine.use(sessionMiddleware);
|
|
|
|
|
2024-04-10 22:26:06 +02:00
|
|
|
app.get('/privacy', (req, res) => {
|
|
|
|
res.render("privacy");
|
|
|
|
});
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
app.get('/', async (req, res) => {
|
2024-04-14 19:17:20 +02:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
|
|
|
if (req.session.activeGame && await redis.json.get(req.session.activeGame)) {
|
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
|
|
|
error: "Your account is currently taking part in a game from another session",
|
|
|
|
fallback: "/",
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
let login = loginState(req);
|
|
|
|
|
2024-04-11 19:10:45 +02:00
|
|
|
if (login == 0) {
|
|
|
|
req.session.userAgent = req.get('user-agent');
|
|
|
|
req.session.loggedIn = 0;
|
|
|
|
|
2024-04-10 20:45:11 +02:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
|
|
|
res.render('landing', {
|
|
|
|
helpers: {
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// res.redirect('/login');
|
2024-04-11 19:10:45 +02:00
|
|
|
} else if (login != 2) {
|
|
|
|
res.redirect("/login");
|
2024-03-13 21:40:24 +01:00
|
|
|
} else if (req.session.nickname == null) {
|
2024-03-28 16:47:48 +01:00
|
|
|
auth.getLanguage(req.session.userId).then(language => {
|
|
|
|
var locale;
|
2024-04-06 01:48:49 +02:00
|
|
|
|
|
|
|
req.session.autoLang = language == null ? true : false;
|
|
|
|
|
2024-03-28 16:47:48 +01:00
|
|
|
if (language) {
|
|
|
|
locale = new Lang([language]);
|
|
|
|
req.session.langs = [language];
|
2024-03-13 21:40:24 +01:00
|
|
|
} else {
|
2024-03-28 16:47:48 +01:00
|
|
|
locale = new Lang(req.acceptsLanguages());
|
|
|
|
req.session.langs = req.acceptsLanguages();
|
2024-03-13 21:40:24 +01:00
|
|
|
}
|
2024-03-28 16:47:48 +01:00
|
|
|
|
|
|
|
auth.getNickname(req.session.userId).then(nickname => {
|
|
|
|
if (nickname != null) {
|
|
|
|
req.session.nickname = nickname;
|
|
|
|
|
|
|
|
res.render('index', {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
t: (key) => { return locale.t(key) },
|
|
|
|
ver: packageJSON.version
|
2024-03-28 16:47:48 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.redirect('/nickname');
|
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
});
|
2024-01-08 16:53:12 +01:00
|
|
|
} else {
|
2024-03-28 16:47:48 +01:00
|
|
|
auth.getLanguage(req.session.userId).then(language => {
|
|
|
|
var locale;
|
|
|
|
if (language) {
|
|
|
|
locale = new Lang([language]);
|
|
|
|
req.session.langs = [language];
|
|
|
|
} else {
|
|
|
|
locale = new Lang(req.acceptsLanguages());
|
|
|
|
req.session.langs = req.acceptsLanguages();
|
2024-03-27 15:51:33 +01:00
|
|
|
}
|
2024-03-28 16:47:48 +01:00
|
|
|
|
|
|
|
res.render('index', {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
t: (key) => { return locale.t(key) },
|
|
|
|
ver: packageJSON.version
|
2024-03-28 16:47:48 +01:00
|
|
|
}
|
|
|
|
});
|
2024-03-27 15:51:33 +01:00
|
|
|
});
|
2024-01-08 16:53:12 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
app.get('/login', (req, res) => {
|
|
|
|
let login = loginState(req);
|
2024-03-27 15:51:33 +01:00
|
|
|
|
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
if (!login) {
|
2024-03-27 15:51:33 +01:00
|
|
|
res.render('login', {
|
|
|
|
helpers: {
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
} else if (login == 1) {
|
|
|
|
res.redirect('/auth');
|
|
|
|
} else {
|
2024-01-08 16:53:12 +01:00
|
|
|
res.redirect('/');
|
2024-03-13 21:40:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/auth', (req, res) => {
|
|
|
|
let login = loginState(req);
|
2024-03-27 15:51:33 +01:00
|
|
|
|
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
if (!login) { // Niezalogowany
|
|
|
|
res.redirect('/login');
|
|
|
|
} else if (login == 1) { // W trakcie autoryzacji
|
2024-03-27 15:51:33 +01:00
|
|
|
res.render('auth', {
|
|
|
|
helpers: {
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
} else { // Zalogowany
|
|
|
|
res.redirect('/auth');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/nickname', (req, res) => {
|
|
|
|
let login = loginState(req);
|
2024-03-27 15:51:33 +01:00
|
|
|
|
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
if (!login) { // Niezalogowany
|
|
|
|
res.redirect('/login');
|
2024-01-08 16:53:12 +01:00
|
|
|
} else {
|
2024-03-27 15:51:33 +01:00
|
|
|
res.render('setup', {
|
|
|
|
helpers: {
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
2024-01-08 16:53:12 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
app.post('/api/login', (req, res) => {
|
|
|
|
let login = loginState(req);
|
2024-03-27 15:51:33 +01:00
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
if (login == 2) {
|
|
|
|
res.redirect('/');
|
|
|
|
} else if (login == 0 && req.body.email != null && validateEmail(req.body.email)) {
|
2024-03-26 12:41:28 +01:00
|
|
|
if (checkFlag('authless')) {
|
|
|
|
auth.loginAuthless(req.body.email).then(async result => {
|
2024-03-13 21:40:24 +01:00
|
|
|
req.session.userId = result.uid;
|
2024-03-26 12:41:28 +01:00
|
|
|
req.session.loggedIn = 2;
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
|
2024-03-26 12:41:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-04-07 21:14:05 +02:00
|
|
|
|
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
|
|
|
auth.startVerification(req.body.email, getIP(req), req.get('user-agent'), locale.lang).then(async result => {
|
2024-03-26 12:41:28 +01:00
|
|
|
if (result.status === 1 || result.status === -1) {
|
2024-03-19 22:33:14 +01:00
|
|
|
req.session.userId = result.uid;
|
|
|
|
|
|
|
|
req.session.loggedIn = 1;
|
|
|
|
res.redirect('/auth');
|
2024-03-13 21:40:24 +01:00
|
|
|
} else {
|
|
|
|
res.sendStatus(500);
|
|
|
|
}
|
2024-03-26 12:41:28 +01:00
|
|
|
}).catch((err) => {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-26 12:41:28 +01:00
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
error: "Unknown login error occured",
|
2024-03-27 15:51:33 +01:00
|
|
|
fallback: "/login",
|
|
|
|
t: (key) => { return locale.t(key) }
|
2024-03-26 12:41:28 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
throw err;
|
2024-03-13 21:40:24 +01:00
|
|
|
});
|
|
|
|
} else {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-18 18:43:13 +01:00
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
error: "Wrong e-mail address",
|
2024-03-27 15:51:33 +01:00
|
|
|
fallback: "/login",
|
2024-04-06 00:05:11 +02:00
|
|
|
t: (key) => { return locale.t(key) }
|
2024-03-18 18:43:13 +01:00
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/api/auth', async (req, res) => {
|
|
|
|
let login = loginState(req);
|
|
|
|
if (login == 2) {
|
|
|
|
res.redirect('/');
|
|
|
|
} else if (login == 1 && req.body.code != null && req.body.code.length <= 10 && req.body.code.length >= 8) {
|
|
|
|
let finishResult = await auth.finishVerification(req.session.userId, req.body.code);
|
|
|
|
if (finishResult) {
|
|
|
|
req.session.loggedIn = 2;
|
|
|
|
res.redirect('/');
|
|
|
|
} else {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-18 18:43:13 +01:00
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
error: "Wrong authorisation code",
|
2024-03-27 15:51:33 +01:00
|
|
|
fallback: "/auth",
|
|
|
|
t: (key) => { return locale.t(key) }
|
2024-03-18 18:43:13 +01:00
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-03-18 18:43:13 +01:00
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
error: "Wrong authorisation code",
|
2024-03-27 15:51:33 +01:00
|
|
|
fallback: "/login",
|
|
|
|
t: (key) => { return locale.t(key) }
|
2024-03-18 18:43:13 +01:00
|
|
|
}
|
|
|
|
});
|
2024-03-13 21:40:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/api/nickname', (req, res) => {
|
2024-04-14 19:17:20 +02:00
|
|
|
if (loginState(req) == 2 && req.body.nickname != null && 3 <= req.body.nickname.length && req.body.nickname.length <= 16) {
|
2024-01-08 16:53:12 +01:00
|
|
|
req.session.nickname = req.body.nickname;
|
2024-03-02 13:56:01 +01:00
|
|
|
req.session.activeGame = null;
|
2024-03-13 21:40:24 +01:00
|
|
|
auth.setNickname(req.session.userId, req.body.nickname).then(() => {
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
} else {
|
2024-04-14 19:17:20 +02:00
|
|
|
const locale = new Lang(req.acceptsLanguages());
|
|
|
|
|
2024-04-06 00:05:11 +02:00
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
error: "The nickname does not meet the requirements: length from 3 to 16 characters",
|
2024-04-06 00:05:11 +02:00
|
|
|
fallback: "/nickname",
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
2024-01-08 16:53:12 +01:00
|
|
|
}
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
|
2024-03-13 21:40:24 +01:00
|
|
|
app.get('/game', async (req, res) => {
|
2024-03-01 22:24:30 +01:00
|
|
|
const game = await redis.json.get(`game:${req.query.id}`);
|
2024-04-14 19:17:20 +02:00
|
|
|
|
|
|
|
// if (req.session.activeGame) {
|
|
|
|
// res.render("error", {
|
|
|
|
// helpers: {
|
|
|
|
// error: "Your account is currently taking part in a game from another session",
|
|
|
|
// fallback: "/",
|
|
|
|
// t: (key) => { return locale.t(key) }
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
if (req.session.nickname == null) {
|
2024-03-13 21:40:24 +01:00
|
|
|
res.redirect('/setup');
|
2024-04-14 19:17:20 +02:00
|
|
|
} else if (!req.query.id || !game || !req.session.activeGame || req.session.activeGame !== req.query.id) {
|
2024-03-31 15:08:31 +02:00
|
|
|
auth.getLanguage(req.session.userId).then(language => {
|
|
|
|
var locale;
|
|
|
|
if (language) {
|
|
|
|
locale = new Lang([language]);
|
|
|
|
req.session.langs = [language];
|
|
|
|
} else {
|
|
|
|
locale = new Lang(req.acceptsLanguages());
|
|
|
|
req.session.langs = req.acceptsLanguages();
|
2024-03-25 17:40:07 +01:00
|
|
|
}
|
2024-03-31 15:08:31 +02:00
|
|
|
|
|
|
|
res.render("error", {
|
|
|
|
helpers: {
|
2024-04-14 19:17:20 +02:00
|
|
|
error: "The specified game was not found",
|
2024-03-31 15:08:31 +02:00
|
|
|
fallback: "/",
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
2024-03-25 17:40:07 +01:00
|
|
|
});
|
2024-03-01 22:24:30 +01:00
|
|
|
} else {
|
2024-03-31 15:08:31 +02:00
|
|
|
auth.getLanguage(req.session.userId).then(language => {
|
|
|
|
var locale;
|
|
|
|
if (language) {
|
|
|
|
locale = new Lang([language]);
|
|
|
|
req.session.langs = [language];
|
|
|
|
} else {
|
|
|
|
locale = new Lang(req.acceptsLanguages());
|
|
|
|
req.session.langs = req.acceptsLanguages();
|
2024-03-27 15:51:33 +01:00
|
|
|
}
|
2024-03-31 15:08:31 +02:00
|
|
|
|
|
|
|
res.render('board', {
|
|
|
|
helpers: {
|
|
|
|
t: (key) => { return locale.t(key) }
|
|
|
|
}
|
|
|
|
});
|
2024-03-27 15:51:33 +01:00
|
|
|
});
|
2024-03-01 22:24:30 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-06 21:09:41 +01:00
|
|
|
app.get("/*", (req, res) => {
|
|
|
|
res.redirect("/?path=" + req.originalUrl);
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
io.on('connection', async (socket) => {
|
|
|
|
const req = socket.request;
|
2024-03-19 22:33:14 +01:00
|
|
|
const session = req.session;
|
2024-03-02 13:56:01 +01:00
|
|
|
socket.session = session;
|
2024-04-11 19:10:45 +02:00
|
|
|
|
|
|
|
if (!session.loggedIn) {
|
|
|
|
socket.on('email login', (email, callback) => {
|
|
|
|
let login = socket.request.session.loggedIn;
|
|
|
|
|
|
|
|
if (login == 0 && email != null && validateEmail(email)) {
|
|
|
|
if (checkFlag('authless')) {
|
|
|
|
auth.loginAuthless(email).then(async result => {
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.userId = result.uid;
|
|
|
|
req.session.loggedIn = 2;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
callback({ status: "ok", next: "done" });
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
auth.startVerification(email, getIPSocket(socket), socket.client.request.headers["user-agent"], locale.lang).then(async result => {
|
|
|
|
if (result.status === 1 || result.status === -1) {
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.userId = result.uid;
|
|
|
|
req.session.loggedIn = 1;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
callback({ status: "ok", next: "auth" });
|
|
|
|
} else {
|
|
|
|
callback({ status: "SrvErr", error: locale.t("landing.Server error") });
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
callback({ success: false, error: locale.t("landing.Unknown error") });
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
auth.loginAuthless(email).then(async result => {
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.userId = result.uid;
|
|
|
|
req.session.loggedIn = 2;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
callback({ success: false, error: locale.t("landing.Wrong email address") });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('email auth', async (code, callback) => {
|
|
|
|
let login = socket.request.session.loggedIn;
|
|
|
|
|
|
|
|
if (login == 1 && code != null && code.length <= 10 && code.length >= 8) {
|
|
|
|
let finishResult = await auth.finishVerification(req.session.userId, code);
|
|
|
|
if (finishResult) {
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.loggedIn = 2;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
callback({ status: "ok", next: "done" });
|
|
|
|
} else {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
callback({ success: false, error: locale.t("landing.Wrong authorisation code") });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
callback({ success: false, error: locale.t("landing.Wrong authorisation code") });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('disconnecting', () => {
|
|
|
|
if (socket.request.session.loggedIn == 1) {
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.loggedIn = 0;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
if (!await GInfo.isPlayerInGame(socket) && session.nickname) {
|
2024-04-11 19:10:45 +02:00
|
|
|
// if (session.nickname == null) {
|
|
|
|
// socket.disconnect();
|
|
|
|
// return;
|
|
|
|
// }
|
2024-04-10 20:45:11 +02:00
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
socket.on('whats my nick', (callback) => {
|
|
|
|
callback(session.nickname);
|
|
|
|
});
|
|
|
|
|
2024-03-24 19:51:49 +01:00
|
|
|
socket.on('my profile', (callback) => {
|
|
|
|
auth.getProfile(session.userId).then((profile) => {
|
2024-04-06 01:48:49 +02:00
|
|
|
profile.uid = session.userId;
|
2024-03-24 19:51:49 +01:00
|
|
|
callback(profile);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-06 01:48:49 +02:00
|
|
|
socket.on('locale options', (callback) => {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
let userLanguage = langs.find((element) => element.id == locale.lang);
|
|
|
|
let userLangs = langs.filter((element) => element.id != locale.lang);
|
|
|
|
|
|
|
|
if (session.autoLang) {
|
|
|
|
userLangs.unshift(userLanguage);
|
|
|
|
userLangs.unshift({ id: "null", name: "Auto" });
|
|
|
|
} else {
|
|
|
|
userLangs.unshift({ id: "null", name: "Auto" });
|
|
|
|
userLangs.unshift(userLanguage);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(userLangs);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('change locale', (locale, callback) => {
|
|
|
|
if (locale === "null" || langs.find((element) => element.id == locale)) {
|
|
|
|
locale = locale === "null" ? null : locale;
|
|
|
|
const conn = mysql.createConnection({ host: process.env.db_host, user: process.env.db_user, password: process.env.db_pass, database: 'statki' });
|
|
|
|
conn.query(`UPDATE accounts SET language = ${conn.escape(locale)} WHERE user_id = ${conn.escape(session.userId)}`, (err) => {
|
|
|
|
if (err) { callback({ status: 'dbErr' }); return; }
|
|
|
|
else callback({ status: 'ok' });
|
|
|
|
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.autoLang = locale ? false : true;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
conn.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
socket.on('create lobby', (callback) => {
|
2024-01-08 16:53:12 +01:00
|
|
|
if (socket.rooms.size === 1) {
|
2024-03-01 22:24:30 +01:00
|
|
|
let id = genID();
|
2024-01-08 16:53:12 +01:00
|
|
|
callback({
|
2024-02-29 21:55:56 +01:00
|
|
|
status: "ok",
|
2024-03-01 22:24:30 +01:00
|
|
|
gameCode: id
|
2024-01-08 16:53:12 +01:00
|
|
|
});
|
2024-03-01 22:24:30 +01:00
|
|
|
|
|
|
|
socket.join(id);
|
2024-01-08 16:53:12 +01:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
status: "alreadyInLobby",
|
2024-03-01 22:24:30 +01:00
|
|
|
gameCode: socket.rooms[1]
|
2024-01-08 16:53:12 +01:00
|
|
|
});
|
|
|
|
}
|
2024-03-01 22:24:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('join lobby', (msg, callback) => {
|
|
|
|
if (io.sockets.adapter.rooms.get(msg) == null || io.sockets.adapter.rooms.get(msg).size > 1) {
|
|
|
|
callback({
|
|
|
|
status: "bad_id"
|
|
|
|
});
|
|
|
|
} else {
|
2024-03-18 18:43:13 +01:00
|
|
|
let opp = io.sockets.sockets.get(io.sockets.adapter.rooms.get(msg).values().next().value);
|
|
|
|
|
|
|
|
if (opp.request.session.userId == session.userId) {
|
|
|
|
callback({
|
|
|
|
status: "cantJoinYourself",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2024-03-01 22:24:30 +01:00
|
|
|
if (socket.rooms.size === 1) {
|
|
|
|
io.to(msg).emit("joined", session.nickname); // Wyślij hostowi powiadomienie o dołączającym graczu
|
|
|
|
// Zmienna opp zawiera socket hosta
|
2024-03-18 18:43:13 +01:00
|
|
|
// let opp = io.sockets.sockets.get(io.sockets.adapter.rooms.get(msg).values().next().value);
|
2024-03-01 22:24:30 +01:00
|
|
|
let oppNickname = opp.request.session.nickname;
|
|
|
|
|
|
|
|
socket.join(msg); // Dołącz gracza do grupy
|
|
|
|
callback({
|
|
|
|
status: "ok",
|
|
|
|
oppNickname: oppNickname,
|
|
|
|
}); // Wyślij dołączonemu graczowi odpowiedź
|
|
|
|
|
|
|
|
// Teraz utwórz objekt partii w trakcie w bazie Redis
|
|
|
|
const gameId = uuidv4();
|
|
|
|
redis.json.set(`game:${gameId}`, '$', {
|
2024-04-14 19:17:20 +02:00
|
|
|
type: 'pvp',
|
2024-04-07 00:07:11 +02:00
|
|
|
hostId: opp.request.session.userId,
|
2024-03-01 22:24:30 +01:00
|
|
|
state: "pregame",
|
2024-03-18 18:43:13 +01:00
|
|
|
startTs: (new Date()).getTime() / 1000,
|
2024-04-06 21:18:38 +02:00
|
|
|
ready: [false, false],
|
2024-03-02 18:28:33 +01:00
|
|
|
boards: [
|
|
|
|
{ // typ 2 to trójmasztowiec pozycja i obrót na planszy które pola zostały trafione
|
2024-03-01 22:24:30 +01:00
|
|
|
ships: [], // zawiera np. {type: 2, posX: 3, posY: 4, rot: 2, hits: [false, false, true]}
|
|
|
|
// pozycja na planszy czy strzał miał udział w zatopieniu statku?
|
2024-03-10 11:59:18 +01:00
|
|
|
shots: [], // zawiera np. {posX: 3, posY: 5}
|
2024-03-18 12:41:50 +01:00
|
|
|
stats: {
|
|
|
|
shots: 0,
|
|
|
|
hits: 0,
|
|
|
|
placedShips: 0,
|
|
|
|
sunkShips: 0,
|
|
|
|
},
|
2024-03-01 22:24:30 +01:00
|
|
|
},
|
2024-03-02 18:28:33 +01:00
|
|
|
{
|
2024-03-01 22:24:30 +01:00
|
|
|
ships: [],
|
|
|
|
shots: [],
|
2024-03-18 12:41:50 +01:00
|
|
|
stats: {
|
|
|
|
shots: 0,
|
|
|
|
hits: 0,
|
|
|
|
placedShips: 0,
|
|
|
|
sunkShips: 0,
|
|
|
|
},
|
2024-03-01 22:24:30 +01:00
|
|
|
}
|
2024-03-02 18:28:33 +01:00
|
|
|
],
|
2024-03-01 22:24:30 +01:00
|
|
|
nextPlayer: 0,
|
|
|
|
});
|
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
req.session.activeGame = gameId;
|
|
|
|
req.session.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
const oppReq = opp.request;
|
|
|
|
|
|
|
|
oppReq.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
oppReq.session.activeGame = gameId;
|
|
|
|
oppReq.session.save();
|
|
|
|
});
|
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
io.to(msg).emit("gameReady", gameId);
|
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
io.sockets.adapter.rooms.get(msg).forEach((sid) => {
|
|
|
|
const s = io.sockets.sockets.get(sid);
|
2024-03-01 22:24:30 +01:00
|
|
|
s.leave(msg);
|
|
|
|
});
|
2024-04-14 19:17:20 +02:00
|
|
|
|
|
|
|
GInfo.timer(gameId, 60, () => {
|
|
|
|
AFKEnd(gameId);
|
|
|
|
});
|
2024-03-01 22:24:30 +01:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
status: "alreadyInLobby",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
socket.on('create pve', (difficulty, callback) => {
|
|
|
|
if (socket.rooms.size === 1) {
|
|
|
|
callback({
|
|
|
|
status: "ok"
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (difficulty) {
|
|
|
|
case 'simple':
|
|
|
|
difficulty = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'smart':
|
|
|
|
difficulty = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'overkill':
|
|
|
|
difficulty = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
difficulty = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Teraz utwórz objekt partii w trakcie w bazie Redis
|
|
|
|
const gameId = uuidv4();
|
|
|
|
redis.json.set(`game:${gameId}`, '$', {
|
|
|
|
type: 'pve',
|
|
|
|
difficulty: difficulty,
|
|
|
|
hostId: session.userId,
|
|
|
|
state: "pregame",
|
|
|
|
startTs: (new Date()).getTime() / 1000,
|
|
|
|
ready: [false, true],
|
|
|
|
boards: [
|
|
|
|
{
|
|
|
|
ships: [],
|
|
|
|
shots: [],
|
|
|
|
stats: {
|
|
|
|
shots: 0,
|
|
|
|
hits: 0,
|
|
|
|
placedShips: 0,
|
|
|
|
sunkShips: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ships: [],
|
|
|
|
shots: [],
|
|
|
|
stats: {
|
|
|
|
shots: 0,
|
|
|
|
hits: 0,
|
|
|
|
placedShips: 0,
|
|
|
|
sunkShips: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
nextPlayer: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
|
|
|
|
|
|
|
session.activeGame = gameId;
|
|
|
|
session.save();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.emit("gameReady", gameId);
|
|
|
|
|
|
|
|
GInfo.timer(gameId, 60, () => {
|
|
|
|
AFKEnd(gameId);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
status: "alreadyInLobby",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-11 19:32:00 +02:00
|
|
|
socket.on('logout', () => {
|
|
|
|
session.destroy();
|
|
|
|
});
|
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
socket.on('disconnecting', () => {
|
2024-03-02 18:28:33 +01:00
|
|
|
if (bships.isPlayerInRoom(socket)) {
|
2024-03-01 22:24:30 +01:00
|
|
|
io.to(socket.rooms[1]).emit("player left");
|
|
|
|
}
|
|
|
|
});
|
2024-04-14 19:17:20 +02:00
|
|
|
} else if (session.nickname && (await GInfo.getPlayerGameData(socket)).data.type === "pvp") {
|
2024-03-02 18:28:33 +01:00
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
2024-03-01 22:24:30 +01:00
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
if (playerGame.data.state === 'pregame') {
|
|
|
|
socket.join(playerGame.id);
|
|
|
|
if (io.sockets.adapter.rooms.get(playerGame.id).size === 2) {
|
2024-03-09 01:11:03 +01:00
|
|
|
GInfo.resetTimer(playerGame.id);
|
2024-03-02 13:56:01 +01:00
|
|
|
io.to(playerGame.id).emit('players ready');
|
2024-03-01 22:24:30 +01:00
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
const members = [...roomMemberIterator(playerGame.id)];
|
|
|
|
for (let i = 0; i < members.length; i++) {
|
|
|
|
const sid = members[i][0];
|
2024-03-03 01:29:11 +01:00
|
|
|
const socket = io.sockets.sockets.get(sid);
|
2024-04-07 00:07:11 +02:00
|
|
|
if (socket.request.session.userId === playerGame.data.hostId) {
|
2024-03-03 01:29:11 +01:00
|
|
|
io.to(sid).emit('player idx', 0);
|
|
|
|
} else {
|
|
|
|
io.to(sid).emit('player idx', 1);
|
|
|
|
}
|
2024-03-02 13:56:01 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 21:18:38 +02:00
|
|
|
let UTCTs = Math.floor((new Date()).getTime() / 1000 + 180);
|
2024-03-02 13:56:01 +01:00
|
|
|
io.to(playerGame.id).emit('turn update', { turn: 0, phase: "preparation", timerToUTC: UTCTs });
|
2024-04-06 21:18:38 +02:00
|
|
|
GInfo.timer(playerGame.id, 180, async () => {
|
|
|
|
finishPrepPhase(socket, playerGame);
|
2024-03-02 18:36:40 +01:00
|
|
|
});
|
2024-03-03 01:29:11 +01:00
|
|
|
|
|
|
|
await redis.json.set(`game:${playerGame.id}`, '$.state', "preparation");
|
2024-03-03 22:59:52 +01:00
|
|
|
} else if (io.sockets.adapter.rooms.get(playerGame.id).size > 2) {
|
|
|
|
socket.disconnect();
|
2024-03-09 01:11:03 +01:00
|
|
|
} else {
|
|
|
|
GInfo.timer(playerGame.id, 30, () => {
|
|
|
|
AFKEnd(playerGame.id);
|
|
|
|
});
|
2024-03-01 22:24:30 +01:00
|
|
|
}
|
2024-04-14 19:17:20 +02:00
|
|
|
} else {
|
|
|
|
socket.disconnect();
|
2024-03-02 13:56:01 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 21:18:38 +02:00
|
|
|
socket.on('ready', async (callback) => {
|
2024-04-06 21:49:27 +02:00
|
|
|
if (!(callback && typeof callback === 'function')) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-07 00:21:09 +02:00
|
|
|
|
2024-04-06 21:18:38 +02:00
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
let timeLeft = await GInfo.timerLeft(playerGame.id);
|
|
|
|
|
|
|
|
if (timeLeft > 170) {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
socket.emit('toast', locale.t("board.You cannot ready up so early"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-06 21:44:18 +02:00
|
|
|
const playerIdx = playerGame.data.hostId === session.userId ? 0 : 1;
|
|
|
|
const userNotReady = !playerGame.data.ready[playerIdx];
|
|
|
|
|
|
|
|
if (playerGame && playerGame.data.state === 'preparation' && userNotReady) {
|
2024-04-06 21:18:38 +02:00
|
|
|
await GInfo.setReady(socket);
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
if (playerGame.data.ready[0] && playerGame.data.ready[1]) {
|
|
|
|
// Both set ready
|
|
|
|
await GInfo.resetTimer(playerGame.id);
|
|
|
|
|
2024-04-06 21:44:18 +02:00
|
|
|
callback();
|
|
|
|
|
2024-04-06 21:18:38 +02:00
|
|
|
await finishPrepPhase(socket, playerGame);
|
|
|
|
} else if (playerGame.data.ready[0] || playerGame.data.ready[1]) {
|
|
|
|
// One player set ready
|
|
|
|
|
2024-04-06 21:44:18 +02:00
|
|
|
callback();
|
|
|
|
|
2024-04-06 21:18:38 +02:00
|
|
|
const members = [...roomMemberIterator(playerGame.id)];
|
|
|
|
for (let i = 0; i < members.length; i++) {
|
|
|
|
const sid = members[i][0];
|
|
|
|
const pSocket = io.sockets.sockets.get(sid);
|
|
|
|
if (pSocket.session.id !== socket.session.id) {
|
|
|
|
const locale = new Lang(pSocket.session.langs);
|
|
|
|
|
|
|
|
pSocket.emit("toast", locale.t("board.Your opponent is ready"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let UTCTs = Math.floor((new Date()).getTime() / 1000 + Math.max(timeLeft / 2.5, 15));
|
|
|
|
io.to(playerGame.id).emit('turn update', { turn: 0, phase: "preparation", timerToUTC: UTCTs });
|
|
|
|
await GInfo.timer(playerGame.id, Math.max(timeLeft / 2.5, 15), async () => {
|
|
|
|
await finishPrepPhase(socket, playerGame);
|
|
|
|
});
|
2024-04-07 00:21:09 +02:00
|
|
|
}
|
2024-04-06 21:18:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-02 18:28:33 +01:00
|
|
|
socket.on('place ship', async (type, posX, posY, rot) => {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
2024-03-02 13:56:01 +01:00
|
|
|
|
2024-04-04 16:08:27 +02:00
|
|
|
if (playerGame && playerGame.data.state === 'preparation') {
|
2024-03-03 01:29:11 +01:00
|
|
|
const playerShips = await GInfo.getPlayerShips(socket);
|
|
|
|
let canPlace = bships.validateShipPosition(playerShips, type, posX, posY, rot);
|
2024-03-03 16:55:38 +01:00
|
|
|
let shipAvailable = bships.getShipsAvailable(playerShips)[type] > 0;
|
2024-03-03 01:29:11 +01:00
|
|
|
|
2024-03-03 16:55:38 +01:00
|
|
|
if (!canPlace) {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
socket.emit("toast", locale.t("board.You cannot place a ship like this"));
|
2024-03-03 16:55:38 +01:00
|
|
|
} else if (!shipAvailable) {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
socket.emit("toast", locale.t("board.You have ran out of ships of that type"));
|
2024-03-03 01:29:11 +01:00
|
|
|
} else {
|
2024-03-07 21:56:44 +01:00
|
|
|
await GInfo.placeShip(socket, { type: type, posX: posX, posY: posY, rot: rot, hits: Array.from(new Array(type+1), () => false) });
|
2024-03-03 22:59:52 +01:00
|
|
|
socket.emit("placed ship", { type: type, posX: posX, posY: posY, rot: rot });
|
2024-03-18 12:41:50 +01:00
|
|
|
await GInfo.incrStat(socket, 'placedShips');
|
2024-03-03 01:29:11 +01:00
|
|
|
}
|
2024-03-02 18:28:33 +01:00
|
|
|
}
|
|
|
|
});
|
2024-03-02 13:56:01 +01:00
|
|
|
|
2024-03-03 22:59:52 +01:00
|
|
|
socket.on('remove ship', async (posX, posY) => {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
2024-04-04 16:08:27 +02:00
|
|
|
if (playerGame && playerGame.data.state === 'preparation') {
|
2024-03-03 22:59:52 +01:00
|
|
|
const deletedShip = await GInfo.removeShip(socket, posX, posY);
|
|
|
|
socket.emit("removed ship", { posX: posX, posY: posY, type: deletedShip.type });
|
2024-03-18 12:41:50 +01:00
|
|
|
await GInfo.incrStat(socket, 'placedShips', -1);
|
2024-03-03 22:59:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-07 21:56:44 +01:00
|
|
|
socket.on('shoot', async (posX, posY) => {
|
2024-03-22 11:47:12 +01:00
|
|
|
let playerGame = await GInfo.getPlayerGameData(socket);
|
2024-03-02 18:28:33 +01:00
|
|
|
|
2024-04-04 16:08:27 +02:00
|
|
|
if (playerGame && playerGame.data.state === 'action') {
|
2024-04-07 00:21:09 +02:00
|
|
|
if (bships.checkTurn(playerGame.data, session.userId)) {
|
2024-04-06 22:30:27 +02:00
|
|
|
const enemyIdx = session.userId === playerGame.data.hostId ? 1 : 0;
|
2024-03-07 21:56:44 +01:00
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
let hit = await GInfo.shootShip(socket, enemyIdx, posX, posY);
|
2024-03-10 11:59:18 +01:00
|
|
|
|
|
|
|
await redis.json.arrAppend(`game:${playerGame.id}`, `.boards[${enemyIdx}].shots`, { posX: posX, posY: posY });
|
2024-03-18 12:41:50 +01:00
|
|
|
await GInfo.incrStat(socket, 'shots');
|
|
|
|
|
2024-03-08 19:18:53 +01:00
|
|
|
if (!hit.status) {
|
2024-03-07 21:56:44 +01:00
|
|
|
io.to(playerGame.id).emit("shot missed", enemyIdx, posX, posY);
|
2024-03-08 19:18:53 +01:00
|
|
|
} else if (hit.status === 1) {
|
|
|
|
io.to(playerGame.id).emit("shot hit", enemyIdx, posX, posY);
|
2024-03-18 12:41:50 +01:00
|
|
|
await GInfo.incrStat(socket, 'hits');
|
2024-03-08 19:18:53 +01:00
|
|
|
} else if (hit.status === 2) {
|
|
|
|
io.to(playerGame.id).emit("shot hit", enemyIdx, posX, posY);
|
2024-03-18 12:41:50 +01:00
|
|
|
await GInfo.incrStat(socket, 'hits');
|
2024-03-08 19:18:53 +01:00
|
|
|
io.to(playerGame.id).emit("ship sunk", enemyIdx, hit.ship);
|
2024-03-18 12:41:50 +01:00
|
|
|
await GInfo.incrStat(socket, 'sunkShips');
|
2024-03-08 19:18:53 +01:00
|
|
|
|
|
|
|
if (hit.gameFinished) {
|
2024-03-08 21:35:01 +01:00
|
|
|
const members = [...roomMemberIterator(playerGame.id)];
|
|
|
|
|
2024-04-13 23:54:25 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-22 11:47:12 +01:00
|
|
|
let hostNickname = hostSocket.session.nickname;
|
|
|
|
let guestNickname = guestSocket.session.nickname;
|
2024-03-09 01:11:03 +01:00
|
|
|
|
|
|
|
hostSocket.emit("game finished", !enemyIdx ? 1 : 0, guestNickname);
|
|
|
|
guestSocket.emit("game finished", !enemyIdx ? 1 : 0, hostNickname);
|
2024-03-08 21:35:01 +01:00
|
|
|
|
2024-03-22 11:47:12 +01:00
|
|
|
playerGame = await GInfo.getPlayerGameData(socket);
|
2024-04-13 23:54:25 +02:00
|
|
|
auth.saveMatch(playerGame.id, (new Date).getTime() / 1000 - playerGame.data.startTs, "pvp", hostSocket.session.userId, guestSocket.session.userId, playerGame.data.boards, enemyIdx ? 1 : 0);
|
2024-03-18 12:41:50 +01:00
|
|
|
|
2024-03-08 23:32:55 +01:00
|
|
|
GInfo.resetTimer(playerGame.id);
|
2024-04-06 22:30:27 +02:00
|
|
|
endGame(playerGame.id);
|
2024-03-08 21:35:01 +01:00
|
|
|
return;
|
2024-03-08 19:18:53 +01:00
|
|
|
}
|
2024-03-10 11:59:18 +01:00
|
|
|
} else if (hit.status === -1) {
|
2024-03-27 15:51:33 +01:00
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
socket.emit("toast", locale.t("board.You have already shot at this field"));
|
2024-03-10 11:59:18 +01:00
|
|
|
return;
|
2024-03-07 21:56:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await GInfo.passTurn(socket);
|
2024-03-08 23:32:55 +01:00
|
|
|
GInfo.resetTimer(playerGame.id);
|
|
|
|
GInfo.timer(playerGame.id, 30, () => {
|
2024-03-07 21:56:44 +01:00
|
|
|
AFKEnd(playerGame.id);
|
|
|
|
});
|
2024-03-02 18:28:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
socket.on('disconnecting', async () => {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
if (playerGame !== null) {
|
|
|
|
AFKEnd(playerGame.id);
|
|
|
|
await GInfo.resetTimer(playerGame.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (session.nickname && (await GInfo.getPlayerGameData(socket)).data.type === "pve") {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
if (playerGame.data.state === 'pregame') {
|
|
|
|
socket.join(playerGame.id);
|
|
|
|
if (io.sockets.adapter.rooms.get(playerGame.id).size === 1) {
|
|
|
|
GInfo.resetTimer(playerGame.id);
|
|
|
|
io.to(playerGame.id).emit('players ready');
|
|
|
|
|
|
|
|
socket.emit('player idx', 0);
|
|
|
|
|
|
|
|
let UTCTs = Math.floor((new Date()).getTime() / 1000 + 180);
|
|
|
|
io.to(playerGame.id).emit('turn update', { turn: 0, phase: "preparation", timerToUTC: UTCTs });
|
|
|
|
GInfo.timer(playerGame.id, 180, async () => {
|
|
|
|
finishPrepPhase(socket, playerGame);
|
|
|
|
placeAIShips(socket);
|
|
|
|
});
|
|
|
|
|
|
|
|
await redis.json.set(`game:${playerGame.id}`, '$.state', "preparation");
|
|
|
|
} else if (io.sockets.adapter.rooms.get(playerGame.id).size > 2) {
|
|
|
|
socket.disconnect();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
socket.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('ready', async (callback) => {
|
|
|
|
if (!(callback && typeof callback === 'function')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
const playerIdx = 0;
|
|
|
|
const userNotReady = !playerGame.data.ready[playerIdx];
|
|
|
|
|
|
|
|
if (playerGame && playerGame.data.state === 'preparation' && userNotReady) {
|
|
|
|
await GInfo.setReady(socket);
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
if (playerGame.data.ready[0] && playerGame.data.ready[1]) {
|
|
|
|
// Both set ready
|
|
|
|
await GInfo.resetTimer(playerGame.id);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
await finishPrepPhase(socket, playerGame);
|
|
|
|
await placeAIShips(socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('place ship', async (type, posX, posY, rot) => {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
if (type < 0 || type > 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (playerGame && playerGame.data.state === 'preparation') {
|
|
|
|
const playerShips = await GInfo.getPlayerShips(socket);
|
|
|
|
let canPlace = bships.validateShipPosition(playerShips, type, posX, posY, rot);
|
|
|
|
let shipAvailable = bships.getShipsAvailable(playerShips)[type] > 0;
|
|
|
|
|
|
|
|
if (!canPlace) {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
socket.emit("toast", locale.t("board.You cannot place a ship like this"));
|
|
|
|
} else if (!shipAvailable) {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
socket.emit("toast", locale.t("board.You have ran out of ships of that type"));
|
|
|
|
} else {
|
|
|
|
await GInfo.placeShip(socket, { type: type, posX: posX, posY: posY, rot: rot, hits: Array.from(new Array(type + 1), () => false) });
|
|
|
|
socket.emit("placed ship", { type: type, posX: posX, posY: posY, rot: rot });
|
|
|
|
await GInfo.incrStat(socket, 'placedShips');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('remove ship', async (posX, posY) => {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
if (playerGame && playerGame.data.state === 'preparation') {
|
|
|
|
const deletedShip = await GInfo.removeShip(socket, posX, posY);
|
|
|
|
socket.emit("removed ship", { posX: posX, posY: posY, type: deletedShip.type });
|
|
|
|
await GInfo.incrStat(socket, 'placedShips', -1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('shoot', async (posX, posY) => {
|
|
|
|
let playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
|
|
|
|
if (playerGame && playerGame.data.state === 'action') {
|
|
|
|
if (bships.checkTurn(playerGame.data, session.userId)) {
|
|
|
|
const enemyIdx = 1;
|
|
|
|
|
|
|
|
let hit = await GInfo.shootShip(socket, enemyIdx, posX, posY);
|
|
|
|
|
|
|
|
await redis.json.arrAppend(`game:${playerGame.id}`, `.boards[${enemyIdx}].shots`, { posX: posX, posY: posY });
|
|
|
|
await GInfo.incrStat(socket, 'shots');
|
|
|
|
|
|
|
|
if (!hit.status) {
|
|
|
|
socket.emit("shot missed", enemyIdx, posX, posY);
|
|
|
|
} else if (hit.status === 1) {
|
|
|
|
socket.emit("shot hit", enemyIdx, posX, posY);
|
|
|
|
await GInfo.incrStat(socket, 'hits');
|
|
|
|
} else if (hit.status === 2) {
|
|
|
|
socket.emit("shot hit", enemyIdx, posX, posY);
|
|
|
|
await GInfo.incrStat(socket, 'hits');
|
|
|
|
io.to(playerGame.id).emit("ship sunk", enemyIdx, hit.ship);
|
|
|
|
await GInfo.incrStat(socket, 'sunkShips');
|
|
|
|
|
|
|
|
if (hit.gameFinished) {
|
|
|
|
let hostNickname = session.nickname;
|
|
|
|
|
|
|
|
let difficulty;
|
|
|
|
|
|
|
|
switch (playerGame.data.difficulty) {
|
|
|
|
case 0:
|
|
|
|
difficulty = "simple";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
difficulty = "smart";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
difficulty = "overkill";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let guestNickname = `AI (${difficulty})`;
|
|
|
|
|
|
|
|
socket.emit("game finished", 0, guestNickname);
|
|
|
|
|
|
|
|
playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
auth.saveMatch(playerGame.id, (new Date).getTime() / 1000 - playerGame.data.startTs, "pve", session.userId, '77777777-77777777-77777777-77777777', playerGame.data.boards, 1, difficulty);
|
|
|
|
|
|
|
|
GInfo.resetTimer(playerGame.id);
|
|
|
|
endGame(playerGame.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (hit.status === -1) {
|
|
|
|
const locale = new Lang(session.langs);
|
|
|
|
|
|
|
|
socket.emit("toast", locale.t("board.You have already shot at this field"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await GInfo.passTurn(socket);
|
|
|
|
|
2024-04-15 20:49:19 +02:00
|
|
|
[posX, posY] = await GInfo.makeAIMove(socket, playerGame.data.difficulty);
|
2024-04-14 19:17:20 +02:00
|
|
|
|
|
|
|
hit = await GInfo.shootShip(socket, 0, posX, posY);
|
|
|
|
|
|
|
|
await redis.json.arrAppend(`game:${playerGame.id}`, `.boards[0].shots`, { posX: posX, posY: posY });
|
|
|
|
await GInfo.incrStat(socket, 'shots', 1, 1);
|
|
|
|
|
|
|
|
if (!hit.status) {
|
|
|
|
socket.emit("shot missed", 0, posX, posY);
|
|
|
|
} else if (hit.status === 1) {
|
|
|
|
socket.emit("shot hit", 0, posX, posY);
|
|
|
|
await GInfo.incrStat(socket, 'hits', 1, 1);
|
|
|
|
} else if (hit.status === 2) {
|
|
|
|
socket.emit("shot hit", 0, posX, posY);
|
|
|
|
await GInfo.incrStat(socket, 'hits', 1, 1);
|
|
|
|
socket.emit("ship sunk", 0, hit.ship);
|
|
|
|
await GInfo.incrStat(socket, 'sunkShips', 1, 1);
|
|
|
|
|
|
|
|
if (hit.gameFinished) {
|
|
|
|
let difficulty;
|
|
|
|
|
|
|
|
switch (playerGame.data.difficulty) {
|
|
|
|
case 0:
|
|
|
|
difficulty = "simple";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
difficulty = "smart";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
difficulty = "overkill";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let guestNickname = `AI (${difficulty})`;
|
|
|
|
|
|
|
|
socket.emit("game finished", 1, guestNickname);
|
|
|
|
|
|
|
|
playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
auth.saveMatch(playerGame.id, (new Date).getTime() / 1000 - playerGame.data.startTs, "pve", session.userId, '77777777-77777777-77777777-77777777', playerGame.data.boards, 0, difficulty);
|
|
|
|
|
|
|
|
GInfo.resetTimer(playerGame.id);
|
|
|
|
endGame(playerGame.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await GInfo.passTurn(socket);
|
|
|
|
|
|
|
|
GInfo.resetTimer(playerGame.id);
|
|
|
|
GInfo.timer(playerGame.id, 30, () => {
|
|
|
|
AFKEnd(playerGame.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-02 18:28:33 +01:00
|
|
|
socket.on('disconnecting', async () => {
|
|
|
|
const playerGame = await GInfo.getPlayerGameData(socket);
|
|
|
|
if (playerGame !== null) {
|
|
|
|
AFKEnd(playerGame.id);
|
2024-03-09 22:12:36 +01:00
|
|
|
await GInfo.resetTimer(playerGame.id);
|
2024-03-02 18:28:33 +01:00
|
|
|
}
|
2024-03-01 22:24:30 +01:00
|
|
|
});
|
|
|
|
}
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
|
2024-03-04 12:23:47 +01:00
|
|
|
server.listen(PORT, () => {
|
|
|
|
console.log(`Server running at http://localhost:${PORT}`);
|
2024-01-08 16:53:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function genID() {
|
|
|
|
return Math.floor(100000 + Math.random() * 900000).toString();
|
2024-03-01 22:24:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 18:28:33 +01:00
|
|
|
function resetUserGame(req) {
|
|
|
|
req.session.reload((err) => {
|
|
|
|
if (err) return socket.disconnect();
|
2024-03-01 22:24:30 +01:00
|
|
|
|
2024-03-02 18:28:33 +01:00
|
|
|
req.session.activeGame = null;
|
|
|
|
req.session.save();
|
|
|
|
});
|
2024-03-01 22:24:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-18 18:43:13 +01:00
|
|
|
function endGame(gameId) {
|
2024-03-03 01:29:11 +01:00
|
|
|
let iterator = roomMemberIterator(gameId);
|
|
|
|
if (iterator != null) {
|
|
|
|
const members = [...iterator];
|
|
|
|
for (let i = 0; i < members.length; i++) {
|
|
|
|
const sid = members[i][0];
|
|
|
|
const socket = io.sockets.sockets.get(sid);
|
|
|
|
socket.leave(gameId);
|
2024-04-05 09:46:34 +02:00
|
|
|
resetUserGame(socket.request);
|
2024-03-03 01:29:11 +01:00
|
|
|
}
|
2024-03-02 18:28:33 +01:00
|
|
|
}
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
redis.unlink(`game:${gameId}`);
|
2024-03-01 22:24:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 18:28:33 +01:00
|
|
|
function AFKEnd(gameId) {
|
|
|
|
io.to(gameId).emit("player left");
|
|
|
|
endGame(gameId);
|
2024-03-02 13:56:01 +01:00
|
|
|
}
|
2024-03-01 22:24:30 +01:00
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
async function finishPrepPhase(socket, playerGame) {
|
|
|
|
await GInfo.endPrepPhase(socket);
|
|
|
|
|
|
|
|
const members = [...roomMemberIterator(playerGame.id)];
|
|
|
|
for (let i = 0; i < members.length; i++) {
|
|
|
|
const sid = members[i][0];
|
|
|
|
const socket = io.sockets.sockets.get(sid);
|
|
|
|
|
|
|
|
let placedShips = await GInfo.depleteShips(socket);
|
2024-04-15 20:49:19 +02:00
|
|
|
if (!placedShips) {
|
|
|
|
io.to(playerGame.id).emit('toast', "An error occured while autoplacing player's ships");
|
|
|
|
endGame(playerGame.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-14 19:17:20 +02:00
|
|
|
placedShips.forEach(shipData => {
|
|
|
|
socket.emit("placed ship", shipData)
|
|
|
|
});
|
|
|
|
|
|
|
|
if (placedShips.length > 0) {
|
|
|
|
const locale = new Lang(socket.session.langs);
|
|
|
|
socket.emit("toast", locale.t("board.Your remaining ships have been randomly placed"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GInfo.timer(playerGame.id, 30, () => {
|
|
|
|
AFKEnd(playerGame.id);
|
|
|
|
});
|
2024-04-15 20:49:19 +02:00
|
|
|
|
|
|
|
return true;
|
2024-04-14 19:17:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function placeAIShips(socket, playerGame) {
|
|
|
|
await GInfo.depleteShips(socket, 1);
|
|
|
|
}
|
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
function roomMemberIterator(id) {
|
2024-03-03 01:29:11 +01:00
|
|
|
return io.sockets.adapter.rooms.get(id) == undefined ? null : io.sockets.adapter.rooms.get(id).entries();
|
2024-03-13 21:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function loginState(req) {
|
|
|
|
if (req.session.loggedIn == null) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return req.session.loggedIn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateEmail(email) {
|
|
|
|
return String(email)
|
|
|
|
.toLowerCase()
|
|
|
|
.match(
|
|
|
|
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
|
|
);
|
2024-03-24 15:50:39 +01:00
|
|
|
};
|
2024-03-26 12:41:28 +01:00
|
|
|
|
|
|
|
function getIP(req) {
|
|
|
|
if (checkFlag("cloudflare_mode")) {
|
|
|
|
return req.headers['cf-connecting-ip'];
|
|
|
|
} else {
|
|
|
|
return req.headers['x-forwarded-for'] || req.socket.remoteAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 19:10:45 +02:00
|
|
|
function getIPSocket(socket) {
|
|
|
|
if (checkFlag("cloudflare_mode")) {
|
|
|
|
return socket.client.request.headers['cf-connecting-ip'];
|
|
|
|
} else {
|
|
|
|
return socket.client.request.headers['x-forwarded-for'] || socket.handshake.address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 12:41:28 +01:00
|
|
|
function checkFlag(key) {
|
2024-03-28 16:47:48 +01:00
|
|
|
if (flags) {
|
|
|
|
return flags.includes(key);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-26 12:41:28 +01:00
|
|
|
}
|