Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added challenge loading, match loading for getFullUser

  • Loading branch information...
commit 59204a829b02f4dd7b2e7b3397720330b87751fa 1 parent 2e85d98
@toantran authored
View
27 coffee/public/js/todochallenge.coffee
@@ -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
+
+
View
15 coffee/services/team.coffee
@@ -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:
View
51 coffee/services/user.coffee
@@ -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
View
17 coffee/services/utils.coffee
@@ -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
43 public/js/todochallenge.js
@@ -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
8 services/team.js
@@ -148,6 +148,7 @@
args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
if (cb == null) cb = function() {};
challenge = {
+ type: 'challenged',
message: params.msg,
matchtype: params.matchtype,
teamid: opponentid
@@ -217,6 +218,7 @@
args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
if (cb == null) cb = function() {};
challenge = {
+ type: 'challenging',
message: msg,
matchtype: matchtype,
teamid: teamid
@@ -320,7 +322,7 @@
if (cb == null) cb = function() {};
if (err) return callback(err);
return utils.mapAsync(_this.teams, function(team, cb) {
- return addMatch(team._id, this.am, cb);
+ return addMatch(team._id, _this.am, cb);
}, cb);
}).then(function() {
var args, cb, err, _i;
@@ -335,7 +337,7 @@
err = arguments[0], args = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), cb = arguments[_i++];
if (cb == null) cb = function() {};
if (err) return callback(err);
- _ref = this.teams;
+ _ref = _this.teams;
_fn = function(team) {
return utils.mapAsync(team.members, function(memberid, mapcb) {
var post;
@@ -343,7 +345,7 @@
post = {
type: 'newmatch',
data: {
- matchid: String(this.am._id)
+ matchid: String(_this.am._id)
},
createdat: new Date()
};
View
81 services/user.js
@@ -871,10 +871,89 @@
return cb(e);
}
}).then(function(err, invites, cb) {
- var _ref;
+ var _ref, _ref2, _ref3, _ref4;
if (cb == null) cb = function() {};
if (err) return callback(err);
if ((_ref = _this.user) != null) _ref.invites = invites;
+ if ((_ref2 = _this.team) != null ? (_ref3 = _ref2.challenges) != null ? _ref3.length : void 0 : void 0) {
+ loadChallenge = function(challenge, loadChallengeCallback) {
+ if (loadChallengeCallback == null) loadChallengeCallback = function() {};
+ return teamRepo.getById(challenge != null ? challenge.teamid : void 0, function(loadChallengeErr, team) {
+ challenge.teamname = team != null ? team.teamname : void 0;
+ return loadChallengeCallback(loadChallengeErr, challenge);
+ });
+ };
+ return utils.mapAsync((_ref4 = _this.team) != null ? _ref4.challenges : void 0, loadChallenge, cb);
+ } else {
+ return cb(null, null);
+ }
+ }).then(function(err, challenges, cb) {
+ var match, matches, matchsvc, _ref;
+ if (cb == null) cb = function() {};
+ _this.team.challenges = challenges;
+ _this.user.challenges = challenges;
+ matches = (function() {
+ var _i, _len, _ref, _ref2, _results;
+ _ref2 = (_ref = this.team) != null ? _ref.matches : void 0;
+ _results = [];
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ match = _ref2[_i];
+ if ((match != null ? match.status : void 0) === 'pending') {
+ _results.push(match);
+ }
+ }
+ return _results;
+ }).call(_this);
+ console.log('pending matches', matches, (_ref = _this.team) != null ? _ref.matches : void 0);
+ if (matches != null ? matches.length : void 0) {
+ matchsvc = require('./match');
+ loadMatch = function(am, loadMatchCb) {
+ if (loadMatchCb == null) loadMatchCb = function() {};
+ return matchsvc.getById(am._id, function(loadMatchErr, fullMatch) {
+ var loadVote, team, vote, _fn, _fn2, _i, _j, _len, _len2, _ref2, _ref3;
+ if (fullMatch != null) fullMatch.hometeam = _this.team;
+ _ref2 = fullMatch != null ? fullMatch.teams : void 0;
+ _fn = function(team) {
+ var _ref3, _ref4;
+ if (!(team != null ? (_ref3 = team._id) != null ? _ref3.equals((_ref4 = _this.team) != null ? _ref4._id : void 0) : void 0 : void 0)) {
+ return fullMatch.opponentteam = team;
+ }
+ };
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ team = _ref2[_i];
+ _fn(team);
+ }
+ _ref3 = fullMatch != null ? fullMatch.votes : void 0;
+ _fn2 = function(vote) {
+ if (String(vote != null ? vote.playerid : void 0) === String(_this.user._id)) {
+ return fullMatch.voted = true;
+ }
+ };
+ for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
+ vote = _ref3[_j];
+ _fn2(vote);
+ }
+ loadVote = function(vote, votecb) {
+ if (votecb == null) votecb = function() {};
+ return newUserRepo.getById(vote.playerid, function(getByIdErr, user) {
+ vote.playername = user != null ? user.nickname : void 0;
+ return votecb(null, vote);
+ });
+ };
+ utils.mapAsync(fullMatch != null ? fullMatch.votes : void 0, loadVote, function(loadVoteErr, fullVotes) {
+ return fullMatch != null ? fullMatch.votes = fullVotes : void 0;
+ });
+ return loadMatchCb(loadMatchErr, fullMatch);
+ });
+ };
+ return utils.mapAsync(matches, loadMatch, cb);
+ } else {
+ return callback(null, _this.user);
+ }
+ }).then(function(err, matches, cb) {
+ if (cb == null) cb = function() {};
+ console.log('Matches ', err, matches);
+ _this.user.matches = matches;
return callback(null, _this.user);
});
};
View
16 services/utils.js
@@ -10,12 +10,20 @@
new_array = [];
errors = [];
runit = function(item, index) {
- return asyncMapFn(item, function(err, result) {
- if (err) errors.push(err);
- new_array[index] = result;
+ try {
+ return asyncMapFn(item, function(err, result) {
+ if (err) errors.push(err);
+ new_array[index] = result;
+ counter--;
+ if (counter === 0) return callback(errors.join(','), new_array);
+ });
+ } catch (e) {
+ console.trace(e);
+ errors.push(e);
+ new_array[index] = null;
counter--;
if (counter === 0) return callback(errors.join(','), new_array);
- });
+ }
};
_results = [];
for (index = 0, _len = array.length; index < _len; index++) {
View
4 views/post/teamchallenged.jade
@@ -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
View
6 views/post/teamchallenging.jade
@@ -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}
View
37 views/profile/challenge.jade
@@ -1,11 +1,11 @@
-- if (challenge.type == 'challenged')
- .alert.alert-error
+- if (typeof challenge.type !== 'undefined' && challenge.type == 'challenged')
+ .alert.alert-block.alert-error
a.close(data-dismiss='alert') x
- h4 You are challenged!
+ h4.alert-heading You are challenged!
p
| Team&nbsp;
- a.label.label-info(href='/team/#{challenge.teamid}')
- | have challenged you for a
+ a.label.label-info(href='/team/#{challenge.teamid}')= challenge.teamname
+ | have challenged you in a
- if (challenge.matchtype == '1')
| match
- else if (challenge.matchtype == '99')
@@ -15,10 +15,23 @@
- if (typeof challenge.msg != 'undefined')
| with message #{challenge.msg}
.btn-group
- button.btn.btn-mini.btn-danger(value='accept', challengingteamid='#{profile.team._id}', challengedteamid='#{challenge.teamid}') Accept
- button.btn.btn-mini.btn-warning(value='decline', challengingteamid='#{profile.team._id}', challengedteamid='#{challenge.teamid}') Decline
-- if (challenge.type == 'challenging')
- .team-challenge.team-challenging(challengingteamid='#{profile.team._id}', challengedteamid='#{challenge.teamid}')
- .team-challenge-text
- .buttons
- button(value='cancel', challengingteamid='#{profile.team._id}', challengedteamid='#{challenge.teamid}') Cancel
+ button.btn.btn-mini.btn-danger(value='accept', data-teamid='#{profile.team._id}', data-otherteamid='#{challenge.teamid}') Accept
+ button.btn.btn-mini.btn-warning(value='decline', data-teamid='#{profile.team._id}', data-otherteamid='#{challenge.teamid}') Decline
+- else
+ .alert.alert-block.alert-info
+ a.close(data-dismiss='alert') x
+ h4.alert-heading You are challenging
+ p
+ | Team&nbsp;
+ a.label.label-info(href='/team/#{challenge.teamid}')= challenge.teamname
+ | in a
+ - if (challenge.matchtype == '1')
+ | match
+ - else if (challenge.matchtype == '99')
+ | death match
+ - else
+ | best of #{challenge.matchtype} match
+ - if (typeof challenge.msg != 'undefined')
+ | with message #{challenge.msg}
+ .btn-group
+ button.btn.btn-mini.btn-danger(value='cancel', data-teamid='#{profile.team._id}', data-otherteamid='#{challenge.teamid}') Cancel
View
9 views/profile/show.jade
@@ -49,9 +49,10 @@ link(rel="stylesheet/less", type="text/css", href="/stylesheets/profile.less")
- if (typeof profile.invites !== 'undefined' && profile.invites.length)
!= partial('invite', profile.invites)
- if (team && team.challenges && team.challenges.length)
- != partial('challenge', team.challenges)
- - if (team && team.matches && team.matches.length)
- != partial('match', team.matches)
+ != partial('challenge', profile.challenges)
+ script(type='text/javascript', src='/js/todochallenge.js')
+ - if (profile && profile.matches && profile.matches.length)
+ != partial('match', profile.matches)
#upload-picture-dlg.modal.hide
form(method="post", enctype="multipart/form-data", action="/player/#{profile._id}/picture")
.modal-header
@@ -65,6 +66,4 @@ link(rel="stylesheet/less", type="text/css", href="/stylesheets/profile.less")
-
-
script(type='text/javascript', src='/js/profile.js')
Please sign in to comment.
Something went wrong with that request. Please try again.