Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

refactor score updater + rename buzzerGameActivity #374

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions app/src/androidTest/java/ch/sdp/vibester/BuzzerScoreUpdaterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BuzzerScoreUpdaterTest {
val scoreArray = arrayOf(0, 0, 0, 0)
val testUpdater = BuzzerScoreUpdater(idArray, scoreArray)
for (id in idArray) {
testUpdater.updateScoresArray(id, true)
testUpdater.updateScoresArray(id)
assertTrue(testUpdater.getMap()[id] == 1)
}
}
Expand All @@ -35,23 +35,12 @@ public class BuzzerScoreUpdaterTest {
val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
val scoreArray = arrayOf(0, 0, 0, 0)
val testUpdater = BuzzerScoreUpdater(idArray, scoreArray)
testUpdater.updateScoresArray(-1, true)
testUpdater.updateScoresArray(-1)
for (id in idArray) {
assertEquals(0, testUpdater.getMap()[id])
}
}

@Test
fun penaltyOnScoreZeroMakesTheScoreRemainZero() {
val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
val scoreArray = arrayOf(0, 0, 0, 0)
val testUpdater = BuzzerScoreUpdater(idArray, scoreArray)
for (id in idArray) {
testUpdater.updateScoresArray(id, false)
assertEquals(0, testUpdater.getMap()[id])
}
}

@Test
fun noWinnerTest() {
val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.activity.game.BuzzerScreenActivity
import ch.sdp.vibester.activity.game.BuzzerGameActivity
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.helper.GameManager
import dagger.hilt.android.testing.HiltAndroidRule
Expand Down Expand Up @@ -134,7 +134,7 @@ class BuzzerSetupActivityTest {
onData(Matchers.anything()).atPosition(0).perform(scrollTo(),click())
enterNames(1)
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(BuzzerScreenActivity::class.java.name))
intended(hasComponent(BuzzerGameActivity::class.java.name))
intended(hasExtra("Number of players", 1))
}

Expand All @@ -147,7 +147,7 @@ class BuzzerSetupActivityTest {
onData(Matchers.anything()).atPosition(1).perform(scrollTo(),click())
enterNames(2)
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(BuzzerScreenActivity::class.java.name))
intended(hasComponent(BuzzerGameActivity::class.java.name))
intended(hasExtra("Number of players", 2))
}

Expand All @@ -160,7 +160,7 @@ class BuzzerSetupActivityTest {
onData(Matchers.anything()).atPosition(2).perform(scrollTo(),click())
enterNames(3)
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(BuzzerScreenActivity::class.java.name))
intended(hasComponent(BuzzerGameActivity::class.java.name))
intended(hasExtra("Number of players", 3))
}

