Skip to content

Commit

Permalink
#5: Fixing 9-Darter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Entreco committed Jan 9, 2018
1 parent 2bd3316 commit a289982
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ class TeamScoreViewModel(val team: Team, startScore: Score,
val score = ObservableField<Score>(startScore)
val currentTeam = ObservableBoolean()

private var nineDartPossible = true
private var isNineDarterStillPossible = true
private var finishFuture: Future<*>? = null

fun turnUpdate(next: Next) {
if(next.state == State.LEG || next.state == State.SET || next.state == State.START){
nineDartPossible = true
}
updateNineDarter(next)
updateLegStarter(next)
updateCurrentTeam(next)
}
Expand Down Expand Up @@ -67,10 +65,17 @@ class TeamScoreViewModel(val team: Team, startScore: Score,
}
}

private fun updateNineDarter(next: Next) {
if (next.state == State.LEG || next.state == State.SET || next.state == State.START) {
isNineDarterStillPossible = true
nineDarter.set(false)
}
}

override fun onNineDartEvent(event: NineDartEvent) {
if (team.contains(event.by())) {
nineDarter.set(event.isPossible() && nineDartPossible)
nineDartPossible = event.isPossible()
nineDarter.set(event.isPossible() && isNineDarterStillPossible)
isNineDarterStillPossible = event.isPossible()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.entreco.domain.play.listeners

import android.util.Log
import nl.entreco.domain.model.Next
import nl.entreco.domain.model.Score
import nl.entreco.domain.model.State
Expand All @@ -19,7 +20,8 @@ interface SpecialEventListener<in T : SpecialEvent> {
handleBust(next)
handleThrown(turn)

if(playing501(scores)) {
if (playing501(scores)) {
Log.w("COCO", "playing01 $by")
handleNineDarter(turn, scores, next, by)
}
}
Expand Down Expand Up @@ -55,12 +57,27 @@ interface SpecialEventListener<in T : SpecialEvent> {

private fun handleNineDarter(turn: Turn, scores: Array<Score>, next: Next, by: Player) {
try {
val event = NineDartEvent(turn.dartsLeft() == 0 && isPossibleNineDarter(turn.total(), scores[next.teamIndex].score), by) as T
val canItBePossible = canItBePossible(turn, scores, next)
val event = NineDartEvent(canItBePossible, by) as T
handle(event)
} catch (ignore: ClassCastException) {
}
}

private fun canItBePossible(turn: Turn, scores: Array<Score>, next: Next) : Boolean{
val score = previousTeamIndex(next.teamIndex, scores).score
val pnd = isPossibleNineDarter(turn.total(), score)
return turn.dartsLeft() == 0 && pnd
}

private fun previousTeamIndex(index: Int, scores: Array<Score>): Score {
var previousIndex = index - 1
if (previousIndex < 0) {
previousIndex = scores.size - 1
}
return scores[previousIndex]
}

private fun isPossibleNineDarter(scored: Int, score: Int): Boolean {
return when {
scored == 180 && score == 321 -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class NineDartEventListenerTest : SpecialEventListenerTest() {
}

fun onScored(turn: Turn, score: Int) {
val scores = arrayOf(Score(score), Score())
val scores = arrayOf(Score(score))
val team = Team(arrayOf(player))
val next = Next(State.NORMAL, team, 0, player, scores[0])
onSpecialEvent(next, turn, player, scores)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ import nl.entreco.domain.common.executors.TestBackground
import nl.entreco.domain.common.executors.TestForeground
import nl.entreco.domain.repository.GameRepository
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner

/**
* Created by entreco on 09/01/2018.
*/
@RunWith(MockitoJUnitRunner::class)
class MarkGameAsFinishedUsecaseTest {

@Mock private lateinit var mockGameRepository : GameRepository
@Mock private lateinit var mockGameRepository: GameRepository
private val bg = TestBackground()
private val fg = TestForeground()
private lateinit var subject : MarkGameAsFinishedUsecase
private lateinit var subject: MarkGameAsFinishedUsecase

private var givenId : Long = -1
private lateinit var givenMarkAsFinishedRequest : MarkGameAsFinishedRequest
private var givenId: Long = -1
private lateinit var givenMarkAsFinishedRequest: MarkGameAsFinishedRequest

@Test
fun `it should execute update`() {
Expand All @@ -32,7 +35,7 @@ class MarkGameAsFinishedUsecaseTest {
@Test
fun `it should not handle any errors`() {
givenSubject()
whenMarkingGameAsFinishedFails(Exception("oops, db corrupt"))
whenMarkingGameAsFinishedFails(RuntimeException("oops, db corrupt"))
thenGameIsMarkedAsFinished()
}

Expand Down

0 comments on commit a289982

Please sign in to comment.