This commit is contained in:
Scott Erickson 2014-03-22 10:13:01 -07:00
commit ad600fe459
3 changed files with 26 additions and 19 deletions
app/views/play
server/routes

View file

@ -19,10 +19,10 @@ module.exports = class LadderTabView extends CocoView
id: 'ladder-tab-view' id: 'ladder-tab-view'
template: require 'templates/play/ladder/ladder_tab' template: require 'templates/play/ladder/ladder_tab'
startsLoading: true startsLoading: true
events: events:
'click .connect-facebook': 'onConnectFacebook' 'click .connect-facebook': 'onConnectFacebook'
subscriptions: subscriptions:
'facebook-logged-in': 'onConnectedWithFacebook' 'facebook-logged-in': 'onConnectedWithFacebook'
@ -32,11 +32,11 @@ module.exports = class LadderTabView extends CocoView
@leaderboards = {} @leaderboards = {}
@refreshLadder() @refreshLadder()
@checkFriends() @checkFriends()
onConnectFacebook: -> onConnectFacebook: ->
@connecting = true @connecting = true
FB.login() FB.login()
onConnectedWithFacebook: -> onConnectedWithFacebook: ->
location.reload() if @connecting location.reload() if @connecting
@ -61,7 +61,7 @@ module.exports = class LadderTabView extends CocoView
method: 'POST' method: 'POST'
success: @facebookFriendsLoaded success: @facebookFriendsLoaded
} }
facebookFriendsLoaded: (result) => facebookFriendsLoaded: (result) =>
friendsMap = {} friendsMap = {}
friendsMap[friend.id] = friend.name for friend in @facebookData friendsMap[friend.id] = friend.name for friend in @facebookData
@ -83,9 +83,10 @@ module.exports = class LadderTabView extends CocoView
$.when(promises...).then(@leaderboardsLoaded) $.when(promises...).then(@leaderboardsLoaded)
leaderboardsLoaded: => leaderboardsLoaded: =>
return if @destroyed
@loadingLeaderboards = false @loadingLeaderboards = false
@renderMaybe() @renderMaybe()
renderMaybe: -> renderMaybe: ->
return if @loadingFriends or @loadingLeaderboards return if @loadingFriends or @loadingLeaderboards
@startsLoading = false @startsLoading = false

View file

@ -107,7 +107,7 @@ module.exports = class SpectateLevelView extends View
team: @getQueryVariable("team") team: @getQueryVariable("team")
@levelLoader.once 'loaded-all', @onLevelLoaderLoaded, @ @levelLoader.once 'loaded-all', @onLevelLoaderLoaded, @
@levelLoader.on 'progress', @onLevelLoaderProgressChanged, @ @levelLoader.on 'progress', @onLevelLoaderProgressChanged, @
@god = new God() @god = new God maxWorkerPoolSize: 1, maxAngels: 1
getRenderData: -> getRenderData: ->
c = super() c = super()

View file

