Match point + previous matches in table
This commit is contained in:
@ -133,7 +133,7 @@ class MatchActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
if (it.ttsEnabled) {
|
||||
if (it.matchFinished) {
|
||||
if (it.matchFinished()) {
|
||||
val (loser, winner) = it.players.sortedBy { player -> player.score }
|
||||
tts?.speak(
|
||||
getString(
|
||||
@ -156,16 +156,23 @@ class MatchActivity : AppCompatActivity() {
|
||||
TextToSpeech.QUEUE_FLUSH,
|
||||
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "MessageId")
|
||||
)
|
||||
if (it.matchPoint()) {
|
||||
tts?.speak(
|
||||
getString(R.string.match_point),
|
||||
TextToSpeech.QUEUE_ADD,
|
||||
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "MessageId")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (it.matchFinished) endMatch()
|
||||
if (it.matchFinished()) endMatch()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateScore(view: View) {
|
||||
matchModel?.apply {
|
||||
if (!matchFinished) {
|
||||
if (!matchFinished()) {
|
||||
for (side in 0..1) {
|
||||
if (view == buttons[side]) {
|
||||
updateScore(side)
|
||||
|
@ -1,20 +1,17 @@
|
||||
package adrienmalin.pingpoints
|
||||
|
||||
import android.arch.lifecycle.LiveData
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.arch.lifecycle.ViewModel
|
||||
|
||||
|
||||
class MatchModel : ViewModel() {
|
||||
var matchStarted: Boolean = false
|
||||
var matchFinished: Boolean = false
|
||||
var players: List<Player> = emptyList()
|
||||
var serviceSide: Int = 0
|
||||
var relaunchSide: Int = 1
|
||||
var ttsEnabled: Boolean = false
|
||||
var sttEnabled: Boolean = false
|
||||
var playId: Int = 0
|
||||
var history: MutableList<Play> = ArrayList()
|
||||
var history: MutableList<Point> = ArrayList()
|
||||
|
||||
fun startMatch(player1Name: String, player2Name:String, starterId: Int, enableTTS: Boolean, enableSTT: Boolean) {
|
||||
matchStarted = true
|
||||
@ -36,14 +33,20 @@ class MatchModel : ViewModel() {
|
||||
serviceSide = relaunchSide.also { relaunchSide = serviceSide }
|
||||
}
|
||||
saveState()
|
||||
}
|
||||
|
||||
// Is match finished?
|
||||
fun matchFinished(): Boolean {
|
||||
val (minScore, maxScore) = players.map { it.score }.sorted()
|
||||
if ((maxScore >= 11) and (maxScore - minScore >= 2)) matchFinished = true
|
||||
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 = Play(players.map { it.score }, serviceSide)
|
||||
val play = Point(players.map { it.score }, serviceSide)
|
||||
if (playId == history.size) {
|
||||
history.add(play)
|
||||
} else {
|
||||
@ -53,7 +56,6 @@ class MatchModel : ViewModel() {
|
||||
}
|
||||
|
||||
fun undo() {
|
||||
matchFinished = false
|
||||
playId--
|
||||
reloadState()
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package adrienmalin.pingpoints
|
||||
|
||||
data class Play (
|
||||
data class Point (
|
||||
val score: List<Int>,
|
||||
val serviceSide: Int
|
||||
)
|
@ -192,7 +192,6 @@ class StarterNameActivity : AppCompatActivity() {
|
||||
putExtra(
|
||||
"starterId",
|
||||
when(radioStarterId) {
|
||||
R.id.radioPlayer1Starts -> 0
|
||||
R.id.radioPlayer2Starts -> 1
|
||||
else -> 0
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import android.os.Bundle
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.support.v7.app.AppCompatDelegate
|
||||
import android.view.View
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.GridView
|
||||
import android.widget.TextView
|
||||
|
||||
|
||||
@ -38,7 +40,7 @@ class VictoryActivity : AppCompatActivity() {
|
||||
putString(
|
||||
"previousMatches",
|
||||
getString(
|
||||
R.string.score_names,
|
||||
R.string.result,
|
||||
it.player1Name,
|
||||
it.score,
|
||||
it.player2Name
|
||||
@ -50,8 +52,16 @@ class VictoryActivity : AppCompatActivity() {
|
||||
|
||||
// UpdateUI
|
||||
findViewById<TextView>(R.id.congrats).text = getString(R.string.congrats, it.winnerName)
|
||||
findViewById<TextView>(R.id.scoreNames).text = getString(R.string.score_names, it.player1Name, it.score, it.player2Name)
|
||||
findViewById<TextView>(R.id.previousMatches).text = it.previousMatches
|
||||
findViewById<GridView>(R.id.resultGrid).adapter = ArrayAdapter<String>(
|
||||
this,
|
||||
android.R.layout.simple_list_item_1,
|
||||
arrayOf(it.player1Name, it.score, it.player2Name)
|
||||
)
|
||||
findViewById<GridView>(R.id.previousMatchesGrid).adapter = ArrayAdapter<String>(
|
||||
this,
|
||||
android.R.layout.simple_list_item_1,
|
||||
it.previousMatches.split("\t|\n".toRegex())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user