Expand All @@ -173,7 +173,7 @@ class BuzzerSetupActivityTest {
onData(Matchers.anything()).atPosition(3).perform(scrollTo(),click())
enterNames(4)
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(BuzzerScreenActivity::class.java.name))
intended(hasComponent(BuzzerGameActivity::class.java.name))
intended(hasExtra("Number of players", 4))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BuzzerScreenActivityTest {

@get:Rule(order = 1)
val testRule = ActivityScenarioRule(
BuzzerScreenActivity::class.java
BuzzerGameActivity::class.java
)

@Before
Expand Down Expand Up @@ -74,7 +74,7 @@ class BuzzerScreenActivityTest {
@Test
fun scoresLayoutIsDisplayed() {
val intent =
Intent(ApplicationProvider.getApplicationContext(), BuzzerScreenActivity::class.java)
Intent(ApplicationProvider.getApplicationContext(), BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -83,14 +83,14 @@ class BuzzerScreenActivityTest {
intent.putExtra("Number of players", mockPlayersNumber)
intent.putExtra("Player Names", mockNameArray)
intent.putExtra("gameManager", setGameManager())
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)
onView(withId(R.id.scoresTable)).check(matches(isDisplayed()))
}

@Test
fun answerAndNextArePresentButInvisibleOnStartup() {
val ctx = ApplicationProvider.getApplicationContext() as Context
val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -99,15 +99,15 @@ class BuzzerScreenActivityTest {
intent.putExtra("Number of players", mockPlayersNumber)
intent.putExtra("Player Names", mockNameArray)
intent.putExtra("gameManager", setGameManager())
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.INVISIBLE)))
onView(withId(R.id.nextSongBuzzer)).check(matches(withEffectiveVisibility(Visibility.GONE)))
}

@Test
fun testDisplayOfBuzzersWithNames() {
val ctx = ApplicationProvider.getApplicationContext() as Context
val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -117,7 +117,7 @@ class BuzzerScreenActivityTest {
intent.putExtra("Player Names", mockNameArray)
val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)
// Did the round start?

onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
Expand All @@ -130,7 +130,7 @@ class BuzzerScreenActivityTest {
fun pressingBuzzerDuringRoundMakesAnswerVisible() {
val ctx = ApplicationProvider.getApplicationContext() as Context

val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -140,7 +140,7 @@ class BuzzerScreenActivityTest {
intent.putExtra("Player Names", mockNameArray)
val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)

onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click())
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
Expand All @@ -153,7 +153,7 @@ class BuzzerScreenActivityTest {
AppPreferences.setStr(ctx.getString(R.string.preferences_game_mode), "local_buzzer")
AppPreferences.setStr(ctx.getString(R.string.preferences_game_genre), "BTS")

val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -165,7 +165,7 @@ class BuzzerScreenActivityTest {
gameManager.setNextSong()
intent.putExtra("gameManager", gameManager)

val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.timeoutAnswer(ctx, null, gameManager)
Assert.assertEquals(false, activity.testGetGameIsOn())
Expand All @@ -175,7 +175,7 @@ class BuzzerScreenActivityTest {
@Test
fun skipTest() {
val ctx = ApplicationProvider.getApplicationContext() as Context
val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -185,7 +185,7 @@ class BuzzerScreenActivityTest {

val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)

onView(withId(R.id.skip_buzzer)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click())
scn.onActivity { activity -> Assert.assertEquals(false, activity.testGetGameIsOn()) }
Expand All @@ -194,7 +194,7 @@ class BuzzerScreenActivityTest {
@Test
fun setAnswerButtonCorrectTest() {
val ctx = ApplicationProvider.getApplicationContext() as Context
val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -204,7 +204,7 @@ class BuzzerScreenActivityTest {

val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)

onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click())
onView(withId(R.id.buttonCorrect)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click())
Expand All @@ -219,7 +219,7 @@ class BuzzerScreenActivityTest {
fun setAnswerButtonWrongTest() {
val ctx = ApplicationProvider.getApplicationContext() as Context

val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 2
Expand All @@ -229,7 +229,7 @@ class BuzzerScreenActivityTest {
val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)

val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)

onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click())
onView(withId(R.id.buttonWrong)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click())
Expand All @@ -247,11 +247,11 @@ class BuzzerScreenActivityTest {
val gameManager = setGameManager(2)
Assert.assertEquals(gameManager.getSongList().size, 2)

val intent = Intent(ApplicationProvider.getApplicationContext(), BuzzerScreenActivity::class.java)
val intent = Intent(ApplicationProvider.getApplicationContext(), BuzzerGameActivity::class.java)
intent.putExtra("Number of players", mockPlayersNumber)
intent.putExtra("Player Names", mockNameArray)
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.testProgressBar()
}
Expand All @@ -270,7 +270,7 @@ class BuzzerScreenActivityTest {
fun prepareWinnerAnnouncementTestNoWinner() {
val ctx = ApplicationProvider.getApplicationContext() as Context

val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 4
Expand All @@ -281,7 +281,7 @@ class BuzzerScreenActivityTest {
val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)

val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)

val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
val scoreArray = arrayOf(0, 0, 0, 0)
Expand All @@ -299,7 +299,7 @@ class BuzzerScreenActivityTest {
fun prepareWinnerAnnouncementTestOneWinner() {
val ctx = ApplicationProvider.getApplicationContext() as Context

val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 4
Expand All @@ -309,7 +309,7 @@ class BuzzerScreenActivityTest {

val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)

val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
val scoreArray = arrayOf(0, 1, 0, 0)
Expand All @@ -326,7 +326,7 @@ class BuzzerScreenActivityTest {
@Test
fun prepareWinnerAnnouncementTestMoreThanOneWinner() {
val ctx = ApplicationProvider.getApplicationContext() as Context
val intent = Intent(ctx, BuzzerScreenActivity::class.java)
val intent = Intent(ctx, BuzzerGameActivity::class.java)

// Put mock extras inside
val mockPlayersNumber = 4
Expand All @@ -337,7 +337,7 @@ class BuzzerScreenActivityTest {
val gameManager = setGameManager()
intent.putExtra("gameManager", gameManager)

val scn: ActivityScenario<BuzzerScreenActivity> = ActivityScenario.launch(intent)
val scn: ActivityScenario<BuzzerGameActivity> = ActivityScenario.launch(intent)
val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
val scoreArray = arrayOf(2, 2, 1, 2)
val testUpdater = BuzzerScoreUpdater(idArray, scoreArray)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
android:name=".activity.game.TypingGameActivity"
android:exported="false" />
<activity
android:name=".activity.game.BuzzerScreenActivity"
android:name=".activity.game.BuzzerGameActivity"
android:exported="false" />
<activity
android:name=".activity.PublicProfileActivity"
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/ch/sdp/vibester/BuzzerScoreUpdater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ class BuzzerScoreUpdater(ids: ArrayList<Int>, scores: Array<Int>) {
* updates the score corresponding to the pressed buzzer
* checks first if the given id is a buzzer id (and not NO_BUZZER_PRESSED)
*/
fun updateScoresArray(id: Int, answerCorrect: Boolean) {
fun updateScoresArray(id: Int) {
if (!buzzerToScoreMap.keys.contains(id)) {
return
}
val point = if (answerCorrect) {1} else {0}
val updatedScore = max(buzzerToScoreMap.getOrDefault(id, 0) + point, 0) // should never get to default
val updatedScore = buzzerToScoreMap.getOrDefault(id, 0) + 1 // should never get to default
buzzerToScoreMap.put(id, updatedScore)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import ch.sdp.vibester.R
import ch.sdp.vibester.activity.game.BuzzerScreenActivity
import ch.sdp.vibester.activity.game.BuzzerGameActivity
import ch.sdp.vibester.helper.GameManager

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ class BuzzerSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListe


fun switchToGame(view: View) {
val intent = Intent(this, BuzzerScreenActivity::class.java)
val intent = Intent(this, BuzzerGameActivity::class.java)
val players = findViewById<LinearLayout>(R.id.playerNames).children.filter { child: View -> child.visibility == View.VISIBLE }
val pNameArray = arrayOfNulls<String>(players.count())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.bumptech.glide.Glide
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

class BuzzerScreenActivity : GameActivity() {
class BuzzerGameActivity : GameActivity() {

private val artworkDim = 200
private val noBuzzerPressed = -1
Expand Down Expand Up @@ -58,7 +58,7 @@ class BuzzerScreenActivity : GameActivity() {
requestWindowFeature(Window.FEATURE_NO_TITLE)
super.onCreate(savedInstanceState)
supportActionBar?.hide()
setContentView(R.layout.activity_buzzer_screen)
setContentView(R.layout.activity_buzzer_game)

val intentExtras = intent.extras
if (intentExtras != null) {
Expand Down Expand Up @@ -225,10 +225,9 @@ class BuzzerScreenActivity : GameActivity() {
answer.visibility = View.INVISIBLE
if (pressedBuzzer >= 0) {
if(button.id==R.id.buttonCorrect) {
scoreUpdater.updateScoresArray(pressedBuzzer, true)
scoreUpdater.updateScoresArray(pressedBuzzer)
gameManager.addCorrectSong()
} else {
scoreUpdater.updateScoresArray(pressedBuzzer, false)
gameManager.addWrongSong()
}
val view = map[pressedBuzzer]?.let { it1 -> findViewById<TextView>(it1) }
Expand Down
Loading