Added random session pair choosing

Due to a bug I cannot test this immediately
This commit is contained in:
Michael Schmatz 2014-03-14 11:30:04 -07:00
parent c0e966a08c
commit 71aa6cc2c6
2 changed files with 60 additions and 7 deletions

View file

@ -423,14 +423,30 @@ module.exports = class SpectateLevelView extends View
onNextGamePressed: (e) ->
console.log "You want to see the next game!"
@sessionOne = "53193c8f7a89df21c4d968e9"
@sessionTwo = "531aa613026834331eac5e7e"
url = "/play/spectate/dungeon-arena?session-one=#{@sessionOne}&session-two=#{@sessionTwo}"
Backbone.Mediator.publish 'router:navigate', {
route: url,
viewClass: SpectateLevelView,
viewArgs: [{spectateSessions:{sessionOne: @sessionOne, sessionTwo: @sessionTwo}}, "dungeon-arena"]}
@fetchRandomSessionPair (err, data) =>
if err? then return console.log "There was an error fetching the random session pair: #{data}"
@sessionOne = data[0]._id
@sessionTwo = data[1]._id
console.log "Playing session #{@sessionOne} against #{@sessionTwo}"
url = "/play/spectate/dungeon-arena?session-one=#{@sessionOne}&session-two=#{@sessionTwo}"
Backbone.Mediator.publish 'router:navigate', {
route: url,
viewClass: SpectateLevelView,
viewArgs: [{spectateSessions:{sessionOne: @sessionOne, sessionTwo: @sessionTwo}}, "dungeon-arena"]}
fetchRandomSessionPair: (cb) ->
console.log "Fetching random session pair!"
randomSessionPairURL = "/db/level/#{@level.get('original')}.#{@level.get('version').major}/random_session_pair"
$.ajax
url: randomSessionPairURL
type: "GET"
complete: (jqxhr, textStatus) ->
if textStatus isnt "success"
cb("error", jqxhr.statusText)
else
cb(null, $.parseJSON(jqxhr.responseText))

View file

@ -31,6 +31,8 @@ LevelHandler = class LevelHandler extends Handler
return @getLeaderboard(req, res, args[0]) if args[1] is 'leaderboard'
return @getMySessions(req, res, args[0]) if args[1] is 'my_sessions'
return @getFeedback(req, res, args[0]) if args[1] is 'feedback'
return @getRandomSessionPair(req,res,args[0]) if args[1] is 'random_session_pair'
return @sendNotFoundError(res)
fetchLevelByIDAndHandleErrors: (id, req, res, callback) ->
@ -146,6 +148,41 @@ LevelHandler = class LevelHandler extends Handler
return @sendDatabaseError(res, err) if err
resultSessions ?= []
@sendSuccess res, resultSessions
getRandomSessionPair: (req, res, id) ->
findParameters = {}
[original,version] = id.split '.'
version = parseInt(version) ? 0
sessionsQueryParameters =
level:
original: original
majorVersion: version
submitted:true
query = Session
.find(sessionsQueryParameters)
.select('team')
.lean()
query.exec (err, resultSessions) =>
return @sendDatabaseError res, err if err? or not resultSessions
teamSessions = _.groupBy resultSessions, 'team'
sessions = []
numberOfTeams = 0
for team of teamSessions
numberOfTeams += 1
sessions.push _.sample(teamSessions[team])
if numberOfTeams != 2 then return @sendDatabaseError res, "There aren't sessions of 2 teams, so cannot choose random opponents!"
@sendSuccess res, sessions
validateLeaderboardRequestParameters: (req) ->
req.query.order = parseInt(req.query.order) ? -1