From c64f4f9b996266b5e21f92a6c4e47bf792d22c51 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Wed, 10 Apr 2024 20:40:49 +0700 Subject: [PATCH 01/16] Add LetterCardAdapter for recycleView and codebase for PlayingRoomViewModel --- .idea/deploymentTargetDropDown.xml | 12 +-- app/src/main/AndroidManifest.xml | 2 +- .../example/chiecnonkydieu/MainActivity.kt | 2 +- .../adapters/LetterCardAdapter.kt | 53 ++++++++++++ .../example/chiecnonkydieu/data/GameModel.kt | 5 +- .../example/chiecnonkydieu/data/LetterCard.kt | 6 ++ .../com/example/chiecnonkydieu/data/Player.kt | 2 +- .../chiecnonkydieu/ui/CreateRoomActivity.kt | 12 ++- .../chiecnonkydieu/ui/WaitingRoomActivity.kt | 7 +- .../{ => playingRoom}/PlayingRoomActivity.kt | 45 +++++++--- .../ui/playingRoom/PlayingRoomViewModel.kt | 84 +++++++++++++++++++ .../chiecnonkydieu/ui/wheel/WheelViewModel.kt | 5 ++ .../main/res/layout/activity_playing_room.xml | 16 +++- app/src/main/res/layout/view_letter_card.xml | 31 +++++++ 14 files changed, 250 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt create mode 100644 app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt rename app/src/main/java/com/example/chiecnonkydieu/ui/{ => playingRoom}/PlayingRoomActivity.kt (82%) create mode 100644 app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt create mode 100644 app/src/main/res/layout/view_letter_card.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 7ce6325..2fe4f5f 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -4,18 +4,18 @@ - + - + - - + + - - + + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 270e0e3..6357de4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,7 +16,7 @@ android:name=".ui.wheel.WheelActivity" android:exported="false" /> ): RecyclerView.Adapter() { + + class LetterCardViewHolder(viewLetterCard: View) : RecyclerView.ViewHolder(viewLetterCard) { + private val tvLetter: TextView = viewLetterCard.findViewById(R.id.text_card_letter) + private val cardView: MaterialCardView = viewLetterCard.findViewById(R.id.card_view_letter_cards) + + fun bind(letterCard: LetterCard) { + if (letterCard.letter == " ") { + cardView.visibility = View.INVISIBLE + } else if (!letterCard.isHidden) { + tvLetter.text = letterCard.letter + tvLetter.visibility = View.VISIBLE + cardView.setCardBackgroundColor(Color.YELLOW) + cardView.visibility = View.VISIBLE + } +// else if (letterCard.isHidden) { +// tvLetter.text = letterCard.letter +// tvLetter.visibility = View.INVISIBLE +// } + } + + } + + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) + : LetterCardViewHolder { + val viewLetterCard = LayoutInflater.from(parent.context) + .inflate(R.layout.view_letter_card, parent, false) + return LetterCardViewHolder(viewLetterCard) + } + + override fun onBindViewHolder(holder: LetterCardViewHolder, position: Int) { + holder.bind(letterCardList[position]) + } + + override fun getItemCount(): Int { + return if (letterCardList.isEmpty()) 0 + else letterCardList.size + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt index 52200b7..a9e4383 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt @@ -7,10 +7,11 @@ data class GameModel( val winner: String = "", var gameStatus: GameStatus = GameStatus.CREATED, val playersList: MutableList = mutableListOf(), - val currentPlayer: Player = Player(), + var currentPlayer: Player = Player(), val currentQuestion: String = "", val currentAnswer: String = "HA NOI", - val currentGuess: String = " A N " + val guessesCharacters: MutableList = mutableListOf(), + val letterCardList: MutableList = mutableListOf() ) diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt b/app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt new file mode 100644 index 0000000..1399c58 --- /dev/null +++ b/app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt @@ -0,0 +1,6 @@ +package com.example.chiecnonkydieu.data + +data class LetterCard( + val letter: String = "A", + var isHidden: Boolean = true +) diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/Player.kt b/app/src/main/java/com/example/chiecnonkydieu/data/Player.kt index 0565137..e6de43a 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/Player.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/Player.kt @@ -3,5 +3,5 @@ package com.example.chiecnonkydieu.data data class Player( val name: String = "Player", val gender: Boolean = true, - val score: Int = 0 + var score: Int = 0 ) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt index d914deb..080f204 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt @@ -12,6 +12,7 @@ import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus +import com.example.chiecnonkydieu.data.LetterCard import com.example.chiecnonkydieu.data.Player import com.example.chiecnonkydieu.databinding.ActivityCreateRoomBinding import kotlinx.coroutines.launch @@ -46,7 +47,16 @@ class CreateRoomActivity : AppCompatActivity() { val gameModel: GameModel = GameModel( gameStatus = GameStatus.CREATED, gameId = binding.tvMaPhong.text.toString().toInt(), - currentQuestion = "Thủ đô của Việt Nam nằm ở đâu?" + currentQuestion = "Thủ đô của Việt Nam nằm ở đâu?", + guessesCharacters = mutableListOf(), + letterCardList = mutableListOf( + LetterCard("H"), + LetterCard("A"), + LetterCard(" "), + LetterCard("N"), + LetterCard("O"), + LetterCard("I"), + ) ) gameModel.playersList.add(Player(binding.edtName.text.toString())) GameData.saveGameModel( diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt index b79b049..a539783 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt @@ -1,7 +1,6 @@ package com.example.chiecnonkydieu.ui import android.content.Intent -import android.graphics.Color import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity @@ -13,14 +12,10 @@ import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData -import com.example.chiecnonkydieu.data.GameData.gameModel import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.Player import com.example.chiecnonkydieu.databinding.ActivityWaitingRoomBinding -import com.google.firebase.database.DataSnapshot -import com.google.firebase.database.DatabaseError -import com.google.firebase.database.ValueEventListener +import com.example.chiecnonkydieu.ui.playingRoom.PlayingRoomActivity import kotlinx.coroutines.launch class WaitingRoomActivity : AppCompatActivity() { diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt similarity index 82% rename from app/src/main/java/com/example/chiecnonkydieu/ui/PlayingRoomActivity.kt rename to app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index 876f2fc..9617ba9 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -1,11 +1,8 @@ -package com.example.chiecnonkydieu.ui +package com.example.chiecnonkydieu.ui.playingRoom -import android.content.DialogInterface import android.content.Intent import android.os.Bundle -import android.text.InputType import android.view.LayoutInflater -import android.view.ViewGroup import android.widget.EditText import android.widget.Toast import androidx.activity.enableEdgeToEdge @@ -13,25 +10,35 @@ import androidx.activity.viewModels import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar -import androidx.core.content.ContentProviderCompat.requireContext import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.LayoutManager import com.example.chiecnonkydieu.R +import com.example.chiecnonkydieu.adapters.LetterCardAdapter import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus +import com.example.chiecnonkydieu.data.LetterCard import com.example.chiecnonkydieu.databinding.ActivityPlayingRoomBinding import com.example.chiecnonkydieu.ui.wheel.WheelActivity import com.example.chiecnonkydieu.ui.wheel.WheelViewModel +import com.google.android.material.carousel.CarouselLayoutManager import kotlinx.coroutines.launch import rubikstudio.library.LuckyWheelView class PlayingRoomActivity : AppCompatActivity() { private lateinit var binding: ActivityPlayingRoomBinding + // TODO + + lateinit var adapter: LetterCardAdapter + lateinit var recyclerView: RecyclerView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() @@ -52,21 +59,30 @@ class PlayingRoomActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) - + val playingRoomViewModel: PlayingRoomViewModel by viewModels() lifecycleScope.launch { GameData.fetchGameModel(intent.getStringExtra("room_id").toString().toInt()) - } GameData.gameModel.observe(this, Observer { gameModel -> updateUi(gameModel) }) + //Setup recyclerview + recyclerView = binding.recyclerviewLetterCards + val layoutManager = LinearLayoutManager(this) + + layoutManager.orientation = LinearLayoutManager.HORIZONTAL // Set the orientation to horizontal if needed + recyclerView.layoutManager = layoutManager + adapter = LetterCardAdapter(GameData.gameModel.value!!.letterCardList) + recyclerView.adapter = adapter + + binding.btnDoan.setOnClickListener { - Toast.makeText(this, "Doan click", Toast.LENGTH_LONG).show() - if (binding.edtDoan.text.toString() == "A") { - // TODO + if (playingRoomViewModel.isUserInputMatch(binding.edtDoan.text.toString()[0])) { + updateAdapterAndRecyclerView() } + } binding.btnGiai.setOnClickListener { @@ -92,9 +108,9 @@ class PlayingRoomActivity : AppCompatActivity() { binding.llWheel.setOnClickListener { goToWheelActivity() } + } - } override fun onSupportNavigateUp(): Boolean { onBackPressed() return true @@ -111,7 +127,6 @@ class PlayingRoomActivity : AppCompatActivity() { updatePlayerList(gameModel) updateCurrentQuestion(gameModel) } - } private fun showDialog() { @@ -183,4 +198,10 @@ class PlayingRoomActivity : AppCompatActivity() { } } } + + private fun updateAdapterAndRecyclerView() { + adapter = LetterCardAdapter(GameData.gameModel.value!!.letterCardList) + recyclerView.adapter = adapter + adapter.notifyDataSetChanged() + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt new file mode 100644 index 0000000..f6e0a26 --- /dev/null +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -0,0 +1,84 @@ +package com.example.chiecnonkydieu.ui.playingRoom + +import androidx.lifecycle.ViewModel +import com.example.chiecnonkydieu.data.GameData +import com.example.chiecnonkydieu.data.GameModel +import com.example.chiecnonkydieu.data.Player +import java.util.Locale + +class PlayingRoomViewModel: ViewModel() { + + private lateinit var gameModel: GameModel + + init { + if (GameData.gameModel.value != null) { + gameModel = GameData.gameModel.value!! + } + } + + fun isUserInputMatch(playerInputChar: Char): Boolean { + val gameModel: GameModel? = GameData.gameModel.value + val playerInputStringLC = playerInputChar.lowercaseChar().toString() + val playerInputCharLC = playerInputChar.lowercaseChar() + gameModel?.let { + if (gameModel.currentAnswer.contains(playerInputStringLC, ignoreCase = true) && + ! gameModel.guessesCharacters.contains(playerInputStringLC)) { + saveGuessedChar(playerInputCharLC) + gameModel.guessesCharacters.add(playerInputStringLC) + gameModel.letterCardList.filter { + it.letter.toLowerCase() == playerInputStringLC + } + .forEach{ + it.isHidden = false + } + GameData.saveGameModel(gameModel) + return true + } + else { + return false; + } + + } + + return false + } + + fun setQuestionAndCurrentWordToBeGuessed(): Boolean { + return false + } + + private fun saveGuessedChar(playerInputChar: Char) { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.guessesCharacters.add(playerInputChar.toString()) + } + } + + + private fun updateScore(indexPlayer: Int, score: Int) { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.playersList[indexPlayer].score += score + } + } + + private fun changeTurn(player: Player) { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.currentPlayer = player + } + } + + private fun nextRound() { + // TODO + } + + private fun checkRoundWin(): Boolean { + return true + } + private fun checkGameWin(): Boolean { + return true + } + + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt index 3153000..0377ddc 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt @@ -3,13 +3,18 @@ package com.example.chiecnonkydieu.ui.wheel import android.content.Context import android.graphics.Color import androidx.core.content.ContextCompat +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.example.chiecnonkydieu.R +import com.example.chiecnonkydieu.data.LetterCard import rubikstudio.library.model.LuckyItem class WheelViewModel: ViewModel() { private val _luckyItemsList: MutableList = mutableListOf() val luckyItemsList:List = _luckyItemsList + private val _letterCardList = MutableLiveData>() + val letterCardList: LiveData> = _letterCardList fun initLuckyItemList(context: Context) { val luckyItem: LuckyItem = LuckyItem() luckyItem.secondaryText = "100" diff --git a/app/src/main/res/layout/activity_playing_room.xml b/app/src/main/res/layout/activity_playing_room.xml index 9976548..9c67953 100644 --- a/app/src/main/res/layout/activity_playing_room.xml +++ b/app/src/main/res/layout/activity_playing_room.xml @@ -6,7 +6,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background" - tools:context=".ui.PlayingRoomActivity"> + tools:context=".ui.playingRoom.PlayingRoomActivity"> - + + + + + + + + \ No newline at end of file From b9cfd03d4e4de7601c6d46c344614f49741e3762 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Fri, 12 Apr 2024 11:41:33 +0700 Subject: [PATCH 02/16] Add logic for the WheelActivity and update GameModel --- .../adapters/LetterCardAdapter.kt | 2 +- .../example/chiecnonkydieu/data/DataSource.kt | 10 ++- .../example/chiecnonkydieu/data/GameData.kt | 2 +- .../example/chiecnonkydieu/data/GameModel.kt | 12 ++- .../data/{ => model}/LetterCard.kt | 2 +- .../chiecnonkydieu/data/{ => model}/Player.kt | 2 +- .../data/model/QuestionAnswer.kt | 6 ++ .../chiecnonkydieu/ui/CreateRoomActivity.kt | 27 +++--- .../chiecnonkydieu/ui/SearchRoomActivity.kt | 3 +- .../ui/playingRoom/PlayingRoomActivity.kt | 88 +++++++++++++++---- .../ui/playingRoom/PlayingRoomViewModel.kt | 73 ++++++++++++--- .../chiecnonkydieu/ui/wheel/WheelActivity.kt | 11 ++- .../chiecnonkydieu/ui/wheel/WheelViewModel.kt | 24 ++++- .../main/res/layout/activity_playing_room.xml | 30 ++++--- app/src/main/res/values/strings.xml | 4 + 15 files changed, 225 insertions(+), 71 deletions(-) rename app/src/main/java/com/example/chiecnonkydieu/data/{ => model}/LetterCard.kt (65%) rename app/src/main/java/com/example/chiecnonkydieu/data/{ => model}/Player.kt (70%) create mode 100644 app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt diff --git a/app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt b/app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt index e568da2..97a264c 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt @@ -7,7 +7,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import android.widget.TextView import com.example.chiecnonkydieu.R -import com.example.chiecnonkydieu.data.LetterCard +import com.example.chiecnonkydieu.data.model.LetterCard import com.google.android.material.card.MaterialCardView class LetterCardAdapter(val letterCardList: List): RecyclerView.Adapter() { diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt b/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt index 59545de..f75fab0 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt @@ -1,5 +1,13 @@ package com.example.chiecnonkydieu.data -import rubikstudio.library.model.LuckyItem +import android.content.Context +import com.example.chiecnonkydieu.R +import com.example.chiecnonkydieu.data.model.QuestionAnswer +val questionAnswerList: List = mutableListOf( + QuestionAnswer("Thủ đô của Việt Nam nằm ở đâu?", "HA NOI"), + QuestionAnswer("Loại hình thể thao nào được yêu thích nhất ở Việt Nam?", "BONG DA"), + QuestionAnswer("Bộ phim khoa học viễn tưởng nào là một trong những tác phẩm nổi tiếng nhất?", "STAR WARS"), + QuestionAnswer("Đâu là bộ phim có tựa đề \"Dặm xanh\"?", "THE GREEN MILE"), +) diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt b/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt index 4374aa6..80d790e 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt @@ -5,6 +5,7 @@ import android.util.Log import android.widget.Toast import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import com.example.chiecnonkydieu.data.model.Player import com.google.firebase.Firebase import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError @@ -12,7 +13,6 @@ import com.google.firebase.database.DatabaseReference import com.google.firebase.database.ValueEventListener import com.google.firebase.database.database import kotlinx.coroutines.CompletableDeferred -import kotlin.coroutines.coroutineContext object GameData { private var _gameModel: MutableLiveData = MutableLiveData() diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt index a9e4383..28b224f 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt @@ -1,6 +1,8 @@ package com.example.chiecnonkydieu.data -import android.health.connect.datatypes.units.Length +import com.example.chiecnonkydieu.data.model.LetterCard +import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.data.model.QuestionAnswer data class GameModel( val gameId: Int = -1, @@ -8,10 +10,11 @@ data class GameModel( var gameStatus: GameStatus = GameStatus.CREATED, val playersList: MutableList = mutableListOf(), var currentPlayer: Player = Player(), - val currentQuestion: String = "", - val currentAnswer: String = "HA NOI", + var currentQuestionAnswer: QuestionAnswer = questionAnswerList[0], val guessesCharacters: MutableList = mutableListOf(), - val letterCardList: MutableList = mutableListOf() + var letterCardList: MutableList = mutableListOf(), + val previousQuestionAnswers: MutableList = mutableListOf(), + var currentSpinValue: String = "" ) @@ -20,5 +23,6 @@ enum class GameStatus { JOINED1, JOINED2, INPROGRESS, + GUESS, FINISHED } diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt b/app/src/main/java/com/example/chiecnonkydieu/data/model/LetterCard.kt similarity index 65% rename from app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt rename to app/src/main/java/com/example/chiecnonkydieu/data/model/LetterCard.kt index 1399c58..4db64a0 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/LetterCard.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/model/LetterCard.kt @@ -1,4 +1,4 @@ -package com.example.chiecnonkydieu.data +package com.example.chiecnonkydieu.data.model data class LetterCard( val letter: String = "A", diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/Player.kt b/app/src/main/java/com/example/chiecnonkydieu/data/model/Player.kt similarity index 70% rename from app/src/main/java/com/example/chiecnonkydieu/data/Player.kt rename to app/src/main/java/com/example/chiecnonkydieu/data/model/Player.kt index e6de43a..7c3f0f5 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/Player.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/model/Player.kt @@ -1,4 +1,4 @@ -package com.example.chiecnonkydieu.data +package com.example.chiecnonkydieu.data.model data class Player( val name: String = "Player", diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt b/app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt new file mode 100644 index 0000000..f7a2baa --- /dev/null +++ b/app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt @@ -0,0 +1,6 @@ +package com.example.chiecnonkydieu.data.model + +data class QuestionAnswer( + val question: String = "Thủ đô của Việt Nam nằm ở đâu?", + val answer: String = "HA NOI" +) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt index 080f204..11d86d3 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt @@ -12,11 +12,11 @@ import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.LetterCard -import com.example.chiecnonkydieu.data.Player +import com.example.chiecnonkydieu.data.model.LetterCard +import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityCreateRoomBinding import kotlinx.coroutines.launch -import kotlin.random.Random class CreateRoomActivity : AppCompatActivity() { private lateinit var binding: ActivityCreateRoomBinding @@ -47,16 +47,11 @@ class CreateRoomActivity : AppCompatActivity() { val gameModel: GameModel = GameModel( gameStatus = GameStatus.CREATED, gameId = binding.tvMaPhong.text.toString().toInt(), - currentQuestion = "Thủ đô của Việt Nam nằm ở đâu?", + currentQuestionAnswer = questionAnswerList[1], guessesCharacters = mutableListOf(), - letterCardList = mutableListOf( - LetterCard("H"), - LetterCard("A"), - LetterCard(" "), - LetterCard("N"), - LetterCard("O"), - LetterCard("I"), - ) + currentPlayer = Player(name = binding.edtName.text.toString()), + letterCardList = getLetterCardListFromAnswer(questionAnswerList[1].answer), + previousQuestionAnswers = mutableListOf() ) gameModel.playersList.add(Player(binding.edtName.text.toString())) GameData.saveGameModel( @@ -72,6 +67,14 @@ class CreateRoomActivity : AppCompatActivity() { return true } + + fun getLetterCardListFromAnswer(answer: String): MutableList{ + val list = mutableListOf() + for (i in answer.indices) { + list.add(LetterCard(answer[i].toString())) + } + return list + } fun goToWaitingRooom() { val intent = Intent(this, WaitingRoomActivity::class.java) intent.putExtra("room_id", binding.tvMaPhong.text.toString()) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt index 023a84a..15dcaa0 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt @@ -11,10 +11,9 @@ import androidx.core.view.WindowInsetsCompat import androidx.lifecycle.lifecycleScope import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData -import com.example.chiecnonkydieu.data.Player +import com.example.chiecnonkydieu.data.model.Player import com.example.chiecnonkydieu.databinding.ActivitySearchRoomBinding import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch class SearchRoomActivity : AppCompatActivity() { diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index 9617ba9..5adf7e1 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -1,9 +1,13 @@ package com.example.chiecnonkydieu.ui.playingRoom import android.content.Intent +import android.opengl.Visibility import android.os.Bundle +import android.text.Editable import android.view.LayoutInflater +import android.view.View import android.widget.EditText +import android.widget.LinearLayout import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels @@ -13,22 +17,21 @@ import androidx.appcompat.widget.Toolbar import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.core.view.marginTop import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope -import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -import androidx.recyclerview.widget.RecyclerView.LayoutManager import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.adapters.LetterCardAdapter import com.example.chiecnonkydieu.data.GameData +import com.example.chiecnonkydieu.data.GameData.gameModel import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.LetterCard +import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityPlayingRoomBinding import com.example.chiecnonkydieu.ui.wheel.WheelActivity import com.example.chiecnonkydieu.ui.wheel.WheelViewModel -import com.google.android.material.carousel.CarouselLayoutManager import kotlinx.coroutines.launch import rubikstudio.library.LuckyWheelView @@ -37,6 +40,9 @@ class PlayingRoomActivity : AppCompatActivity() { private lateinit var binding: ActivityPlayingRoomBinding // TODO + var indexWheel: Int = -1 + var currentValueWheel: String = "" + lateinit var adapter: LetterCardAdapter lateinit var recyclerView: RecyclerView override fun onCreate(savedInstanceState: Bundle?) { @@ -78,20 +84,30 @@ class PlayingRoomActivity : AppCompatActivity() { recyclerView.adapter = adapter + + + binding.btnDoan.setOnClickListener { if (playingRoomViewModel.isUserInputMatch(binding.edtDoan.text.toString()[0])) { updateAdapterAndRecyclerView() + Toast.makeText(this, "Bạn đoán đúng rồi", Toast.LENGTH_LONG).show() + } + else { + Toast.makeText(this, "Bạn đoán sai rồi", Toast.LENGTH_LONG).show() } + binding.edtDoan.text?.clear() } binding.btnGiai.setOnClickListener { - Toast.makeText(this, "Giai click", Toast.LENGTH_LONG).show() - showDialog() + showDialog(playingRoomViewModel) + } binding.iconButton.setOnClickListener { Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show() + playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerList.random()) + updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) } // Wheel @@ -100,14 +116,12 @@ class PlayingRoomActivity : AppCompatActivity() { wheelViewModel.initLuckyItemList(this) luckyWheelView.setData(wheelViewModel.luckyItemsList) - luckyWheelView.setLuckyRoundItemSelectedListener { - Toast.makeText(this, "on click", Toast.LENGTH_LONG).show() - goToWheelActivity() - } luckyWheelView.isTouchEnabled = false binding.llWheel.setOnClickListener { goToWheelActivity() } + + } @@ -122,15 +136,46 @@ class PlayingRoomActivity : AppCompatActivity() { private fun updateUi(gameModel: GameModel?) { if (gameModel != null) { - gameModel.gameStatus = GameStatus.INPROGRESS - updatePlayerList(gameModel) - updateCurrentQuestion(gameModel) + updateWheel(gameModel) + updateVisibility(gameModel) + updateCurrentQuestionAndAnswer(gameModel) + updateAdapterAndRecyclerView() + } + } + + fun updateWheel(gameModel: GameModel) { + if (!gameModel.currentSpinValue.isNullOrEmpty() && gameModel.currentSpinValue != currentValueWheel ) { + val wheelViewModel: WheelViewModel by viewModels() + for (i in wheelViewModel.luckyItemsList.indices) { + if (wheelViewModel.luckyItemsList[i].secondaryText == gameModel.currentSpinValue) { + indexWheel = i + currentValueWheel = gameModel.currentSpinValue + break + } + } + binding.luckyWheel.setRound(0) + binding.luckyWheel.startLuckyWheelWithTargetIndex(indexWheel) + } + } + fun updateVisibility(gameModel: GameModel) { + + if (gameModel.gameStatus == GameStatus.INPROGRESS) { + binding.llHint.visibility = View.GONE + binding.tilDoan.visibility = View.GONE + binding.btnDoan.visibility = View.GONE + binding.btnGiai.visibility = View.GONE + } + else { + binding.llHint.visibility = View.VISIBLE + binding.tilDoan.visibility = View.VISIBLE + binding.btnDoan.visibility = View.VISIBLE + binding.btnGiai.visibility = View.VISIBLE } } - private fun showDialog() { - var m_Text: String = "" + private fun showDialog(viewModel: PlayingRoomViewModel) { + val builder = AlertDialog.Builder(this) builder.setTitle("Nhập lời giải cho câu hỏi") val viewInflated = LayoutInflater.from(this).inflate(R.layout.dialog_edittext, null, false) @@ -138,8 +183,12 @@ class PlayingRoomActivity : AppCompatActivity() { builder.setView(viewInflated) builder.setPositiveButton(android.R.string.ok) { dialog, which -> + + if (input.text.toString() == GameData.gameModel.value!!.currentQuestionAnswer.answer) { + Toast.makeText(this, "giai thanh cong", Toast.LENGTH_LONG).show() + viewModel.makeAllLetterCardReveal() + } dialog.dismiss() - m_Text = input.text.toString() } builder.setNegativeButton(android.R.string.cancel) { dialog, which -> @@ -149,10 +198,13 @@ class PlayingRoomActivity : AppCompatActivity() { builder.show() + + } - private fun updateCurrentQuestion(gameModel: GameModel) { - binding.tvQuestion.text = gameModel.currentQuestion + private fun updateCurrentQuestionAndAnswer(gameModel: GameModel) { + binding.tvQuestion.text = gameModel.currentQuestionAnswer.question + updateAdapterAndRecyclerView() } private fun updatePlayerList(gameModel: GameModel) { diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index f6e0a26..b006af3 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -1,10 +1,13 @@ package com.example.chiecnonkydieu.ui.playingRoom +import androidx.core.text.isDigitsOnly import androidx.lifecycle.ViewModel import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel -import com.example.chiecnonkydieu.data.Player -import java.util.Locale +import com.example.chiecnonkydieu.data.GameStatus +import com.example.chiecnonkydieu.data.model.LetterCard +import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.data.model.QuestionAnswer class PlayingRoomViewModel: ViewModel() { @@ -21,29 +24,47 @@ class PlayingRoomViewModel: ViewModel() { val playerInputStringLC = playerInputChar.lowercaseChar().toString() val playerInputCharLC = playerInputChar.lowercaseChar() gameModel?.let { - if (gameModel.currentAnswer.contains(playerInputStringLC, ignoreCase = true) && + if (gameModel.currentQuestionAnswer.answer.contains( + playerInputStringLC, + ignoreCase = true + ) && ! gameModel.guessesCharacters.contains(playerInputStringLC)) { saveGuessedChar(playerInputCharLC) - gameModel.guessesCharacters.add(playerInputStringLC) gameModel.letterCardList.filter { it.letter.toLowerCase() == playerInputStringLC } .forEach{ it.isHidden = false } + updateStatusGameModel(GameStatus.INPROGRESS) + updateScore(gameModel) GameData.saveGameModel(gameModel) return true } else { return false; } - } return false } - fun setQuestionAndCurrentWordToBeGuessed(): Boolean { + fun setQuestionAndCurrentWordToBeGuessed(questionAnswer: QuestionAnswer): Boolean { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + if (!gameModel.previousQuestionAnswers.contains(questionAnswer)) { + gameModel.previousQuestionAnswers.add(questionAnswer) + gameModel.currentQuestionAnswer = questionAnswer + val tempLetterCardList = mutableListOf() + for (i in questionAnswer.answer.indices) { + tempLetterCardList.add(LetterCard(questionAnswer.answer[i].toString())) + } + gameModel.letterCardList = tempLetterCardList + GameData.saveGameModel(gameModel) + return true + } + return false + } return false } @@ -54,11 +75,24 @@ class PlayingRoomViewModel: ViewModel() { } } - - private fun updateScore(indexPlayer: Int, score: Int) { + fun updateStatusGameModel(gameStatus: GameStatus) { val gameModel: GameModel? = GameData.gameModel.value gameModel?.let { - gameModel.playersList[indexPlayer].score += score + gameModel.gameStatus = gameStatus + } + } + + fun updateScore(gameModel: GameModel) { + gameModel.let { + val currentPlayer = gameModel.currentPlayer + val currentSpinValue = gameModel.currentSpinValue + val index = gameModel.playersList.indexOfFirst { it.name == currentPlayer.name } + gameModel.playersList[index].score += currentSpinValue.toInt() + if (currentSpinValue.isDigitsOnly()) { + + } else { + + } } } @@ -73,10 +107,27 @@ class PlayingRoomViewModel: ViewModel() { // TODO } - private fun checkRoundWin(): Boolean { + + fun checkRoundWin(): Boolean { + for (i in gameModel.letterCardList.indices) { + if (gameModel.letterCardList[i].isHidden) { + return false + } + } return true + GameData.saveGameModel(gameModel) } - private fun checkGameWin(): Boolean { + + fun makeAllLetterCardReveal() { + for (i in gameModel.letterCardList.indices) { + if (gameModel.letterCardList[i].isHidden) { + gameModel.letterCardList[i].isHidden = false + } + } + GameData.saveGameModel(gameModel) + } + + fun checkGameWin(): Boolean { return true } diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt index 25df369..2eb6838 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt @@ -14,9 +14,12 @@ import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.get import com.example.chiecnonkydieu.R +import com.example.chiecnonkydieu.data.GameData +import com.example.chiecnonkydieu.data.GameStatus import com.example.chiecnonkydieu.databinding.ActivityWheelBinding import rubikstudio.library.LuckyWheelView import rubikstudio.library.model.LuckyItem +import java.util.concurrent.locks.ReentrantLock class WheelActivity : AppCompatActivity() { private lateinit var binding: ActivityWheelBinding @@ -45,23 +48,25 @@ class WheelActivity : AppCompatActivity() { wheelViewModel.initLuckyItemList(this) luckyWheelView.setData(wheelViewModel.luckyItemsList) - luckyWheelView.isTouchEnabled = false luckyWheelView.setOnTouchListener { v, event -> + countDownTimer.cancel() val indexAns = wheelViewModel.getIndexAfterRotate() luckyWheelView.setRound(3) luckyWheelView.startLuckyWheelWithTargetIndex(indexAns) Toast.makeText(this, wheelViewModel.getStringItemAtIndex(indexAns), Toast.LENGTH_LONG).show() - return@setOnTouchListener true + wheelViewModel.updateCurrentSpinValue(wheelViewModel.getStringItemAtIndex(indexAns)) + return@setOnTouchListener false } binding.btnQuay.setOnClickListener { + countDownTimer.cancel() val indexAns = wheelViewModel.getIndexAfterRotate() luckyWheelView.setRound(3) luckyWheelView.startLuckyWheelWithTargetIndex(indexAns) Toast.makeText(this, wheelViewModel.getStringItemAtIndex(indexAns), Toast.LENGTH_LONG).show() -// Toast.makeText(this, wheelViewModel.luckyItemsList.get(3).secondaryText, Toast.LENGTH_LONG).show() + wheelViewModel.updateCurrentSpinValue(wheelViewModel.getStringItemAtIndex(indexAns)) } // count down timer diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt index 0377ddc..96dcef0 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt @@ -1,13 +1,16 @@ package com.example.chiecnonkydieu.ui.wheel import android.content.Context -import android.graphics.Color import androidx.core.content.ContextCompat +import androidx.core.text.isDigitsOnly import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.example.chiecnonkydieu.R -import com.example.chiecnonkydieu.data.LetterCard +import com.example.chiecnonkydieu.data.GameData +import com.example.chiecnonkydieu.data.GameModel +import com.example.chiecnonkydieu.data.GameStatus +import com.example.chiecnonkydieu.data.model.LetterCard import rubikstudio.library.model.LuckyItem class WheelViewModel: ViewModel() { private val _luckyItemsList: MutableList = mutableListOf() @@ -89,4 +92,21 @@ class WheelViewModel: ViewModel() { _luckyItemsList[index].topText } } + + fun updateStatusGameModel(gameStatus: GameStatus) { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.gameStatus = gameStatus + } + } + + fun updateCurrentSpinValue(spinValue: String) { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.currentSpinValue = spinValue + updateStatusGameModel(GameStatus.GUESS) + GameData.saveGameModel(gameModel) + } + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_playing_room.xml b/app/src/main/res/layout/activity_playing_room.xml index 9c67953..7c7a3d8 100644 --- a/app/src/main/res/layout/activity_playing_room.xml +++ b/app/src/main/res/layout/activity_playing_room.xml @@ -34,7 +34,7 @@ app:layout_constraintTop_toBottomOf="@id/toolbar"> @@ -123,7 +123,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" - android:text="10" /> + android:text="0" /> @@ -178,7 +178,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" - android:text="10" /> + android:text="0" /> @@ -186,8 +186,10 @@ @@ -319,27 +321,27 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" - android:focusable="true" android:elevation="100dp" + android:focusable="true" + app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@+id/btn_giai" - app:layout_constraintEnd_toEndOf="parent"> + app:layout_constraintTop_toBottomOf="@+id/btn_giai"> + app:lkwTopTextSize="12sp" /> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b807f7..bc83527 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,4 +25,8 @@ Đến lượt của bạn Còn lại %s s Nhập lời giải + + + + \ No newline at end of file From 2887c2702e2daa381268e75a20324ad42b2b2caf Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Thu, 18 Apr 2024 11:02:44 +0700 Subject: [PATCH 03/16] Use connectivityManager to check internet state --- .../example/chiecnonkydieu/MainActivity.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/src/main/java/com/example/chiecnonkydieu/MainActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/MainActivity.kt index 134ae6f..01c98c5 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/MainActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/MainActivity.kt @@ -1,9 +1,13 @@ package com.example.chiecnonkydieu +import android.content.Context import android.content.Intent +import android.net.ConnectivityManager import android.os.Bundle +import android.view.View import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.example.chiecnonkydieu.databinding.ActivityMainBinding @@ -11,6 +15,7 @@ import com.example.chiecnonkydieu.ui.CreateRoomActivity import com.example.chiecnonkydieu.ui.playingRoom.PlayingRoomActivity import com.example.chiecnonkydieu.ui.SearchRoomActivity import com.example.chiecnonkydieu.ui.wheel.WheelActivity +import com.google.android.material.snackbar.Snackbar class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding @@ -20,6 +25,9 @@ class MainActivity : AppCompatActivity() { binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) + if (!isNetworkAvailable()) { + showNoInternetMessage() + } ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) @@ -57,4 +65,22 @@ class MainActivity : AppCompatActivity() { intent.putExtra("room_id", "7232") startActivity(intent) } + + private fun isNetworkAvailable(): Boolean { + val connectivityManager = + getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val networkInfo = connectivityManager.activeNetworkInfo + return networkInfo != null && networkInfo.isConnected + } + + private fun showNoInternetMessage() { + val rootView: View = findViewById(android.R.id.content) + val snackbar = Snackbar.make( + rootView, + "Không có kết nối internet", + Snackbar.LENGTH_INDEFINITE + ) + snackbar.view.setBackgroundColor(ContextCompat.getColor(this, R.color.orange)) + snackbar.show() + } } \ No newline at end of file From 7a8c82ed516093e9af9260672f857815cc73592c Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Thu, 18 Apr 2024 11:17:48 +0700 Subject: [PATCH 04/16] Add feature change turn when a player enter a letter --- .idea/deploymentTargetDropDown.xml | 2 +- app/build.gradle.kts | 3 + .../chiecnonkydieu/ui/CreateRoomActivity.kt | 33 +++++++- .../chiecnonkydieu/ui/SearchRoomActivity.kt | 20 +++++ .../chiecnonkydieu/ui/WaitingRoomActivity.kt | 9 ++ .../{ => ui}/adapters/LetterCardAdapter.kt | 2 +- .../ui/playingRoom/PlayingRoomActivity.kt | 82 +++++++++++++++++-- .../ui/playingRoom/PlayingRoomViewModel.kt | 34 +++++++- .../main/res/layout/activity_playing_room.xml | 2 +- 9 files changed, 170 insertions(+), 17 deletions(-) rename app/src/main/java/com/example/chiecnonkydieu/{ => ui}/adapters/LetterCardAdapter.kt (97%) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 2fe4f5f..8c164cd 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -15,7 +15,7 @@ - + diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 882371f..7b54978 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -57,4 +57,7 @@ dependencies { //wheel implementation("androidx.navigation:navigation-dynamic-features-fragment:2.7.7") implementation ("com.github.thanhniencung:LuckyWheel:a6110f5128") + + // DataStore + implementation("androidx.datastore:datastore-preferences:1.0.0") } \ No newline at end of file diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt index 11d86d3..41b19fb 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt @@ -1,12 +1,19 @@ package com.example.chiecnonkydieu.ui +import android.content.Context import android.content.Intent import android.os.Bundle +import android.util.Log import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.lifecycleScope import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData @@ -16,10 +23,19 @@ import com.example.chiecnonkydieu.data.model.LetterCard import com.example.chiecnonkydieu.data.model.Player import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityCreateRoomBinding +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch + +val Context.dataStore: DataStore by preferencesDataStore(name = "player_data") +val CURRENT_PLAYER = stringPreferencesKey("current_player") class CreateRoomActivity : AppCompatActivity() { + private lateinit var binding: ActivityCreateRoomBinding + + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() @@ -57,6 +73,8 @@ class CreateRoomActivity : AppCompatActivity() { GameData.saveGameModel( gameModel ) + saveCurrentPlayerToPreferencesDataStore(binding.edtName.text.toString()) + } goToWaitingRooom() } @@ -68,14 +86,25 @@ class CreateRoomActivity : AppCompatActivity() { } - fun getLetterCardListFromAnswer(answer: String): MutableList{ + private fun saveCurrentPlayerToPreferencesDataStore(currentPlayer: String) { + lifecycleScope.launch { + dataStore.edit { settings -> + settings[CURRENT_PLAYER] = currentPlayer + } + val savedPlayer = dataStore.data.first()[CURRENT_PLAYER] + Log.d("DataStore", "Saved player from create: $savedPlayer") + } + } + + + private fun getLetterCardListFromAnswer(answer: String): MutableList{ val list = mutableListOf() for (i in answer.indices) { list.add(LetterCard(answer[i].toString())) } return list } - fun goToWaitingRooom() { + private fun goToWaitingRooom() { val intent = Intent(this, WaitingRoomActivity::class.java) intent.putExtra("room_id", binding.tvMaPhong.text.toString()) startActivity(intent) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt index 15dcaa0..e138757 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt @@ -1,23 +1,32 @@ package com.example.chiecnonkydieu.ui +import android.content.Context import android.content.Intent import android.os.Bundle +import android.util.Log import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.lifecycleScope import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.model.Player import com.example.chiecnonkydieu.databinding.ActivitySearchRoomBinding import kotlinx.coroutines.async +import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch class SearchRoomActivity : AppCompatActivity() { private lateinit var binding: ActivitySearchRoomBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() @@ -47,6 +56,7 @@ class SearchRoomActivity : AppCompatActivity() { }.await() if (isJoined) { + saveCurrentPlayerToPreferencesDataStore(binding.edtName.text.toString()) goToWaitingRooom() } else { Toast.makeText(applicationContext, "Error when connect game", Toast.LENGTH_LONG).show() @@ -56,6 +66,16 @@ class SearchRoomActivity : AppCompatActivity() { } } + private fun saveCurrentPlayerToPreferencesDataStore(currentPlayer: String) { + lifecycleScope.launch { + dataStore.edit { settings -> + settings[CURRENT_PLAYER] = currentPlayer + } + val savedPlayer = dataStore.data.first()[CURRENT_PLAYER] + Log.d("DataStore", "Saved player from search: $savedPlayer") + } + } + fun goToWaitingRooom() { try { val intent = Intent(this, WaitingRoomActivity::class.java) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt index a539783..9fe73a1 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt @@ -1,5 +1,6 @@ package com.example.chiecnonkydieu.ui +import android.content.Context import android.content.Intent import android.os.Bundle import androidx.activity.enableEdgeToEdge @@ -8,18 +9,25 @@ import androidx.appcompat.widget.Toolbar import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus +import com.example.chiecnonkydieu.data.model.Player import com.example.chiecnonkydieu.databinding.ActivityWaitingRoomBinding import com.example.chiecnonkydieu.ui.playingRoom.PlayingRoomActivity import kotlinx.coroutines.launch class WaitingRoomActivity : AppCompatActivity() { private lateinit var binding: ActivityWaitingRoomBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() @@ -138,6 +146,7 @@ class WaitingRoomActivity : AppCompatActivity() { } + override fun onSupportNavigateUp(): Boolean { onBackPressed() return true diff --git a/app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt similarity index 97% rename from app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt rename to app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt index 97a264c..63cf748 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/adapters/LetterCardAdapter.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt @@ -1,4 +1,4 @@ -package com.example.chiecnonkydieu.adapters +package com.example.chiecnonkydieu.ui.adapters import android.graphics.Color import android.view.LayoutInflater diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index 5adf7e1..b936d31 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -1,5 +1,6 @@ package com.example.chiecnonkydieu.ui.playingRoom +import android.content.Context import android.content.Intent import android.opengl.Visibility import android.os.Bundle @@ -18,24 +19,38 @@ import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.marginTop +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.viewModelScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.chiecnonkydieu.R -import com.example.chiecnonkydieu.adapters.LetterCardAdapter +import com.example.chiecnonkydieu.ui.adapters.LetterCardAdapter import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameData.gameModel import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityPlayingRoomBinding +import com.example.chiecnonkydieu.ui.dataStore import com.example.chiecnonkydieu.ui.wheel.WheelActivity import com.example.chiecnonkydieu.ui.wheel.WheelViewModel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import rubikstudio.library.LuckyWheelView + +private val Context.dataStore: DataStore by preferencesDataStore(name = "player_data") +val CURRENT_PLAYER = stringPreferencesKey("current_player") class PlayingRoomActivity : AppCompatActivity() { private lateinit var binding: ActivityPlayingRoomBinding // TODO @@ -80,7 +95,7 @@ class PlayingRoomActivity : AppCompatActivity() { layoutManager.orientation = LinearLayoutManager.HORIZONTAL // Set the orientation to horizontal if needed recyclerView.layoutManager = layoutManager - adapter = LetterCardAdapter(GameData.gameModel.value!!.letterCardList) + adapter = LetterCardAdapter(gameModel.value!!.letterCardList) recyclerView.adapter = adapter @@ -100,7 +115,11 @@ class PlayingRoomActivity : AppCompatActivity() { } binding.btnGiai.setOnClickListener { - showDialog(playingRoomViewModel) +// showDialog(playingRoomViewModel) + lifecycleScope.launch { + Toast.makeText(applicationContext, "Current player: " + checkCurrentPlayer().toString(), Toast.LENGTH_LONG).show() + } + } @@ -118,7 +137,12 @@ class PlayingRoomActivity : AppCompatActivity() { luckyWheelView.setData(wheelViewModel.luckyItemsList) luckyWheelView.isTouchEnabled = false binding.llWheel.setOnClickListener { - goToWheelActivity() + if (checkCurrentPlayer()) { + goToWheelActivity() + } + else { + Toast.makeText(applicationContext, "Không phải lượt của bạn!", Toast.LENGTH_LONG).show() + } } @@ -134,6 +158,15 @@ class PlayingRoomActivity : AppCompatActivity() { startActivity(intent) } + + fun checkCurrentPlayer(): Boolean { + var savedPlayer: String? = "" + lifecycleScope.launchWhenCreated { + savedPlayer = dataStore.data.first()[CURRENT_PLAYER] + } + return savedPlayer == GameData.gameModel.value?.currentPlayer?.name + + } private fun updateUi(gameModel: GameModel?) { if (gameModel != null) { updatePlayerList(gameModel) @@ -154,8 +187,16 @@ class PlayingRoomActivity : AppCompatActivity() { break } } - binding.luckyWheel.setRound(0) - binding.luckyWheel.startLuckyWheelWithTargetIndex(indexWheel) + // Kiem tra xem neu la nguoi choi hien tai thi chi cho round la 0, neu khong thi cho 3 + if (true) { + // TODO + binding.luckyWheel.setRound(0) + binding.luckyWheel.startLuckyWheelWithTargetIndex(indexWheel) + } + else { + binding.luckyWheel.setRound(3) + binding.luckyWheel.startLuckyWheelWithTargetIndex(indexWheel) + } } } fun updateVisibility(gameModel: GameModel) { @@ -166,7 +207,7 @@ class PlayingRoomActivity : AppCompatActivity() { binding.btnDoan.visibility = View.GONE binding.btnGiai.visibility = View.GONE } - else { + else if (checkCurrentPlayer()){ binding.llHint.visibility = View.VISIBLE binding.tilDoan.visibility = View.VISIBLE binding.btnDoan.visibility = View.VISIBLE @@ -217,6 +258,8 @@ class PlayingRoomActivity : AppCompatActivity() { binding.imgLogo1.setImageResource(R.drawable.ic_man) binding.tvPlayerName1.text = gameModel.playersList[0].name binding.tvScore1.text = gameModel.playersList[0].score.toString() + binding.cvPlayer2.visibility = View.INVISIBLE + binding.cvPlayer3.visibility = View.INVISIBLE } 2 -> { // player1 and player 2 @@ -229,7 +272,7 @@ class PlayingRoomActivity : AppCompatActivity() { binding.tvScore1.text = gameModel.playersList[0].score.toString() binding.tvScore2.text = gameModel.playersList[1].score.toString() - + binding.cvPlayer3.visibility = View.INVISIBLE } @@ -249,10 +292,31 @@ class PlayingRoomActivity : AppCompatActivity() { binding.tvScore3.text = gameModel.playersList[2].score.toString() } } + + val colorResId = R.color.orange + val currentPlayerColor = ContextCompat.getColor(this, colorResId) + + val colorResBackgroundId = R.color.background + val backgroundColor = ContextCompat.getColor(this, colorResBackgroundId) + if (gameModel.currentPlayer.name == binding.tvPlayerName1.text.toString()) { + binding.cvPlayer1.setCardBackgroundColor(currentPlayerColor) + binding.cvPlayer2.setCardBackgroundColor(backgroundColor) + binding.cvPlayer3.setCardBackgroundColor(backgroundColor) + } + else if (gameModel.currentPlayer.name == binding.tvPlayerName2.text.toString()){ + binding.cvPlayer2.setCardBackgroundColor(currentPlayerColor) + binding.cvPlayer1.setCardBackgroundColor(backgroundColor) + binding.cvPlayer3.setCardBackgroundColor(backgroundColor) + } + else { + binding.cvPlayer3.setCardBackgroundColor(currentPlayerColor) + binding.cvPlayer2.setCardBackgroundColor(backgroundColor) + binding.cvPlayer1.setCardBackgroundColor(backgroundColor) + } } private fun updateAdapterAndRecyclerView() { - adapter = LetterCardAdapter(GameData.gameModel.value!!.letterCardList) + adapter = LetterCardAdapter(gameModel.value!!.letterCardList) recyclerView.adapter = adapter adapter.notifyDataSetChanged() } diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index b006af3..0862b6c 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -1,18 +1,34 @@ package com.example.chiecnonkydieu.ui.playingRoom +import android.content.Context import androidx.core.text.isDigitsOnly +import androidx.datastore.core.DataStore +import androidx.datastore.dataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.example.chiecnonkydieu.data.GameData +import com.example.chiecnonkydieu.data.GameData.gameModel import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus import com.example.chiecnonkydieu.data.model.LetterCard import com.example.chiecnonkydieu.data.model.Player import com.example.chiecnonkydieu.data.model.QuestionAnswer +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch + + class PlayingRoomViewModel: ViewModel() { private lateinit var gameModel: GameModel + init { if (GameData.gameModel.value != null) { gameModel = GameData.gameModel.value!! @@ -38,14 +54,17 @@ class PlayingRoomViewModel: ViewModel() { } updateStatusGameModel(GameStatus.INPROGRESS) updateScore(gameModel) + changeTurn() GameData.saveGameModel(gameModel) return true } else { + updateStatusGameModel(GameStatus.INPROGRESS) + changeTurn() + GameData.saveGameModel(gameModel) return false; } } - return false } @@ -60,6 +79,7 @@ class PlayingRoomViewModel: ViewModel() { tempLetterCardList.add(LetterCard(questionAnswer.answer[i].toString())) } gameModel.letterCardList = tempLetterCardList + GameData.saveGameModel(gameModel) return true } @@ -96,10 +116,18 @@ class PlayingRoomViewModel: ViewModel() { } } - private fun changeTurn(player: Player) { + + + private fun changeTurn() { val gameModel: GameModel? = GameData.gameModel.value + var indexNewPlayer = -1 gameModel?.let { - gameModel.currentPlayer = player + for (i in gameModel.playersList.indices) { + if (gameModel.playersList[i].name == gameModel.currentPlayer.name) { + indexNewPlayer = (i + 1) % gameModel.playersList.size + } + } + gameModel.currentPlayer = gameModel.playersList[indexNewPlayer] } } diff --git a/app/src/main/res/layout/activity_playing_room.xml b/app/src/main/res/layout/activity_playing_room.xml index 7c7a3d8..7474ba4 100644 --- a/app/src/main/res/layout/activity_playing_room.xml +++ b/app/src/main/res/layout/activity_playing_room.xml @@ -25,7 +25,7 @@ android:layout_height="wrap_content" android:elevation="20dp" android:orientation="vertical" - app:cardBackgroundColor="@color/orange" + app:cardBackgroundColor="@color/background" app:cardCornerRadius="20dp" app:contentPadding="10dp" app:layout_constraintEnd_toStartOf="@+id/cv_player2" From 2c5596d3b51ba81d58a568fdb3da1116c6487aec Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Fri, 19 Apr 2024 21:24:25 +0700 Subject: [PATCH 05/16] Show winner dialog when all character is unhidden --- .idea/deploymentTargetDropDown.xml | 15 +----- .../example/chiecnonkydieu/data/DataSource.kt | 2 +- .../example/chiecnonkydieu/data/GameData.kt | 2 +- .../example/chiecnonkydieu/data/GameModel.kt | 8 ++-- .../{data => }/model/LetterCard.kt | 2 +- .../chiecnonkydieu/{data => }/model/Player.kt | 2 +- .../{data => }/model/QuestionAnswer.kt | 2 +- .../chiecnonkydieu/ui/CreateRoomActivity.kt | 4 +- .../chiecnonkydieu/ui/SearchRoomActivity.kt | 2 +- .../chiecnonkydieu/ui/WaitingRoomActivity.kt | 2 +- .../ui/adapters/LetterCardAdapter.kt | 2 +- .../ui/playingRoom/PlayingRoomActivity.kt | 46 ++++++++++++++++--- .../ui/playingRoom/PlayingRoomViewModel.kt | 30 ++++++++---- .../chiecnonkydieu/ui/wheel/WheelViewModel.kt | 2 +- app/src/main/res/values/strings.xml | 3 ++ 15 files changed, 79 insertions(+), 45 deletions(-) rename app/src/main/java/com/example/chiecnonkydieu/{data => }/model/LetterCard.kt (65%) rename app/src/main/java/com/example/chiecnonkydieu/{data => }/model/Player.kt (70%) rename app/src/main/java/com/example/chiecnonkydieu/{data => }/model/QuestionAnswer.kt (75%) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 8c164cd..0c0c338 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -3,20 +3,7 @@ - - - - - - - - - - - - - - + diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt b/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt index f75fab0..53e836e 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt @@ -2,7 +2,7 @@ package com.example.chiecnonkydieu.data import android.content.Context import com.example.chiecnonkydieu.R -import com.example.chiecnonkydieu.data.model.QuestionAnswer +import com.example.chiecnonkydieu.model.QuestionAnswer val questionAnswerList: List = mutableListOf( QuestionAnswer("Thủ đô của Việt Nam nằm ở đâu?", "HA NOI"), diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt b/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt index 80d790e..3e2b65f 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/GameData.kt @@ -5,7 +5,7 @@ import android.util.Log import android.widget.Toast import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.model.Player import com.google.firebase.Firebase import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt index 28b224f..e71516d 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt @@ -1,8 +1,8 @@ package com.example.chiecnonkydieu.data -import com.example.chiecnonkydieu.data.model.LetterCard -import com.example.chiecnonkydieu.data.model.Player -import com.example.chiecnonkydieu.data.model.QuestionAnswer +import com.example.chiecnonkydieu.model.LetterCard +import com.example.chiecnonkydieu.model.Player +import com.example.chiecnonkydieu.model.QuestionAnswer data class GameModel( val gameId: Int = -1, @@ -24,5 +24,7 @@ enum class GameStatus { JOINED2, INPROGRESS, GUESS, + ENDROUND, + WAITING_TO_CONTINUE, FINISHED } diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/model/LetterCard.kt b/app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt similarity index 65% rename from app/src/main/java/com/example/chiecnonkydieu/data/model/LetterCard.kt rename to app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt index 4db64a0..5bea5b1 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/model/LetterCard.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt @@ -1,4 +1,4 @@ -package com.example.chiecnonkydieu.data.model +package com.example.chiecnonkydieu.model data class LetterCard( val letter: String = "A", diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/model/Player.kt b/app/src/main/java/com/example/chiecnonkydieu/model/Player.kt similarity index 70% rename from app/src/main/java/com/example/chiecnonkydieu/data/model/Player.kt rename to app/src/main/java/com/example/chiecnonkydieu/model/Player.kt index 7c3f0f5..a99bb25 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/model/Player.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/model/Player.kt @@ -1,4 +1,4 @@ -package com.example.chiecnonkydieu.data.model +package com.example.chiecnonkydieu.model data class Player( val name: String = "Player", diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt b/app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt similarity index 75% rename from app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt rename to app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt index f7a2baa..b7e1a3e 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/model/QuestionAnswer.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt @@ -1,4 +1,4 @@ -package com.example.chiecnonkydieu.data.model +package com.example.chiecnonkydieu.model data class QuestionAnswer( val question: String = "Thủ đô của Việt Nam nằm ở đâu?", diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt index 41b19fb..fcaa1b5 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt @@ -19,8 +19,8 @@ import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.model.LetterCard -import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.model.LetterCard +import com.example.chiecnonkydieu.model.Player import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityCreateRoomBinding import kotlinx.coroutines.flow.Flow diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt index e138757..24e5f88 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/SearchRoomActivity.kt @@ -18,7 +18,7 @@ import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.lifecycleScope import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData -import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.model.Player import com.example.chiecnonkydieu.databinding.ActivitySearchRoomBinding import kotlinx.coroutines.async import kotlinx.coroutines.flow.first diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt index 9fe73a1..d0318d0 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/WaitingRoomActivity.kt @@ -20,7 +20,7 @@ import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.model.Player +import com.example.chiecnonkydieu.model.Player import com.example.chiecnonkydieu.databinding.ActivityWaitingRoomBinding import com.example.chiecnonkydieu.ui.playingRoom.PlayingRoomActivity import kotlinx.coroutines.launch diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt index 63cf748..6619f16 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/adapters/LetterCardAdapter.kt @@ -7,7 +7,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import android.widget.TextView import com.example.chiecnonkydieu.R -import com.example.chiecnonkydieu.data.model.LetterCard +import com.example.chiecnonkydieu.model.LetterCard import com.google.android.material.card.MaterialCardView class LetterCardAdapter(val letterCardList: List): RecyclerView.Adapter() { diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index b936d31..7866880 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -28,6 +28,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewModelScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.example.chiecnonkydieu.MainActivity import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.ui.adapters.LetterCardAdapter import com.example.chiecnonkydieu.data.GameData @@ -36,6 +37,7 @@ import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityPlayingRoomBinding +import com.example.chiecnonkydieu.ui.WaitingRoomActivity import com.example.chiecnonkydieu.ui.dataStore import com.example.chiecnonkydieu.ui.wheel.WheelActivity import com.example.chiecnonkydieu.ui.wheel.WheelViewModel @@ -53,7 +55,7 @@ private val Context.dataStore: DataStore by preferencesDataStore(na val CURRENT_PLAYER = stringPreferencesKey("current_player") class PlayingRoomActivity : AppCompatActivity() { private lateinit var binding: ActivityPlayingRoomBinding - // TODO + val playingRoomViewModel: PlayingRoomViewModel by viewModels() var indexWheel: Int = -1 var currentValueWheel: String = "" @@ -80,7 +82,7 @@ class PlayingRoomActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) - val playingRoomViewModel: PlayingRoomViewModel by viewModels() + lifecycleScope.launch { GameData.fetchGameModel(intent.getStringExtra("room_id").toString().toInt()) } @@ -106,6 +108,9 @@ class PlayingRoomActivity : AppCompatActivity() { if (playingRoomViewModel.isUserInputMatch(binding.edtDoan.text.toString()[0])) { updateAdapterAndRecyclerView() Toast.makeText(this, "Bạn đoán đúng rồi", Toast.LENGTH_LONG).show() +// if (playingRoomViewModel.checkRoundWin() && checkCurrentPlayer()) { +// showWinnerDialog(playingRoomViewModel) +// } } else { Toast.makeText(this, "Bạn đoán sai rồi", Toast.LENGTH_LONG).show() @@ -115,10 +120,10 @@ class PlayingRoomActivity : AppCompatActivity() { } binding.btnGiai.setOnClickListener { -// showDialog(playingRoomViewModel) - lifecycleScope.launch { - Toast.makeText(applicationContext, "Current player: " + checkCurrentPlayer().toString(), Toast.LENGTH_LONG).show() - } + showDialog(playingRoomViewModel) +//// lifecycleScope.launch { +//// Toast.makeText(applicationContext, "Current player: " + checkCurrentPlayer().toString(), Toast.LENGTH_LONG).show() +//// } } @@ -174,6 +179,10 @@ class PlayingRoomActivity : AppCompatActivity() { updateVisibility(gameModel) updateCurrentQuestionAndAnswer(gameModel) updateAdapterAndRecyclerView() + if (gameModel.gameStatus == GameStatus.ENDROUND && !checkCurrentPlayer()) { + showWinnerDialog(playingRoomViewModel) + playingRoomViewModel.updateStatusGameModel(GameStatus.WAITING_TO_CONTINUE) + } } } @@ -227,7 +236,8 @@ class PlayingRoomActivity : AppCompatActivity() { if (input.text.toString() == GameData.gameModel.value!!.currentQuestionAnswer.answer) { Toast.makeText(this, "giai thanh cong", Toast.LENGTH_LONG).show() - viewModel.makeAllLetterCardReveal() + viewModel.makeAllLetterCardReveal() + showWinnerDialog(viewModel) } dialog.dismiss() } @@ -237,9 +247,31 @@ class PlayingRoomActivity : AppCompatActivity() { } builder.show() + } + + private fun showWinnerDialog(viewModel: PlayingRoomViewModel) { + viewModel.updateGameEndRound() + val builder = AlertDialog.Builder(this) + builder.setTitle("Xin chức mừng") + builder.setMessage("Đáp án chính xác là: ${GameData.gameModel.value!!.currentQuestionAnswer.answer}") + builder.setPositiveButton(R.string.btn_next) { dialog, which -> + viewModel.updateStatusGameModel(GameStatus.INPROGRESS) + viewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerList.random()) + updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) + Toast.makeText(applicationContext, "choi tiep click", Toast.LENGTH_LONG).show() + dialog.dismiss() + } + builder.setNegativeButton(R.string.btn_thoat) { dialog, which -> + dialog.cancel() + val intent = Intent(this, MainActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK + startActivity(intent) + finish() + } + builder.show() } diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index 0862b6c..10e5579 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -10,11 +10,12 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameData.gameModel +import com.example.chiecnonkydieu.data.GameData.saveGameModel import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.model.LetterCard -import com.example.chiecnonkydieu.data.model.Player -import com.example.chiecnonkydieu.data.model.QuestionAnswer +import com.example.chiecnonkydieu.model.LetterCard +import com.example.chiecnonkydieu.model.Player +import com.example.chiecnonkydieu.model.QuestionAnswer import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch @@ -79,7 +80,7 @@ class PlayingRoomViewModel: ViewModel() { tempLetterCardList.add(LetterCard(questionAnswer.answer[i].toString())) } gameModel.letterCardList = tempLetterCardList - + resetGuessedCharacter() GameData.saveGameModel(gameModel) return true } @@ -103,6 +104,7 @@ class PlayingRoomViewModel: ViewModel() { } fun updateScore(gameModel: GameModel) { + // TODO xu ly item, extra turn, miss turn gameModel.let { val currentPlayer = gameModel.currentPlayer val currentSpinValue = gameModel.currentSpinValue @@ -143,7 +145,7 @@ class PlayingRoomViewModel: ViewModel() { } } return true - GameData.saveGameModel(gameModel) + } fun makeAllLetterCardReveal() { @@ -152,12 +154,20 @@ class PlayingRoomViewModel: ViewModel() { gameModel.letterCardList[i].isHidden = false } } - GameData.saveGameModel(gameModel) +// GameData.saveGameModel(gameModel) } - - fun checkGameWin(): Boolean { - return true + fun resetGuessedCharacter() { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.guessesCharacters.clear() + } + } + fun updateGameEndRound() { + val gameModel: GameModel? = GameData.gameModel.value + gameModel?.let { + gameModel.gameStatus = GameStatus.ENDROUND + GameData.saveGameModel(gameModel) + } } - } \ No newline at end of file diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt index 96dcef0..ed79511 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt @@ -10,7 +10,7 @@ import com.example.chiecnonkydieu.R import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus -import com.example.chiecnonkydieu.data.model.LetterCard +import com.example.chiecnonkydieu.model.LetterCard import rubikstudio.library.model.LuckyItem class WheelViewModel: ViewModel() { private val _luckyItemsList: MutableList = mutableListOf() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bc83527..d7a97cb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -26,6 +26,9 @@ Còn lại %s s Nhập lời giải + Thoát + Chơi tiếp + From d152c8992b1a24559db6db6999213199cea1238d Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Fri, 19 Apr 2024 21:54:08 +0700 Subject: [PATCH 06/16] Add logic for next button when showing winner dialog --- .../ui/playingRoom/PlayingRoomActivity.kt | 18 ++++++++++++++---- .../ui/playingRoom/PlayingRoomViewModel.kt | 8 ++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index 7866880..5941085 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -108,9 +108,11 @@ class PlayingRoomActivity : AppCompatActivity() { if (playingRoomViewModel.isUserInputMatch(binding.edtDoan.text.toString()[0])) { updateAdapterAndRecyclerView() Toast.makeText(this, "Bạn đoán đúng rồi", Toast.LENGTH_LONG).show() -// if (playingRoomViewModel.checkRoundWin() && checkCurrentPlayer()) { + if (playingRoomViewModel.checkRoundWin() && checkCurrentPlayer()) { // showWinnerDialog(playingRoomViewModel) -// } + Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() + + } } else { Toast.makeText(this, "Bạn đoán sai rồi", Toast.LENGTH_LONG).show() @@ -130,7 +132,11 @@ class PlayingRoomActivity : AppCompatActivity() { binding.iconButton.setOnClickListener { Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show() - playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerList.random()) + var questionAnswerToBeGuess = questionAnswerList.random() + while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { + questionAnswerToBeGuess = questionAnswerList.random() + } + playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerToBeGuess) updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) } @@ -258,7 +264,11 @@ class PlayingRoomActivity : AppCompatActivity() { builder.setPositiveButton(R.string.btn_next) { dialog, which -> viewModel.updateStatusGameModel(GameStatus.INPROGRESS) - viewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerList.random()) + var questionAnswerToBeGuess = questionAnswerList.random() + while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { + questionAnswerToBeGuess = questionAnswerList.random() + } + playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerToBeGuess) updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) Toast.makeText(applicationContext, "choi tiep click", Toast.LENGTH_LONG).show() dialog.dismiss() diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index 10e5579..e20f54e 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -109,10 +109,14 @@ class PlayingRoomViewModel: ViewModel() { val currentPlayer = gameModel.currentPlayer val currentSpinValue = gameModel.currentSpinValue val index = gameModel.playersList.indexOfFirst { it.name == currentPlayer.name } - gameModel.playersList[index].score += currentSpinValue.toInt() if (currentSpinValue.isDigitsOnly()) { + gameModel.playersList[index].score += currentSpinValue.toInt() + } else if (currentSpinValue == "ITEM"){ + // TODO + } else if (currentSpinValue == "MISS_TURN") { - } else { + } + else if (currentSpinValue == "EXTRA_TURN") { } } From 602b4326b1ec663ca9f1a441192a1aac87e45af0 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Sat, 20 Apr 2024 11:18:35 +0700 Subject: [PATCH 07/16] Add feature checkRoundWin when all letters is revealed --- .../chiecnonkydieu/model/LetterCard.kt | 2 +- .../ui/playingRoom/PlayingRoomActivity.kt | 30 +++++++++++-------- .../ui/playingRoom/PlayingRoomViewModel.kt | 16 ++++++---- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt b/app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt index 5bea5b1..85271e5 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/model/LetterCard.kt @@ -2,5 +2,5 @@ package com.example.chiecnonkydieu.model data class LetterCard( val letter: String = "A", - var isHidden: Boolean = true + var isHidden: Boolean = letter != " " ) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index 5941085..cfb9e6d 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -107,16 +107,17 @@ class PlayingRoomActivity : AppCompatActivity() { binding.btnDoan.setOnClickListener { if (playingRoomViewModel.isUserInputMatch(binding.edtDoan.text.toString()[0])) { updateAdapterAndRecyclerView() - Toast.makeText(this, "Bạn đoán đúng rồi", Toast.LENGTH_LONG).show() - if (playingRoomViewModel.checkRoundWin() && checkCurrentPlayer()) { -// showWinnerDialog(playingRoomViewModel) - Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() + Toast.makeText(this, "Bạn đoán đúng rồi", Toast.LENGTH_SHORT).show() - } } else { Toast.makeText(this, "Bạn đoán sai rồi", Toast.LENGTH_LONG).show() } + + if (playingRoomViewModel.checkRoundWin() ) { + showWinnerDialog(playingRoomViewModel) + Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() + } binding.edtDoan.text?.clear() } @@ -131,13 +132,18 @@ class PlayingRoomActivity : AppCompatActivity() { } binding.iconButton.setOnClickListener { - Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show() - var questionAnswerToBeGuess = questionAnswerList.random() - while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { - questionAnswerToBeGuess = questionAnswerList.random() + if (playingRoomViewModel.checkRoundWin() ) { + showWinnerDialog(playingRoomViewModel) + Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() + } - playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerToBeGuess) - updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) +// Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show() +// var questionAnswerToBeGuess = questionAnswerList.random() +// while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { +// questionAnswerToBeGuess = questionAnswerList.random() +// } +// playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerToBeGuess) +// updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) } // Wheel @@ -185,6 +191,7 @@ class PlayingRoomActivity : AppCompatActivity() { updateVisibility(gameModel) updateCurrentQuestionAndAnswer(gameModel) updateAdapterAndRecyclerView() + if (gameModel.gameStatus == GameStatus.ENDROUND && !checkCurrentPlayer()) { showWinnerDialog(playingRoomViewModel) playingRoomViewModel.updateStatusGameModel(GameStatus.WAITING_TO_CONTINUE) @@ -263,7 +270,6 @@ class PlayingRoomActivity : AppCompatActivity() { builder.setMessage("Đáp án chính xác là: ${GameData.gameModel.value!!.currentQuestionAnswer.answer}") builder.setPositiveButton(R.string.btn_next) { dialog, which -> - viewModel.updateStatusGameModel(GameStatus.INPROGRESS) var questionAnswerToBeGuess = questionAnswerList.random() while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { questionAnswerToBeGuess = questionAnswerList.random() diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index e20f54e..3817e45 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -80,6 +80,7 @@ class PlayingRoomViewModel: ViewModel() { tempLetterCardList.add(LetterCard(questionAnswer.answer[i].toString())) } gameModel.letterCardList = tempLetterCardList + gameModel.gameStatus = GameStatus.INPROGRESS resetGuessedCharacter() GameData.saveGameModel(gameModel) return true @@ -143,13 +144,18 @@ class PlayingRoomViewModel: ViewModel() { fun checkRoundWin(): Boolean { - for (i in gameModel.letterCardList.indices) { - if (gameModel.letterCardList[i].isHidden) { - return false + val gameModel: GameModel? = GameData.gameModel.value + var count = 0 + gameModel?.let { + + for (i in gameModel.letterCardList.indices) { + if (gameModel.letterCardList[i].isHidden) { + count++ + } } - } - return true + } + return count == 0 } fun makeAllLetterCardReveal() { From 23f57e8f4f9e294fd9fd4ecbaa99d0bb16fe29a6 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Sun, 21 Apr 2024 16:55:44 +0700 Subject: [PATCH 08/16] Fix bug having two instances of PlayingRoomActivity in BackStack --- app/src/main/AndroidManifest.xml | 1 + .../chiecnonkydieu/ui/WaitingRoomActivity.kt | 15 +++++++++++---- .../ui/playingRoom/PlayingRoomActivity.kt | 16 +++++++++------- .../ui/playingRoom/PlayingRoomViewModel.kt | 5 ++++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6357de4..6ada2ec 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,6 +27,7 @@ android:exported="false" /> updateUi(gameModel) - if (gameModel.gameStatus == GameStatus.INPROGRESS) { + if (gameModel.gameStatus == GameStatus.INPROGRESS && !checkCurrentPlayer()) { goToPlayingRoom() finish() } @@ -76,6 +74,7 @@ class WaitingRoomActivity : AppCompatActivity() { ) } goToPlayingRoom() + finish() } @@ -102,6 +101,14 @@ class WaitingRoomActivity : AppCompatActivity() { // }) // } + fun checkCurrentPlayer(): Boolean { + var savedPlayer: String? = "" + lifecycleScope.launchWhenCreated { + savedPlayer = dataStore.data.first()[com.example.chiecnonkydieu.ui.playingRoom.CURRENT_PLAYER] + } + return savedPlayer == GameData.gameModel.value?.currentPlayer?.name + + } private fun updateUi(gameModel: GameModel?) { if (gameModel != null) { when (gameModel.playersList.size) { diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index cfb9e6d..a659f9c 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -114,10 +114,11 @@ class PlayingRoomActivity : AppCompatActivity() { Toast.makeText(this, "Bạn đoán sai rồi", Toast.LENGTH_LONG).show() } - if (playingRoomViewModel.checkRoundWin() ) { - showWinnerDialog(playingRoomViewModel) - Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() - } +// if (playingRoomViewModel.checkRoundWin()) { +// playingRoomViewModel.updateGameEndRound() +// showWinnerDialog(playingRoomViewModel) +// Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() +// } binding.edtDoan.text?.clear() } @@ -192,7 +193,7 @@ class PlayingRoomActivity : AppCompatActivity() { updateCurrentQuestionAndAnswer(gameModel) updateAdapterAndRecyclerView() - if (gameModel.gameStatus == GameStatus.ENDROUND && !checkCurrentPlayer()) { + if (gameModel.gameStatus == GameStatus.ENDROUND) { showWinnerDialog(playingRoomViewModel) playingRoomViewModel.updateStatusGameModel(GameStatus.WAITING_TO_CONTINUE) } @@ -250,7 +251,8 @@ class PlayingRoomActivity : AppCompatActivity() { if (input.text.toString() == GameData.gameModel.value!!.currentQuestionAnswer.answer) { Toast.makeText(this, "giai thanh cong", Toast.LENGTH_LONG).show() viewModel.makeAllLetterCardReveal() - showWinnerDialog(viewModel) + viewModel.updateGameEndRound() +// showWinnerDialog(viewModel) } dialog.dismiss() } @@ -264,7 +266,7 @@ class PlayingRoomActivity : AppCompatActivity() { private fun showWinnerDialog(viewModel: PlayingRoomViewModel) { - viewModel.updateGameEndRound() +// viewModel.updateGameEndRound() val builder = AlertDialog.Builder(this) builder.setTitle("Xin chức mừng") builder.setMessage("Đáp án chính xác là: ${GameData.gameModel.value!!.currentQuestionAnswer.answer}") diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index 3817e45..7c2ebb6 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -55,6 +55,9 @@ class PlayingRoomViewModel: ViewModel() { } updateStatusGameModel(GameStatus.INPROGRESS) updateScore(gameModel) + if (checkRoundWin()) { + updateStatusGameModel(GameStatus.ENDROUND) + } changeTurn() GameData.saveGameModel(gameModel) return true @@ -81,7 +84,7 @@ class PlayingRoomViewModel: ViewModel() { } gameModel.letterCardList = tempLetterCardList gameModel.gameStatus = GameStatus.INPROGRESS - resetGuessedCharacter() + gameModel.guessesCharacters.clear() GameData.saveGameModel(gameModel) return true } From 4f1706a39737107e90e6103295aab1459c086ff4 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Mon, 29 Apr 2024 10:14:26 +0700 Subject: [PATCH 09/16] Add hint feature for playing room to show hints --- .idea/deploymentTargetDropDown.xml | 15 ++- .../example/chiecnonkydieu/data/DataSource.kt | 15 ++- .../example/chiecnonkydieu/data/GameModel.kt | 1 - .../com/example/chiecnonkydieu/model/Hint.kt | 6 ++ .../chiecnonkydieu/model/QuestionAnswer.kt | 4 +- .../ui/playingRoom/PlayingRoomActivity.kt | 61 ++++++++---- .../ui/playingRoom/PlayingRoomViewModel.kt | 1 - app/src/main/res/drawable/ic_add.png | Bin 0 -> 810 bytes app/src/main/res/layout/list_hint.xml | 89 ++++++++++++++++++ app/src/main/res/values/colors.xml | 2 + 10 files changed, 168 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/example/chiecnonkydieu/model/Hint.kt create mode 100644 app/src/main/res/drawable/ic_add.png create mode 100644 app/src/main/res/layout/list_hint.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 0c0c338..a471588 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -3,7 +3,20 @@ - + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt b/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt index 53e836e..8a9726c 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt @@ -2,12 +2,19 @@ package com.example.chiecnonkydieu.data import android.content.Context import com.example.chiecnonkydieu.R +import com.example.chiecnonkydieu.model.Hint import com.example.chiecnonkydieu.model.QuestionAnswer + +val hintList:MutableList = mutableListOf( + Hint("Đây là hint 1"), + Hint("Đây là hint 2"), + Hint("Đây là hint 3") +) val questionAnswerList: List = mutableListOf( - QuestionAnswer("Thủ đô của Việt Nam nằm ở đâu?", "HA NOI"), - QuestionAnswer("Loại hình thể thao nào được yêu thích nhất ở Việt Nam?", "BONG DA"), - QuestionAnswer("Bộ phim khoa học viễn tưởng nào là một trong những tác phẩm nổi tiếng nhất?", "STAR WARS"), - QuestionAnswer("Đâu là bộ phim có tựa đề \"Dặm xanh\"?", "THE GREEN MILE"), + QuestionAnswer("Thủ đô của Việt Nam nằm ở đâu?", "HA NOI", "Hà Nội", hintList), + QuestionAnswer("Loại hình thể thao nào được yêu thích nhất ở Việt Nam?", "BONG DA", "Bóng Đá", hintList), + QuestionAnswer("Bộ phim khoa học viễn tưởng nào là một trong những tác phẩm nổi tiếng nhất?", "STARWARS", "STARWARS", hintList), + QuestionAnswer("Đâu là bộ phim có tựa đề \"Dặm xanh\"?", "THE GREEN MILES", "THE GREEN MILES", hintList), ) diff --git a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt index e71516d..ad21f65 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt @@ -15,7 +15,6 @@ data class GameModel( var letterCardList: MutableList = mutableListOf(), val previousQuestionAnswers: MutableList = mutableListOf(), var currentSpinValue: String = "" - ) enum class GameStatus { diff --git a/app/src/main/java/com/example/chiecnonkydieu/model/Hint.kt b/app/src/main/java/com/example/chiecnonkydieu/model/Hint.kt new file mode 100644 index 0000000..df70231 --- /dev/null +++ b/app/src/main/java/com/example/chiecnonkydieu/model/Hint.kt @@ -0,0 +1,6 @@ +package com.example.chiecnonkydieu.model + +data class Hint( + val title: String = "Đây là hint", + val isOpen: Boolean = false +) diff --git a/app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt b/app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt index b7e1a3e..edad3a6 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/model/QuestionAnswer.kt @@ -2,5 +2,7 @@ package com.example.chiecnonkydieu.model data class QuestionAnswer( val question: String = "Thủ đô của Việt Nam nằm ở đâu?", - val answer: String = "HA NOI" + val answer: String = "HA NOI", + val answerCoDau: String = "Hà Nội", + val hintList: MutableList = mutableListOf() ) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index a659f9c..c47ef36 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -2,55 +2,48 @@ package com.example.chiecnonkydieu.ui.playingRoom import android.content.Context import android.content.Intent -import android.opengl.Visibility import android.os.Bundle -import android.text.Editable import android.view.LayoutInflater import android.view.View import android.widget.EditText +import android.widget.ImageView import android.widget.LinearLayout +import android.widget.TextView import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat -import androidx.core.view.marginTop import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.viewModelScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.chiecnonkydieu.MainActivity import com.example.chiecnonkydieu.R -import com.example.chiecnonkydieu.ui.adapters.LetterCardAdapter import com.example.chiecnonkydieu.data.GameData import com.example.chiecnonkydieu.data.GameData.gameModel import com.example.chiecnonkydieu.data.GameModel import com.example.chiecnonkydieu.data.GameStatus import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityPlayingRoomBinding -import com.example.chiecnonkydieu.ui.WaitingRoomActivity +import com.example.chiecnonkydieu.ui.adapters.LetterCardAdapter import com.example.chiecnonkydieu.ui.dataStore import com.example.chiecnonkydieu.ui.wheel.WheelActivity import com.example.chiecnonkydieu.ui.wheel.WheelViewModel -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.firstOrNull -import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import rubikstudio.library.LuckyWheelView - private val Context.dataStore: DataStore by preferencesDataStore(name = "player_data") val CURRENT_PLAYER = stringPreferencesKey("current_player") class PlayingRoomActivity : AppCompatActivity() { @@ -133,11 +126,12 @@ class PlayingRoomActivity : AppCompatActivity() { } binding.iconButton.setOnClickListener { - if (playingRoomViewModel.checkRoundWin() ) { - showWinnerDialog(playingRoomViewModel) - Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() - - } +// if (playingRoomViewModel.checkRoundWin() ) { +// showWinnerDialog(playingRoomViewModel) +// Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() +// +// } + showHintDialog() // Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show() // var questionAnswerToBeGuess = questionAnswerList.random() // while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { @@ -192,7 +186,6 @@ class PlayingRoomActivity : AppCompatActivity() { updateVisibility(gameModel) updateCurrentQuestionAndAnswer(gameModel) updateAdapterAndRecyclerView() - if (gameModel.gameStatus == GameStatus.ENDROUND) { showWinnerDialog(playingRoomViewModel) playingRoomViewModel.updateStatusGameModel(GameStatus.WAITING_TO_CONTINUE) @@ -269,7 +262,7 @@ class PlayingRoomActivity : AppCompatActivity() { // viewModel.updateGameEndRound() val builder = AlertDialog.Builder(this) builder.setTitle("Xin chức mừng") - builder.setMessage("Đáp án chính xác là: ${GameData.gameModel.value!!.currentQuestionAnswer.answer}") + builder.setMessage("Đáp án chính xác là: ${GameData.gameModel.value!!.currentQuestionAnswer.answerCoDau}") builder.setPositiveButton(R.string.btn_next) { dialog, which -> var questionAnswerToBeGuess = questionAnswerList.random() @@ -293,6 +286,38 @@ class PlayingRoomActivity : AppCompatActivity() { } + private fun showHintDialog() { + val viewInflated = LayoutInflater.from(this).inflate(R.layout.list_hint, null, false) + val listHint = viewInflated.findViewById(R.id.cl_listHint) + val alertDialog = AlertDialog.Builder(this) + .setTitle("Choose Hint") + .setView(listHint) + .setNegativeButton("Cancel", null) + .show() + + listHint.findViewById(R.id.ll_hint1).setOnClickListener { + Toast.makeText(applicationContext, "hint1 click", Toast.LENGTH_LONG).show() + listHint.findViewById(R.id.img_add1).visibility = View.GONE + listHint.findViewById(R.id.tv_hint1).text = GameData.gameModel.value!!.currentQuestionAnswer.hintList[0].title + } + listHint.findViewById(R.id.ll_hint2).setOnClickListener { + Toast.makeText(applicationContext, "hint2 click", Toast.LENGTH_LONG).show() + listHint.findViewById(R.id.img_add2).visibility = View.GONE + listHint.findViewById(R.id.tv_hint2).text = GameData.gameModel.value!!.currentQuestionAnswer.hintList[1].title + } + listHint.findViewById(R.id.ll_hint3).setOnClickListener { + Toast.makeText(applicationContext, "hint3 click", Toast.LENGTH_LONG).show() + listHint.findViewById(R.id.img_add3).visibility = View.GONE + listHint.findViewById(R.id.tv_hint3).text = GameData.gameModel.value!!.currentQuestionAnswer.hintList[2].title + } + + + } + + private fun showHint() { + + } + private fun updateCurrentQuestionAndAnswer(gameModel: GameModel) { binding.tvQuestion.text = gameModel.currentQuestionAnswer.question updateAdapterAndRecyclerView() diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index 7c2ebb6..803e89c 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -127,7 +127,6 @@ class PlayingRoomViewModel: ViewModel() { } - private fun changeTurn() { val gameModel: GameModel? = GameData.gameModel.value var indexNewPlayer = -1 diff --git a/app/src/main/res/drawable/ic_add.png b/app/src/main/res/drawable/ic_add.png new file mode 100644 index 0000000000000000000000000000000000000000..472da266c3e9a13cc89002c2f929fca31ca754c7 GIT binary patch literal 810 zcmV+_1J(SAP)4Q_K~#7FtyWEL z(?Asd#tE=!X}zqJ0PYDWC%`#DWdca7nx3G^36h+E^aM>-NFWkeuug73dIEx!NQe#Y zf<;xw!+YcZza(Hj$?}ZH^S<}qyq^)+hAUmi!CniR_7so?Bt1eb;KHWWYkJS{YB}2` zMij1msGF#uOCu9oLB!JSqq!RX9ns{`ThhFp`pOn8$eOul8FZSTWC(BX8hG<#v6+b7 zbhtlF3An@>pK{$ZIWpi@Op-#D3nL_RTnn#P^J1cL{cA)b=MZZPR#v-Qe6068LIoSQ z&a=2>TBKdQ8O^n9a#rx~K=vPbUj_%j_3?oE}45=8Hg*ylapk_BF_ zTCi*zS?J0|lAb&?R|AsM_vIKk*l#C^?pM0u&tly`8L(x7LSD(jMPQYttP!H+Ey&@{ zXR--SU=?}5w@M>x%-AoN_#aRaN=M)^JojD@r~a}HD;bjIAU>;?^a89(R60%ApApHp zJ|K4G~U-v zJQ4~jr7-&Ox|t06NsYstH44sYPJ%ldFCToS`onIb^)m@q@q~_PaC?@OX(A6eZv?e- z*Zq#rYFZa6gw4c$ZhqXCQYg5b7*t}M9*r)rJ7&~oIz@$9Y}^`LJ~XNZGVj{OX+c%G o=<>2NQHIzsq6xM7{C1y<{907*qoM6N<$g3ktN4*&oF literal 0 HcmV?d00001 diff --git a/app/src/main/res/layout/list_hint.xml b/app/src/main/res/layout/list_hint.xml new file mode 100644 index 0000000..62e7ff8 --- /dev/null +++ b/app/src/main/res/layout/list_hint.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 6ce16a8..9f360c2 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -13,4 +13,6 @@ #11E132 + #00AE1C + \ No newline at end of file From cda01e5066c0e9b99edae827df88435dad838ab1 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Mon, 29 Apr 2024 10:31:10 +0700 Subject: [PATCH 10/16] Fix bug showing 2 winner dialog in playing room --- .../chiecnonkydieu/ui/CreateRoomActivity.kt | 5 +++- .../ui/playingRoom/PlayingRoomActivity.kt | 27 +++---------------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt index fcaa1b5..e54ee78 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/CreateRoomActivity.kt @@ -23,6 +23,7 @@ import com.example.chiecnonkydieu.model.LetterCard import com.example.chiecnonkydieu.model.Player import com.example.chiecnonkydieu.data.questionAnswerList import com.example.chiecnonkydieu.databinding.ActivityCreateRoomBinding +import com.example.chiecnonkydieu.model.QuestionAnswer import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map @@ -67,7 +68,9 @@ class CreateRoomActivity : AppCompatActivity() { guessesCharacters = mutableListOf(), currentPlayer = Player(name = binding.edtName.text.toString()), letterCardList = getLetterCardListFromAnswer(questionAnswerList[1].answer), - previousQuestionAnswers = mutableListOf() + previousQuestionAnswers = mutableListOf().apply { + add(questionAnswerList[1]) + } ) gameModel.playersList.add(Player(binding.edtName.text.toString())) GameData.saveGameModel( diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index c47ef36..cb56d93 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -52,6 +52,7 @@ class PlayingRoomActivity : AppCompatActivity() { var indexWheel: Int = -1 var currentValueWheel: String = "" + private var isDialogShowing = false lateinit var adapter: LetterCardAdapter lateinit var recyclerView: RecyclerView @@ -118,27 +119,10 @@ class PlayingRoomActivity : AppCompatActivity() { binding.btnGiai.setOnClickListener { showDialog(playingRoomViewModel) -//// lifecycleScope.launch { -//// Toast.makeText(applicationContext, "Current player: " + checkCurrentPlayer().toString(), Toast.LENGTH_LONG).show() -//// } - - } binding.iconButton.setOnClickListener { -// if (playingRoomViewModel.checkRoundWin() ) { -// showWinnerDialog(playingRoomViewModel) -// Toast.makeText(this, "Da doan xong", Toast.LENGTH_LONG).show() -// -// } showHintDialog() -// Toast.makeText(this, "Hint click", Toast.LENGTH_LONG).show() -// var questionAnswerToBeGuess = questionAnswerList.random() -// while (GameData.gameModel.value!!.previousQuestionAnswers.contains(questionAnswerToBeGuess)) { -// questionAnswerToBeGuess = questionAnswerList.random() -// } -// playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerToBeGuess) -// updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) } // Wheel @@ -186,7 +170,7 @@ class PlayingRoomActivity : AppCompatActivity() { updateVisibility(gameModel) updateCurrentQuestionAndAnswer(gameModel) updateAdapterAndRecyclerView() - if (gameModel.gameStatus == GameStatus.ENDROUND) { + if (gameModel.gameStatus == GameStatus.ENDROUND && !isDialogShowing) { showWinnerDialog(playingRoomViewModel) playingRoomViewModel.updateStatusGameModel(GameStatus.WAITING_TO_CONTINUE) } @@ -245,7 +229,6 @@ class PlayingRoomActivity : AppCompatActivity() { Toast.makeText(this, "giai thanh cong", Toast.LENGTH_LONG).show() viewModel.makeAllLetterCardReveal() viewModel.updateGameEndRound() -// showWinnerDialog(viewModel) } dialog.dismiss() } @@ -259,6 +242,7 @@ class PlayingRoomActivity : AppCompatActivity() { private fun showWinnerDialog(viewModel: PlayingRoomViewModel) { + isDialogShowing = true // viewModel.updateGameEndRound() val builder = AlertDialog.Builder(this) builder.setTitle("Xin chức mừng") @@ -272,6 +256,7 @@ class PlayingRoomActivity : AppCompatActivity() { playingRoomViewModel.setQuestionAndCurrentWordToBeGuessed(questionAnswerToBeGuess) updateCurrentQuestionAndAnswer(GameData.gameModel.value!!) Toast.makeText(applicationContext, "choi tiep click", Toast.LENGTH_LONG).show() + isDialogShowing = false dialog.dismiss() } @@ -312,10 +297,6 @@ class PlayingRoomActivity : AppCompatActivity() { } - } - - private fun showHint() { - } private fun updateCurrentQuestionAndAnswer(gameModel: GameModel) { From 5b0b8660d0549e6316e72f6109406d900b064d85 Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Tue, 30 Apr 2024 22:32:50 +0700 Subject: [PATCH 11/16] Handle the "MISS TURN" feature in wheel --- .../ui/playingRoom/PlayingRoomActivity.kt | 10 ++++--- .../ui/playingRoom/PlayingRoomViewModel.kt | 9 +++---- .../chiecnonkydieu/ui/wheel/WheelViewModel.kt | 26 ++++++++++++++++--- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt index cb56d93..d3fec9a 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomActivity.kt @@ -161,7 +161,6 @@ class PlayingRoomActivity : AppCompatActivity() { savedPlayer = dataStore.data.first()[CURRENT_PLAYER] } return savedPlayer == GameData.gameModel.value?.currentPlayer?.name - } private fun updateUi(gameModel: GameModel?) { if (gameModel != null) { @@ -186,9 +185,14 @@ class PlayingRoomActivity : AppCompatActivity() { currentValueWheel = gameModel.currentSpinValue break } + if (wheelViewModel.luckyItemsList[i].topText == gameModel.currentSpinValue) { + indexWheel = i + currentValueWheel = gameModel.currentSpinValue + break + } } // Kiem tra xem neu la nguoi choi hien tai thi chi cho round la 0, neu khong thi cho 3 - if (true) { + if (checkCurrentPlayer()) { // TODO binding.luckyWheel.setRound(0) binding.luckyWheel.startLuckyWheelWithTargetIndex(indexWheel) @@ -232,7 +236,6 @@ class PlayingRoomActivity : AppCompatActivity() { } dialog.dismiss() } - builder.setNegativeButton(android.R.string.cancel) { dialog, which -> dialog.cancel() } @@ -268,7 +271,6 @@ class PlayingRoomActivity : AppCompatActivity() { finish() } builder.show() - } private fun showHintDialog() { diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt index 803e89c..dcdc7a4 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/playingRoom/PlayingRoomViewModel.kt @@ -117,16 +117,15 @@ class PlayingRoomViewModel: ViewModel() { gameModel.playersList[index].score += currentSpinValue.toInt() } else if (currentSpinValue == "ITEM"){ // TODO - } else if (currentSpinValue == "MISS_TURN") { - - } - else if (currentSpinValue == "EXTRA_TURN") { - + } else if (currentSpinValue == "MISS TURN") { + changeTurn() + updateStatusGameModel(GameStatus.INPROGRESS) } } } + private fun changeTurn() { val gameModel: GameModel? = GameData.gameModel.value var indexNewPlayer = -1 diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt index ed79511..3570c5d 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelViewModel.kt @@ -35,9 +35,8 @@ class WheelViewModel: ViewModel() { _luckyItemsList.add(luckyItem3) val luckyItem4: LuckyItem = LuckyItem() - luckyItem4.topText = "EXTRA TURN" - luckyItem4.icon = R.drawable.ic_man - luckyItem4.color = ContextCompat.getColor(context, R.color.purple_500) + luckyItem4.secondaryText = "1000" + luckyItem4.color = ContextCompat.getColor(context, R.color.orange) _luckyItemsList.add(luckyItem4) val luckyItem5: LuckyItem = LuckyItem() @@ -100,11 +99,30 @@ class WheelViewModel: ViewModel() { } } + private fun changeTurn() { + val gameModel: GameModel? = GameData.gameModel.value + var indexNewPlayer = -1 + gameModel?.let { + for (i in gameModel.playersList.indices) { + if (gameModel.playersList[i].name == gameModel.currentPlayer.name) { + indexNewPlayer = (i + 1) % gameModel.playersList.size + } + } + gameModel.currentPlayer = gameModel.playersList[indexNewPlayer] + } + } + fun updateCurrentSpinValue(spinValue: String) { val gameModel: GameModel? = GameData.gameModel.value gameModel?.let { gameModel.currentSpinValue = spinValue - updateStatusGameModel(GameStatus.GUESS) + if (spinValue.isDigitsOnly() || spinValue == "ITEM") { + updateStatusGameModel(GameStatus.GUESS) + } + else if (spinValue == "MISS TURN"){ + updateStatusGameModel(GameStatus.INPROGRESS) + changeTurn() + } GameData.saveGameModel(gameModel) } From 7237898b335003a020979933c60c29653e56672d Mon Sep 17 00:00:00 2001 From: tungnk123 Date: Tue, 30 Apr 2024 22:33:32 +0700 Subject: [PATCH 12/16] Add demo button for testing "MISS TURN" and "ITEM" --- .idea/deploymentTargetDropDown.xml | 8 ++--- .../chiecnonkydieu/ui/wheel/WheelActivity.kt | 18 ++++++++++ app/src/main/res/layout/activity_wheel.xml | 35 +++++++++++++++---- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index a471588..dd45733 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -4,9 +4,9 @@ - + - + @@ -14,8 +14,8 @@ - - + + diff --git a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt index 2eb6838..f30a72d 100644 --- a/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt +++ b/app/src/main/java/com/example/chiecnonkydieu/ui/wheel/WheelActivity.kt @@ -69,6 +69,24 @@ class WheelActivity : AppCompatActivity() { wheelViewModel.updateCurrentSpinValue(wheelViewModel.getStringItemAtIndex(indexAns)) } + binding.btnMissTurnDemo.setOnClickListener { + countDownTimer.cancel() + val indexAns = 6 // vi tri mac dinh cua miss turn item + luckyWheelView.setRound(3) + luckyWheelView.startLuckyWheelWithTargetIndex(indexAns) + Toast.makeText(this, wheelViewModel.getStringItemAtIndex(indexAns), Toast.LENGTH_LONG).show() + wheelViewModel.updateCurrentSpinValue(wheelViewModel.getStringItemAtIndex(indexAns)) + } + + binding.btnItemDemo.setOnClickListener { + countDownTimer.cancel() + val indexAns = 10 // vi tri mac dinh cua item demo + luckyWheelView.setRound(3) + luckyWheelView.startLuckyWheelWithTargetIndex(indexAns) + Toast.makeText(this, wheelViewModel.getStringItemAtIndex(indexAns), Toast.LENGTH_LONG).show() + wheelViewModel.updateCurrentSpinValue(wheelViewModel.getStringItemAtIndex(indexAns)) + } + // count down timer countDownTimer = object : CountDownTimer(7000, 1000) { override fun onTick(millisUntilFinished: Long) { diff --git a/app/src/main/res/layout/activity_wheel.xml b/app/src/main/res/layout/activity_wheel.xml index 77346c7..b1fc980 100644 --- a/app/src/main/res/layout/activity_wheel.xml +++ b/app/src/main/res/layout/activity_wheel.xml @@ -4,8 +4,8 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" - android:background="@color/background" android:layout_height="match_parent" + android:background="@color/background" tools:context=".ui.wheel.WheelActivity"> + app:layout_constraintTop_toBottomOf="@+id/tv_conLaiXGiay"> + app:lkwTopTextSize="12sp" /> +