Less match finish calculation

This commit is contained in:
adrienmalin
2018-12-05 08:55:37 +01:00
parent bfea110ecc
commit d65944be2e
2 changed files with 12 additions and 15 deletions

View File

@ -147,14 +147,14 @@ class MatchActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
if (ttsEnabled) ttsSpeak() if (ttsEnabled) ttsSpeak()
if (matchFinished()) endMatch() if (matchFinished) endMatch()
else if (sttEnabled and !ttsEnabled) launchStt() else if (sttEnabled and !ttsEnabled) launchStt()
} }
} }
fun ttsSpeak() { fun ttsSpeak() {
matchModel?.apply { matchModel?.apply {
if (matchFinished()) { if (matchFinished) {
val (loser, winner) = players.sortedBy { it.score } val (loser, winner) = players.sortedBy { it.score }
tts?.speak( tts?.speak(
getString( getString(
@ -177,7 +177,7 @@ class MatchActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
TextToSpeech.QUEUE_FLUSH, TextToSpeech.QUEUE_FLUSH,
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "MessageId") hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "MessageId")
) )
if (matchPoint()) { if (matchPoint) {
tts?.speak( tts?.speak(
getString(R.string.match_point), getString(R.string.match_point),
TextToSpeech.QUEUE_ADD, TextToSpeech.QUEUE_ADD,
@ -190,7 +190,7 @@ class MatchActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
fun launchStt() { fun launchStt() {
matchModel?.apply { matchModel?.apply {
if (sttEnabled) { if (sttEnabled and !matchFinished) {
try { try {
startActivityForResult( startActivityForResult(
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply { Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
@ -268,7 +268,7 @@ class MatchActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
fun updateScore(view: View) { fun updateScore(view: View) {
matchModel?.apply { matchModel?.apply {
if (!matchFinished()) { if (!matchFinished) {
for (side in 0..1) { for (side in 0..1) {
if (view == buttons[side]) { if (view == buttons[side]) {
updateScore(players[side]) updateScore(players[side])

View File

@ -5,6 +5,8 @@ import android.arch.lifecycle.ViewModel
class MatchModel : ViewModel() { class MatchModel : ViewModel() {
var matchStarted: Boolean = false var matchStarted: Boolean = false
var matchFinished: Boolean = false
var matchPoint: Boolean = false
var players: List<Player> = emptyList() var players: List<Player> = emptyList()
var serviceSide: Int = 0 var serviceSide: Int = 0
var relaunchSide: Int = 1 var relaunchSide: Int = 1
@ -32,19 +34,14 @@ class MatchModel : ViewModel() {
if ((players.sumBy { it.score } % 2 == 0) or (players.all { it.score >= 10 })) { if ((players.sumBy { it.score } % 2 == 0) or (players.all { it.score >= 10 })) {
serviceSide = relaunchSide.also { relaunchSide = serviceSide } serviceSide = relaunchSide.also { relaunchSide = serviceSide }
} }
val (minScore, maxScore) = players.map { it.score }.sorted()
matchFinished = (maxScore >= 11) and (maxScore - minScore >= 2)
matchPoint = (maxScore >= 10) and (maxScore - minScore >= 1)
saveState() saveState()
} }
fun matchFinished(): Boolean {
val (minScore, maxScore) = players.map { it.score }.sorted()
return (maxScore >= 11) and (maxScore - minScore >= 2)
}
fun matchPoint(): Boolean {
val (minScore, maxScore) = players.map { it.score }.sorted()
return (maxScore >= 10) and (maxScore - minScore >= 1)
}
fun saveState() { fun saveState() {
val play = Point(players.map { it.score }, serviceSide) val play = Point(players.map { it.score }, serviceSide)
if (playId == history.size) { if (playId == history.size) {