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

Kamila/set score game #226

Merged
merged 24 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c119dc1
TypinGame: inject UserRepo
kamilababayeva Apr 30, 2022
4db7d6e
TypinGameActivity: set score per each game
kamilababayeva Apr 30, 2022
62077ba
GameManager: set mode for the game
kamilababayeva Apr 30, 2022
8dc937f
DataGetter: simplify docymentation
kamilababayeva Apr 30, 2022
a8792aa
DataGetter: fix the function to set relative integer field
kamilababayeva Apr 30, 2022
74a9553
DataGetter: fix the function updateBestFIeldInt
kamilababayeva Apr 30, 2022
1e2058f
DataGetter: new funciton updateBestSubFieldInt
kamilababayeva Apr 30, 2022
4bc6094
DataGetter: update the comment
kamilababayeva Apr 30, 2022
7514892
DataGetter: new FieldSubFieldInt
kamilababayeva Apr 30, 2022
161fd80
DataGetter: fix the documentation
kamilababayeva Apr 30, 2022
fd43f16
TypingGameActivity: fix the mocking
kamilababayeva May 1, 2022
63c1723
Update app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
kamilababayeva May 1, 2022
10a2874
Update app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
kamilababayeva May 1, 2022
951d645
Update app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
kamilababayeva May 1, 2022
c5b27dd
modify database calls and change appropriately in the code
kamilababayeva May 1, 2022
47f7c18
Update app/src/androidTest/java/ch/sdp/vibester/activity/TypingGameAc…
kamilababayeva May 2, 2022
92f5f77
Update app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt
kamilababayeva May 2, 2022
05e1082
Update app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
kamilababayeva May 2, 2022
62af945
Merge branch 'main' into kamila/set_score_game
kamilababayeva May 2, 2022
6def1ef
Merge branch 'main' into kamila/set_score_game
kamilababayeva May 2, 2022
e71d054
SearchUserActivity: update score after game
kamilababayeva May 2, 2022
1c7dce2
ProfileActivityTest: fix the tests
kamilababayeva May 2, 2022
e0ceca2
fix test partly
kamilababayeva May 2, 2022
71b39aa
UserProfileAdapter: fix the merge
kamilababayeva May 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import androidx.test.espresso.matcher.ViewMatchers.withSpinnerText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.Matchers
import org.junit.After
import org.junit.Before
Expand All @@ -24,13 +26,18 @@ import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class GameSetupActivityTest {

@get: Rule
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get: Rule(order = 1)
val activityRule = ActivityScenarioRule(GameSetupActivity::class.java)

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ProfileActivityTest {
every { mockUsersRepo.getUserData(any()) } answers {
secondArg<(User) -> Unit>().invoke(mockProfile)
}
every { mockUsersRepo.updateFieldString(any(), any(), any()) } answers {}
every { mockUsersRepo.setFieldValue(any(), any(), any()) } answers {}

every { mockUsersRepo.getUserData(any())} answers {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.TestMode
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.helper.TypingGameManager
import ch.sdp.vibester.model.Song
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
import io.mockk.mockk
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
Expand All @@ -30,14 +35,18 @@ import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class TypingGameActivityTest {

@JvmField
@get:Rule
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val activityRule = ActivityScenarioRule(TypingGameActivity::class.java)

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
}

Expand All @@ -46,6 +55,17 @@ class TypingGameActivityTest {
Intents.release()
}

@BindValue
@JvmField
val mockUsersRepo = mockk<DataGetter>()

private fun createMockInvocation() {
every {mockUsersRepo.setSubFieldValue(any(), any(), any(),any())} answers {}
every {mockUsersRepo.updateFieldInt(any(), any(), any(), any())} answers {}
every {mockUsersRepo.setFieldValue(any(), any(), any())} answers {}
every {mockUsersRepo.updateSubFieldInt(any(), any(), any(), any(), any())} answers {}
kamilababayeva marked this conversation as resolved.
Show resolved Hide resolved
}

private val expectedSize = 200
private fun setGameManager(numSongs:Int = 1, valid: Boolean = true): TypingGameManager {
val epilogue = "{\"tracks\":{\"track\":["
Expand Down Expand Up @@ -110,7 +130,7 @@ class TypingGameActivityTest {

@Test
fun guessLayoutTest() {
TestMode.setTest()
createMockInvocation()
val inputTxt = """
{
"resultCount":1,
Expand Down Expand Up @@ -163,6 +183,7 @@ class TypingGameActivityTest {
// Do not put gameManager as an extra
val scn: ActivityScenario<TypingGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
createMockInvocation()
scn.onActivity { activity ->
activity.checkAnswer(ctx, songTest, gameManager)
}
Expand All @@ -188,6 +209,7 @@ class TypingGameActivityTest {
Intent(ApplicationProvider.getApplicationContext(), TypingGameActivity::class.java)
val scn: ActivityScenario<TypingGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
createMockInvocation()
scn.onActivity { activity ->
activity.checkAnswer(ctx, songTest, gameManager)
}
Expand All @@ -201,7 +223,7 @@ class TypingGameActivityTest {
*/
@Test
fun checkIntentOnEnding() {
TestMode.setTest()
createMockInvocation()
val inputTxt = """
{
"resultCount":1,
Expand Down Expand Up @@ -249,6 +271,7 @@ class TypingGameActivityTest {
val intent = Intent(ApplicationProvider.getApplicationContext(), TypingGameActivity::class.java)
val scn: ActivityScenario<TypingGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
createMockInvocation()
scn.onActivity { activity ->
activity.testFirstRound(ctx, gameManager)
activity.testProgressBar()
Expand Down Expand Up @@ -276,7 +299,7 @@ class TypingGameActivityTest {

@Test
fun nextButtonOnClick(){
TestMode.setTest()
createMockInvocation()
val gameManager = setGameManager(2)
assertEquals(gameManager.getSongList().size, 2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CreateProfileActivity : AppCompatActivity() {

btCreateAcc.setOnClickListener {
if (!TestMode.isTest()){
dataGetter.updateFieldString(FireBaseAuthenticator.getCurrentUID(), username.text.toString(), "username")
dataGetter.setFieldValue(FireBaseAuthenticator.getCurrentUID(), "username", username.text.toString())
}
startNewActivity(email)
}
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ class GameSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListene
var method = ""
var artist = ""
var tag = ""
var mode = ""
val uri = LastfmUri()
when (view.id) {
R.id.btsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "BTS" }
R.id.kpopButton -> {method = LastfmMethod.BY_TAG.method; tag = "kpop" }
R.id.imagDragonsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Imagine Dragons"}
R.id.rockButton-> {method = LastfmMethod.BY_TAG.method; tag = "rock" }
R.id.topTracksButton -> {method = LastfmMethod.BY_CHART.method}
R.id.billieEilishButton -> {method =LastfmMethod.BY_ARTIST.method; artist = "Billie Eilish"}
R.id.btsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "BTS"; mode = "BTS" }
jiabaow marked this conversation as resolved.
Show resolved Hide resolved
R.id.kpopButton -> {method = LastfmMethod.BY_TAG.method; tag = "kpop"; mode = "kpop" }
jiabaow marked this conversation as resolved.
Show resolved Hide resolved
R.id.imagDragonsButton -> {method = LastfmMethod.BY_ARTIST.method; artist = "Imagine Dragons"; mode = "Imagine Dragons"}
jiabaow marked this conversation as resolved.
Show resolved Hide resolved
R.id.rockButton-> {method = LastfmMethod.BY_TAG.method; tag = "rock"; mode = "rock" }
jiabaow marked this conversation as resolved.
Show resolved Hide resolved
R.id.topTracksButton -> {method = LastfmMethod.BY_CHART.method; mode = "top tracks"}
R.id.billieEilishButton -> {method =LastfmMethod.BY_ARTIST.method; artist = "Billie Eilish"; mode = "billie eilish"}
jiabaow marked this conversation as resolved.
Show resolved Hide resolved
}
uri.method = method
uri.artist = artist
Expand All @@ -158,6 +159,7 @@ class GameSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListene
findViewById<ConstraintLayout>(R.id.chooseGenre).visibility = GONE
findViewById<ConstraintLayout>(R.id.chooseDifficulty).visibility = VISIBLE

gameManager.gameMode = mode
setGameSongList(uri)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ProfileActivity : AppCompatActivity() {
findViewById<TextView>(textId).text = input.text.toString()

if(name == "username"){
dataGetter.updateFieldString(FireBaseAuthenticator.getCurrentUID(), input.text.toString(), "username")
dataGetter.setFieldValue(FireBaseAuthenticator.getCurrentUID(),"username", input.text.toString())
kamilababayeva marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
18 changes: 11 additions & 7 deletions app/src/main/java/ch/sdp/vibester/activity/TypingGameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ import ch.sdp.vibester.helper.DisplayContents
import ch.sdp.vibester.helper.GameManager
import ch.sdp.vibester.helper.TypingGameManager
import ch.sdp.vibester.model.Song
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import javax.inject.Inject

/**
* Class that represent a game
*/

@AndroidEntryPoint
class TypingGameActivity : GameActivity() {
private lateinit var gameManager: TypingGameManager
private var gameIsOn: Boolean = true // done to avoid clicks on songs after the round is over

@Inject
lateinit var dataGetter: DataGetter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
Expand Down Expand Up @@ -212,12 +217,11 @@ class TypingGameActivity : GameActivity() {
toggleNextBtnVisibility(true)

// If currently not in test and has finished the game, update the scores
if (isEndGame(gameManager) && !TestMode.isTest()) {
val dataGetter = DataGetter()
dataGetter.updateRelativeFieldInt(FireBaseAuthenticator.getCurrentUID(), 1, "totalGames")
dataGetter.updateRelativeFieldInt(FireBaseAuthenticator.getCurrentUID(), gameManager.getCorrectSongs().size, "correctSongs")
dataGetter.updateBestFieldInt(FireBaseAuthenticator.getCurrentUID(), gameManager.getScore(), "bestScore")

if (isEndGame(gameManager)) {
dataGetter.updateFieldInt(FireBaseAuthenticator.getCurrentUID(), "totalGames", 1, method = "sum")
dataGetter.updateFieldInt(FireBaseAuthenticator.getCurrentUID(), "correctSongs", gameManager.getCorrectSongs().size, method = "sum")
dataGetter.updateFieldInt(FireBaseAuthenticator.getCurrentUID(), "bestScore", gameManager.getScore(), method = "best")
dataGetter.updateSubFieldInt(FireBaseAuthenticator.getCurrentUID(), gameManager.getScore(), "scores", gameManager.gameMode, method = "best")
}

}
Expand Down
95 changes: 46 additions & 49 deletions app/src/main/java/ch/sdp/vibester/database/DataGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,78 @@ class DataGetter @Inject constructor() {
private val dbRoomRef = Database.get().getReference("rooms")
private val authenticator: FireBaseAuthenticator = FireBaseAuthenticator()


/**
* This function updates a specific string field of a user in the database
* @param userID the id of the user which is being updated
* @param newVal (String) the new value of the field that is being updated
* @param fieldName the field name of the field that is being updated
* Set field value
* @param uid user identifier
* @param newVal new value to set
* @param fieldName
*/
fun updateFieldString(userID: String, newVal: String, fieldName: String) {
dbUserRef.child(userID)
fun setFieldValue(uid: String, fieldName: String, newVal: Any) {
dbUserRef.child(uid)
.child(fieldName)
.setValue(newVal)
}

/**
* This function updates a specific int field of a user in the database
* @param userID the id of the user which is being updated
* @param newVal (Int) the new value of the field that is being updated
* @param fieldName the field name of the field that is being updated
* Set subfield value
* @param uid user identifier
* @param newVal new value to set
* @param fieldName
* @param subFieldName
*/
fun updateFieldInt(userID: String, newVal: Int, fieldName: String) {
dbUserRef.child(userID)
fun setSubFieldValue(uid: String, fieldName: String, subFieldName: String, newVal: Any) {
dbUserRef.child(uid)
.child(fieldName)
.child(subFieldName)
.setValue(newVal)
}


/**
* This function increments a specific int field of a user in the database
* @param userID the id of the user which is being updated
* @param newVal (Int) the increment value of the field that is being updated
* @param fieldName the field name of the field that is being updated
* Update integer value in a subfield based on method sum/best
* @param userID
* @param newVal
* @param fieldName
*/
fun updateRelativeFieldInt(userID: String, newVal: Int, fieldName: String) {
if(!TestMode.isTest()) {
dbUserRef.child(userID).child(fieldName)
.get().addOnSuccessListener { t ->
if(!TestMode.isTest()) {
updateFieldInt(userID, (t.value as Long?)!!.toInt() + newVal, fieldName)
fun updateFieldInt(uid: String, fieldName: String, newVal: Int, method:String) {
dbUserRef.child(uid).child(fieldName)
.get().addOnSuccessListener { t ->
var finalVal = newVal
if(t.value != null) {
Copy link

Choose a reason for hiding this comment

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

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think it is worth spending time refactoring these two duplicate functions?

val previousVal = (t.value as Long?)!!.toInt()
when (method) {
"sum" -> finalVal += previousVal
"best" -> finalVal = maxOf(previousVal, newVal)
}
}
}
setFieldValue(uid, fieldName, finalVal)
}
}


/**
* This function sets the best between current and given in argument value
* @param userID the id of the user which is being updated
* @param newVal (Int) the new value of the field that is being compared and updated
* @param fieldName the field name of the field that is being updated
* Update the integer value of subfield based on method sum/best
* @param userID
* @param newVal
* @param fieldName
* @param subFieldName
*/
fun updateBestFieldInt(userID: String, newVal: Int, fieldName: String) {
if(!TestMode.isTest()) {
dbUserRef.child(userID).child(fieldName)
.get().addOnSuccessListener { t ->
if(!TestMode.isTest()) {
updateFieldInt(userID, maxOf((t.value as Long?)!!.toInt(), newVal), fieldName)
fun updateSubFieldInt(userID: String, newVal: Int, fieldName: String, subFieldName: String, method: String) {
kamilababayeva marked this conversation as resolved.
Show resolved Hide resolved
dbUserRef.child(userID).child(fieldName).child(subFieldName)
.get().addOnSuccessListener { t ->
var finalVal = newVal
kamilababayeva marked this conversation as resolved.
Show resolved Hide resolved
if(t.value != null) {
kamilababayeva marked this conversation as resolved.
Show resolved Hide resolved
val previousVal = (t.value as Long?)!!.toInt()
when (method) {
"sum" -> finalVal += previousVal
"best" -> finalVal = maxOf(previousVal, newVal)
}
}
}
setSubFieldValue(userID, fieldName, subFieldName, finalVal)
}
}


/**
* Update a subfield of user's field
* @param userID the id of the user which is being updated
* @param newVal (Boolean) the new value of the field that is being updated
* @param fieldName the field name of the field that is being updated
* @param subFieldName the field name of the field that is being updated
*/
fun updateFieldSubFieldBoolean(userID: String, newVal: Boolean, fieldName: String, subFieldName: String) {
dbUserRef.child(userID)
.child(fieldName)
.child(subFieldName)
.setValue(newVal)
}

/**
* This function creates a new user account in the database
* @param email the email of the new user
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/ch/sdp/vibester/helper/GameManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ open class GameManager : Serializable {
private var score = 0
open var gameSize = 5
var numPlayedSongs = 0
var gameMode = ""
open lateinit var currentSong: Song
var gameSongList: MutableList<Pair<String, String>> = mutableListOf()
private val correctSongs = mutableListOf<Song>() //TODO: question: why this is a val but the next one is a var?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class UserProfileAdapter constructor(val users: MutableList<User>):
addFriendBtn.setOnClickListener{
val currentUser = authenticator.getCurrUser()
if(currentUser != null){
usersRepo.updateFieldSubFieldBoolean(currentUser!!.uid, true, "friends", user.uid)
usersRepo.setSubFieldValue(currentUser.uid, "friends", user.uid, true)
addFriendBtn.visibility = View.INVISIBLE
itemView.findViewById<ImageView>(R.id.addedFriendIcon).visibility = View.VISIBLE
}
Expand Down