Permalink
Browse files

Added challenge loading, match loading for getFullUser

  • Loading branch information...
toantran committed Mar 12, 2012
1 parent 2e85d98 commit 59204a829b02f4dd7b2e7b3397720330b87751fa
@@ -0,0 +1,27 @@
+jQuery ($) ->
+
+ onbuttonclick = ->
+ btnVal = $(@).attr 'value'
+ teamid = $(@).attr 'data-teamid'
+ otherteamid = $(@).attr 'data-otherteamid'
+ btn = $(@)
+
+ switch btnVal
+ when 'accept'
+ url = '/team/challengeaccept'
+ data = challengingteamid: otherteamid, challengedteamid: teamid
+ when 'decline'
+ url = '/team/challengedecline'
+ data = challengingteamid: otherteamid, challengedteamid: teamid
+ when 'cancel'
+ url = '/team/challengecancel'
+ data = challengingteamid: teamid, challengedteamid: otherteamid
+
+ $.post(url, data)
+ .success (resp) =>
+ if resp?.success
+ $(btn).closest('alert').alert 'close'
+
+ $('button').on 'click', onbuttonclick
+
+
@@ -119,7 +119,8 @@ exports.createTeamChallenge = (params, callback = ->) ->
cb()
.then (args..., cb = ->) =>
# create challenge for challenged team
- challenge =
+ challenge =
+ type: 'challenged'
message: params.msg
matchtype: params.matchtype
teamid: opponentid
@@ -167,6 +168,7 @@ exports.createTeamChallenge = (params, callback = ->) ->
.then (args..., cb = ->) ->
# create challenge for challenging team
challenge =
+ type: 'challenging'
message: msg
matchtype: matchtype
teamid: teamid
@@ -237,10 +239,11 @@ exports.acceptChallenge = (inputs, callback = ->) ->
status: 'pending'
teams: teams
matchSvc.createMatch am, cb
- .then (err, @am, cb = ->) =>
+ .then (err, ams, cb = ->) =>
+ @am = ams?.[0]
return callback(err) if err
# add match to teams
- utils.mapAsync @teams, (team, cb) ->
+ utils.mapAsync @teams, (team, cb) =>
addMatch( team._id, @am, cb )
, cb
.then (err, args..., cb = ->) ->
@@ -249,11 +252,11 @@ exports.acceptChallenge = (inputs, callback = ->) ->
newTeamRepo.removeChallenge inputs.challengedteamid, inputs.challengingteamid
newTeamRepo.removeChallenge inputs.challengingteamid, inputs.challengedteamid
cb()
- .then (err, args..., cb = ->) ->
+ .then (err, args..., cb = ->) =>
return callback(err) if err
for team in @teams
- do (team) ->
- utils.mapAsync team.members, (memberid, mapcb = ->) ->
+ do (team) =>
+ utils.mapAsync team.members, (memberid, mapcb = ->) =>
post =
type: 'newmatch'
data:
@@ -652,7 +652,6 @@ exports.getFullUser = (userid, callback = ->) ->
cb null, null
.then (err, fullposts, cb = ->) =>
-
if fullposts?
posts = (post for post in fullposts when post?.desc?)
else
@@ -677,6 +676,56 @@ exports.getFullUser = (userid, callback = ->) ->
@user?.invites = invites
+ # Load challenges
+ if @team?.challenges?.length
+
+ loadChallenge = (challenge, loadChallengeCallback = ->) ->
+ teamRepo.getById challenge?.teamid, (loadChallengeErr, team) ->
+ challenge.teamname = team?.teamname
+ loadChallengeCallback loadChallengeErr, challenge
+
+ utils.mapAsync @team?.challenges, loadChallenge, cb
+ else
+ cb null, null
+ .then ( err, challenges, cb = ->) =>
+ @team.challenges = challenges
+ @user.challenges = challenges
+
+ # Load pending matches
+ matches = (match for match in @team?.matches when match?.status is 'pending')
+
+ console.log 'pending matches', matches, @team?.matches
+ if matches?.length
+
+ matchsvc = require './match'
+ loadMatch = (am, loadMatchCb = ->) =>
+ matchsvc.getById am._id, (loadMatchErr, fullMatch) =>
+ fullMatch?.hometeam = @team
+ for team in fullMatch?.teams
+ do (team) =>
+ if not team?._id?.equals( @team?._id )
+ fullMatch.opponentteam = team
+ for vote in fullMatch?.votes
+ do (vote) =>
+ if String(vote?.playerid) is String(@user._id)
+ fullMatch.voted = true
+
+ loadVote = (vote, votecb = ->) ->
+ newUserRepo.getById vote.playerid, (getByIdErr, user) ->
+ vote.playername = user?.nickname
+ votecb null, vote
+
+ utils.mapAsync fullMatch?.votes, loadVote, (loadVoteErr, fullVotes) ->
+ fullMatch?.votes = fullVotes
+
+ loadMatchCb loadMatchErr, fullMatch
+
+ utils.mapAsync matches, loadMatch, cb
+ else
+ callback null, @user
+ .then ( err, matches, cb = ->) =>
+ console.log 'Matches ', err, matches
+ @user.matches = matches
callback null, @user
@@ -3,14 +3,23 @@ _mapAsync = (array = [], asyncMapFn, callback = ->) ->
new_array = []
errors = []
runit = (item, index) ->
- asyncMapFn item, (err, result) ->
- if err
- errors.push err
+ try
+ asyncMapFn item, (err, result) ->
+ if err
+ errors.push err
- new_array[index] = result
+ new_array[index] = result
+ counter--
+ if counter is 0
+ callback errors.join(','), new_array
+ catch e
+ console.trace e
+ errors.push e
+ new_array[index] = null
counter--
if counter is 0
callback errors.join(','), new_array
+
runit item, index for item, index in array
View
@@ -0,0 +1,43 @@
+(function() {
+
+ jQuery(function($) {
+ var onbuttonclick;
+ onbuttonclick = function() {
+ var btn, btnVal, data, otherteamid, teamid, url,
+ _this = this;
+ btnVal = $(this).attr('value');
+ teamid = $(this).attr('data-teamid');
+ otherteamid = $(this).attr('data-otherteamid');
+ btn = $(this);
+ switch (btnVal) {
+ case 'accept':
+ url = '/team/challengeaccept';
+ data = {
+ challengingteamid: otherteamid,
+ challengedteamid: teamid
+ };
+ break;
+ case 'decline':
+ url = '/team/challengedecline';
+ data = {
+ challengingteamid: otherteamid,
+ challengedteamid: teamid
+ };
+ break;
+ case 'cancel':
+ url = '/team/challengecancel';
+ data = {
+ challengingteamid: teamid,
+ challengedteamid: otherteamid
+ };
+ }
+ return $.post(url, data).success(function(resp) {
+ if (resp != null ? resp.success : void 0) {
+ return $(btn).closest('alert').alert('close');
+ }
+ });
+ };
+ return $('button').on('click', onbuttonclick);
+ });
+
+}).call(this);
View

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

Oops, something went wrong.
View

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

Oops, something went wrong.
View

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

Oops, something went wrong.
@@ -5,9 +5,9 @@ a( href="/profile/#{playerid}")= playername
- if (typeof matchtype != 'undefined')
- if (matchtype == '1')
| in one game
- - else if (matchtype = '99')
+ - else if (matchtype == '99')
| in a death match
- else
| in a best of #{matchtype} games
- if (typeof matchmsg != 'undefined')
- | with a message #{matchmsg}
+ | with a message #{matchmsg}us
@@ -3,11 +3,11 @@ a( href="/profile/#{playerid}")= playername
| challenged team
a( href="/team/#{teamid}")= teamname
- if (typeof matchtype != 'undefined')
- - if (matchtype == '1')
+ - if (matchtype === '1')
| in one game
- - else if (matchtype = '99')
+ - else if (matchtype === '99')
| in a death match
- else
| in a best of #{matchtype} games
-- if (typeof matchmsg != 'undefined')
+- if (typeof matchmsg !== 'undefined')
| with a message #{matchmsg}
Oops, something went wrong.

0 comments on commit 59204a8

Please sign in to comment.