Skip to content

Commit

Permalink
Added match controller
Browse files Browse the repository at this point in the history
  • Loading branch information
toantran committed Mar 14, 2012
1 parent 2aa1576 commit d13bbb1
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 197 deletions.
49 changes: 49 additions & 0 deletions coffee/controllers/match.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

exports.update = (req, res, next) ->
matchid = req.params.id
teamid = req.param 'teamid', ''
playerid = req.param 'playerid', ''
result = req.param 'result', ''
callback = (err) ->
res.send
success: false
error: err

if not matchid or not teamid or not playerid or not result
res.send
success: false
error: 'Ids empty'
else if result isnt 'win' and result isnt 'lose'
res.send
success: false
error: 'Invalid result'
else
matchsvc = require '../services/match'
utils = require '../services/utils'

utils.execute( matchsvc.addVote, matchid, playerid, teamid, result )
.then (err, args..., cb = ->) ->
return callback(err) if err

matchsvc.getById matchid, cb
.then (err, @am, cb = -> ) =>
return callback(err) if err
return callback('match not found') if not @am?

@totalplayers = @am?.teams?.map( (team) -> team?.members?.length ? 0 ).reduce( (prev, curr) ->
prev + curr
, 0 )
@totalvotes = @am?.votes?.length ? 0
@totalDecisions = matchsvc.countDecisions @am
if @totalvotes >= @totalplayers or @totalDecisions > @totalplayers / 2
matchsvc.finalize @am, cb
res.send
success: true
.then (err) =>
console.log err






32 changes: 28 additions & 4 deletions coffee/services/match.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,35 @@ exports.cancel = (am, callback = ->) ->


exports.addVote = (matchid, playerid, teamid, result, callback = ->) ->
console.assert matchid, 'matchid cannot be null or 0'
throw 'matchid cannot be null or 0' unless matchid
console.assert playerid, 'playerid cannot be null or 0'
throw 'playerid cannot be null or 0' unless matchid
console.assert teamid, 'teamid cannot be null or 0'
throw 'teamid cannot be null or 0' unless matchid
console.assert result is 'win' or result is 'lose', 'result must be win or lose'
throw 'result must be win or lose' unless result is 'win' or result is 'lose'

vote = {playerid, teamid, count: if result is 'win' then 1 else -1}
matchid = new matchRepo.ObjectId(matchid) if typeof matchid is 'string'
findObj =
_id: matchid
updateObj =
$addToSet:
votes: vote
$set:
updatedat: new Date()

try
matchRepo.update findObj, updateObj, {}, callback
catch e
console.trace e
throw e



exports.countVotes = (am, callback = ->) ->
callback 0 unless am?.votes?
exports.countDecisions = (am) ->
return 0 unless am?.votes?

results = {}
# initialize the result object
Expand All @@ -152,6 +175,7 @@ exports.countVotes = (am, callback = ->) ->
results[teamid].count += count

Math.abs (results[String(am.teams[1]._id)].count - results[String(am.teams[0]._id)].count)



exports.finalize = (am, callback = -> ) ->
Expand Down Expand Up @@ -205,7 +229,7 @@ exports.finalize = (am, callback = -> ) ->
console.log "Team #{teamid} update stats with error #{err}" if err?
cb.apply @, arguments

makeUpdatePlayerStats = () ->
makeUpdatePlayersStats = () ->
players = []
for team in am.teams
do (team) ->
Expand Down Expand Up @@ -242,7 +266,7 @@ exports.finalize = (am, callback = -> ) ->
console.log "Match #{am._id} Set team #{team._id} match complete with error #{err}" if err?
cb null

utils.seriesAsync [makeSetMatchComplete(), makeUpdateTeamStats(), makeUpdatePlayerStats(), makeSetTeamMatchComplete()], am, () ->
utils.seriesAsync [makeSetMatchComplete(), makeUpdateTeamStats(), makeUpdatePlayersStats(), makeSetTeamMatchComplete()], am, () ->
console.log 'Done!'


Expand Down
240 changes: 56 additions & 184 deletions controllers/match.js

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

Loading

0 comments on commit d13bbb1

Please sign in to comment.