remade old shit

This commit is contained in:
leca 2024-04-06 05:53:37 +03:00
parent 8e041a0992
commit 009e60d984
5 changed files with 86 additions and 40 deletions

View File

@ -6,6 +6,5 @@ RUN npm ci --omit=dev
COPY html ./html
COPY views ./views
COPY src ./src
CMD ["node", "src/index.js"]
EXPOSE 7865 8080

View File

@ -32,7 +32,7 @@ That's it!
# Tips & tricks
1. Server settings are located in `./settings.json`, you need to change serverAddress in order to deploy
1. Server settings are located in `./pb-data/settings.json`, you need to change serverAddress after frist launch in order to deploy
2. When catches SIGUSR1, server will save a board with name read from settings file. SIGINT (CTRL + C) does the same but exit server when board is saved **KILL WITH SIGTERM WILL NOT SAVE THE BOARD!**
2. When catches SIGUSR1, server will save a board with name read from settings file. SIGINT (CTRL + C) and SIGTERM does the same but exit server when board is saved

View File

@ -5,9 +5,8 @@ services:
image: pixelbattle
build: .
ports:
- 8080:8080
- 7865:7865
- 8089:8089
volumes:
- ./board.png:/usr/src/app/board.png
- ./settings.json:/usr/src/app/settings.json
- ./pb-data:/usr/src/app/data
# - ./settings.json:/usr/src/app/settings.json
container_name: pixelbattle

View File

@ -27,16 +27,7 @@ function drawTimer() {
function connect() {
// socket.close();
serverAddress = document.getElementById("server-address").value;
serverPort = document.getElementById("server-port").value
console.log(`Connecting ${serverAddress}:${serverPort}`)
socket = new WebSocket(`wss://${serverAddress}:${serverPort}`)
timer.textContent="Board is loading, please wait";
socket.addEventListener("open", (event) => {
socket.send(JSON.stringify({code:0}));
});
socket.addEventListener("message", (event) => {
const handler = (event) => {
event.data.text().then(function(packet){
packet = JSON.parse(packet)
let code = packet.code
@ -74,9 +65,36 @@ function connect() {
break
}
});
}
serverAddress = document.getElementById("server-address").value;
serverPort = document.getElementById("server-port").value
console.log()
if (location.protocol == "https:") {
console.log(`Connecting wss://${serverAddress}:${serverPort}`)
socket = new WebSocket(`wss://${serverAddress}:${serverPort}`)
} else {
console.log(`Connecting ws://${serverAddress}:${serverPort}`)
socket = new WebSocket(`ws://${serverAddress}:${serverPort}`)
}
socket.addEventListener("open", (event) => {
socket.send(JSON.stringify({code:0}));
setInterval(() => {pingServer(socket)}, 10000);
});
// socket.send("{\"code\":0}");
socket.addEventListener("message", (event) => {
handler(event);
});
socket.addEventListener('close', (socket) => {
console.log(`Connecting ws://${serverAddress}:${serverPort}`)
socket = new WebSocket(`ws://${serverAddress}:${serverPort}`);
})
timer.textContent="Board is loading, please wait";
}
function drawPixel(x, y, color) {
@ -300,3 +318,8 @@ function trackTransforms(ctx){
return pt.matrixTransform(xform.inverse());
}
}
const pingServer = (socket) => {
console.log("Pinging server");
socket.send(JSON.stringify({}));
}

View File

@ -1,3 +1,5 @@
const config_path = "/usr/src/app/data/settings.json";
const WebSocket = require('ws');
const path = require('path');
const pug = require('pug');
@ -10,15 +12,30 @@ const app = express();
app.use(express.static(path.join(__dirname, '../html')));
app.set('view engine', 'pug');
const config = require("../settings.json");
let config;
if (!fs.existsSync(config_path)) {
config = {
"listenPort": 8080,
"boardWidth": 1920,
"boardHeight": 1080,
"serverAddress": "example.com",
"saveFile": "/usr/src/app/data/board.png",
"timeBetweenPixels": 5,
"autoSaveTime": 120
}
fs.writeFileSync(config_path, JSON.stringify(config));
} else {
config = JSON.parse(fs.readFileSync(config_path));
}
const httpPort = config.listenPort;
const exposedPort = config.exposedPort;
const serverAddress = config.serverAddress;
const saveFile = config.saveFile;
const boardWidth = config.boardWidth;
const boardHeight = config.boardHeight;
const timeBetweenPixels = config.timeBetweenPixels;
const autoSaveTime = config.autoSaveTime;
var toQuit = false;
var board = new Array(boardWidth * boardHeight * 4);
@ -26,9 +43,10 @@ var board = new Array(boardWidth * boardHeight * 4);
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) => {
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);
@ -111,6 +129,8 @@ server.on('connection', (client) => {
// client.send("{\"code\":3}")
}
break;
case undefined:
break;
default:
console.log("Packet cannot be understood: ", packet);
client.send("{\"code\":-1}");
@ -130,9 +150,8 @@ const pingClient = () => {
};
setInterval(pingClient, 15000)
app.get('/', (req, res) => {
res.render('index.pug', { root: __dirname, server: serverAddress, port: exposedPort });
res.render('index.pug', { root: __dirname, server: serverAddress, port: httpPort });
})
app.use(function (err, req, res, next) {
if (err) console.log(err);
@ -157,7 +176,7 @@ const save = (err, image) => {
}
}
image.write(`./${saveFile}`, (err) => {
image.write(`${saveFile}`, (err) => {
if (err) throw err;
console.log("Saved")
if (toQuit) process.exit();
@ -167,16 +186,22 @@ const save = (err, image) => {
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);
});
const save_notify = (signal) => {
console.log(`Caught ${signal}, saving ${boardWidth}x${boardHeight} image`)
process.on('SIGINT', () => {
//save
console.log(`Caught SIGINT, saving ${boardWidth}x${boardHeight} image and quitting`)
toQuit = true;
// if (signal == 'SIGUSR') {
// toQuit = false;
// } else {
// toQuit = true;
// }
toQuit = signal == 'SIGUSR1' || signal == 'AUTOSAVE'? false : true;
let image = new Jimp(boardWidth, boardHeight, save);
});
}
setInterval(() => { save_notify('AUTOSAVE'); }, autoSaveTime * 1000);
process.on('SIGUSR1', () => { save_notify('SIGUSR'); });
process.on('SIGINT', () => { save_notify('SIGINT'); });
process.on('SIGTERM', () => { save_notify('SIGTERM'); });