statki/index.js

133 lines
3.4 KiB
JavaScript
Raw Normal View History

import express from 'express';
import { createServer } from 'node:http';
import { Server } from 'socket.io';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { v4 as uuidv4 } from 'uuid';
import session from "express-session";
import { engine } from 'express-handlebars';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
2024-01-08 16:53:12 +01:00
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.engine('handlebars', engine());
app.set('view engine', 'handlebars');
app.set('views', './views');
const server = createServer(app);
const io = new Server(server);
2024-01-07 00:59:08 +01:00
app.set('trust proxy', 1);
const sessionMiddleware = session({
secret: uuidv4(),
resave: true,
saveUninitialized: true,
});
app.use(sessionMiddleware);
app.use(express.static(path.join(__dirname, 'public')));
io.engine.use(sessionMiddleware);
app.get("/", (req, res) => {
2024-01-08 16:53:12 +01:00
if (req.session.nickname == null) {
res.redirect("/setup");
} else {
res.render('index');
}
});
app.get("/setup", (req, res) => {
if (req.session.nickname != null) {
res.redirect('/');
} else {
res.render("setup");
}
});
app.post('/api/setup-profile', function (req, res) {
if (req.session.nickname == null && 4 < req.body.nickname.length && req.body.nickname.length < 16) {
2024-01-08 16:53:12 +01:00
req.session.nickname = req.body.nickname;
}
res.redirect("/")
});
app.get("/*", (req, res) => {
res.redirect("/?path=" + req.originalUrl);
});
io.on('connection', (socket) => {
const session = socket.request.session;
2024-01-08 16:53:12 +01:00
if (session.nickname==null) {
socket.disconnect();
return;
}
2024-01-08 16:53:12 +01:00
socket.on('create lobby', (callback) => {
if (socket.rooms.size === 1) {
let id = genID();
callback({
status: "ok",
gameCode: id
});
2024-01-08 16:53:12 +01:00
socket.join(id);
} else {
callback({
status: "alreadyInLobby",
gameCode: socket.rooms[1]
});
}
});
socket.on('join lobby', (msg, callback) => {
if (io.sockets.adapter.rooms.get(msg) == null) {
callback({
status: "bad_id"
});
} else {
if (socket.rooms.size === 1) {
io.to(msg).emit("joined", session.nickname);
let opp = io.sockets.sockets.get(io.sockets.adapter.rooms.get(msg).values().next().value);
let oppNickname = opp.request.session.nickname;
2024-01-08 16:53:12 +01:00
socket.join(msg);
callback({
status: "ok",
oppNickname: oppNickname,
2024-01-08 16:53:12 +01:00
});
} else {
callback({
status: "alreadyInLobby",
gameCode: id,
2024-01-08 16:53:12 +01:00
});
}
}
});
socket.on('leave lobby', (callback) => {
if (socket.rooms.size === 2) {
socket.leave(socket.rooms[1]);
callback({
status: "ok"
});
} else {
callback({
status: "youreNotInLobby"
});
}
});
});
server.listen(7777, () => {
console.log('Server running at http://localhost:7777');
2024-01-08 16:53:12 +01:00
});
function genID() {
return Math.floor(100000 + Math.random() * 900000).toString();
}