-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaimio_sp_app.js
133 lines (103 loc) · 3.79 KB
/
daimio_sp_app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var app = require('http').createServer(handler)
, fs = require('fs')
, io = require('socket.io').listen(app, { log: false })
, D = require('daimio')
, mongo = require('mongodb')
, db = new mongo.Db('screenperfect', new mongo.Server('localhost', 27017, {auto_reconnect: true}), {w: 0});
D.Etc.db = db // expose DB connection to Daimio
function handler (req, res) {
// NOTE: we're grabbing these fresh in response to each request for development. DO NOT DO THIS IN PRODUCTION.
// move these lines outside the handler so the html is cached over the lifetime of the server.
var menu_html = fs.readFileSync(__dirname+'/daimio_sp_menu.html', 'utf8')
, client_html = fs.readFileSync(__dirname+'/daimio_sp_client_control.html', 'utf8')
, admin_html = fs.readFileSync(__dirname+'/daimio_sp_admin.html', 'utf8')
, viz_html = fs.readFileSync(__dirname+'/daimio_sp_viz.html', 'utf8')
, daimio_js = fs.readFileSync(__dirname+'/daimio_composite.js', 'utf8')
if(req.url.match(/^\/public\//)) {
res.writeHead(200)
res.end(fs.readFileSync('.' + req.url, 'utf8')) // TODO: async this
return
}
if(req.url === '/favicon.ico') {
res.writeHead(200, {'Content-Type': 'image/x-icon'})
res.end()
return
}
if(req.url === '/daimio_composite.js') {
res.writeHead(200, {"Content-Type": "application/javascript"})
res.end(daimio_js)
return
}
if(req.url.replace(/\/$/, '').split('/').slice(-1)[0] == 'admin') {
res.writeHead(200, {"Content-Type": "text/html"})
res.end(admin_html)
return
}
if(req.url.replace(/\/$/, '').split('/').slice(-1)[0] == 'viz') {
res.writeHead(200, {"Content-Type": "text/html"})
res.end(viz_html)
return
}
if(req.url.match(/(client|control)\/?$/)) {
res.writeHead(200, {"Content-Type": "text/html"})
res.end(client_html)
return
}
res.writeHead(200, {"Content-Type": "text/html"})
res.end(menu_html)
}
io.on('connection', function (socket) {
socket.on('get-games', function (data) {
var query = {}
try {
D.Etc.db.collection('games', function(err, c) {
c.find(query).toArray(function(err, games) {
socket.emit('games-data', games)
})
})
} catch (e) {return false}
})
socket.on('request-data', function (data) {
var game_id = data.game
, session = data.session || 1 // TODO: randomize
, query = {}
if(!game_id || game_id.length != 24)
return false
try {
query = {_id: new mongo.ObjectID(game_id)}
console.log('joining: ', game_id, query, data)
socket.join(game_id)
D.Etc.db.collection('games', function(err, c) {
c.find(query).limit(1).toArray(function(err, games) {
io.sockets.in(game_id).emit('game-data', games[0])
})
})
} catch (e) {return false}
})
// TODO: track active room and bounce it to new clients on connection
// TODO: allow local video paths to punch through, then change the mongo urls
// YAGNI: multiple sessions
socket.on('save-game', function (game) {
try {
db.collection('games', function(err, c) {
if(game._id)
game._id = mongo.ObjectID(game._id)
c.save(game) // sync-style is ok here, because we're not waiting for confirmation
db.collection('history', function(err, c) {
c.insert({cron: new Date(), game: game})
})
console.log('saved: ', game)
console.log('save bounce: ', game._id)
io.sockets.in(game._id).emit('game-data', game)
})
} catch (e) {return false}
})
socket.on('bounce', function (data) {
io.sockets.in(data.game).emit('bounced', data.room)
})
})
db.open(function(err, db) {
if(err)
return console.log('DB refused to open: ', err)
app.listen(8888)
})