-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
91 lines (76 loc) · 2.88 KB
/
app.coffee
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
express = require 'express'
socketio = require 'socket.io'
fs = require 'fs'
app = express.createServer()
io = socketio.listen(app)
io.configure () =>
io.set "transports", ["xhr-polling"]
io.set "polling duration", 10
app.configure =>
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.set 'view options', {layout:false}
app.set 'jsonp callback', true
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use express.static(__dirname + '/public')
app.configure 'development', () =>
app.use express.errorHandler({ dumpExceptions: true, showStack: true })
app.configure 'production', () =>
app.use express.errorHandler()
global.tv_code = 2848
get_random_code = () -> Math.floor(Math.random() * 10000)
endsWith = (str, suffix) -> str.indexOf(suffix, str.length - suffix.length) != -1
shuffle = (o) ->
j = x = i = o.length
while i
j = parseInt(Math.random() * i)
x = o[--i]
o[i] = o[j]
o[j] = x
return o
bySortedValue = (obj) ->
tuples = ([key, obj[key]] for key of obj)
tuples.sort((a, b) -> if a[1] < b[1] then 1 else if a[1] > b[1] then -1 else 0)
init_quiz = () ->
fs.readdir('public/images/quiz/', (err, files) ->
global.quiz_bucket = shuffle(files.filter((filename) -> endsWith(filename, '.jpg')).map((filename) -> filename.split('.')[0]))
global.quiz_info = {stage:1, quiz:quiz_bucket.pop()}
global.devices = {}
)
#Socket.io
io.sockets.on 'connection', (socket) =>
socket.on 'mobile connect', (data) =>
if parseInt(data.code) != tv_code
socket.emit 'connect ack', {success:false, msg:'TV Code is invalid, check again'}
else if data.nickname of devices
socket.emit 'connect ack', {success:false, msg:'Nickname is already taken!'}
else
devices[data.nickname] = 0
socket.emit 'connect ack', {success:true}
socket.broadcast.emit 'update ranking', bySortedValue(devices)[0..2]
#RestAPI
app.get '/quiz/submit', (req, res) =>
io.sockets.emit 'update trial', {nickname:req.query['nickname'], trial:req.query['answer']}
if req.query['answer'] == quiz_info.quiz.toUpperCase()
global.quiz_info = {stage:quiz_info.stage + 1, quiz:quiz_bucket.pop()}
devices[req.query['nickname']]++
io.sockets.emit 'quiz next', {nickname:req.query['nickname']}
io.sockets.emit 'update ranking', bySortedValue(devices)[0..2]
res.json {success:true}
else
res.json {success:false}
app.get '/quiz', (req, res) =>
res.json quiz_info
#Routes
app.get '/', (req, res) =>
init_quiz()
global.tv_code = get_random_code()
res.render('game', {
code: tv_code
})
console.log tv_code
init_quiz()
app.listen process.env.PORT || 5000, () =>
console.log "Express server listening on port %d in %s mode", app.address().port, app.settings.env