From 3b3c8929e1fc8ec1b5d48609de742a5781426188 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 25 Nov 2020 00:26:49 +0800 Subject: [PATCH] Use mimi-server --- package.json | 72 +++++++++++++++++++++++------------------------ server.js | 34 +++++++--------------- src/core/color.js | 4 +-- 3 files changed, 48 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index cdea233..c265cc9 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,38 @@ { - "name": "paper-io", - "version": "0.0.1", - "description": "An multiplayer-IO type game (cloned from Paper-IO)", - "main": "server.js", - "scripts": { - "build": "browserify client.js | terser > public/js/bundle.js", - "build-dev": "watchify client.js -o public/js/bundle.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/stevenjoezhang/paper.io.git" - }, - "keywords": [ - "Paper-IO", - "IO", - "Game" - ], - "author": "Mimi (https://zhangshuqiao.org)", - "license": "MIT", - "bugs": { - "url": "https://github.com/stevenjoezhang/paper.io/issues" - }, - "homepage": "https://github.com/stevenjoezhang/paper.io", - "dependencies": { - "@fortawesome/fontawesome-free": "^5.13.0", - "chalk": "^3.0.0", - "express": "^4.17.1", - "jquery": "^3.4.1", - "socket.io": "^2.3.0", - "socket.io-client": "^2.3.0" - }, - "devDependencies": { - "browserify": "^16.5.0", - "terser": "^4.6.3", - "watchify": "^3.11.1" - } + "name": "paper-io", + "version": "0.0.1", + "private": true, + "description": "An multiplayer-IO type game (cloned from Paper-IO)", + "main": "server.js", + "scripts": { + "build": "browserify client.js | terser > public/js/bundle.js", + "build-dev": "watchify client.js -o public/js/bundle.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/stevenjoezhang/paper.io.git" + }, + "keywords": [ + "Paper-IO", + "IO", + "Game" + ], + "author": "Mimi (https://zhangshuqiao.org)", + "license": "MIT", + "bugs": { + "url": "https://github.com/stevenjoezhang/paper.io/issues" + }, + "homepage": "https://github.com/stevenjoezhang/paper.io", + "dependencies": { + "@fortawesome/fontawesome-free": "^5.13.0", + "jquery": "^3.4.1", + "mimi-server": "^0.0.1", + "socket.io": "^2.3.0", + "socket.io-client": "^2.3.0" + }, + "devDependencies": { + "browserify": "^16.5.0", + "terser": "^4.6.3", + "watchify": "^3.11.1" + } } diff --git a/server.js b/server.js index 7bacd12..ad41c03 100644 --- a/server.js +++ b/server.js @@ -1,36 +1,22 @@ -//https://github.com/socketio/socket.io/blob/master/examples/chat/index.js +// https://github.com/socketio/socket.io/blob/master/examples/chat/index.js +const MiServer = require("mimi-server"); const express = require("express"); -const app = express(); const path = require("path"); -const os = require("os"); -const chalk = require("chalk"); -const server = require("http").createServer(app); -const io = require("socket.io")(server); const { exec, fork } = require("child_process"); const config = require("./config.json"); config.dev ? exec("npm run build-dev") : exec("npm run build"); -if (!(config.port >= 0 && config.port < 65536 && config.port % 1 === 0)) { - console.error("[ERROR] `port` argument must be an integer >= 0 and < 65536. Default value will be used."); - config.port = 8080; -} const port = process.env.PORT || config.port; -server.listen(port, () => { - console.log(chalk.yellow("Server available on:")); - const ifaces = os.networkInterfaces(); - Object.keys(ifaces).forEach(dev => { - ifaces[dev].forEach(details => { - if (details.family === 'IPv4') { - console.log((` http://${details.address}:${chalk.green(port.toString())}`)); - } - }); - }); - console.log("Hit CTRL-C to stop the server"); +const { app, server } = new MiServer({ + port, + static: path.join(__dirname, "public") }); -//Routing -app.use(express.static(path.join(__dirname, "public"))); + +const io = require("socket.io")(server); + +// Routing app.use("/font", express.static(path.join(__dirname, "node_modules/@fortawesome/fontawesome-free"))); const Game = require("./src/game-server"); @@ -59,6 +45,6 @@ setInterval(() => { for (let i = 0; i < parseInt(config.bots); i++) { fork(path.join(__dirname, "paper-io-bot.js"), [`ws://localhost:${port}`], { - stdio: 'inherit' + stdio: "inherit" }); } diff --git a/src/core/color.js b/src/core/color.js index af5ea9b..8fa83e0 100644 --- a/src/core/color.js +++ b/src/core/color.js @@ -3,12 +3,12 @@ function verifyRange() { if (arguments[i] < 0 || arguments[i] > 1) throw new RangeError("H, S, L, and A parameters must be between the range [0, 1]"); } } -//https://stackoverflow.com/a/9493060/7344257 +// https://stackoverflow.com/a/9493060/7344257 function hslToRgb(h, s, l) { let r, g, b; if (s == 0) r = g = b = l; //Achromatic else { - const hue2rgb = function hue2rgb(p, q, t) { + const hue2rgb = function(p, q, t) { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t;