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

Margaux/buzzer score updater #128

Merged
merged 5 commits into from
Mar 31, 2022
Merged

Margaux/buzzer score updater #128

merged 5 commits into from
Mar 31, 2022

Conversation

zwierski
Copy link
Collaborator

@zwierski zwierski commented Mar 31, 2022

This PR contains the new class BuzzerScoreUpdater, which keeps track of a local game's scores and updates them when a user correctly guesses a song.
pr_bsu_1
pr_bsu_2
pr_bsu_3

}
setAnswerButton(answer, findViewById(R.id.buttonCorrect), updater, buzToScoreMap)
setAnswerButton(answer, findViewById(R.id.buttonWrong), updater, buzToScoreMap)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

setAnswerButton(answer, findViewById(R.id.buttonCorrect), updater, buzToScoreMap)
setAnswerButton(answer, findViewById(R.id.buttonWrong), updater, buzToScoreMap)
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

import ch.sdp.vibester.R


class GamescreenActivity: AppCompatActivity() {

private val MAX_N_PLAYERS = 4
private val NO_BUZZER_PRESSED = -1
private val rowsIdArray = arrayOf(R.id.row_0, R.id.row_1, R.id.row_2, R.id.row_3)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

private val MAX_N_PLAYERS = 4
private val NO_BUZZER_PRESSED = -1
private val rowsIdArray = arrayOf(R.id.row_0, R.id.row_1, R.id.row_2, R.id.row_3)
private val buzIds = arrayOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not really make sense to refactor, this is just declaration and initialisation of 2 arrays with values from the xml.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Worst case, if these buzzers and rows are the only children of the layout, can iterate over them and use getChild to set their values. Leaving the comment here in case we need to do so in the future.

@zwierski zwierski marked this pull request as ready for review March 31, 2022 15:13
@@ -6,25 +6,43 @@ import org.junit.Test

public class BuzzerScoreUpdaterTest {

// FIXME: Uncomment this when the infinite loop issue in the class is fixed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it fixed? If so, maybe it's time to remove this :)

Copy link
Collaborator

@Tsathogguaa Tsathogguaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@zwierski zwierski linked an issue Mar 31, 2022 that may be closed by this pull request
@zwierski zwierski self-assigned this Mar 31, 2022
Copy link
Collaborator

@kamilababayeva kamilababayeva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super happy that now the scores are stored! Nice work!

There is a problem with the use of assert in the testing, please take a look. Thanks!

}
for (value in scoreArray) {
assert(testUpdater.getMap().values.contains(value))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was searching and I believe it is not correct to use java's assert in the test. Because in this case the java's exception will appear, not junit's exception. I would change assert on junit's assertTrue or assertEquals.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here ExpectedMap is not used, so it can be deleted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll change this!

val testUpdater = BuzzerScoreUpdater(idArray, scoreArray)
testUpdater.updateScoresArray(-1)
for (id in idArray) {
assert(testUpdater.getMap()[id]==0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(testUpdater.getMap()[id]==0)
assertTrue(testUpdater.getMap()[id]==0)

val testUpdater = BuzzerScoreUpdater(idArray, scoreArray)
for (id in idArray) {
testUpdater.updateScoresArray(id)
assert(testUpdater.getMap()[id]==1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(testUpdater.getMap()[id]==1)
assertTrue(testUpdater.getMap()[id]==1)

private fun initMap(ids: Array<Int>, scores: Array<Int>): LinkedHashMap<Int, Int> {
var theMap = LinkedHashMap<Int, Int>()
var i = 0
while (i < min(ids.size, scores.size)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you take min here? Should not both arrays be the same size?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to perform a same size check and then use one of the sizes in the while loop, the app kept crashing, even though I only have cases where the scores are initialized according to the number of players (so should always be the same size). I used the min function as a quick fix for this, and I'll try to implement a proper check when refactoring.

if (!buzzerToScoreMap.keys.contains(id)) {
return
}
val updatedScore = buzzerToScoreMap.getOrDefault(id, 0) + 1 // should never get to default,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the buzzer is initiated, there is no need for default, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using the "get" function: however, it returns an Int? which is not castable to Int, even with a null check.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here you can update the score in buzzer directly, like that:
buzzerToScoreMap[id]?.plus(1)

* checks first if the given id
*/
fun updateScoresArray(id: Int) {
if (!buzzerToScoreMap.keys.contains(id)) {
Copy link
Collaborator

@kamilababayeva kamilababayeva Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check is needed as well? The array will be initialized with exactly entered number of ids, so there could not be a missing id in general... or am I missing something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check ensures that no update will be attempted if the parameter is equal to NO_BUZZER_PRESSED (which should not happen but I still added a check just in case). I'll update the doc to make this clearer.

@kamilababayeva kamilababayeva self-requested a review March 31, 2022 20:32
@codeclimate
Copy link

codeclimate bot commented Mar 31, 2022

Code Climate has analyzed commit 4109b04 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Duplication 2

The test coverage on the diff in this pull request is 82.6% (80% is the threshold).

This pull request will bring the total coverage in the repository to 90.6%.

View more on Code Climate.

@zwierski zwierski merged commit 597e67a into main Mar 31, 2022
@zwierski zwierski deleted the margaux/buzzer-score-updater branch March 31, 2022 20:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix BuzzerScoreUpdater (#57)
3 participants