remade old shit
This commit is contained in:
parent
8e041a0992
commit
009e60d984
|
@ -6,6 +6,5 @@ RUN npm ci --omit=dev
|
||||||
COPY html ./html
|
COPY html ./html
|
||||||
COPY views ./views
|
COPY views ./views
|
||||||
COPY src ./src
|
COPY src ./src
|
||||||
|
|
||||||
CMD ["node", "src/index.js"]
|
CMD ["node", "src/index.js"]
|
||||||
EXPOSE 7865 8080
|
EXPOSE 7865 8080
|
|
@ -32,7 +32,7 @@ That's it!
|
||||||
|
|
||||||
# Tips & tricks
|
# 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
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,8 @@ services:
|
||||||
image: pixelbattle
|
image: pixelbattle
|
||||||
build: .
|
build: .
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- 8089:8089
|
||||||
- 7865:7865
|
|
||||||
volumes:
|
volumes:
|
||||||
- ./board.png:/usr/src/app/board.png
|
- ./pb-data:/usr/src/app/data
|
||||||
- ./settings.json:/usr/src/app/settings.json
|
# - ./settings.json:/usr/src/app/settings.json
|
||||||
container_name: pixelbattle
|
container_name: pixelbattle
|
|
@ -27,16 +27,7 @@ function drawTimer() {
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
// socket.close();
|
// socket.close();
|
||||||
serverAddress = document.getElementById("server-address").value;
|
const handler = (event) => {
|
||||||
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) => {
|
|
||||||
event.data.text().then(function(packet){
|
event.data.text().then(function(packet){
|
||||||
packet = JSON.parse(packet)
|
packet = JSON.parse(packet)
|
||||||
let code = packet.code
|
let code = packet.code
|
||||||
|
@ -74,9 +65,36 @@ function connect() {
|
||||||
break
|
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) {
|
function drawPixel(x, y, color) {
|
||||||
|
@ -300,3 +318,8 @@ function trackTransforms(ctx){
|
||||||
return pt.matrixTransform(xform.inverse());
|
return pt.matrixTransform(xform.inverse());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pingServer = (socket) => {
|
||||||
|
console.log("Pinging server");
|
||||||
|
socket.send(JSON.stringify({}));
|
||||||
|
}
|
||||||
|
|
63
src/index.js
63
src/index.js
|
@ -1,3 +1,5 @@
|
||||||
|
const config_path = "/usr/src/app/data/settings.json";
|
||||||
|
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const pug = require('pug');
|
const pug = require('pug');
|
||||||
|
@ -10,15 +12,30 @@ const app = express();
|
||||||
app.use(express.static(path.join(__dirname, '../html')));
|
app.use(express.static(path.join(__dirname, '../html')));
|
||||||
app.set('view engine', 'pug');
|
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 httpPort = config.listenPort;
|
||||||
const exposedPort = config.exposedPort;
|
|
||||||
const serverAddress = config.serverAddress;
|
const serverAddress = config.serverAddress;
|
||||||
const saveFile = config.saveFile;
|
const saveFile = config.saveFile;
|
||||||
const boardWidth = config.boardWidth;
|
const boardWidth = config.boardWidth;
|
||||||
const boardHeight = config.boardHeight;
|
const boardHeight = config.boardHeight;
|
||||||
const timeBetweenPixels = config.timeBetweenPixels;
|
const timeBetweenPixels = config.timeBetweenPixels;
|
||||||
|
const autoSaveTime = config.autoSaveTime;
|
||||||
var toQuit = false;
|
var toQuit = false;
|
||||||
|
|
||||||
var board = new Array(boardWidth * boardHeight * 4);
|
var board = new Array(boardWidth * boardHeight * 4);
|
||||||
|
@ -26,9 +43,10 @@ var board = new Array(boardWidth * boardHeight * 4);
|
||||||
if (!fs.existsSync(saveFile)) {
|
if (!fs.existsSync(saveFile)) {
|
||||||
console.log("No save file found, creating blank board.");
|
console.log("No save file found, creating blank board.");
|
||||||
board.fill(255);
|
board.fill(255);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.log("Save file found, loading")
|
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 x = 0; x < boardWidth; x++) {
|
||||||
for (let y = 0; y < boardHeight; y++) {
|
for (let y = 0; y < boardHeight; y++) {
|
||||||
pixelNumber = evaulatePixelNumber(x * 4, y * 4);
|
pixelNumber = evaulatePixelNumber(x * 4, y * 4);
|
||||||
|
@ -111,6 +129,8 @@ server.on('connection', (client) => {
|
||||||
// client.send("{\"code\":3}")
|
// client.send("{\"code\":3}")
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case undefined:
|
||||||
|
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}");
|
||||||
|
@ -130,9 +150,8 @@ const pingClient = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
setInterval(pingClient, 15000)
|
setInterval(pingClient, 15000)
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
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) {
|
app.use(function (err, req, res, next) {
|
||||||
if (err) console.log(err);
|
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;
|
if (err) throw err;
|
||||||
console.log("Saved")
|
console.log("Saved")
|
||||||
if (toQuit) process.exit();
|
if (toQuit) process.exit();
|
||||||
|
@ -167,16 +186,22 @@ const save = (err, image) => {
|
||||||
|
|
||||||
process.stdin.resume();
|
process.stdin.resume();
|
||||||
|
|
||||||
process.on('SIGUSR1', () => {
|
const save_notify = (signal) => {
|
||||||
//save
|
console.log(`Caught ${signal}, saving ${boardWidth}x${boardHeight} image`)
|
||||||
console.log(`Caught SIGUSR1, saving ${boardWidth}x${boardHeight} image`)
|
|
||||||
toQuit = false;
|
|
||||||
let image = new Jimp(boardWidth, boardHeight, save);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
// if (signal == 'SIGUSR') {
|
||||||
//save
|
// toQuit = false;
|
||||||
console.log(`Caught SIGINT, saving ${boardWidth}x${boardHeight} image and quitting`)
|
// } else {
|
||||||
toQuit = true;
|
// toQuit = true;
|
||||||
let image = new Jimp(boardWidth, boardHeight, save);
|
// }
|
||||||
});
|
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'); });
|
||||||
|
|
Loading…
Reference in New Issue