This commit is contained in:
George Saines 2014-03-02 18:21:46 -08:00
commit 94b98a2d46
2 changed files with 38 additions and 37 deletions
app/views/play/level/tome
server/queues

View file

@ -129,6 +129,7 @@ module.exports = class TomeView extends View
@thangSpells[thang.id].push spellKey @thangSpells[thang.id].push spellKey
unless method.cloneOf unless method.cloneOf
skipProtectAPI = @getQueryVariable("skip_protect_api") is "true" or @options.levelID isnt 'brawlwood' skipProtectAPI = @getQueryVariable("skip_protect_api") is "true" or @options.levelID isnt 'brawlwood'
skipProtectAPI = true # gah, it's so slow :( and somehow still affects simulation
skipFlow = @getQueryVariable("skip_flow") is "true" or @options.levelID is 'brawlwood' skipFlow = @getQueryVariable("skip_flow") is "true" or @options.levelID is 'brawlwood'
spell = @spells[spellKey] = new Spell programmableMethod: method, spellKey: spellKey, pathComponents: pathPrefixComponents.concat(pathComponents), session: @options.session, supermodel: @supermodel, skipFlow: skipFlow, skipProtectAPI: skipProtectAPI, worker: @worker spell = @spells[spellKey] = new Spell programmableMethod: method, spellKey: spellKey, pathComponents: pathPrefixComponents.concat(pathComponents), session: @options.session, supermodel: @supermodel, skipFlow: skipFlow, skipProtectAPI: skipProtectAPI, worker: @worker
for thangID, spellKeys of @thangSpells for thangID, spellKeys of @thangSpells

View file

