mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-02-17 08:50:58 -05:00
Improved interface for simulating games
This commit is contained in:
parent
f85a55c8b7
commit
e593fd23b4
6 changed files with 50 additions and 16 deletions
|
@ -6,10 +6,13 @@ God = require 'lib/God'
|
|||
module.exports = class Simulator
|
||||
|
||||
constructor: ->
|
||||
_.extend @, Backbone.Events
|
||||
@trigger 'statusUpdate', 'Starting simulation!'
|
||||
@retryDelayInSeconds = 10
|
||||
@taskURL = '/queue/scoring'
|
||||
|
||||
fetchAndSimulateTask: =>
|
||||
@trigger 'statusUpdate', 'Fetching simulation data!'
|
||||
$.ajax
|
||||
url: @taskURL
|
||||
type: "GET"
|
||||
|
@ -19,6 +22,7 @@ module.exports = class Simulator
|
|||
handleFetchTaskError: (errorData) =>
|
||||
console.log "There were no games to score. Error: #{JSON.stringify errorData}"
|
||||
console.log "Retrying in #{@retryDelayInSeconds}"
|
||||
@trigger 'statusUpdate', 'There were no games to simulate! Trying again in 10 seconds.'
|
||||
|
||||
@simulateAnotherTaskAfterDelay()
|
||||
|
||||
|
@ -27,6 +31,7 @@ module.exports = class Simulator
|
|||
_.delay @fetchAndSimulateTask, retryDelayInMilliseconds
|
||||
|
||||
setupSimulationAndLoadLevel: (taskData) =>
|
||||
@trigger 'statusUpdate', 'Setting up simulation!'
|
||||
@task = new SimulationTask(taskData)
|
||||
@supermodel = new SuperModel()
|
||||
@god = new God maxWorkerPoolSize: 1, maxAngels: 1 # Start loading worker.
|
||||
|
@ -35,6 +40,7 @@ module.exports = class Simulator
|
|||
@levelLoader.once 'loaded-all', @simulateGame
|
||||
|
||||
simulateGame: =>
|
||||
@trigger 'statusUpdate', 'All resources loaded, simulating!', @task.getSessions()
|
||||
@assignWorldAndLevelFromLevelLoaderAndDestroyIt()
|
||||
@setupGod()
|
||||
|
||||
|
@ -70,6 +76,7 @@ module.exports = class Simulator
|
|||
@sendResultsBackToServer taskResults
|
||||
|
||||
sendResultsBackToServer: (results) =>
|
||||
@trigger 'statusUpdate', 'Simulation completed, sending results back to server!'
|
||||
console.log "Sending result back to server!"
|
||||
$.ajax
|
||||
url: "/queue/scoring"
|
||||
|
@ -79,10 +86,12 @@ module.exports = class Simulator
|
|||
error: @handleTaskResultsTransferError
|
||||
complete: @cleanupAndSimulateAnotherTask
|
||||
|
||||
handleTaskResultsTransferSuccess: (result) ->
|
||||
handleTaskResultsTransferSuccess: (result) =>
|
||||
console.log "Task registration result: #{JSON.stringify result}"
|
||||
@trigger 'statusUpdate', 'Results were successfully sent back to server!'
|
||||
|
||||
handleTaskResultsTransferError: (error) ->
|
||||
handleTaskResultsTransferError: (error) =>
|
||||
@trigger 'statusUpdate', 'There was an error sending the results back to the server.'
|
||||
console.log "Task registration error: #{JSON.stringify error}"
|
||||
|
||||
cleanupAndSimulateAnotherTask: =>
|
||||
|
@ -205,7 +214,7 @@ module.exports = class Simulator
|
|||
|
||||
class SimulationTask
|
||||
constructor: (@rawData) ->
|
||||
console.log 'Simulating sessions', (session.id for session in @getSessions())
|
||||
console.log 'Simulating sessions', (session for session in @getSessions())
|
||||
|
||||
getLevelName: ->
|
||||
levelName = @rawData.sessions?[0]?.levelID
|
||||
|
|
|
@ -25,6 +25,4 @@ block content
|
|||
div.homepage_button
|
||||
a#beginner-campaign(href="/play/level/rescue-mission")
|
||||
canvas(width="125", height="150")
|
||||
button(data-i18n="home.play").btn.btn-warning.btn-lg.highlight Play
|
||||
if me.isAdmin()
|
||||
button.btn.btn-warning.btn-lg.highlight#simulate-button SIMULATE
|
||||
button(data-i18n="home.play").btn.btn-warning.btn-lg.highlight Play
|
|
@ -29,6 +29,13 @@ block content
|
|||
if(!myRow)
|
||||
a(href="/play/level/#{level.get('slug') || level.id}/?team=#{team.otherTeam}&opponent=#{session.id}") COMPETE
|
||||
|
||||
if me.isAdmin()
|
||||
unless me.attributes.anonymous
|
||||
hr
|
||||
button.btn.btn-warning.btn-lg.highlight#simulate-button(style="margin-bottom:10px") Simulate all games
|
||||
button.btn.btn-warning.btn-lg.highlight#simulate-button(style="margin-bottom:10px;") Simulate Games!
|
||||
p(style="display:inline; margin-left:10px;")
|
||||
if simulationStatus
|
||||
| #{simulationStatus}
|
||||
else
|
||||
| By simulating games you can get your game ranked faster!
|
||||
if me.isAdmin()
|
||||
button.btn.btn-warning.btn-lg.highlight#simulate-all-button(style="margin-bottom:10px; float: right;") RESET AND SIMULATE GAMES
|
|
@ -11,7 +11,6 @@ module.exports = class HomeView extends View
|
|||
events:
|
||||
'mouseover #beginner-campaign': 'onMouseOverButton'
|
||||
'mouseout #beginner-campaign': 'onMouseOutButton'
|
||||
'click #simulate-button': 'onSimulateButtonClick'
|
||||
|
||||
getRenderData: ->
|
||||
c = super()
|
||||
|
@ -100,7 +99,3 @@ module.exports = class HomeView extends View
|
|||
destroy: ->
|
||||
@wizardSprite?.destroy()
|
||||
super()
|
||||
|
||||
onSimulateButtonClick: (e) =>
|
||||
simulator = new Simulator()
|
||||
simulator.fetchAndSimulateTask()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
RootView = require 'views/kinds/RootView'
|
||||
Level = require 'models/Level'
|
||||
Simulator = require 'lib/simulator/Simulator'
|
||||
LevelSession = require 'models/LevelSession'
|
||||
CocoCollection = require 'models/CocoCollection'
|
||||
LeaderboardCollection = require 'collections/LeaderboardCollection'
|
||||
|
@ -22,8 +23,9 @@ module.exports = class LadderView extends RootView
|
|||
|
||||
events:
|
||||
'click #simulate-button': 'onSimulateButtonClick'
|
||||
'click #simulate-all-button': 'onSimulateAllButtonClick'
|
||||
|
||||
onSimulateButtonClick: (e) ->
|
||||
onSimulateAllButtonClick: (e) ->
|
||||
submitIDs = _.pluck @leaderboards[@teams[0]].topPlayers.models, "id"
|
||||
for ID in submitIDs
|
||||
$.ajax
|
||||
|
@ -34,12 +36,33 @@ module.exports = class LadderView extends RootView
|
|||
alert "Simulating all games!"
|
||||
alert "(do not push more than once pls)"
|
||||
|
||||
|
||||
onSimulateButtonClick: (e) ->
|
||||
@simulator.fetchAndSimulateTask()
|
||||
|
||||
updateSimulationStatus: (simulationStatus, sessions)->
|
||||
@simulationStatus = simulationStatus
|
||||
try
|
||||
if sessions?
|
||||
#TODO: Fetch names from Redis, the creatorName is denormalized
|
||||
creatorNames = (session.creatorName for session in sessions)
|
||||
@simulationStatus = "Simulating game between "
|
||||
for index in [0...creatorNames.length]
|
||||
unless creatorNames[index]
|
||||
creatorNames[index] = "Anonymous"
|
||||
@simulationStatus += " and " + creatorNames[index]
|
||||
@simulationStatus += "..."
|
||||
catch e
|
||||
console.log "There was a problem with the named simulation status: #{e}"
|
||||
@render()
|
||||
|
||||
|
||||
constructor: (options, @levelID) ->
|
||||
super(options)
|
||||
@level = new Level(_id:@levelID)
|
||||
@level.fetch()
|
||||
@level.once 'sync', @onLevelLoaded, @
|
||||
@simulator = new Simulator()
|
||||
@simulator.on 'statusUpdate', @updateSimulationStatus, @
|
||||
|
||||
# @sessions = new LevelSessionsCollection(levelID)
|
||||
# @sessions.fetch({})
|
||||
|
@ -86,6 +109,7 @@ module.exports = class LadderView extends RootView
|
|||
description = @level.get('description')
|
||||
ctx.description = if description then marked(description) else ''
|
||||
ctx.link = "/play/level/#{@level.get('name')}"
|
||||
ctx.simulationStatus = @simulationStatus
|
||||
ctx.teams = []
|
||||
ctx.levelID = @levelID
|
||||
for team in @teams or []
|
||||
|
|
|
@ -12,7 +12,7 @@ TaskLog = require './task/ScoringTask'
|
|||
bayes = new (require 'bayesian-battle')()
|
||||
|
||||
scoringTaskQueue = undefined
|
||||
scoringTaskTimeoutInSeconds = 400
|
||||
scoringTaskTimeoutInSeconds = 120
|
||||
|
||||
|
||||
module.exports.setup = (app) -> connectToScoringQueue()
|
||||
|
@ -200,6 +200,7 @@ constructTaskObject = (taskMessageBody, callback) ->
|
|||
"teamSpells": session.teamSpells ? {}
|
||||
"levelID": session.levelID
|
||||
"creator": session.creator
|
||||
"creatorName":session.creatorName
|
||||
|
||||
taskObject.sessions.push sessionInformation
|
||||
callback err, taskObject
|
||||
|
|
Loading…
Reference in a new issue