@ -31,6 +31,11 @@ getAllLadderScores = (next) ->
# Query to get sessions to make histogram # Query to get sessions to make histogram
# db.level.sessions.find({"submitted":true,"levelID":"brawlwood",team:"ogres"},{"_id":0,"totalScore":1}) # db.level.sessions.find({"submitted":true,"levelID":"brawlwood",team:"ogres"},{"_id":0,"totalScore":1})
DEBUGGING = false
LADDER_PREGAME_INTERVAL = 2 * 3600 * 1000 # Send emails two hours before players last submitted.
getTimeFromDaysAgo = (now, daysAgo) ->
t = now - 86400 * 1000 * daysAgo - LADDER_PREGAME_INTERVAL
isRequestFromDesignatedCronHandler = (req, res) -> isRequestFromDesignatedCronHandler = (req, res) ->
if req.ip isnt config.mail.cronHandlerPublicIP and req.ip isnt config.mail.cronHandlerPrivateIP if req.ip isnt config.mail.cronHandlerPublicIP and req.ip isnt config.mail.cronHandlerPrivateIP
console.log "RECEIVED REQUEST FROM IP #{req.ip}(headers indicate #{req.headers['x-forwarded-for']}" console.log "RECEIVED REQUEST FROM IP #{req.ip}(headers indicate #{req.headers['x-forwarded-for']}"
@ -40,25 +45,22 @@ isRequestFromDesignatedCronHandler = (req, res) ->
return false return false
return true return true
handleLadderUpdate = (req, res) -> handleLadderUpdate = (req, res) ->
log.info("Going to see about sending ladder update emails.") log.info("Going to see about sending ladder update emails.")
requestIsFromDesignatedCronHandler = isRequestFromDesignatedCronHandler req, res requestIsFromDesignatedCronHandler = isRequestFromDesignatedCronHandler req, res
#unless requestIsFromDesignatedCronHandler then return return unless requestIsFromDesignatedCronHandler or DEBUGGING
res.send('Great work, Captain Cron! I can take it from here.') res.send('Great work, Captain Cron! I can take it from here.')
res.end() res.end()
# TODO: somehow fetch the histograms # TODO: somehow fetch the histograms
emailDays = [1, 2, 4, 7, 30] emailDays = [1, 2, 4, 7, 30]
now = new Date() now = new Date()
getTimeFromDaysAgo = (daysAgo) ->
# 2 hours before the date
t = now - (86400 * daysAgo + 2 * 3600) * 1000
for daysAgo in emailDays for daysAgo in emailDays
# Get every session that was submitted in a 5-minute window after the time. # Get every session that was submitted in a 5-minute window after the time.
startTime = getTimeFromDaysAgo daysAgo startTime = getTimeFromDaysAgo now, daysAgo
endTime = startTime + 5 * 60 * 1000 endTime = startTime + 5 * 60 * 1000
#endTime = startTime + 1.5 * 60 * 60 * 1000 # Debugging: make sure there's something to send if DEBUGGING
endTime = startTime + 15 * 60 * 1000 # Debugging: make sure there's something to send
findParameters = {submitted: true, submitDate: {$gt: new Date(startTime), $lte: new Date(endTime)}} findParameters = {submitted: true, submitDate: {$gt: new Date(startTime), $lte: new Date(endTime)}}
# TODO: think about putting screenshots in the email # TODO: think about putting screenshots in the email
selectString = "creator team levelName levelID totalScore matches submitted submitDate scoreHistory" selectString = "creator team levelName levelID totalScore matches submitted submitDate scoreHistory"
@ -71,9 +73,9 @@ handleLadderUpdate = (req, res) ->
log.error "Couldn't fetch ladder updates for #{findParameters}\nError: #{err}" log.error "Couldn't fetch ladder updates for #{findParameters}\nError: #{err}"
return errors.serverError res, "Ladder update email query failed: #{JSON.stringify(err)}" return errors.serverError res, "Ladder update email query failed: #{JSON.stringify(err)}"
log.info "Found #{results.length} ladder sessions to email updates about for #{daysAgo} day(s) ago." log.info "Found #{results.length} ladder sessions to email updates about for #{daysAgo} day(s) ago."
sendLadderUpdateEmail result, daysAgo for result in results sendLadderUpdateEmail result, now, daysAgo for result in results
sendLadderUpdateEmail = (session, daysAgo) -> sendLadderUpdateEmail = (session, now, daysAgo) ->
User.findOne({_id: session.creator}).select("name email firstName lastName emailSubscriptions preferredLanguage").lean().exec (err, user) -> User.findOne({_id: session.creator}).select("name email firstName lastName emailSubscriptions preferredLanguage").lean().exec (err, user) ->
if err if err
log.error "Couldn't find user for #{session.creator} from session #{session._id}" log.error "Couldn't find user for #{session.creator} from session #{session._id}"
@ -89,19 +91,23 @@ sendLadderUpdateEmail = (session, daysAgo) ->
# Fetch the most recent defeat and victory, if there are any. # Fetch the most recent defeat and victory, if there are any.
# (We could look at strongest/weakest, but we'd have to fetch everyone, or denormalize more.) # (We could look at strongest/weakest, but we'd have to fetch everyone, or denormalize more.)
matches = _.filter session.matches, (match) -> match.date >= (new Date() - 86400 * 1000 * daysAgo) matches = _.filter session.matches, (match) -> match.date >= getTimeFromDaysAgo now, daysAgo
defeats = _.filter matches, (match) -> match.metrics.rank is 1 and match.opponents[0].metrics.rank is 0 defeats = _.filter matches, (match) -> match.metrics.rank is 1 and match.opponents[0].metrics.rank is 0
victories = _.filter matches, (match) -> match.metrics.rank is 0 and match.opponents[0].metrics.rank is 1 victories = _.filter matches, (match) -> match.metrics.rank is 0 and match.opponents[0].metrics.rank is 1
#ties = _.filter matches, (match) -> match.metrics.rank is 0 and match.opponents[0].metrics.rank is 0
defeat = _.last defeats defeat = _.last defeats
victory = _.last victories victory = _.last victories
#log.info "#{user.name} had #{matches.length} matches from last #{daysAgo} days out of #{session.matches.length} total matches. #{defeats.length} defeats, #{victories.length} victories, and #{ties.length} ties."
#matchInfos = ("\t#{match.date}\t#{match.date >= getTimeFromDaysAgo(now, daysAgo)}\t#{match.metrics.rank}\t#{match.opponents[0].metrics.rank}" for match in session.matches)
#log.info "Matches:\n#{matchInfos.join('\n')}"
sendEmail = (defeatContext, victoryContext) -> sendEmail = (defeatContext, victoryContext) ->
# TODO: do something with the preferredLanguage? # TODO: do something with the preferredLanguage?
context = context =
email_id: sendwithus.templates.ladder_update_email email_id: sendwithus.templates.ladder_update_email
recipient: recipient:
address: user.email address: if DEBUGGING then 'nick@codecombat.com' else user.email
#address: 'nick@codecombat.com' # Debugging
name: name name: name
email_data: email_data:
name: name name: name