@ -23,13 +23,13 @@ connectToScoringQueue = ->
if error? then throw new Error "There was an error registering the scoring queue: #{error}" if error? then throw new Error "There was an error registering the scoring queue: #{error}"
scoringTaskQueue = data scoringTaskQueue = data
log.info "Connected to scoring task queue!" log.info "Connected to scoring task queue!"
module.exports.addPairwiseTaskToQueueFromRequest = (req, res) -> module.exports.addPairwiseTaskToQueueFromRequest = (req, res) ->
taskPair = req.body.sessions taskPair = req.body.sessions
addPairwiseTaskToQueue req.body.sessions (err, success) -> addPairwiseTaskToQueue req.body.sessions (err, success) ->
if err? then return errors.serverError res, "There was an error adding pairwise tasks: #{err}" if err? then return errors.serverError res, "There was an error adding pairwise tasks: #{err}"
sendResponseObject req, res, {"message":"All task pairs were succesfully sent to the queue"} sendResponseObject req, res, {"message":"All task pairs were succesfully sent to the queue"}
addPairwiseTaskToQueue = (taskPair, cb) -> addPairwiseTaskToQueue = (taskPair, cb) ->
LevelSession.findOne(_id:taskPair[0]).lean().exec (err, firstSession) => LevelSession.findOne(_id:taskPair[0]).lean().exec (err, firstSession) =>
@ -42,9 +42,9 @@ addPairwiseTaskToQueue = (taskPair, cb) ->
if e then return cb e, false if e then return cb e, false
sendEachTaskPairToTheQueue taskPairs, (taskPairError) -> sendEachTaskPairToTheQueue taskPairs, (taskPairError) ->
if taskPairError? then return cb taskPairError,false if taskPairError? then return cb taskPairError,false
cb null, true cb null, true
module.exports.createNewTask = (req, res) -> module.exports.createNewTask = (req, res) ->
requestSessionID = req.body.session requestSessionID = req.body.session
@ -105,6 +105,7 @@ module.exports.processTaskResult = (req, res) ->
return handleTimedOutTask req, res, clientResponseObject if hasTaskTimedOut taskLogJSON.sentDate return handleTimedOutTask req, res, clientResponseObject if hasTaskTimedOut taskLogJSON.sentDate
scoringTaskQueue.deleteMessage clientResponseObject.receiptHandle, (err) -> scoringTaskQueue.deleteMessage clientResponseObject.receiptHandle, (err) ->
console.log "Deleted message."
if err? then return errors.badInput res, "The queue message is already back in the queue, rejecting results." if err? then return errors.badInput res, "The queue message is already back in the queue, rejecting results."
logTaskComputation clientResponseObject, taskLog, (logErr) -> logTaskComputation clientResponseObject, taskLog, (logErr) ->
@ -117,14 +118,14 @@ module.exports.processTaskResult = (req, res) ->
addMatchToSessions clientResponseObject, newScoresObject, (err, data) -> addMatchToSessions clientResponseObject, newScoresObject, (err, data) ->
if err? then return errors.serverError res, "There was an error updating the sessions with the match! #{JSON.stringify err}" if err? then return errors.serverError res, "There was an error updating the sessions with the match! #{JSON.stringify err}"
originalSessionID = clientResponseObject.originalSessionID originalSessionID = clientResponseObject.originalSessionID
originalSessionTeam = clientResponseObject.originalSessionTeam originalSessionTeam = clientResponseObject.originalSessionTeam
originalSessionRank = parseInt clientResponseObject.originalSessionRank originalSessionRank = parseInt clientResponseObject.originalSessionRank
determineIfSessionShouldContinueAndUpdateLog originalSessionID, originalSessionRank, (err, sessionShouldContinue) -> determineIfSessionShouldContinueAndUpdateLog originalSessionID, originalSessionRank, (err, sessionShouldContinue) ->
if err? then return errors.serverError res, "There was an error determining if the session should continue, #{err}" if err? then return errors.serverError res, "There was an error determining if the session should continue, #{err}"
if sessionShouldContinue if sessionShouldContinue
opposingTeam = calculateOpposingTeam(originalSessionTeam) opposingTeam = calculateOpposingTeam(originalSessionTeam)
opponentID = _.pull(_.keys(newScoresObject), originalSessionID) opponentID = _.pull(_.keys(newScoresObject), originalSessionID)
@ -133,7 +134,7 @@ module.exports.processTaskResult = (req, res) ->
findNearestBetterSessionID originalSessionID, sessionNewScore, opponentNewScore, opponentID ,opposingTeam, (err, opponentSessionID) -> findNearestBetterSessionID originalSessionID, sessionNewScore, opponentNewScore, opponentID ,opposingTeam, (err, opponentSessionID) ->
if err? then return errors.serverError res, "There was an error finding the nearest sessionID!" if err? then return errors.serverError res, "There was an error finding the nearest sessionID!"
unless opponentSessionID then return sendResponseObject req, res, {"message":"There were no more games to rank(game is at top!"} unless opponentSessionID then return sendResponseObject req, res, {"message":"There were no more games to rank(game is at top!"}
addPairwiseTaskToQueue [originalSessionID, opponentSessionID], (err, success) -> addPairwiseTaskToQueue [originalSessionID, opponentSessionID], (err, success) ->
if err? then return errors.serverError res, "There was an error sending the pairwise tasks to the queue!" if err? then return errors.serverError res, "There was an error sending the pairwise tasks to the queue!"
sendResponseObject req, res, {"message":"The scores were updated successfully and more games were sent to the queue!"} sendResponseObject req, res, {"message":"The scores were updated successfully and more games were sent to the queue!"}
@ -141,19 +142,19 @@ module.exports.processTaskResult = (req, res) ->
console.log "Player lost, achieved rank #{originalSessionRank}" console.log "Player lost, achieved rank #{originalSessionRank}"
sendResponseObject req, res, {"message":"The scores were updated successfully, person lost so no more games are being inserted!"} sendResponseObject req, res, {"message":"The scores were updated successfully, person lost so no more games are being inserted!"}
determineIfSessionShouldContinueAndUpdateLog = (sessionID, sessionRank, cb) -> determineIfSessionShouldContinueAndUpdateLog = (sessionID, sessionRank, cb) ->
queryParameters = queryParameters =
_id: sessionID _id: sessionID
updateParameters = updateParameters =
"$inc": {} "$inc": {}
if sessionRank is 0 if sessionRank is 0
updateParameters["$inc"] = {numberOfWinsAndTies: 1} updateParameters["$inc"] = {numberOfWinsAndTies: 1}
else else
updateParameters["$inc"] = {numberOfLosses: 1} updateParameters["$inc"] = {numberOfLosses: 1}
LevelSession.findOneAndUpdate queryParameters, updateParameters,{select: 'numberOfWinsAndTies numberOfLosses'}, (err, updatedSession) -> LevelSession.findOneAndUpdate queryParameters, updateParameters,{select: 'numberOfWinsAndTies numberOfLosses'}, (err, updatedSession) ->
if err? then return cb err, updatedSession if err? then return cb err, updatedSession
updatedSession = updatedSession.toObject() updatedSession = updatedSession.toObject()
@ -170,16 +171,16 @@ determineIfSessionShouldContinueAndUpdateLog = (sessionID, sessionRank, cb) ->
else else
console.log "Ratio(#{ratio}) is good, so continuing simulations" console.log "Ratio(#{ratio}) is good, so continuing simulations"
cb null, true cb null, true
findNearestBetterSessionID = (sessionID, sessionTotalScore, opponentSessionTotalScore, opponentSessionID, opposingTeam, cb) -> findNearestBetterSessionID = (sessionID, sessionTotalScore, opponentSessionTotalScore, opponentSessionID, opposingTeam, cb) ->
retrieveAllOpponentSessionIDs sessionID, (err, opponentSessionIDs) -> retrieveAllOpponentSessionIDs sessionID, (err, opponentSessionIDs) ->
if err? then return cb err, null if err? then return cb err, null
queryParameters = queryParameters =
totalScore: totalScore:
$gt:opponentSessionTotalScore $gt:opponentSessionTotalScore
_id: _id:
$nin: opponentSessionIDs $nin: opponentSessionIDs
"level.original": "52d97ecd32362bc86e004e87" "level.original": "52d97ecd32362bc86e004e87"
"level.majorVersion": 0 "level.majorVersion": 0
@ -187,20 +188,20 @@ findNearestBetterSessionID = (sessionID, sessionTotalScore, opponentSessionTotal
submittedCode: submittedCode:
$exists: true $exists: true
team: opposingTeam team: opposingTeam
limitNumber = 1 limitNumber = 1
sortParameters = sortParameters =
totalScore: 1 totalScore: 1
selectString = '_id totalScore' selectString = '_id totalScore'
query = LevelSession.findOne(queryParameters) query = LevelSession.findOne(queryParameters)
.sort(sortParameters) .sort(sortParameters)
.limit(limitNumber) .limit(limitNumber)
.select(selectString) .select(selectString)
.lean() .lean()
console.log "Finding session with score near #{opponentSessionTotalScore}" console.log "Finding session with score near #{opponentSessionTotalScore}"
query.exec (err, session) -> query.exec (err, session) ->
if err? then return cb err, session if err? then return cb err, session
@ -208,7 +209,7 @@ findNearestBetterSessionID = (sessionID, sessionTotalScore, opponentSessionTotal
console.log "Found session with score #{session.totalScore}" console.log "Found session with score #{session.totalScore}"
cb err, session._id cb err, session._id
retrieveAllOpponentSessionIDs = (sessionID, cb) -> retrieveAllOpponentSessionIDs = (sessionID, cb) ->
query = LevelSession.findOne({"_id":sessionID}) query = LevelSession.findOne({"_id":sessionID})
.select('matches.opponents.sessionID') .select('matches.opponents.sessionID')
@ -217,14 +218,14 @@ retrieveAllOpponentSessionIDs = (sessionID, cb) ->
if err? then return cb err, null if err? then return cb err, null
opponentSessionIDs = (match.opponents[0].sessionID for match in session.matches) opponentSessionIDs = (match.opponents[0].sessionID for match in session.matches)
cb err, opponentSessionIDs cb err, opponentSessionIDs
calculateOpposingTeam = (sessionTeam) -> calculateOpposingTeam = (sessionTeam) ->
teams = ['ogres','humans'] teams = ['ogres','humans']
opposingTeams = _.pull teams, sessionTeam opposingTeams = _.pull teams, sessionTeam
return opposingTeams[0] return opposingTeams[0]
validatePermissions = (req, sessionID, callback) -> validatePermissions = (req, sessionID, callback) ->
if isUserAnonymous req then return callback null, false if isUserAnonymous req then return callback null, false
if isUserAdmin req then return callback null, true if isUserAdmin req then return callback null, true
@ -298,17 +299,17 @@ fetchInitialSessionsToRankAgainst = (opposingTeam, callback) ->
submittedCode: submittedCode:
$exists: true $exists: true
team: opposingTeam team: opposingTeam
sortParameters = sortParameters =
totalScore: 1 totalScore: 1
limitNumber = 1 limitNumber = 1
query = LevelSession.find(findParameters) query = LevelSession.find(findParameters)
.sort(sortParameters) .sort(sortParameters)
.limit(limitNumber) .limit(limitNumber)
query.exec callback query.exec callback
generateTaskPairs = (submittedSessions, sessionToScore) -> generateTaskPairs = (submittedSessions, sessionToScore) ->
@ -444,4 +445,3 @@ retrieveOldSessionData = (sessionID, callback) ->
"totalScore":session.totalScore ? (25 - 1.8*(25/3)) "totalScore":session.totalScore ? (25 - 1.8*(25/3))
"id": sessionID "id": sessionID
callback err, oldScoreObject callback err, oldScoreObject