mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-24 08:08:15 -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()
|
@cleanupSimulation()
|
||||||
@god?.destroy()
|
@god?.destroy()
|
||||||
super()
|
super()
|
||||||
|
|
||||||
fetchAndSimulateOneGame: (humanGameID, ogresGameID) =>
|
fetchAndSimulateOneGame: (humanGameID, ogresGameID) =>
|
||||||
return if @destroyed
|
return if @destroyed
|
||||||
$.ajax
|
$.ajax
|
||||||
|
@ -35,7 +36,7 @@ module.exports = class Simulator extends CocoClass
|
||||||
error: (errorData) ->
|
error: (errorData) ->
|
||||||
console.log "There was an error fetching two games! #{JSON.stringify errorData}"
|
console.log "There was an error fetching two games! #{JSON.stringify errorData}"
|
||||||
success: (taskData) =>
|
success: (taskData) =>
|
||||||
|
@trigger 'statusUpdate', 'Setting up simulation...'
|
||||||
#refactor this
|
#refactor this
|
||||||
@task = new SimulationTask(taskData)
|
@task = new SimulationTask(taskData)
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ module.exports = class Simulator extends CocoClass
|
||||||
@listenToOnce @supermodel, 'loaded-all', @simulateSingleGame
|
@listenToOnce @supermodel, 'loaded-all', @simulateSingleGame
|
||||||
simulateSingleGame: ->
|
simulateSingleGame: ->
|
||||||
return if @destroyed
|
return if @destroyed
|
||||||
console.log "Commencing simulation!"
|
@trigger 'statusUpdate', 'Simulating...'
|
||||||
@assignWorldAndLevelFromLevelLoaderAndDestroyIt()
|
@assignWorldAndLevelFromLevelLoaderAndDestroyIt()
|
||||||
@setupGod()
|
@setupGod()
|
||||||
try
|
try
|
||||||
|
@ -57,6 +58,7 @@ module.exports = class Simulator extends CocoClass
|
||||||
catch err
|
catch err
|
||||||
console.log err
|
console.log err
|
||||||
@handleSingleSimulationError()
|
@handleSingleSimulationError()
|
||||||
|
|
||||||
commenceSingleSimulation: ->
|
commenceSingleSimulation: ->
|
||||||
@god.createWorld @generateSpellsObject()
|
@god.createWorld @generateSpellsObject()
|
||||||
Backbone.Mediator.subscribeOnce 'god:infinite-loop', @handleSingleSimulationInfiniteLoop, @
|
Backbone.Mediator.subscribeOnce 'god:infinite-loop', @handleSingleSimulationInfiniteLoop, @
|
||||||
|
@ -89,10 +91,22 @@ module.exports = class Simulator extends CocoClass
|
||||||
else if ogreSessionRank < humanSessionRank
|
else if ogreSessionRank < humanSessionRank
|
||||||
console.log "GAMERESULT:ogres"
|
console.log "GAMERESULT:ogres"
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
|
else
|
||||||
|
@sendSingleGameBackToServer(taskResults)
|
||||||
|
|
||||||
@cleanupSimulation()
|
@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: =>
|
fetchAndSimulateTask: =>
|
||||||
|
@ -120,12 +134,13 @@ module.exports = class Simulator extends CocoClass
|
||||||
console.error "There was a horrible Error: #{JSON.stringify errorData}"
|
console.error "There was a horrible Error: #{JSON.stringify errorData}"
|
||||||
@trigger 'statusUpdate', 'There was an error fetching games to simulate. Retrying in 10 seconds.'
|
@trigger 'statusUpdate', 'There was an error fetching games to simulate. Retrying in 10 seconds.'
|
||||||
@simulateAnotherTaskAfterDelay()
|
@simulateAnotherTaskAfterDelay()
|
||||||
|
|
||||||
|
|
||||||
handleNoGamesResponse: ->
|
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
|
console.log info
|
||||||
@trigger 'statusUpdate', info
|
@trigger 'statusUpdate', info
|
||||||
@simulateAnotherTaskAfterDelay()
|
@fetchAndSimulateOneGame()
|
||||||
application.tracker?.trackEvent 'Simulator Result', label: "No Games"
|
application.tracker?.trackEvent 'Simulator Result', label: "No Games"
|
||||||
|
|
||||||
simulateAnotherTaskAfterDelay: =>
|
simulateAnotherTaskAfterDelay: =>
|
||||||
|
|
|
@ -106,25 +106,85 @@ module.exports.getTwoGames = (req, res) ->
|
||||||
|
|
||||||
unless ogresGameID and humansGameID
|
unless ogresGameID and humansGameID
|
||||||
#fetch random games here
|
#fetch random games here
|
||||||
return errors.badInput(res, "You need to supply two games(for now)")
|
queryParams =
|
||||||
LevelSession.findOne(_id: humansGameID).lean().exec (err, humanSession) =>
|
"levelID":"greed"
|
||||||
if err? then return errors.serverError(res, "Couldn't find the human game")
|
"submitted":true
|
||||||
LevelSession.findOne(_id: ogresGameID).lean().exec (err, ogreSession) =>
|
"team":"humans"
|
||||||
if err? then return errors.serverError(res, "Couldn't find the ogre game")
|
selection = "team totalScore transpiledCode teamSpells levelID creatorName creator"
|
||||||
taskObject =
|
LevelSession.count queryParams, (err, numberOfHumans) =>
|
||||||
"messageGenerated": Date.now()
|
query = LevelSession
|
||||||
"sessions": []
|
.find(queryParams)
|
||||||
for session in [humanSession, ogreSession]
|
.limit(1)
|
||||||
sessionInformation =
|
.select(selection)
|
||||||
"sessionID": session._id
|
.skip(Math.floor(Math.random()*numberOfHumans))
|
||||||
"team": session.team ? "No team"
|
.lean()
|
||||||
"transpiledCode": session.transpiledCode
|
query.exec (err, randomSession) =>
|
||||||
"teamSpells": session.teamSpells ? {}
|
if err? then return errors.serverError(res, "Couldn't select a top 15 random session!")
|
||||||
"levelID": session.levelID
|
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
|
async.waterfall [
|
||||||
sendResponseObject req, res, taskObject
|
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) ->
|
module.exports.createNewTask = (req, res) ->
|
||||||
requestSessionID = req.body.session
|
requestSessionID = req.body.session
|
||||||
originalLevelID = req.body.originalLevelID
|
originalLevelID = req.body.originalLevelID
|
||||||
|
|
|
@ -21,6 +21,10 @@ module.exports.setup = (app) ->
|
||||||
app.post '/queue/scoring/getTwoGames', (req, res) ->
|
app.post '/queue/scoring/getTwoGames', (req, res) ->
|
||||||
handler = loadQueueHandler 'scoring'
|
handler = loadQueueHandler 'scoring'
|
||||||
handler.getTwoGames req, res
|
handler.getTwoGames req, res
|
||||||
|
|
||||||
|
app.put '/queue/scoring/recordTwoGames', (req, res) ->
|
||||||
|
handler = loadQueueHandler 'scoring'
|
||||||
|
handler.recordTwoGames req, res
|
||||||
|
|
||||||
app.all '/queue/*', (req, res) ->
|
app.all '/queue/*', (req, res) ->
|
||||||
setResponseHeaderToJSONContentType res
|
setResponseHeaderToJSONContentType res
|
||||||
|
|
Loading…
Reference in a new issue