papercats/server.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-11-24 11:26:49 -05:00
// https://github.com/socketio/socket.io/blob/master/examples/chat/index.js
2022-11-12 04:28:24 -05:00
import MiServer from "mimi-server";
import { Server } from "socket.io";
import express from "express";
import path from "path";
import { exec, fork } from "child_process";
import { config } from "./config.js";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2019-02-21 11:19:33 -05:00
2020-03-04 22:54:21 -05:00
config.dev ? exec("npm run build-dev") : exec("npm run build");
2019-02-21 11:19:33 -05:00
2020-07-24 03:11:35 -04:00
const port = process.env.PORT || config.port;
2019-02-21 11:19:33 -05:00
2020-11-24 11:26:49 -05:00
const { app, server } = new MiServer({
port,
static: path.join(__dirname, "public")
});
2020-11-24 11:26:49 -05:00
2022-11-12 04:28:24 -05:00
const io = new Server(server);
2020-11-24 11:26:49 -05:00
// Routing
2020-04-16 06:44:34 -04:00
app.use("/font", express.static(path.join(__dirname, "node_modules/@fortawesome/fontawesome-free")));
2017-02-28 02:20:32 -05:00
2022-11-12 04:28:24 -05:00
import Game from "./src/game-server.js";
2020-07-24 03:11:35 -04:00
const game = new Game();
2020-11-26 10:12:54 -05:00
2020-02-03 22:10:32 -05:00
io.on("connection", socket => {
socket.on("hello", (data, fn) => {
2019-01-15 10:42:15 -05:00
//TODO: error checking.
2019-02-22 00:22:21 -05:00
if (data.god && game.addGod(socket)) {
fn(true);
return;
}
2019-01-15 10:42:15 -05:00
if (data.name && data.name.length > 32) fn(false, "Your name is too long!");
2019-02-21 20:36:35 -05:00
else if (!game.addPlayer(socket, data.name)) fn(false, "There're too many platers!");
2019-01-15 10:42:15 -05:00
else fn(true);
});
socket.on("pings", (fn) => {
2019-01-15 10:42:15 -05:00
socket.emit("pongs");
socket.disconnect();
});
});
setInterval(() => {
2019-02-21 11:19:33 -05:00
game.tickFrame();
}, 1000 / 60);
2019-02-22 04:35:16 -05:00
2020-07-24 03:11:35 -04:00
for (let i = 0; i < parseInt(config.bots); i++) {
2020-07-22 08:56:04 -04:00
fork(path.join(__dirname, "paper-io-bot.js"), [`ws://localhost:${port}`], {
2020-11-24 11:26:49 -05:00
stdio: "inherit"
2019-02-22 04:35:16 -05:00
});
}