Skip to content

Commit

Permalink
update packages, switch to io.js, add support for conditional http re…
Browse files Browse the repository at this point in the history
…quests
  • Loading branch information
fooey committed Apr 4, 2015
1 parent 3f3b578 commit d26a208
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 249 deletions.
11 changes: 11 additions & 0 deletions .jshintrc
@@ -0,0 +1,11 @@
{
"curly": true,
"eqeqeq": true,
"maxparams": 3,
"noempty": true,
"unused": true,
"esnext": true,
"strict": true,
"node": true,
"jquery": true
}
2 changes: 1 addition & 1 deletion Procfile
@@ -1 +1 @@
web: node --harmony server.js
web: node server.js
4 changes: 2 additions & 2 deletions config/server.js
Expand Up @@ -13,7 +13,7 @@ const pubFolder = path.join(process.cwd(), 'public');
const faviconPath = path.join(pubFolder, 'images/gw2-dragon-32.png');


module.exports = function(app, express) {
module.exports = function(app/*, express*/) {
if (process.env.NODE_ENV === 'development') {
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
app.locals.pretty = true;
Expand All @@ -33,7 +33,7 @@ module.exports = function(app, express) {


// set a cookie
app.use(function(req, res, next) {
app.use((req, res, next) => {
var uaUUID = req.cookies.uaUUID;

if (!uaUUID) {
Expand Down
10 changes: 7 additions & 3 deletions gruntfile.js
Expand Up @@ -25,11 +25,12 @@ module.exports = function(grunt) {
dev: {
script: 'server.js',
options: {
nodeArgs: ['--harmony'],//,'--debug'
"execMap": {
"js": "iojs",
},
watchedExtensions: ['js', 'jade', 'json'],
// delayTime: 1,
env: {
PORT: '3001',
PORT: '3000',
NODE_ENV: 'development'
},
callback: function(nodemon) {
Expand Down Expand Up @@ -80,6 +81,9 @@ module.exports = function(grunt) {


browserify: {
options: {
transform: [require('babelify')],
},
app: {
src: 'public/js/src/app.js',
dest: 'public/js/dist/app.js',
Expand Down
73 changes: 31 additions & 42 deletions lib/emblem2.js
Expand Up @@ -5,8 +5,6 @@

"use strict";

const util = require('util');

const async = require('async');
const _ = require('lodash');

Expand Down Expand Up @@ -76,7 +74,7 @@ function draw(emblemData, size, bgColor, onDrawComplete) {

optimize: ['merge', optimizeSVG],
},
function(callback, data) {
(callback, data) => {
onDrawComplete(data.optimize);
});
}
Expand All @@ -100,7 +98,7 @@ function drawBackground(emblemData, size, callback) {
const opacity = bg.t ? INSTANCE.bg_op : 1;
const transformMatrix = getTransformMatrix('bg', emblemData.flags, size);

drawShapes(bg.p, bgColor, opacity, transformMatrix, function(err, shapes) {
drawShapes(bg.p, bgColor, opacity, transformMatrix, (err, shapes) => {
callback(null, shapes.join('\n'));
});
}
Expand All @@ -120,32 +118,32 @@ function drawForeground(emblemData, size, callback) {

var fgPaths = [];
async.series([
function(next) {
drawShapes(fg.p1, color1, 1, transformMatrix, function(err, shapes) {
(next) => {
drawShapes(fg.p1, color1, 1, transformMatrix, (err, shapes) => {
fgPaths = fgPaths.concat(shapes);
next();
});
},
function(next) {
drawShapes(fg.p2, color, 1, transformMatrix, function(err, shapes) {
(next) => {
drawShapes(fg.p2, color, 1, transformMatrix, (err, shapes) => {
fgPaths = fgPaths.concat(shapes);
next();
});
},
function(next) {
drawShapes(fg.pto2, INSTANCE.pto2_color, INSTANCE.pto2_op, transformMatrix, function(err, shapes) {
(next) => {
drawShapes(fg.pto2, INSTANCE.pto2_color, INSTANCE.pto2_op, transformMatrix, (err, shapes) => {
fgPaths = fgPaths.concat(shapes);
next();
});
},
function(next) {
drawShapes(fg.pt1, color1, INSTANCE.pt1_op, transformMatrix, function(err, shapes) {
(next) => {
drawShapes(fg.pt1, color1, INSTANCE.pt1_op, transformMatrix, (err, shapes) => {
fgPaths = fgPaths.concat(shapes);
next();
});
},
],
function(err) {
(err) => {
callback(null, fgPaths.join('\n'));
});
}
Expand All @@ -162,20 +160,18 @@ function drawShapes(shapes, fill, opacity, matrixArray, callback) {

var pathAttribs = [
'stroke="none"',
util.format('fill="%s"', fill),
`fill="${fill}"`,
];
if (opacity !== 1) {
pathAttribs.push(util.format('opacity="%d"', opacity));
pathAttribs.push(`opacity="${opacity}"`);
}
if (matrix !== INSTANCE.defaultMatrix.join(',')) {
pathAttribs.push(util.format('transform="matrix(%s)"', matrix));
pathAttribs.push(`transform="matrix(${matrix})"`);
}

async.concat(
shapes,
function(shapePath, nextPath) {
nextPath(null, util.format('<path %s d="%s"></path>', pathAttribs.join(' '), shapePath));
},
(shapePath, next) => next(null, `<path ${pathAttribs.join(' ')} d="${shapePath}" />`),
callback
);
}
Expand All @@ -189,7 +185,7 @@ function drawShapes(shapes, fill, opacity, matrixArray, callback) {
function mergeSVG(size, bgColor, callback, autoData) {
callback(null, [
getSvgStyle(size),
'<desc>Created by http://guilds.gw2w2w.com</desc>',
'<desc>Created by http://guilds.gw2w2w.com/</desc>',
getSvgRect(size, bgColor),
autoData.drawBG,
autoData.drawFG,
Expand All @@ -200,9 +196,10 @@ function mergeSVG(size, bgColor, callback, autoData) {


function optimizeSVG(callback, autoData) {
svgo.optimize(autoData.merge, function(result) {
callback(null, result.data);
});
svgo.optimize(
autoData.merge,
(result) => callback(null, result.data)
);
}


Expand All @@ -213,31 +210,22 @@ function optimizeSVG(callback, autoData) {
*/

function getSvgStyle(size) {
return util.format(
'<svg width="100%" height="100%" viewBox="0 0 %d %d" version="1.1" xmlns="http://www.w3.org/2000/svg">',
size,
size
);
return `<svg width="100%" height="100%" viewBox="0 0 ${size} ${size}" version="1.1" xmlns="http://www.w3.org/2000/svg">`;
}

function getSvgRect(size, bgColor) {
return util.format(
'<rect x="0" y="0" width="%d" height="%d" fill="%s" stroke="none"></rect>',
size,
size,
bgColor
);
return `<rect x="0" y="0" width="${size}" height="${size}" fill="${bgColor}" stroke="none" />`;
}



function getTransformMatrix(layer, flags, size) {
const flips = getFlips(flags);

const flipBackgroundHorizontal = (layer === 'bg' && flips['FlipBackgroundHorizontal']);
const flipForegroundHorizontal = (layer === 'fg' && flips['FlipForegroundHorizontal']);
const flipBackgroundVertical = (layer === 'bg' && flips['FlipBackgroundVertical']);
const flipForegroundVertical = (layer === 'fg' && flips['FlipForegroundVertical']);
const flipBackgroundHorizontal = (layer === 'bg' && flips.FlipBackgroundHorizontal);
const flipForegroundHorizontal = (layer === 'fg' && flips.FlipForegroundHorizontal);
const flipBackgroundVertical = (layer === 'bg' && flips.FlipBackgroundVertical);
const flipForegroundVertical = (layer === 'fg' && flips.FlipForegroundVertical);
const flipLayerHorizontal = (flipBackgroundHorizontal || flipForegroundHorizontal);
const flipLayerVertical = (flipBackgroundVertical || flipForegroundVertical);

Expand Down Expand Up @@ -272,9 +260,10 @@ function getFlips(flags) {
'FlipForegroundVertical': 0,
};

_.forEach(flags, function(flag) {
flips[flag] = 1;
});
_.forEach(
flags,
(flag) => flips[flag] = 1
);

return flips;
}
Expand All @@ -287,7 +276,7 @@ function getColor(colorId) {

function componentToHex(c) {
const hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
return hex.length === 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
Expand Down
47 changes: 22 additions & 25 deletions lib/guilds.js
@@ -1,10 +1,9 @@
"use strict";

var _ = require('lodash');
const _ = require('lodash');

var gw2api = require('gw2api');

var slugifier = require('./slugifier.js');
const gw2api = require('gw2api');
const slugifier = require('./slugifier.js');



Expand All @@ -17,18 +16,15 @@ var slugifier = require('./slugifier.js');
*/

module.exports = {
getById: getById,
getByName: getByName,
getBySlug: getBySlug,
getById,
getByName,
getBySlug,
slugify: slugifier.slugify,
deslugify: slugifier.deslugify,
};


var guildMap = {};

var cacheTime = (1000 * 60 * 60 * 4); // 4 hours
var pruningThrottle = (1000 * 60); // 1 minute
const cacheTime = (1000 * 60 * 60 * 4); // 4 hours
const pruningThrottle = (1000 * 60); // 1 minute



Expand All @@ -41,8 +37,8 @@ var pruningThrottle = (1000 * 60); // 1 minute


function getById(guildId, fnCallback) {
var now = Date.now();
var guild = GLOBAL.guilds[guildId];
const now = Date.now();
const guild = GLOBAL.guilds[guildId];

if (guild && guild.guild_id && guild.expires > now) {
// console.log('getById::cache hit', slug);
Expand All @@ -57,22 +53,21 @@ function getById(guildId, fnCallback) {
}


setTimeout(pruneExpiredThrottled, 10);
setImmediate(pruneExpiredThrottled);
}



function getBySlug(slug, fnCallback) {
var now = Date.now();
var guild = _.find(GLOBAL.guilds, {slug: slug});
const guild = _.find(GLOBAL.guilds, {slug: slug});
// console.log('getBySlug', slug);

if (guild && guild.guild_id && !isStale(guild.expires)) {
// console.log('getBySlug::cache hit', slug);
fnCallback(null, guild);
}
else {
var guildName = slugifier.deslugify(slug);
const guildName = slugifier.deslugify(slug);
// console.log('getBySlug::cache miss', guildName);
gw2api.getGuildDetails(
{guild_name: guildName},
Expand All @@ -81,7 +76,7 @@ function getBySlug(slug, fnCallback) {
}


setTimeout(pruneExpiredThrottled, 10);
setImmediate(pruneExpiredThrottled);
}


Expand All @@ -95,18 +90,22 @@ function getByName(guildName, fnCallback) {

function setGuild(fnCallback, err, guildData) {
// console.log('setGuild()', guildData);
const now = Date.now();

if (err || !guildData || !guildData.guild_id) {
err = 'NOTFOUND';
guildData = {};
guildData.expires = Date.now() + (5 * 1000); // only cache failed lookups for 5 seconds
guildData.lastmod = now;
guildData.expires = now + (5 * 1000); // only cache failed lookups for 5 seconds
}
else {
// console.log('new gulid', guildData.guild_name);
guildData.slug = slugifier.slugify(guildData.guild_name);
guildData.expires = Date.now() + cacheTime;
guildData.expires = now + cacheTime;
}

guildData.lastmod = now;


GLOBAL.guilds[guildData.guild_id] = guildData;

Expand All @@ -116,16 +115,14 @@ function setGuild(fnCallback, err, guildData) {


function pruneExpired() {
var now = Date.now();

_.each(GLOBAL.guilds, function(guild, key) {
_.each(GLOBAL.guilds, (guild, key) => {
if (isStale(guild.expires)) {
delete GLOBAL.guilds[key];
}
});
}

var pruneExpiredThrottled = _.debounce(pruneExpired, pruningThrottle);
const pruneExpiredThrottled = _.debounce(pruneExpired, pruningThrottle);


function isStale(expires) {
Expand Down
21 changes: 8 additions & 13 deletions lib/slugifier.js
@@ -1,3 +1,4 @@
'use strict';

/*
*
Expand All @@ -6,16 +7,10 @@
*/

module.exports = {
slugify: slugify,
deslugify: deslugify,
};



function slugify(str) {
return encodeURIComponent(str.replace(/ /g, '-')).toLowerCase();
}

function deslugify(str) {
return decodeURIComponent(str).replace(/-/g, ' ');
}
slugify(str) {
return encodeURIComponent(str.replace(/ /g, '-')).toLowerCase();
},
deslugify(str) {
return decodeURIComponent(str).replace(/-/g, ' ');
},
};

0 comments on commit d26a208

Please sign in to comment.