forked from greylockhackfest/07.21.2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (52 loc) · 1.69 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var express = require('express')
, app = express.createServer()
, io = require('socket.io').listen(app)
, colors = require('colors')
, markdown = require('markdown').markdown
, fs = require('fs')
, PORT = process.env.PORT || 8000
;
var graph = {};
var fileContents = fs.readFileSync("graphData.json",'utf8');
graph = JSON.parse(fileContents);
var Game = require('./game').Game;
var game = new Game();
game.graph = graph;
// Configure redis.
if (process.env.REDISTOGO_URL) {
var rtg = require("url").parse(process.env.REDISTOGO_URL);
var redis = require("redis").createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(":")[1]);
} else {
var redis = require("redis").createClient();
}
game.redis = redis;
// match app routes before serving static file of that name
app.use(app.router);
app.use(express.static(__dirname + '/public'));
io.sockets.on('connection', function (socket) {
socket.on('login', function(opts) {
game.addClient(socket, opts);
game.getArticles(socket, function(data) {
socket.emit('articles', data);
});
socket.emit('articleTarget', game.articleTarget.name);
socket.emit('currentArticle', game.world.players[socket.playerId].article.name)
});
socket.on('logout', function() {
game.removeClient(socket);
});
socket.on('disconnect', function() {
game.removeClient(socket);
});
socket.on('setArticle', function(articleId) {
game.setArticle(socket.playerId, articleId, function() {
game.getArticles(socket, function(data) {
socket.emit('articles', data);
});
});
});
});
console.log('Your highness, at your service:'.yellow
+ ' http://localhost:%d'.magenta, PORT);
app.listen(PORT);