Permalink
Browse files

created news from a match result

  • Loading branch information...
1 parent d13bbb1 commit 5c9953ce3460260a74c0aa12fff1c914daba78f4 @toantran committed Mar 15, 2012
@@ -210,6 +210,10 @@ exports.finalize = (am, callback = -> ) ->
do (teamid, result) ->
console.log 'team %s count=%d win=%s', teamid, result.count, result.win
+ news =
+ matchid: am?._id
+ winningteamid: if results[String(am.teams[1]._id)].win then am.teams[1]._id else am.teams[0]._id
+ losingteamid: if results[String(am.teams[1]._id)].win then am.teams[0]._id else am.teams[1]._id
makeSetMatchComplete = () ->
(m, cb = ->) ->
@@ -265,8 +269,22 @@ exports.finalize = (am, callback = -> ) ->
console.timeEnd "Match #{am._id} Set team #{team._id} match complete"
console.log "Match #{am._id} Set team #{team._id} match complete with error #{err}" if err?
cb null
+
+ makeCreateNews = () ->
+ (m, maincb = ->) ->
+ utils.execute( teamSvc.getById, news.winningteamid )
+ .then (err, winningteam, cb = ->) ->
+ news.winningteamname = winningteam?.teamname
+ teamSvc.getById news.losingteamid, cb
+ .then (err, losingteam, cb = ->) ->
+ news.losingteamname = losingteam?.teamname
+
+ newsSvc = require './news'
+ newsSvc.createMatchResultNews news, cb
+ .then ->
+ maincb null, m
- utils.seriesAsync [makeSetMatchComplete(), makeUpdateTeamStats(), makeUpdatePlayersStats(), makeSetTeamMatchComplete()], am, () ->
+ utils.seriesAsync [makeSetMatchComplete(), makeUpdateTeamStats(), makeUpdatePlayersStats(), makeSetTeamMatchComplete(), makeCreateNews()], am, () ->
console.log 'Done!'
@@ -11,3 +11,36 @@ exports.getHighlights = (callback = ->) ->
return callback(err) if err? or not cursor?
cursor.sort createdat: -1
cursor.toArray callback
+
+
+exports.createNews = createNews = (news..., callback = ->) ->
+ [newsObj, newsTpl, newsData] = news
+
+ if typeof newsObj is 'string'
+ [newsTpl, newsData] = [newsObj, newsTpl]
+ newsObj =
+ newstpl : newsTpl
+ newsdata : newsData
+
+ newsObj.newstpl ?= newsTpl
+ newsObj.newsdata ?= newsData
+ newsObj.newsstatus ?= 'active'
+ newsObj.newsdate ?= new Date()
+
+ newsrepo.create newsObj, (err, insertedObjs) ->
+ insertedNews = insertedObjs?[0]
+ callback err, insertedNews
+
+
+exports.createMatchResultNews = (matchdata, callback = ->) ->
+ news =
+ highlight: 1
+ newsdate: new Date()
+ newsstatus: 'active'
+ newsdata: matchdata
+ caption: 'Match Result'
+ pictureurl: '/images/matchresult.jpg'
+ createNews news, 'matchresult', matchdata, callback
+
+
+
@@ -461,6 +461,7 @@ exports.resetStats = (teamid, callback = ->) ->
updateObj =
$unset:
stats: 1
+ records: 1
try
newTeamRepo.update findObj, updateObj, {}, callback
@@ -490,6 +491,7 @@ exports.updateStats = (teamid, opponentid, win, callback = ->) ->
updatedat: new Date()
$addToSet:
posts: statLog
+ records: statLog
try
newTeamRepo.update findObj, updateObj, {}, callback
@@ -515,6 +517,8 @@ exports.updateStatsSilent = (teamid, opponentid, win, callback = ->) ->
createdat: new Date()
updateObj =
$inc: incObj
+ $addToSet:
+ records: statLog
try
newTeamRepo.update findObj, updateObj, {}, callback
@@ -570,11 +574,12 @@ exports.sortingTeams = (team1, team2) ->
exports.getAll = (availableOnly, callback = ->) ->
- query = {}
+ query =
+ hidden:
+ $ne: 1
if availableOnly
- query =
- '$or': [{members: null}, {members: {$size: 0}}, {members: {$size: 1}}]
+ query.$or = [{members: null}, {members: {$size: 0}}, {members: {$size: 1}}]
try
newTeamRepo.read query, (readErr, cursor) ->
if readErr?
@@ -144,6 +144,7 @@ exports.resetStats = (userid, callback = ->) ->
updateObj =
$unset:
stats: 1
+ records: 1
try
newUserRepo.update findObj, updateObj, {}, callback
@@ -169,13 +170,14 @@ exports.updateStats = (userid, opponentid, win, callback = ->) ->
data:
opponentid: opponentid
result: if win then 'win' else 'lose'
- createdat: new Date()
+ createdat: new Date()
updateObj =
$inc: incObj
$set:
updatedat: new Date()
$addToSet:
posts: statLog
+ records: statLog
try
newUserRepo.update findObj, updateObj, {}, callback
@@ -195,8 +197,17 @@ exports.updateStatsSilent = (userid, opponentid, win, callback = ->) ->
opponentid = String opponentid if typeof opponentid isnt 'string'
findObj = _id : userid
incObj = if win then {'stats.win':1} else {'stats.loss': 1}
+ statLog =
+ id: new newUserRepo.ObjectId()
+ type: 'matchresult'
+ data:
+ opponentid: opponentid
+ result: if win then 'win' else 'lose'
+ createdat: new Date()
updateObj =
$inc: incObj
+ $addToSet:
+ records: statLog
try
newUserRepo.update findObj, updateObj, {}, callback
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Oops, something went wrong.
View
@@ -1,5 +1,6 @@
(function() {
- var newsrepo;
+ var createNews, newsrepo,
+ __slice = Array.prototype.slice;
newsrepo = require('../repository/news');
@@ -27,4 +28,41 @@
});
};
+ exports.createNews = createNews = function() {
+ var callback, news, newsData, newsObj, newsTpl, _i, _ref;
+ news = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), callback = arguments[_i++];
+ if (callback == null) callback = function() {};
+ newsObj = news[0], newsTpl = news[1], newsData = news[2];
+ if (typeof newsObj === 'string') {
+ _ref = [newsObj, newsTpl], newsTpl = _ref[0], newsData = _ref[1];
+ newsObj = {
+ newstpl: newsTpl,
+ newsdata: newsData
+ };
+ }
+ if (newsObj.newstpl == null) newsObj.newstpl = newsTpl;
+ if (newsObj.newsdata == null) newsObj.newsdata = newsData;
+ if (newsObj.newsstatus == null) newsObj.newsstatus = 'active';
+ if (newsObj.newsdate == null) newsObj.newsdate = new Date();
+ return newsrepo.create(newsObj, function(err, insertedObjs) {
+ var insertedNews;
+ insertedNews = insertedObjs != null ? insertedObjs[0] : void 0;
+ return callback(err, insertedNews);
+ });
+ };
+
+ exports.createMatchResultNews = function(matchdata, callback) {
+ var news;
+ if (callback == null) callback = function() {};
+ news = {
+ highlight: 1,
+ newsdate: new Date(),
+ newsstatus: 'active',
+ newsdata: matchdata,
+ caption: 'Match Result',
+ pictureurl: '/images/matchresult.jpg'
+ };
+ return createNews(news, 'matchresult', matchdata, callback);
+ };
+
}).call(this);
View
Oops, something went wrong.
View
Oops, something went wrong.
@@ -64,6 +64,8 @@ link(rel="stylesheet/less", type="text/css", href="/stylesheets/profile.less")
input(name='image', type='file')
.modal-footer
input.btn.btn-primary(type='submit') Save
+#team-records-dlg.modal.hide
+

0 comments on commit 5c9953c

Please sign in to comment.