Browse Source

Moved room config to separate category, fixes #3 and many related config bugs

master
asiekierka 6 years ago
parent
commit
7e888885fc
5 changed files with 30 additions and 25 deletions
  1. 0
    1
      TODO
  2. 7
    3
      app.js
  3. 6
    3
      config-default.json
  4. 3
    1
      config.json
  5. 14
    17
      types.js

+ 0
- 1
TODO View File

@@ -2,7 +2,6 @@ Mandatory:
* Refactor app.use in app.js.
* Refactor a LOT of other code.
* Make code run with Underscore better (throttle, isUndefined, etc.)
* Refactor room config (move to room{...})

Recommended:
* Clean up the UI [move Smoothen to a separate Brush menu. Or hide it entirely for now.]

+ 7
- 3
app.js View File

@@ -11,14 +11,18 @@ var express = require('express')
eval(fs.readFileSync('lib/grayboard-util.js','utf8'));
eval(fs.readFileSync('lib/grayboard-canvas.js','utf8'));

var config = JSON.parse(fs.readFileSync('config.json','utf8'))
var configFile = JSON.parse(fs.readFileSync('config.json','utf8'))
, configDef = JSON.parse(fs.readFileSync('config-default.json','utf8'))
, NodeCanvas = require('canvas')
, Room = require('./types.js').Room
, User = require('./types.js').User
, Chat = require('./chat.js').Chat;

Room.defaultConfig = _.defaults(config,configDef);
var config = _.defaults(configFile,configDef);

for(var key in config)
if(_.isObject(config[key]))
config[key] = _.defaults(configFile[key],configDef[key]);

server.listen(config.port);

@@ -84,7 +88,7 @@ io.sockets.on('connection', function(socket) {
socket.on('join_room', function(data) {
roomName = Room.getName(data);
socket.join(data);
var room = Room.create(roomName);
var room = Room.create(roomName,config.room);
var chat = new Chat(room,config.chat);
var user = new User(socket,"");
user.genNickname(room.config.nickname);

+ 6
- 3
config-default.json View File

@@ -1,14 +1,17 @@
{ "_comment": "THIS CONFIG FILE CONTAINS DEFAULT INFORMATION.",
"_comment2": "CHANGE CONFIG.JSON INSTEAD.",
"width": 800,
"height": 480,
"port": 80,
"nickname": "Anonymous",
"chat": {
"messageColor": "#101010",
"serverColor": "#205080",
"meColor": "#701070",
"disabled": []
},
"room": {
"width": 800,
"height": 480,
"nickname": "Anonymous",
"autoBackup": false
}
}


+ 3
- 1
config.json View File

@@ -1,4 +1,6 @@
{ "port": 8080,
"nickname": "Anonymous"
"room": {
"nickname": "Anonymous"
}
}


+ 14
- 17
types.js View File

@@ -1,23 +1,26 @@
// Dependencies
var NodeCanvas = require('canvas'),
fs = require('fs'),
JSON = require('JSON2');
JSON = require('JSON2'),
_ = require('underscore');

// Constructor
var Room = function(name,width,height) {
var Room = function(name,defConfig) {
console.log("Created room " + name);
this.name = name;
if(!this.loadConfig()) this.defaultConfig(width,height);
if(!this.loadConfig(defConfig)) this.config = defConfig;
this.width = this.config.width; this.height = this.config.height;
this.nodeCanvas = new NodeCanvas(this.width,this.height);
this.canvas = new Canvas(undefined,this.nodeCanvas.getContext('2d'),this.width,this.height);
this.canvas.clear();
this.loadBackup();
this.autoBackup();
this.users = new Array();
}
Room.rooms = new Array();
Room.create = function(name) {
if(typeof Room.rooms[name] == "undefined")
Room.rooms[name] = new Room(name);
Room.create = function(name,config) {
if(_.isUndefined(Room.rooms[name]))
Room.rooms[name] = new Room(name,config);
return Room.rooms[name];
}
Room.remove = function(name) {
@@ -36,16 +39,14 @@ Room.prototype.getName = function() { return this.name; }
Room.prototype.getPath = function() { return "rooms/" + this.name + "/"; }

// Config load/save
Room.prototype.loadConfig = function(json) {
if(this.config) return true;
Room.prototype.loadConfig = function(defConfig) {
try {
this.config = json || JSON.parse(fs.readFileSync(this.getPath() + 'config.json','utf8'));
if(!this.config) return false;
this.width = this.config.width;
this.height = this.config.height;
var config = JSON.parse(fs.readFileSync(this.getPath() + 'config.json','utf8'));
if(!config) return false;
this.config = _.defaults(config, defConfig);
return true;
}
catch(e) {}
catch(e) { return false; }
}

Room.prototype.getInitString = function() {
@@ -55,10 +56,6 @@ Room.prototype.getInitString = function() {
return JSON.stringify(cmd);
}

Room.prototype.defaultConfig = function() {
return this.loadConfig(Room.defaultConfig);
}

// User handling
Room.prototype.addUser = function(user) { user.room = this; this.users[user.id] = user; }
Room.prototype.getUser = function(id) { return this.users[id]; }

Loading…
Cancel
Save