Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
simpler way to handle repeated screen events
Browse files Browse the repository at this point in the history
seems to work very well and to fix coin and opponent name detection !
  • Loading branch information
tyrcho committed Oct 19, 2014
1 parent 824be99 commit 32d0d1f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CompanionState extends Logging {
var mode = GameMode.UNDETECTED
var deckSlot: Option[Int] = None
var rank: Option[Rank] = None
var findingOpponent = false
var isNewArenaRun = false
var iterationsSinceClassCheckingStarted = 0
var iterationsSinceYourTurn = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ class GameMonitor(
private def handleGameEvent(evt: GameEvent): Unit = try {
debug(evt)
evt match {
case s: ScreenEvent => handleScreenEvent(s)
case s: ScreenEvent =>
companionState.findingOpponent = false
handleScreenEvent(s)

case FindingOpponent =>
case FindingOpponent if !companionState.findingOpponent =>
companionState.findingOpponent = true
uiLog.info(s"Finding opponent, new match will start soon ...")
uiLog.divider()
matchState.nextMatch(companionState)
Expand All @@ -126,11 +129,14 @@ class GameMonitor(
matchState.started = true

case StartingHand(image) =>
companionState.findingOpponent = false
testForCoin(image)
testForOpponentName(image)
testForYourClass(image)
testForOpponentClass(image)
iterationsSinceClassCheckingStarted += 1

case _ =>
}
} catch {
case t: Throwable =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,28 @@ class ImageToEvent(
}
}

def canRepeatEvent(screen: Screen) =
(Seq(PLAY_LOBBY, PRACTICE_LOBBY, VERSUS_LOBBY) contains screen) ||
screen.group == ScreenGroup.MATCH_END

private def eventFromScreen(newScreen: Screen, image: BufferedImage): Option[GameEvent] =
if (lastScreen.isEmpty || lastScreen.get != newScreen || canRepeatEvent(newScreen)) {
if (lastScreen != Some(newScreen)) {
debug(s"Screen : $lastScreen => $newScreen")
private def eventFromScreen(newScreen: Screen, image: BufferedImage): Option[GameEvent] = {
if (newScreen == PLAY_LOBBY && imageShowsPlayBackground(image))
None
else if (lastScreen == FINDING_OPPONENT && iterationsSinceFindingOpponent < 5) {
iterationsSinceFindingOpponent += 1
None
} else {
iterationsSinceFindingOpponent = 0
val screenToEvent: PartialFunction[Screen, GameEvent] = _ match {
case s if (Seq(PLAY_LOBBY, ARENA_LOBBY, PRACTICE_LOBBY, VERSUS_LOBBY) contains s) ||
(s.group == ScreenGroup.MATCH_END) => ScreenEvent(s, image)
case ARENA_END => ArenaRunEnd
case FINDING_OPPONENT => FindingOpponent
case s if s.group == MATCH_START => StartingHand(image)
case s if s.group == MATCH_PLAYING => FirstTurn(image)
}
if (newScreen == PLAY_LOBBY && imageShowsPlayBackground(image))
None
else if (lastScreen == FINDING_OPPONENT && iterationsSinceFindingOpponent < 5) {
iterationsSinceFindingOpponent += 1
None
} else {
iterationsSinceFindingOpponent = 0
val screenToEvent: PartialFunction[Screen, GameEvent] = _ match {
case s if (Seq(PLAY_LOBBY, ARENA_LOBBY, PRACTICE_LOBBY, VERSUS_LOBBY) contains s) ||
(s.group == ScreenGroup.MATCH_END) => ScreenEvent(s, image)
case ARENA_END => ArenaRunEnd
case FINDING_OPPONENT => FindingOpponent
case s if s.group == MATCH_START => StartingHand(image)
case s if s.group == MATCH_PLAYING => FirstTurn(image)
}
if (screenToEvent.isDefinedAt(newScreen) || newScreen == FINDING_OPPONENT) {
lastScreen = Some(newScreen)
}
screenToEvent.lift(newScreen)
if (screenToEvent.isDefinedAt(newScreen)) {
lastScreen = Some(newScreen)
}
} else None
screenToEvent.lift(newScreen)
}
}

/**
* <p>Sometimes the OS X version captures a screenshot where, apparently, Hearthstone hasn't finished compositing the screen
Expand Down

0 comments on commit 32d0d1f

Please sign in to comment.