Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #68 from bilts/performance

Turn slow mode into fast mode
  • Loading branch information...
commit a99d12aa8efc4018e0a41c47f3baf17538a922f7 2 parents e6f3456 + b51bf28
@rspeer authored
View
8 basicAI.coffee
@@ -82,13 +82,15 @@ class BasicAI
choiceSet = {}
for choice in choices
choiceSet[choice] = choice
+
+ nullable = null in choices
# Get the priority list.
- priority = priorityfunc.bind(this)(state, my)
+ priority = priorityfunc.call(this, state, my)
# Now look up all the preferences in that list. The moment we encounter
# a valid choice, we can return it.
for preference in priority
- if preference is null and null in choices
+ if preference is null and nullable
return null
if choiceSet[preference]?
return choiceSet[preference]
@@ -103,7 +105,7 @@ class BasicAI
if (choice is null) or (choice is no)
value = 0
else
- value = valuefunc.bind(this)(state, choice, my)
+ value = valuefunc.call(this, state, choice, my)
if value > bestValue
bestValue = value
bestChoice = choice
View
27 playWeb.coffee
@@ -24,10 +24,6 @@ makeStrategy = (changes) ->
ai[key] = value
ai
-# Setting `fast` to true will takesome shortcuts to play the game
-# really quickly. These include
-# producing no output, and not returning control to the browser between
-# game steps.
playGame = (strategies, options, ret) ->
ais = (makeStrategy(item) for item in strategies)
@@ -38,30 +34,7 @@ playGame = (strategies, options, ret) ->
state = new State().setUpWithOptions(ais, options)
ret ?= options.log
- if options.fast
- options.log = () ->
- playFast(state, options, ret)
- else
- window.setZeroTimeout -> playStep(state, options, ret)
-playStep = (state, options, ret) ->
- if state.gameIsOver()
- ret(state)
- else
- try
- state.doPlay()
- if state.phase == 'buy' and (not state.extraturn) and options.grapher?
- options.grapher.recordMoney(state.current.ai.name, state.current.turnsTaken, state.current.coins)
- if state.phase == 'cleanup' and (not state.extraturn) and options.grapher?
- options.grapher.recordVP(state.current.ai.name, state.current.turnsTaken, state.current.getVP(state))
- window.setZeroTimeout -> playStep(state, options, ret)
- catch err
- errorHandler = options.errorHandler ? (alert ? console.log)
- errorHandler(err.message)
- window.donePlaying()
- throw err
-
-playFast = (state, options, ret) ->
until state.gameIsOver()
try
state.doPlay()
View
16 web/multiLog.coffee
@@ -21,6 +21,12 @@ class MultiLog
getCurrent: ->
@pages[@currentPage-1]
+ getCurrentPage: ->
+ @currentPage
+
+ getLastPage: ->
+ @pages.length
+
updateOutput: ->
if @pages[@currentPage-1]?
@outputElt.html(@pages[@currentPage-1])
@@ -39,10 +45,16 @@ class MultiLog
"next page"
updatePaginator: ->
+ # Avoid updating the DOM if the new pages are the same as the old
+ pages = @pagesShown()
+ oldPages = @pagesRendered
+ if oldPages? and oldPages.length == pages.length and pages[0] == oldPages[0]
+ return if @currentPage == @renderedPage
+
prev = "<li class='#{this.prevButtonClass()}'><a href='#'>&larr; Previous</a></li>"
next = "<li class='#{this.nextButtonClass()}'><a href='#'>Next &rarr;</a></li>"
items = [prev]
- for pageNum in this.pagesShown()
+ for pageNum in pages
if pageNum == @currentPage
item = "<li class='active page'><a href='#'>#{pageNum}</a></li>"
else
@@ -51,6 +63,8 @@ class MultiLog
items.push(next)
@paginatorElt.html('<ul>' + items.join('') + '</ul>')
this.updateEvents()
+ @pagesRendered = pages
+ @renderedPage = @currentPage
updateEvents: ->
$('.page').click (event) =>
View
46 web/play.html
@@ -91,13 +91,6 @@ <h2 class="tagline">A simulator for Dominion strategies.</h2>
<span>Include Colony and Platinum</span>
</label>
</li>
- <li>
- <label>
- <input type="checkbox" name="optionsCheckboxes" value="fast"
- id="fast" />
- <span>Play fast (without much output)</span>
- </label>
- </li>
</ul>
<a id="playButton" href="#" class="btn success">
Start playing
@@ -203,22 +196,27 @@ <h2 class="tagline">A simulator for Dominion strategies.</h2>
singleGameResult = (state) =>
result = state.getWinners().toString() + " wins!"
multiLog.addLineToEnd(result)
- multiLog.updateOutput()
+ # Avoid refreshing the output if it's not visible
+ multiLog.updateOutput() if multiLog.getCurrentPage() == multiLog.getLastPage()
tracker.recordGame(state)
errorHandler = (error) => alert(error.message)
+ startTime = null
+
begunPlaying = ->
window.playing = true
$('#playButton').removeClass('success').addClass('danger')
$('#playButton').text('Stop playing')
+ startTime = new Date()
- donePlaying = ->
+ donePlaying = (count) ->
window.playing = false
grapher.updateGraphs()
tracker.updateScoresOnPage()
$('#playButton').removeClass('danger').addClass('success')
$('#playButton').text('Start playing')
+ console?.log?("Played #{count} games in #{(new Date() - startTime) / 1000.0}s");
window.donePlaying = donePlaying
playOneGame = ->
@@ -231,7 +229,6 @@ <h2 class="tagline">A simulator for Dominion strategies.</h2>
log: log
colonies: $('#colonies').is(':checked')
randomizeOrder: $('#randomize').is(':checked')
- fast: $('#fast').is(':checked')
tracker: tracker
grapher: grapher
}
@@ -241,6 +238,7 @@ <h2 class="tagline">A simulator for Dominion strategies.</h2>
playUntil = (condition, count) ->
+ startTime = new Date();
scripts = [leftEd.getValue(), rightEd.getValue()]
compiled = compileStrategies(scripts, errorHandler)
return if not compiled
@@ -249,32 +247,30 @@ <h2 class="tagline">A simulator for Dominion strategies.</h2>
log: log
colonies: $('#colonies').is(':checked')
randomizeOrder: $('#randomize').is(':checked')
- fast: $('#fast').is(':checked')
tracker: tracker
grapher: grapher
}
playCount = 0
begunPlaying()
+ timeControlStarted = new Date()
playLoop = (result) ->
singleGameResult(result)
if condition() or (playCount >= count) or (not window.playing)
- donePlaying()
+ donePlaying(playCount)
else
- if (not options.fast) then multiLog.addPageQuietly('')
- nextIteration = ->
- playCount++
- playGame(compiled, options, playLoop)
- if (not options.fast) and (playCount % 10 == 0)
- grapher.updateGraphs()
- if (not options.fast) or (playCount % 20 == 0)
- tracker.updateScoresOnPage()
- if (not options.fast) or (playCount % 10) == 0
- window.setZeroTimeout(nextIteration)
+ multiLog.addPageQuietly('')
+ msWithControl = new Date() - timeControlStarted
+ playCount++
+ if msWithControl > 250
+ tracker.updateScoresOnPage()
+ grapher.updateGraphs()
+ window.setZeroTimeout ->
+ timeControlStarted = new Date()
+ playGame(compiled, options, playLoop)
else
- # no time to stop and give control to the Web browser!
- nextIteration()
+ playGame(compiled, options, playLoop)
- if (not options.fast) then multiLog.addPageQuietly('')
+ multiLog.addPageQuietly('')
playGame(compiled, options, playLoop)
$('#playButton').click (event) ->
Please sign in to comment.
Something went wrong with that request. Please try again.