Permalink
Browse files

copying levels, dashboard and ui improvements

  • Loading branch information...
jomoho committed Jul 31, 2011
1 parent f7bb5a5 commit 7f9b81b53ff47a545107600550593b9dfdb0d5e1
View
No changes.
View
@@ -97,4 +97,6 @@ Grid.prototype.resize= function(width, height, clear){
}
this.blit(copy,0,0);
};
-
+Grid.prototype.rename = function(name){
+ this.name = name;
+};
View
@@ -0,0 +1,59 @@
+/*
+ * Javascript Humane Dates
+ * Copyright (c) 2008 Dean Landolt (deanlandolt.com)
+ * Re-write by Zach Leatherman (zachleat.com)
+ *
+ * Adopted from the John Resig's pretty.js
+ * at http://ejohn.org/blog/javascript-pretty-date
+ * and henrah's proposed modification
+ * at http://ejohn.org/blog/javascript-pretty-date/#comment-297458
+ *
+ * Licensed under the MIT license.
+ */
+
+
+module.exports.date = function humane_date(date){
+ var time_formats = [
+ [60, 'Just Now'],
+ [90, '1 Minute'], // 60*1.5
+ [3600, 'Minutes', 60], // 60*60, 60
+ [5400, '1 Hour'], // 60*60*1.5
+ [86400, 'Hours', 3600], // 60*60*24, 60*60
+ [129600, '1 Day'], // 60*60*24*1.5
+ [604800, 'Days', 86400], // 60*60*24*7, 60*60*24
+ [907200, '1 Week'], // 60*60*24*7*1.5
+ [2628000, 'Weeks', 604800], // 60*60*24*(365/12), 60*60*24*7
+ [3942000, '1 Month'], // 60*60*24*(365/12)*1.5
+ [31536000, 'Months', 2628000], // 60*60*24*365, 60*60*24*(365/12)
+ [47304000, '1 Year'], // 60*60*24*365*1.5
+ [3153600000, 'Years', 31536000], // 60*60*24*365*100, 60*60*24*365
+ [4730400000, '1 Century'], // 60*60*24*365*100*1.5
+ ];
+ date = new Date(''+date);
+ var dt = new Date(),
+ seconds = ((dt - date) / 1000),
+ token = ' Ago',
+ i = 0,
+ format;
+
+ if (seconds < 0) {
+ seconds = Math.abs(seconds);
+ token = '';
+ }
+
+ while (format = time_formats[i++]) {
+ if (seconds < format[0]) {
+ if (format.length == 2) {
+ return format[1] + (i > 1 ? token : ''); // Conditional so we don't return Just Now Ago
+ } else {
+ return Math.round(seconds / format[2]) + ' ' + format[1] + (i > 1 ? token : '');
+ }
+ }
+ }
+
+ // overflow for centuries
+ if(seconds > 4730400000)
+ return Math.round(seconds / 4730400000) + ' Centuries' + token;
+
+ return date+'';
+};
View
53 io.js
@@ -87,33 +87,38 @@ activateProtocol = function(io, client, fn){
//syncing
client.on(client.channel, function(msg){
- msg.user = client.user._id;
- redisclient.publish(client.channel, JSON.stringify(msg));
-
+ msg.user = client.user._id;
+
if(msg.type === 'function' && msg.name){
- levels.load(level._id, function(err, level){
- var grid = new Grid({
- width: level.width,
- height: level.height,
- name: level.name,
- data: level.data
- });
- var args = [];
- for(var i in msg.args){
- args[i] = msg.args[i];
- }
- grid[msg.name].apply(grid, args);
- level.lastUpdate= Date.now();
- level.name = grid.name;
- level.width = grid.width;
- level.height = grid.height;
- level.data = JSON.stringify(grid.data);
- level.save(function(err){
- if(err){
- console.log(err);
+ if(msg.context === 'grid'){
+ levels.load(level._id, function(err, level){
+ var grid = new Grid({
+ width: level.width,
+ height: level.height,
+ name: level.name,
+ data: level.data
+ });
+ var args = [];
+ for(var i in msg.args){
+ args[i] = msg.args[i];
}
+ grid[msg.name].apply(grid, args);
+ level.lastUpdate= Date.now();
+ level.name = grid.name;
+ level.width = grid.width;
+ level.height = grid.height;
+ level.data = JSON.stringify(grid.data);
+ level.save(function(err){
+ if(err){
+ console.log(err);
+ }
+ });
});
- });
+ }else if(msg.context === 'chat'){
+ msg.args[0].time = Date.now();
+ msg.args[0].username = client.user.name;
+ }
+ redisclient.publish(client.channel, JSON.stringify(msg));
}
});
fn(level);
View
@@ -1,6 +1,7 @@
var mongoose = require('mongoose'),
crypto = require('crypto'),
- redis = require('redis');
+ redis = require('redis'),
+ users = require('./users');
var Schema = mongoose.Schema,
LevelSchema,
@@ -85,14 +86,18 @@ LevelSchema = new Schema({
'date': {type: Date, 'default': Date.now},
'lastUpdate': {type: Date, 'default': Date.now},
'creator': {type: Schema.ObjectId},
+ 'creatorName': {type: String, 'default': 'anonymous'},
'width': {type: Number, 'default': 16},
'height': {type: Number, 'default': 16},
'data': {type: String, 'default': initData()}
});
-
+LevelSchema.pre('save', function(next) {
+ console.log(this.creator);
+ next();
+});
mongoose.model('Level', LevelSchema);
-Level = mongoose.model('Level');
+module.exports.Level = Level = mongoose.model('Level');
module.exports.load = function(id, fn){
Level.findById( id, function(err, lvl){
@@ -104,14 +109,27 @@ module.exports.load = function(id, fn){
});
};
-module.exports.create = function( fn){
- var lvl = new Level();
- lvl.save(function(err){
+module.exports.create = function(userId, fn){
+ users.User.findOne({_id: userId}, function(err, user){
if(err){
console.log(err);
+ fn(err, null);
}
- fn(err, lvl);
+ console.log(user.name + ' created a level');
+ var level = new Level();
+ level.creator = user._id;
+ level.creatorName = user.name;
+
+
+ level.save(function(err){
+ if(err){
+ console.log(err);
+ }
+ console.log(user.name + ' created the level: '+ level.name);
+ fn(err, level);
+ });
});
+
};
module.exports.update = function(lvl){
lvl.lastUpdate = Date.now();
Oops, something went wrong.

0 comments on commit 7f9b81b

Please sign in to comment.