try catch

This commit is contained in:
leca 2024-04-28 17:56:45 +03:00
parent bd0eb1eb66
commit c2b484b88d
5 changed files with 162 additions and 42 deletions

0
src/admin.js Normal file
View File

23
src/config.js Normal file
View File

@ -0,0 +1,23 @@
const fs = require('fs')
module.exports = {
config: {},
initConfig: (config_path) => {
if (!fs.existsSync(config_path)) {
this.config = {
"listenPort": 8080,
"boardWidth": 1920,
"boardHeight": 1080,
"serverAddress": "example.com",
"saveFile": "./data//board.png",
"timeBetweenPixels": 5,
"autoSaveTime": 120
}
fs.writeFileSync(config_path, JSON.stringify(this.config));
} else {
this.config = JSON.parse(fs.readFileSync(config_path));
}
console.log(this.config);
}
}

9
src/db.js Normal file
View File

@ -0,0 +1,9 @@
// const pg = require('pg');
// const { Client } = pg;
// const client = Client();
// const init_database = async () => {
// await client.connect();
// await client.query('CREATE TABLE IF NOT EXIST users');
// }

View File

@ -104,6 +104,7 @@ server.on('connection', (client) => {
console.log(`New client has connected, assigning id ${client.id} to it.`)
client.on('message', function (msg) {
try {
let packet, content, code;
try {
packet = JSON.parse(msg.toString());
@ -148,6 +149,7 @@ server.on('connection', (client) => {
console.log("Packet cannot be understood: ", packet);
client.send("{\"code\":-1}");
}
}catch (e) { console.log(e) }
});
client.on('close', function () {

86
src/utils.js Normal file
View File

@ -0,0 +1,86 @@
module.exports = function() {
this.start = async () => {
if (!fs.existsSync(config_path)) {
config = {
"listenPort": 8080,
"boardWidth": 1920,
"boardHeight": 1080,
"serverAddress": "example.com",
"saveFile": "./data//board.png",
"timeBetweenPixels": 5,
"autoSaveTime": 120
}
fs.writeFileSync(config_path, JSON.stringify(config));
} else {
config = JSON.parse(fs.readFileSync(config_path));
}
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")
});
}
};
this.save = async (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();
});
};
this.save_notify = (signal) => {
console.log(`Caught ${signal}, saving ${boardWidth}x${boardHeight} image`)
toQuit = signal == 'SIGUSR1' || signal == 'AUTOSAVE' ? false : true;
let image = new Jimp(boardWidth, boardHeight, save);
};
this.evaulatePixelNumber = (x, y) => {
let pixelNumber;
if (y > 0)
pixelNumber = (y) * boardWidth + x;
if (y == 0)
pixelNumber = x;
return pixelNumber;
};
this.boradcastOnline = () => {
let response = {};
for (let i = 0; i < clients.length; i++) {
response.code = 2;
response.online = clients.length;
clients[i].send(Buffer.from(JSON.stringify(response)));
}
}
}