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

View File

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