Permalink
Browse files

finished profile page

  • Loading branch information...
1 parent 59204a8 commit 2aa1576663d9ef6876fd615245f6a4b256235adb @toantran committed Mar 13, 2012
@@ -6,15 +6,20 @@ exports.join = (req, res, next) ->
userid = req.param 'playerid', ''
if teamid and userid
- teamSvc.createJoinRequest teamid, userid, (err) ->
- res.send
- success: not err?
+ try
+ teamSvc.createJoinRequest teamid, userid, (err) ->
+ res.send
+ success: not err?
+ catch e
+ console.trace e
+ next e
else
- next()
+ next 'ids cannot be empty or 0'
exports.join.authenticated = true
exports.join.methods = ['POST']
exports.join.action = ':id/join'
+
###
POST
URL /team/challenge
@@ -31,15 +36,19 @@ exports.challenge = (req, res, next) ->
success: false
error: 'Ids empty'
else
- teamSvc.createTeamChallenge {teamid, opponentplayerid, matchtype, msg }, (error, result) =>
- if error
- res.send
- success: false
- error: error
- else
- res.send
- success: true
- result: result
+ try
+ teamSvc.createTeamChallenge {teamid, opponentplayerid, matchtype, msg }, (error, result) =>
+ if error
+ res.send
+ success: false
+ error: error
+ else
+ res.send
+ success: true
+ result: result
+ catch e
+ console.trace e
+ next e
exports.challenge.authenticated = true
exports.challenge.methods = ['POST']
@@ -18,9 +18,9 @@ jQuery ($) ->
data = challengingteamid: teamid, challengedteamid: otherteamid
$.post(url, data)
- .success (resp) =>
+ .success (resp) =>
if resp?.success
- $(btn).closest('alert').alert 'close'
+ $(btn).closest('.alert').alert 'close'
$('button').on 'click', onbuttonclick
@@ -0,0 +1,19 @@
+jQuery ($) ->
+
+ onresultbtnclick = ->
+ matchid = $(@).attr 'data-matchid'
+ teamid = $(@).attr 'data-teamid'
+ playerid = $(@).attr 'data-playerid'
+ vote = $(@).attr 'data-vote'
+
+ $.ajax
+ url: '/match/' + matchid
+ type: 'PUT'
+ data:
+ teamid: teamid
+ playerid: playerid
+ result: vote
+ success: (data) ->
+ window.location.href = window.location.href
+
+ $('.todo-match button').on 'click', onresultbtnclick
@@ -16,3 +16,40 @@ exports.remove = () ->
exports.getById = () ->
baseRepo.getById.apply baseRepo, arguments
exports.ObjectId = ObjectId
+
+
+exports.addVote = (matchid, vote, callback = ->) ->
+ console.assert matchid, 'matchid cannot be null or 0'
+ throw 'matchid cannot be null or 0' unless matchid
+
+ matchid = new ObjectId(matchid) if typeof matchid is 'string'
+
+ findObj =
+
+ baseRepo.update
+
+ getCollection(db, function(error, collection) {
+ checkErrorFn( error, errorFn, function() {
+ collection.findAndModify( {
+ _id: new ObjectId(matchid)
+ }, {
+ }, {
+ $addToSet: {
+ votes: vote
+ }
+ , $set: {
+ updatedat: new Date()
+ }
+ }, {
+ safe: true
+ , 'new': true
+ }, function(error, m) {
+ if (m && (m.length === +m.length)) {
+ m = m[0];
+ }
+ callback( error, m);
+ db.close();
+ });
+ });
+ });
+}
@@ -127,6 +127,31 @@ exports.cancel = (am, callback = ->) ->
utils.seriesAsync [makeSetMatchStatus('cancelled'), makeRemoveTeamsMatch(am.teams), makeCreatePlayersPost(am.teams)], am, (err, result) ->
console.timeEnd 'Match cancelling'
callback.apply @, arguments
+
+
+exports.addVote = (matchid, playerid, teamid, result, callback = ->) ->
+ vote = {playerid, teamid, count: if result is 'win' then 1 else -1}
+
+
+
+exports.countVotes = (am, callback = ->) ->
+ callback 0 unless am?.votes?
+
+ results = {}
+ # initialize the result object
+ results[String(am.teams[0]._id)] =
+ count: 0
+ win: false
+ opponentid: String(am.teams[1]._id)
+ results[String(am.teams[1]._id)] =
+ count: 0
+ win: false
+ opponentid: String(am.teams[0]._id)
+
+ for {teamid, count} in am.votes
+ results[teamid].count += count
+
+ Math.abs (results[String(am.teams[1]._id)].count - results[String(am.teams[0]._id)].count)
exports.finalize = (am, callback = -> ) ->
@@ -148,7 +173,7 @@ exports.finalize = (am, callback = -> ) ->
win: false
opponentid: String(am.teams[0]._id)
- for {teamid, count} in am.votes
+ for {teamid, count} in am?.votes
results[teamid].count += count
maxCount = Math.max results[String(am.teams[1]._id)].count, results[String(am.teams[0]._id)].count
@@ -147,8 +147,22 @@ exports.makePostGen = (user) ->
catch e
console.trace e
returnback e
-
- when 'teamchallenging', 'challengedeclined', 'challengedeclining', 'challengecancelled', 'challengeremoved'
+ when 'challengecancelled'
+ teamsvc.getById post?.data?.teamid, (err, team) ->
+ return returnback(err) if err
+
+ setPictureUrl team?.pictureurl
+ data.teamid = team?._id
+ data.teamname = team?.teamname
+
+ try
+ genDesc()
+ returnback null
+ catch e
+ console.trace e
+ returnback e
+
+ when 'teamchallenging', 'challengedeclined', 'challengedeclining', 'challengeremoved', 'challengecancelling'
setPictureUrl user?.pictureurl
data.matchtype = post?.data?.matchtype
Oops, something went wrong.

0 comments on commit 2aa1576

Please sign in to comment.