Webpage for Games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.7 KiB

import {ServerGame} from "./game_standard"
import {Room} from "../room"
import {Client} from "../client"
import {log} from "../logger";
import fs = require("fs");
export class GlobalDraw extends ServerGame {
lines: any[];
pixels: any[][];
linesPath = "json_data/global_draw/lines.json";
pixelsPath = "json_data/global_draw/pixels.json";
pixelCount = 1000;
constructor(lobby: Room, settings: Settings.Global) {
super(lobby, settings);
this.lines = [];
this.pixels = [];
for (let x = 0; x < this.pixelCount; x++) {
let column = [];
for (let y = 0; y < this.pixelCount; y++) {
column.push({x: x, y: y, c: "#ffffff"});
}
this.pixels.push(column);
}
let linesLoaded = false;
let pixelsLoaded = false;
this.loadDrawingsFromFile(this.linesPath, (data: any[]) => {
this.lines = data;
}, () => {
linesLoaded = true;
if (pixelsLoaded) {
this.startSaveInterval();
}
});
this.loadDrawingsFromFile(this.pixelsPath, (data: any[]) => {
for (let x = 0; x < this.pixelCount; x++) {
for (let y = 0; y < this.pixelCount; y++) {
if (data[x])
if (data[x][y])
this.pixels[x][y].c = data[x][y].c
}
}
}, () => {
pixelsLoaded = true;
if (linesLoaded) {
this.startSaveInterval();
}
});
}
startSaveInterval() {
this.saveAllDrawingsToFile();
//Saves once every day
setInterval(() => this.saveAllDrawingsToFile(), 1000 * 60 * 60 * 24);
}
addLine(line: any) {
this.lines.push(line);
this.room.toAll('add-line', line)
}
fillPixel(pixel: any) {
this.pixels[pixel.x][pixel.y].c = pixel.c;
this.room.toAll('fill-pixel', pixel)
}
loadDrawingsFromFile(drawingsPath: string, successs: (data: any[]) => void, done: () => void) {
fs.readFile(drawingsPath, 'utf8', (err, data) => {
if (err)
log('load-error', null, this.room, err.message);
else {
try {
let parsed = JSON.parse(data);
log('load-success', null, this.room);
successs(parsed);
} catch (e) {
log('parse-error', null, this.room, e.message);
}
}
done();
});
}
saveDrawingsToFile(drawings: any[], drawingsPath: string, callback: (err: any) => void) {
let splits = drawingsPath.split('/');
let path = splits.slice(0, splits.length - 1).reduce((prev, curr) => prev + '/' + curr);
let name = splits[splits.length - 1];
if (!fs.existsSync(path)) {
fs.mkdirSync(path, {recursive: true});
}
fs.writeFile(drawingsPath, JSON.stringify(drawings), callback);
}
saveAllDrawingsToFile() {
let linesSaved = false;
let pixelsSaved = false;
this.saveDrawingsToFile(this.lines, this.linesPath, (err) => {
if (err)
log('save-error', null, this.room, err.message);
else {
linesSaved = true;
if (pixelsSaved) {
this.room.toAll('all-saved');
linesSaved = false;
pixelsSaved = false
}
log('save-success', null, this.room, 'Successfully saved lines to file')
}
});
this.saveDrawingsToFile(this.pixels, this.pixelsPath, (err) => {
if (err)
log('save-error', null, this.room, err.message);
else {
pixelsSaved = true;
if (linesSaved) {
this.room.toAll('all-saved');
pixelsSaved = false;
linesSaved = false
}
log('save-success', null, this.room, 'Successfully saved pixels to file')
}
});
}
addClient(client: Client): void {
this.setEvents(client);
}
setEvents(client: Client): void {
super.setEvents(client);
let socket = client.socket;
socket.on('add-line', (line) => this.addLine(line));
socket.on('fill-pixel', (pixel) => this.fillPixel(pixel));
socket.on('request-all-lines', () => socket.emit('add-all', this.lines));
socket.on('request-all-pixels', () => socket.emit('fill-all', this.pixels));
socket.on('save-all', () => this.saveAllDrawingsToFile());
}
}