mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-02-17 08:50:58 -05:00
Fixed various bugs in simulator and put in home view
This commit is contained in:
parent
643cafc89f
commit
63a5769bcf
2 changed files with 20 additions and 203 deletions
|
@ -1,6 +1,7 @@
|
|||
SuperModel = require 'models/SuperModel'
|
||||
LevelLoader = require 'lib/LevelLoader'
|
||||
GoalManager = require 'lib/world/GoalManager'
|
||||
God = require 'lib/God'
|
||||
|
||||
module.exports = class Simulator
|
||||
|
||||
|
@ -27,10 +28,10 @@ module.exports = class Simulator
|
|||
|
||||
setupSimulationAndLoadLevel: (taskData) =>
|
||||
@task = new SimulationTask(taskData)
|
||||
@superModel = new SuperModel()
|
||||
@supermodel = new SuperModel()
|
||||
@god = new God()
|
||||
|
||||
@levelLoader = new LevelLoader @task.getLevelName(), @superModel, @task.getFirstSessionID()
|
||||
@levelLoader = new LevelLoader @task.getLevelName(), @supermodel, @task.getFirstSessionID()
|
||||
@levelLoader.once 'loaded-all', @simulateGame
|
||||
|
||||
simulateGame: =>
|
||||
|
@ -50,7 +51,7 @@ module.exports = class Simulator
|
|||
|
||||
setupGod: ->
|
||||
@god.level = @level.serialize @supermodel
|
||||
@god.worldClassMap = world.classMap
|
||||
@god.worldClassMap = @world.classMap
|
||||
@setupGoalManager()
|
||||
@setupGodSpells()
|
||||
|
||||
|
@ -65,7 +66,7 @@ module.exports = class Simulator
|
|||
|
||||
processResults: (simulationResults) ->
|
||||
taskResults = @formTaskResultsObject simulationResults
|
||||
sendResultsBackToServer taskResults
|
||||
@sendResultsBackToServer taskResults
|
||||
|
||||
sendResultsBackToServer: (results) =>
|
||||
$.ajax
|
||||
|
@ -95,40 +96,33 @@ module.exports = class Simulator
|
|||
calculationTime: 500
|
||||
sessions: []
|
||||
|
||||
for session in @task.sessions
|
||||
for session in @task.getSessions()
|
||||
sessionResult =
|
||||
sessionID: session.sessionID
|
||||
sessionChangedTime: session.sessionChangedTime
|
||||
metrics:
|
||||
rank: @calculateSessionRank session.sessionID, simulationResults.goalStates
|
||||
rank: @calculateSessionRank session.sessionID, simulationResults.goalStates, @task.generateTeamToSessionMap()
|
||||
|
||||
taskResults.sessions.push sessionResult
|
||||
|
||||
return taskResults
|
||||
|
||||
|
||||
calculateSessionRank: (sessionID, goalStates) ->
|
||||
calculateSessionRank: (sessionID, goalStates, teamSessionMap) ->
|
||||
humansDestroyed = goalStates["destroy-humans"].status is "success"
|
||||
ogresDestroyed = goalStates["destroy-ogres"].status is "success"
|
||||
console.log "Humans destroyed:#{humansDestroyed}"
|
||||
console.log "Ogres destroyed:#{ogresDestroyed}"
|
||||
console.log "Team Session Map: #{JSON.stringify @teamSessionMap}"
|
||||
if humansDestroyed is ogresDestroyed
|
||||
return 0
|
||||
else if humansDestroyed and @teamSessionMap["ogres"] is sessionID
|
||||
else if humansDestroyed and teamSessionMap["ogres"] is sessionID
|
||||
return 0
|
||||
else if humansDestroyed and @teamSessionMap["ogres"] isnt sessionID
|
||||
else if humansDestroyed and teamSessionMap["ogres"] isnt sessionID
|
||||
return 1
|
||||
else if ogresDestroyed and @teamSessionMap["humans"] is sessionID
|
||||
else if ogresDestroyed and teamSessionMap["humans"] is sessionID
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
|
||||
|
||||
|
||||
fetchGoalsFromWorldNoteChain: -> return @god.goalManager.world.scripts[0].noteChain[0].goals.add
|
||||
|
||||
|
||||
manuallyGenerateGoalStates: ->
|
||||
goalStates =
|
||||
"destroy-humans":
|
||||
|
@ -142,30 +136,25 @@ module.exports = class Simulator
|
|||
"Ogre Base": false
|
||||
status: "incomplete"
|
||||
|
||||
|
||||
|
||||
setupGodSpells: ->
|
||||
@generateSpellsObject()
|
||||
@god.spells = @spells
|
||||
|
||||
|
||||
generateSpellsObject: (currentUserCodeMap) ->
|
||||
@userCodeMap = currentUserCodeMap
|
||||
generateSpellsObject: ->
|
||||
@currentUserCodeMap = @task.generateSpellKeyToSourceMap()
|
||||
@spells = {}
|
||||
for thang in @level.attributes.thangs
|
||||
continue if @thangIsATemplate thang
|
||||
@generateSpellKeyToSourceMapPropertiesFromThang thang
|
||||
|
||||
|
||||
thangIsATemplate: (thang) ->
|
||||
for component in thang.components
|
||||
continue unless @componentHasProgrammableMethods component
|
||||
for methodName, method of component.config.programmableMethods
|
||||
return true if methodBelongsToTemplateThang method
|
||||
return true if @methodBelongsToTemplateThang method
|
||||
|
||||
return false
|
||||
|
||||
|
||||
componentHasProgrammableMethods: (component) -> component.config? and _.has component.config, 'programmableMethods'
|
||||
|
||||
methodBelongsToTemplateThang: (method) -> typeof method is 'string'
|
||||
|
@ -181,17 +170,14 @@ module.exports = class Simulator
|
|||
@createSpellThang thang, method, spellKey
|
||||
@transpileSpell thang, spellKey, methodName
|
||||
|
||||
|
||||
|
||||
|
||||
generateSpellKeyFromThangIDAndMethodName: (thang, methodName) ->
|
||||
spellKeyComponents = [thang.id, methodName]
|
||||
pathComponents[0] = _.string.slugify pathComponents[0]
|
||||
pathComponents.join '/'
|
||||
spellKeyComponents[0] = _.string.slugify spellKeyComponents[0]
|
||||
spellKeyComponents.join '/'
|
||||
|
||||
createSpellAndAssignName: (spellKey, spellName) ->
|
||||
@spells[spellKey] ?= {}
|
||||
@spells[spellKey].name = methodName
|
||||
@spells[spellKey].name = spellName
|
||||
|
||||
createSpellThang: (thang, method, spellKey) ->
|
||||
@spells[spellKey].thangs ?= {}
|
||||
|
@ -203,12 +189,6 @@ module.exports = class Simulator
|
|||
source = @currentUserCodeMap[slugifiedThangID]?[methodName] ? ""
|
||||
@spells[spellKey].thangs[thang.id].aether.transpile source
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
createAether: (methodName, method) ->
|
||||
aetherOptions =
|
||||
functionName: methodName
|
||||
|
@ -216,18 +196,9 @@ module.exports = class Simulator
|
|||
includeFlow: false
|
||||
return new Aether aetherOptions
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class SimulationTask
|
||||
constructor: (@rawData) ->
|
||||
|
||||
|
||||
getLevelName: ->
|
||||
levelName = @rawData.sessions?[0]?.levelID
|
||||
return levelName if levelName?
|
||||
|
|
|
@ -2,10 +2,7 @@ View = require 'views/kinds/RootView'
|
|||
template = require 'templates/home'
|
||||
WizardSprite = require 'lib/surface/WizardSprite'
|
||||
ThangType = require 'models/ThangType'
|
||||
LevelLoader = require 'lib/LevelLoader'
|
||||
God = require 'lib/God'
|
||||
|
||||
GoalManager = require 'lib/world/GoalManager'
|
||||
Simulator = require 'lib/simulator/Simulator'
|
||||
|
||||
module.exports = class HomeView extends View
|
||||
id: 'home-view'
|
||||
|
@ -105,156 +102,5 @@ module.exports = class HomeView extends View
|
|||
@wizardSprite?.destroy()
|
||||
|
||||
onSimulateButtonClick: (e) =>
|
||||
@alreadyPostedResults = false
|
||||
console.log "Simulating world!"
|
||||
$.ajax
|
||||
url: "/queue/scoring"
|
||||
type: "GET"
|
||||
error: (data) =>
|
||||
console.log "There are no games to score. Error: #{JSON.stringify data}"
|
||||
console.log "Retrying in ten seconds..."
|
||||
_.delay @onSimulateButtonClick, 10000
|
||||
success: (data) =>
|
||||
console.log data
|
||||
levelName = data.sessions[0].levelID
|
||||
#TODO: Refactor. So much refactor.
|
||||
@taskData = data
|
||||
@teamSessionMap = @generateTeamSessionMap data
|
||||
world = {}
|
||||
god = new God()
|
||||
levelLoader = new LevelLoader(levelName, @supermodel, data.sessions[0].sessionID)
|
||||
levelLoader.once 'loaded-all', =>
|
||||
world = levelLoader.world
|
||||
level = levelLoader.level
|
||||
levelLoader.destroy()
|
||||
god.level = level.serialize @supermodel
|
||||
god.worldClassMap = world.classMap
|
||||
god.goalManager = new GoalManager(world)
|
||||
#move goals in here
|
||||
goalsToAdd = god.goalManager.world.scripts[0].noteChain[0].goals.add
|
||||
god.goalManager.goals = goalsToAdd
|
||||
god.goalManager.goalStates =
|
||||
"destroy-humans":
|
||||
keyFrame: 0
|
||||
killed:
|
||||
"Human Base": false
|
||||
status: "incomplete"
|
||||
"destroy-ogres":
|
||||
keyFrame:0
|
||||
killed:
|
||||
"Ogre Base": false
|
||||
status: "incomplete"
|
||||
god.spells = @filterProgrammableComponents level.attributes.thangs, @generateSpellToSourceMap data.sessions
|
||||
god.createWorld()
|
||||
@god = god
|
||||
Backbone.Mediator.subscribeOnce 'god:new-world-created', @onWorldCreated, @
|
||||
|
||||
onWorldCreated: (data) ->
|
||||
return if @alreadyPostedResults
|
||||
taskResults = @translateGoalStatesIntoTaskResults data.goalStates
|
||||
@god?.destroy()
|
||||
|
||||
$.ajax
|
||||
url: "/queue/scoring"
|
||||
data: taskResults
|
||||
type: 'PUT'
|
||||
success: (result) =>
|
||||
console.log "TASK REGISTRATION RESULT:#{JSON.stringify result}"
|
||||
error: (error) =>
|
||||
console.log "TASK REGISTRATION ERROR:#{JSON.stringify error}"
|
||||
complete: (result) =>
|
||||
@alreadyPostedResults = true
|
||||
@onSimulateButtonClick()
|
||||
|
||||
|
||||
translateGoalStatesIntoTaskResults: (goalStates) =>
|
||||
taskResults = {}
|
||||
taskResults =
|
||||
taskID: @taskData.taskID
|
||||
receiptHandle: @taskData.receiptHandle
|
||||
calculationTime: 500
|
||||
sessions: []
|
||||
|
||||
for session in @taskData.sessions
|
||||
sessionResult =
|
||||
sessionID: session.sessionID
|
||||
sessionChangedTime: session.sessionChangedTime
|
||||
metrics:
|
||||
rank: @calculateSessionRank session.sessionID, goalStates
|
||||
taskResults.sessions.push sessionResult
|
||||
taskResults
|
||||
|
||||
calculateSessionRank: (sessionID, goalStates) ->
|
||||
humansDestroyed = goalStates["destroy-humans"].status is "success"
|
||||
ogresDestroyed = goalStates["destroy-ogres"].status is "success"
|
||||
console.log "Humans destroyed:#{humansDestroyed}"
|
||||
console.log "Ogres destroyed:#{ogresDestroyed}"
|
||||
console.log "Team Session Map: #{JSON.stringify @teamSessionMap}"
|
||||
if humansDestroyed is ogresDestroyed
|
||||
return 0
|
||||
else if humansDestroyed and @teamSessionMap["ogres"] is sessionID
|
||||
return 0
|
||||
else if humansDestroyed and @teamSessionMap["ogres"] isnt sessionID
|
||||
return 1
|
||||
else if ogresDestroyed and @teamSessionMap["humans"] is sessionID
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
|
||||
|
||||
generateTeamSessionMap: (task) ->
|
||||
teamSessionMap = {}
|
||||
for session in @taskData.sessions
|
||||
teamSessionMap[session.team] = session.sessionID
|
||||
teamSessionMap
|
||||
|
||||
|
||||
|
||||
filterProgrammableComponents: (thangs, spellToSourceMap) =>
|
||||
spells = {}
|
||||
for thang in thangs
|
||||
isTemplate = false
|
||||
for component in thang.components
|
||||
if component.config? and _.has component.config,'programmableMethods'
|
||||
for methodName, method of component.config.programmableMethods
|
||||
if typeof method is 'string'
|
||||
isTemplate = true
|
||||
break
|
||||
|
||||
pathComponents = [thang.id,methodName]
|
||||
pathComponents[0] = _.string.slugify pathComponents[0]
|
||||
spellKey = pathComponents.join '/'
|
||||
spells[spellKey] ?= {}
|
||||
spells[spellKey].thangs ?= {}
|
||||
spells[spellKey].name = methodName
|
||||
thangID = _.string.slugify thang.id
|
||||
spells[spellKey].thangs[thang.id] ?= {}
|
||||
spells[spellKey].thangs[thang.id].aether = @createAether methodName, method
|
||||
if spellToSourceMap[thangID]? then source = spellToSourceMap[thangID][methodName] else source = ""
|
||||
spells[spellKey].thangs[thang.id].aether.transpile source
|
||||
if isTemplate
|
||||
break
|
||||
|
||||
spells
|
||||
|
||||
createAether : (methodName, method) ->
|
||||
aetherOptions =
|
||||
functionName: methodName
|
||||
protectAPI: false
|
||||
includeFlow: false
|
||||
return new Aether aetherOptions
|
||||
|
||||
generateSpellToSourceMap: (sessions) ->
|
||||
spellKeyToSourceMap = {}
|
||||
spellSources = {}
|
||||
for session in sessions
|
||||
teamSpells = session.teamSpells[session.team]
|
||||
_.merge spellSources, _.pick(session.code, teamSpells)
|
||||
|
||||
#merge common ones, this overwrites until the last session
|
||||
commonSpells = session.teamSpells["common"]
|
||||
if commonSpells?
|
||||
_.merge spellSources, _.pick(session.code, commonSpells)
|
||||
|
||||
spellSources
|
||||
|
||||
simulator = new Simulator()
|
||||
simulator.fetchAndSimulateTask()
|
||||
|
|
Loading…
Reference in a new issue