mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
Simulator fetch random Greed games when queue empty
This commit is contained in:
parent
b8dc4ed939
commit
97cbf55695
3 changed files with 102 additions and 23 deletions
|
@ -23,6 +23,7 @@ module.exports = class Simulator extends CocoClass
|
|||
@cleanupSimulation()
|
||||
@god?.destroy()
|
||||
super()
|
||||
|
||||
fetchAndSimulateOneGame: (humanGameID, ogresGameID) =>
|
||||
return if @destroyed
|
||||
$.ajax
|
||||
|
@ -35,7 +36,7 @@ module.exports = class Simulator extends CocoClass
|
|||
error: (errorData) ->
|
||||
console.log "There was an error fetching two games! #{JSON.stringify errorData}"
|
||||
success: (taskData) =>
|
||||
|
||||
@trigger 'statusUpdate', 'Setting up simulation...'
|
||||
#refactor this
|
||||
@task = new SimulationTask(taskData)
|
||||
|
||||
|
@ -49,7 +50,7 @@ module.exports = class Simulator extends CocoClass
|
|||
@listenToOnce @supermodel, 'loaded-all', @simulateSingleGame
|
||||
simulateSingleGame: ->
|
||||
return if @destroyed
|
||||
console.log "Commencing simulation!"
|
||||
@trigger 'statusUpdate', 'Simulating...'
|
||||
@assignWorldAndLevelFromLevelLoaderAndDestroyIt()
|
||||
@setupGod()
|
||||
try
|
||||
|
@ -57,6 +58,7 @@ module.exports = class Simulator extends CocoClass
|
|||
catch err
|
||||
console.log err
|
||||
@handleSingleSimulationError()
|
||||
|
||||
commenceSingleSimulation: ->
|
||||
@god.createWorld @generateSpellsObject()
|
||||
Backbone.Mediator.subscribeOnce 'god:infinite-loop', @handleSingleSimulationInfiniteLoop, @
|
||||
|
@ -89,10 +91,22 @@ module.exports = class Simulator extends CocoClass
|
|||
else if ogreSessionRank < humanSessionRank
|
||||
console.log "GAMERESULT:ogres"
|
||||
process.exit(0)
|
||||
else
|
||||
@sendSingleGameBackToServer(taskResults)
|
||||
|
||||
@cleanupSimulation()
|
||||
|
||||
sendSingleGameBackToServer: (results) ->
|
||||
@trigger 'statusUpdate', 'Simulation completed, sending results back to server!'
|
||||
|
||||
|
||||
$.ajax
|
||||
url: "/queue/scoring/recordTwoGames"
|
||||
data: results
|
||||
type: "PUT"
|
||||
parse: true
|
||||
success: @handleTaskResultsTransferSuccess
|
||||
error: @handleTaskResultsTransferError
|
||||
complete: @cleanupAndSimulateAnotherTask
|
||||
|
||||
|
||||
fetchAndSimulateTask: =>
|
||||
|
@ -120,12 +134,13 @@ module.exports = class Simulator extends CocoClass
|
|||
console.error "There was a horrible Error: #{JSON.stringify errorData}"
|
||||
@trigger 'statusUpdate', 'There was an error fetching games to simulate. Retrying in 10 seconds.'
|
||||
@simulateAnotherTaskAfterDelay()
|
||||
|
||||
|
||||
handleNoGamesResponse: ->
|
||||
info = 'There were no games to simulate--all simulations are done or in process. Retrying in 10 seconds.'
|
||||
info = 'Finding game to simulate...'
|
||||
console.log info
|
||||
@trigger 'statusUpdate', info
|
||||
@simulateAnotherTaskAfterDelay()
|
||||
@fetchAndSimulateOneGame()
|
||||
application.tracker?.trackEvent 'Simulator Result', label: "No Games"
|
||||
|
||||
simulateAnotherTaskAfterDelay: =>
|
||||
|
|
|
@ -106,25 +106,85 @@ module.exports.getTwoGames = (req, res) ->
|
|||
|
||||
unless ogresGameID and humansGameID
|
||||
#fetch random games here
|
||||
return errors.badInput(res, "You need to supply two games(for now)")
|
||||
LevelSession.findOne(_id: humansGameID).lean().exec (err, humanSession) =>
|
||||
if err? then return errors.serverError(res, "Couldn't find the human game")
|
||||
LevelSession.findOne(_id: ogresGameID).lean().exec (err, ogreSession) =>
|
||||
if err? then return errors.serverError(res, "Couldn't find the ogre game")
|
||||
taskObject =
|
||||
"messageGenerated": Date.now()
|
||||
"sessions": []
|
||||
for session in [humanSession, ogreSession]
|
||||
sessionInformation =
|
||||
"sessionID": session._id
|
||||
"team": session.team ? "No team"
|
||||
"transpiledCode": session.transpiledCode
|
||||
"teamSpells": session.teamSpells ? {}
|
||||
"levelID": session.levelID
|
||||
queryParams =
|
||||
"levelID":"greed"
|
||||
"submitted":true
|
||||
"team":"humans"
|
||||
selection = "team totalScore transpiledCode teamSpells levelID creatorName creator"
|
||||
LevelSession.count queryParams, (err, numberOfHumans) =>
|
||||
query = LevelSession
|
||||
.find(queryParams)
|
||||
.limit(1)
|
||||
.select(selection)
|
||||
.skip(Math.floor(Math.random()*numberOfHumans))
|
||||
.lean()
|
||||
query.exec (err, randomSession) =>
|
||||
if err? then return errors.serverError(res, "Couldn't select a top 15 random session!")
|
||||
randomSession = randomSession[0]
|
||||
queryParams =
|
||||
"levelID":"greed"
|
||||
"submitted":true
|
||||
"totalScore":
|
||||
$lte: randomSession.totalScore
|
||||
"team": "ogres"
|
||||
query = LevelSession
|
||||
.find(queryParams)
|
||||
.select(selection)
|
||||
.sort(totalScore: -1)
|
||||
.limit(1)
|
||||
.lean()
|
||||
query.exec (err, otherSession) =>
|
||||
if err? then return errors.serverError(res, "Couldnt' select the other top 15 random session!")
|
||||
otherSession = otherSession[0]
|
||||
taskObject =
|
||||
"messageGenerated": Date.now()
|
||||
"sessions": []
|
||||
for session in [randomSession, otherSession]
|
||||
sessionInformation =
|
||||
"sessionID": session._id
|
||||
"team": session.team ? "No team"
|
||||
"transpiledCode": session.transpiledCode
|
||||
"teamSpells": session.teamSpells ? {}
|
||||
"levelID": session.levelID
|
||||
"creatorName": session.creatorName
|
||||
"creator": session.creator
|
||||
"totalScore": session.totalScore
|
||||
|
||||
taskObject.sessions.push sessionInformation
|
||||
sendResponseObject req, res, taskObject
|
||||
else
|
||||
LevelSession.findOne(_id: humansGameID).lean().exec (err, humanSession) =>
|
||||
if err? then return errors.serverError(res, "Couldn't find the human game")
|
||||
LevelSession.findOne(_id: ogresGameID).lean().exec (err, ogreSession) =>
|
||||
if err? then return errors.serverError(res, "Couldn't find the ogre game")
|
||||
taskObject =
|
||||
"messageGenerated": Date.now()
|
||||
"sessions": []
|
||||
for session in [humanSession, ogreSession]
|
||||
sessionInformation =
|
||||
"sessionID": session._id
|
||||
"team": session.team ? "No team"
|
||||
"transpiledCode": session.transpiledCode
|
||||
"teamSpells": session.teamSpells ? {}
|
||||
"levelID": session.levelID
|
||||
|
||||
taskObject.sessions.push sessionInformation
|
||||
sendResponseObject req, res, taskObject
|
||||
|
||||
module.exports.recordTwoGames = (req, res) ->
|
||||
@clientResponseObject = req.body
|
||||
|
||||
taskObject.sessions.push sessionInformation
|
||||
sendResponseObject req, res, taskObject
|
||||
|
||||
async.waterfall [
|
||||
fetchLevelSession.bind(@)
|
||||
updateSessions.bind(@)
|
||||
indexNewScoreArray.bind(@)
|
||||
addMatchToSessions.bind(@)
|
||||
updateUserSimulationCounts.bind(@, req.user._id)
|
||||
], (err, successMessageObject) ->
|
||||
if err? then return errors.serverError res, "There was an error recording the single game:#{err}"
|
||||
sendResponseObject req, res, {"message":"The single game was submitted successfully!"}
|
||||
|
||||
|
||||
module.exports.createNewTask = (req, res) ->
|
||||
requestSessionID = req.body.session
|
||||
originalLevelID = req.body.originalLevelID
|
||||
|
|
|
@ -21,6 +21,10 @@ module.exports.setup = (app) ->
|
|||
app.post '/queue/scoring/getTwoGames', (req, res) ->
|
||||
handler = loadQueueHandler 'scoring'
|
||||
handler.getTwoGames req, res
|
||||
|
||||
app.put '/queue/scoring/recordTwoGames', (req, res) ->
|
||||
handler = loadQueueHandler 'scoring'
|
||||
handler.recordTwoGames req, res
|
||||
|
||||
app.all '/queue/*', (req, res) ->
|
||||
setResponseHeaderToJSONContentType res
|
||||
|
|
Loading…
Reference in a new issue