2023-07-31 19:18:21 +03:00
|
|
|
const WebSocket = require('ws');
|
2023-08-01 11:32:54 +03:00
|
|
|
const express = require('express');
|
|
|
|
const http = express();
|
|
|
|
const path = require('path');
|
2023-08-01 09:25:16 +03:00
|
|
|
const pug = require('pug');
|
2023-08-01 11:32:54 +03:00
|
|
|
const Jimp = require('jimp');
|
2023-08-01 11:53:03 +03:00
|
|
|
const fs = require('fs');
|
2023-08-01 09:25:16 +03:00
|
|
|
|
|
|
|
http.use(express.static(path.join(__dirname, 'html')));
|
|
|
|
http.set('view engine', 'pug');
|
|
|
|
|
2023-07-31 19:18:21 +03:00
|
|
|
const config = require("./settings.json");
|
2023-08-01 11:32:54 +03:00
|
|
|
const httpPort = config.httpPort;
|
|
|
|
const appPort = config.appPort;
|
|
|
|
const serverAddress = config.serverAddress;
|
|
|
|
const saveFile = config.saveFile;
|
|
|
|
const boardWidth = config.boardWidth;
|
|
|
|
const boardHeight = config.boardHeight;
|
2023-08-01 09:25:16 +03:00
|
|
|
|
2023-08-01 11:32:54 +03:00
|
|
|
var toQuit = false;
|
2023-07-31 19:18:21 +03:00
|
|
|
|
2023-08-01 11:32:54 +03:00
|
|
|
var board = new Array(boardWidth * boardHeight * 4);
|
2023-08-01 11:53:03 +03:00
|
|
|
|
|
|
|
if (!fs.existsSync(saveFile)) {
|
|
|
|
console.log("No save file found, creating blank board.");
|
|
|
|
board.fill(255);
|
|
|
|
} else {
|
|
|
|
console.log("Save file found, loading")
|
|
|
|
let image = Jimp.read(`./${saveFile}`, (err, image) => {
|
|
|
|
for (let x = 0; x < boardWidth; x ++) {
|
|
|
|
for (let y = 0; y < boardHeight; y ++) {
|
|
|
|
pixelNumber = evaulatePixelNumber(x * 4, y * 4);
|
|
|
|
let pixel = Jimp.intToRGBA(image.getPixelColor(x, y))
|
|
|
|
board[pixelNumber + 0] = pixel.r
|
|
|
|
board[pixelNumber + 1] = pixel.g
|
|
|
|
board[pixelNumber + 2] = pixel.b
|
|
|
|
board[pixelNumber + 3] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("Loaded")
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-31 19:18:21 +03:00
|
|
|
|
|
|
|
const server = new WebSocket.Server({
|
|
|
|
port: appPort
|
|
|
|
});
|
|
|
|
|
|
|
|
let clients = [];
|
|
|
|
|
|
|
|
const evaulatePixelNumber = (x, y) => {
|
|
|
|
let pixelNumber;
|
|
|
|
if (y > 0)
|
2023-08-01 11:32:54 +03:00
|
|
|
pixelNumber = (y) * boardWidth + x;
|
2023-07-31 19:18:21 +03:00
|
|
|
if (y == 0)
|
2023-08-01 11:32:54 +03:00
|
|
|
pixelNumber = x;
|
|
|
|
return pixelNumber;
|
2023-07-31 19:18:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
server.on('connection', function(client) {
|
|
|
|
clients.push(client);
|
|
|
|
|
|
|
|
// When you receive a message, send that message to every socket.
|
|
|
|
client.on('message', function(msg) {
|
2023-07-31 20:46:04 +03:00
|
|
|
let packet, content, code;
|
2023-07-31 19:18:21 +03:00
|
|
|
try {
|
|
|
|
packet = JSON.parse(msg.toString());
|
2023-08-01 11:32:54 +03:00
|
|
|
content = packet.content;
|
|
|
|
code = packet.code;
|
2023-07-31 19:18:21 +03:00
|
|
|
} catch (e) {console.log(e)}
|
|
|
|
let response = {};
|
|
|
|
switch(code) {
|
|
|
|
case 0:
|
|
|
|
response.code = 0;
|
|
|
|
response.content = board;
|
|
|
|
client.send(Buffer.from(JSON.stringify(response)));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
response.code = 1;
|
|
|
|
response.content = content;
|
2023-08-01 11:32:54 +03:00
|
|
|
console.log(`response content ${response.content}`);
|
2023-07-31 19:18:21 +03:00
|
|
|
contentJson = JSON.parse(content);
|
2023-07-31 20:46:04 +03:00
|
|
|
let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4);
|
2023-07-31 19:18:21 +03:00
|
|
|
if (contentJson.x < 0 || contentJson.y < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2023-07-31 20:46:04 +03:00
|
|
|
board[pixelNumber + 0] = contentJson.r;
|
|
|
|
board[pixelNumber + 1] = contentJson.g;
|
|
|
|
board[pixelNumber + 2] = contentJson.b;
|
|
|
|
board[pixelNumber + 3] = 255;
|
2023-07-31 19:18:21 +03:00
|
|
|
clients.forEach(c => c.send(Buffer.from(JSON.stringify(response))));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Packet cannot be understood: ", packet);
|
2023-08-01 11:32:54 +03:00
|
|
|
client.send("{\"code\":-1}");
|
2023-07-31 19:18:21 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('close', function() {
|
|
|
|
clients = clients.filter(s => s !== client);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
http.get('/', (req, res) => {
|
2023-08-01 11:32:54 +03:00
|
|
|
res.render('index.pug', {root: __dirname, server: serverAddress, port:appPort});
|
2023-07-31 19:18:21 +03:00
|
|
|
})
|
|
|
|
|
2023-08-01 09:25:16 +03:00
|
|
|
http.listen(httpPort, "0.0.0.0", () => {
|
2023-08-01 11:32:54 +03:00
|
|
|
console.log(`Starting pixelbattle http server on port ${httpPort}`);
|
2023-07-31 19:18:21 +03:00
|
|
|
})
|
2023-08-01 11:32:54 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|