Permalink
Browse files

Added match controller

  • Loading branch information...
1 parent 2aa1576 commit d13bbb19ae5a6367c0e4f5f3c33def1ccd50387e @toantran committed Mar 14, 2012
Showing with 169 additions and 197 deletions.
  1. +49 −0 coffee/controllers/match.coffee
  2. +28 −4 coffee/services/match.coffee
  3. +56 −184 controllers/match.js
  4. +36 −9 services/match.js
@@ -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
+
+
+
+
+
+
@@ -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
@@ -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 = -> ) ->
@@ -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) ->
@@ -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!'
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit d13bbb1

Please sign in to comment.