-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 1.5 KB
/
index.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
var async = require('async')
var request = require('browser-request')
var hexaworld = require('hexaworld-app/app.js')
// TODO add these using a template
var logsUrl = 'http://beta.hexa.world:3000'
var emitPeriod = 500 // ms
function startGame () {
request.post({ url: '/games', json: true }, function (rsp, res, body) {
var names = body.names
var levels = body.levels
var id = body.id
console.log('creating game ' + id + ' out of ' + JSON.stringify(names))
var game = hexaworld('container', levels)
console.log('container: ' + document.getElementById('container'))
setupLogging(id, game)
})
}
function listGames (cb) {
request.get({ url: '/levels', json: true }, function (rsp, res, body) {
if (res.statusCode === 200) {
return cb(null, body)
} else {
return cb(new Error('bad response: ' + rsp))
}
})
}
function setupLogging (id, game) {
var socket = io.connect(logsUrl)
socket.on('message', function (data, flags) {
console.log('received message: ' + data)
})
// register game-related callbacks here
var buffer = []
var sendBuffer = function () {
console.log('in sendBuffer, emitting ' + buffer.length + ' events')
async.each(buffer, function (event, next) {
socket.emit('event', event)
next(null)
})
buffer = []
setTimeout(sendBuffer, emitPeriod)
}
setTimeout(sendBuffer, emitPeriod)
game.events.onAny(function (event) {
event = { id: id, tag: this.event, event: event }
buffer.push(event)
})
}
startGame()