Board can now be saved but not loaded (for now)

This commit is contained in:
leca 2023-08-01 11:32:54 +03:00
parent a41441d94f
commit e5a760dbdf
4 changed files with 1577 additions and 19 deletions

View File

@ -1,19 +1,24 @@
const WebSocket = require('ws'); const WebSocket = require('ws');
const express = require('express') const express = require('express');
const http = express() const http = express();
const path = require('path') const path = require('path');
const pug = require('pug'); const pug = require('pug');
const Jimp = require('jimp');
http.use(express.static(path.join(__dirname, 'html'))); http.use(express.static(path.join(__dirname, 'html')));
http.set('view engine', 'pug'); http.set('view engine', 'pug');
const config = require("./settings.json"); const config = require("./settings.json");
const httpPort = config.httpPort const httpPort = config.httpPort;
const appPort = config.appPort const appPort = config.appPort;
const serverAddress = config.serverAddress const serverAddress = config.serverAddress;
const saveFile = config.saveFile;
const boardWidth = config.boardWidth;
const boardHeight = config.boardHeight;
var toQuit = false;
var board = new Array(config.boardHeight * config.boardWidth * 4); var board = new Array(boardWidth * boardHeight * 4);
board.fill(255); board.fill(255);
const server = new WebSocket.Server({ const server = new WebSocket.Server({
@ -25,10 +30,10 @@ let clients = [];
const evaulatePixelNumber = (x, y) => { const evaulatePixelNumber = (x, y) => {
let pixelNumber; let pixelNumber;
if (y > 0) if (y > 0)
pixelNumber = (y) * config.boardWidth + x pixelNumber = (y) * boardWidth + x;
if (y == 0) if (y == 0)
pixelNumber = x pixelNumber = x;
return pixelNumber return pixelNumber;
} }
server.on('connection', function(client) { server.on('connection', function(client) {
@ -39,10 +44,9 @@ server.on('connection', function(client) {
let packet, content, code; let packet, content, code;
try { try {
packet = JSON.parse(msg.toString()); packet = JSON.parse(msg.toString());
content = packet.content content = packet.content;
code = packet.code code = packet.code;
} catch (e) {console.log(e)} } catch (e) {console.log(e)}
console.log(packet)
let response = {}; let response = {};
switch(code) { switch(code) {
case 0: case 0:
@ -53,7 +57,7 @@ server.on('connection', function(client) {
case 1: case 1:
response.code = 1; response.code = 1;
response.content = content; response.content = content;
console.log(`response content ${response.content}`) console.log(`response content ${response.content}`);
contentJson = JSON.parse(content); contentJson = JSON.parse(content);
let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4); let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4);
if (contentJson.x < 0 || contentJson.y < 0) { if (contentJson.x < 0 || contentJson.y < 0) {
@ -67,7 +71,7 @@ server.on('connection', function(client) {
break; break;
default: default:
console.log("Packet cannot be understood: ", packet); console.log("Packet cannot be understood: ", packet);
client.send("{\"code\":-1}") client.send("{\"code\":-1}");
} }
}); });
@ -77,9 +81,59 @@ server.on('connection', function(client) {
}); });
http.get('/', (req, res) => { http.get('/', (req, res) => {
res.render('index.pug', {root: __dirname, server: serverAddress, port:appPort}) res.render('index.pug', {root: __dirname, server: serverAddress, port:appPort});
}) })
http.listen(httpPort, "0.0.0.0", () => { http.listen(httpPort, "0.0.0.0", () => {
console.log(`Starting pixelbattle http server on port ${httpPort}`) console.log(`Starting pixelbattle http server on port ${httpPort}`);
}) })
const rgbToHex = (r, g, b) => '#' + [r, g, b].map(x => {
const hex = x.toString(16)
return hex.length === 1 ? '0' + hex : hex
}).join('')
function d2h(d) { return (+d).toString(16).toUpperCase(); }
const save = (err, image) => {
if (err) throw err;
console.log()
for (let x = 0; x < boardWidth; x ++) {
for (let y = 0; y < boardHeight; y ++) {
let pixelNumber = evaulatePixelNumber(x * 4 , y * 4);
image.setPixelColor(
Jimp.rgbaToInt(
Number(board[pixelNumber + 0]),
Number(board[pixelNumber + 1]),
Number(board[pixelNumber + 2]),
Number(board[pixelNumber + 3])
),
x, y
);
}
}
image.write(`./${saveFile}`, (err) => {
if (err) throw err;
console.log("Saved")
if (toQuit) process.exit();
});
}
process.stdin.resume();
process.on('SIGUSR1', () => {
//save
console.log(`Caught SIGUSR1, saving ${boardWidth}x${boardHeight} image`)
toQuit = false;
let image = new Jimp(boardWidth, boardHeight, save);
});
process.on('SIGINT', () => {
//save
console.log(`Caught SIGINT, saving ${boardWidth}x${boardHeight} image and quitting`)
toQuit = true;
let image = new Jimp(boardWidth, boardHeight, save);
});

1501
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,8 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js", "start": "node index.js",
"dev": "nodemon index.js" "dev": "nodemon index.js",
"prod": "node index.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -20,6 +21,7 @@
"license": "GPL-3.0-or-later", "license": "GPL-3.0-or-later",
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2",
"jimp": "^0.22.10",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"pug": "^3.0.2", "pug": "^3.0.2",
"ws": "^8.13.0" "ws": "^8.13.0"

View File

@ -3,5 +3,6 @@
"httpPort": 8080, "httpPort": 8080,
"boardWidth": 1920, "boardWidth": 1920,
"boardHeight": 1080, "boardHeight": 1080,
"serverAddress": "localhost" "serverAddress": "localhost",
"saveFile": "board.png"
} }