Skip to content

Commit

Permalink
average and round scores for total
Browse files Browse the repository at this point in the history
  • Loading branch information
gradus committed Feb 18, 2012
1 parent 83f7f6f commit 366a238
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
11 changes: 9 additions & 2 deletions lib/calculateScore.coffee
Expand Up @@ -6,13 +6,20 @@ db = require('mongoskin').db(process.env.MONGO or 'localhost:27017/scoreboard')
scores = db.collection('scores')
scores.open -> pin.emit 'mongo.collection.scores.opened'

# function to Round numbers
roundNum = (num, dec) ->
Math.round(num*Math.pow(10,dec))/Math.pow(10,dec)


pin.on 'calculateScore', (score) ->
# Do calculations with score
scores.find().toArray (err, result) ->
scoresLength = result.length
@total = 0
for totals in result
for key,val of totals
if key == "score"
@total += parseInt(val)
pin.emit 'setTotal', total: @total
@total += parseInt(val.score)
averageScore = @total / scoresLength
pin.emit 'setTotal', roundNum(averageScore, 2)
pin.emit 'setScore', score
2 changes: 1 addition & 1 deletion lib/setScore.coffee
Expand Up @@ -7,7 +7,7 @@ scores = db.collection('scores')
scores.open -> pin.emit 'mongo.collection.scores.opened'

pin.on 'setScore', (score) ->
scores.save score, (err, result) ->
scores.save score: score, (err, result) ->
if err?
console.log 'Error trying to save score to mongo'
else
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -29,7 +29,7 @@
"nock": "*"
},
"engines": {
"node": "0.6 || 0.7"
"node": "0.6.11 || 0.7.x"
},
"subdomain": "surf-scoreboard"
}
}
2 changes: 1 addition & 1 deletion public/js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions public/src/app.coffee
Expand Up @@ -4,7 +4,7 @@ $ ->
$('#scoring').append(" <li> <div class='alert alert-warning'> <p>Score: #{score.score}</p> </li>")
now.displayTotal = (total) ->
$('#totals').html('')
$('#totals').append(" <li> <div class='alert alert-warning'> <p>Total: #{total.total}</p> </li>")
$('#totals').append(" <li> <div class='alert alert-warning'> <p>Total: #{total}</p> </li>")
hideCurrent = ->
# current page fade
$('.page.current').fadeOut(500).removeClass('current')
Expand All @@ -16,7 +16,7 @@ $ ->
$(page).addClass('current').fadeIn(1000)
setTimeout( show, 1000)

$('.nav li a').click ->
$('.nav li a').click ->
hideCurrent()
showPage($(this).attr('href'))

Expand Down
12 changes: 6 additions & 6 deletions server.coffee
Expand Up @@ -24,23 +24,23 @@ app.post '/', express.bodyParser(), (req, resp) ->
resp.cookie('judge_name', req.body.judge_name, { maxAge: 12000 * 10000 })
resp.cookie('heat_num', req.body.heat_num, { maxAge: 400 * 10000 })
resp.redirect '#scores'
resp.end fs.readFileSync('./public/index.html')
#resp.end fs.readFileSync('./public/index.html')

app.post '/scores', express.bodyParser(), (req, resp) ->
result = validateScore(req.body)
console.log req.body
if result.valid
pin.on 'displayScore', (score) ->
resp.json errors: null, score: score
pin.emit 'calculateScore', req.body
#resp.end
else
resp.json errors: result.errors, records: null
resp.json errors: result.errors, score: null
#resp.end

# Listen
app.listen 3000, -> console.log 'Listening on port 3000'

pin.on 'displayScore', (score) ->
everyone.now.displayScore(score)
pin.on 'setTotal', (total) ->
console.log total
everyone.now.displayTotal(total)
pin.on 'setTotal', (averageScore) ->
everyone.now.displayTotal(averageScore)

0 comments on commit 366a238

Please sign in to comment.