Skip to content

Commit

Permalink
Merge pull request #2 from tungnk123/feature/playing_room
Browse files Browse the repository at this point in the history
Feature/playing room
  • Loading branch information
tungnk123 authored May 19, 2024
2 parents 1a528c1 + 04bc986 commit c23ad6e
Show file tree
Hide file tree
Showing 34 changed files with 1,996 additions and 254 deletions.
2 changes: 1 addition & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ 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")

// gemini
implementation("com.google.android.material:material:1.11.0")
implementation("com.google.ai.client.generativeai:generativeai:0.4.0")
}
32 changes: 21 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@
android:theme="@style/Theme.ChiecNonKyDieu"
tools:targetApi="31">
<activity
android:name=".ui.wheel.WheelActivity"
android:name=".ui.admin.HomeAdminActivity"
android:exported="false" />
<activity
android:name=".ui.PlayingRoomActivity"
android:launchMode="singleTop"
android:name=".ui.admin.LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.admin.AddQuestionActivity"
android:exported="false" />
<activity
android:name=".ui.wheel.WheelActivity"
android:exported="false" />
<activity
android:name=".ui.playingRoom.PlayingRoomActivity"
android:exported="false"
android:launchMode="singleTop" />
<activity
android:name=".ui.SearchRoomActivity"
android:exported="false" />
Expand All @@ -27,16 +42,11 @@
android:exported="false" />
<activity
android:name=".ui.WaitingRoomActivity"
android:exported="false" />
android:exported="false"
android:launchMode="singleTop" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:exported="false"/>

<meta-data
android:name="preloaded_fonts"
Expand Down
31 changes: 30 additions & 1 deletion app/src/main/java/com/example/chiecnonkydieu/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
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
import com.example.chiecnonkydieu.ui.CreateRoomActivity
import com.example.chiecnonkydieu.ui.PlayingRoomActivity
import com.example.chiecnonkydieu.ui.playingRoom.PlayingRoomActivity
import com.example.chiecnonkydieu.ui.SearchRoomActivity
import com.example.chiecnonkydieu.ui.admin.AddQuestionActivity
import com.example.chiecnonkydieu.ui.wheel.WheelActivity
import com.google.android.material.snackbar.Snackbar

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
Expand All @@ -20,6 +26,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)
Expand Down Expand Up @@ -50,11 +59,31 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}



}
private fun goToPlayingRoom() {
// TODO Set cung
val intent = Intent(this, PlayingRoomActivity::class.java)
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()
}
}
28 changes: 27 additions & 1 deletion app/src/main/java/com/example/chiecnonkydieu/data/DataSource.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package com.example.chiecnonkydieu.data

import rubikstudio.library.model.LuckyItem
import com.example.chiecnonkydieu.model.QuestionAnswer
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase


val database: DatabaseReference = Firebase.database.reference
const val REFERENCE_CAUHOI = "CAU HOI"
val questionAnswerList: MutableList<QuestionAnswer> = mutableListOf(
QuestionAnswer(
"Hà Nội",
"HA NOI",
"Thủ đô của Việt Nam nằm ở đâu?",
"Hint1",
"Hint2",
"Hint3",
"Thong tin"
),
QuestionAnswer(
"Hà Nội",
"HA NOI",
"Thủ đô của Việt Nam nằm ở đâu?",
"Hint1",
"Hint2",
"Hint3",
"Thong tin"
),

)
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.chiecnonkydieu.model.Player
import com.google.firebase.Firebase
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
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<GameModel> = MutableLiveData()
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/com/example/chiecnonkydieu/data/GameModel.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package com.example.chiecnonkydieu.data

import android.health.connect.datatypes.units.Length
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,
val winner: String = "",
var gameStatus: GameStatus = GameStatus.CREATED,
val playersList: MutableList<Player> = mutableListOf<Player>(),
val currentPlayer: Player = Player(),
val currentQuestion: String = "",
val currentAnswer: String = "HA NOI",
val currentGuess: String = " A N "

var currentPlayer: Player = Player(),
var currentQuestionAnswer: QuestionAnswer = questionAnswerList[0],
val guessesCharacters: MutableList<String> = mutableListOf(),
var letterCardList: MutableList<LetterCard> = mutableListOf(),
val previousQuestionAnswers: MutableList<QuestionAnswer> = mutableListOf(),
var currentSpinValue: String = ""
)

enum class GameStatus {
CREATED,
JOINED1,
JOINED2,
INPROGRESS,
GUESS,
ENDROUND,
WAITING_TO_CONTINUE,
FINISHED
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/example/chiecnonkydieu/model/Hint.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.chiecnonkydieu.model

data class Hint(
val title: String = "Đây là hint",
val isOpen: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.chiecnonkydieu.model

data class LetterCard(
val letter: String = "A",
var isHidden: Boolean = letter != " "
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.chiecnonkydieu.data
package com.example.chiecnonkydieu.model

data class Player(
val name: String = "Player",
val gender: Boolean = true,
val score: Int = 0
var score: Int = 0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.chiecnonkydieu.model


data class QuestionAnswer(
var DoiTuong : String = "",
var DoiTuongInHoa : String = "",
var CauHoi : String = "",
var Hint1 : String = "",
var Hint2 : String = "",
var Hint3 : String = "",
var ThongTin : String = ""
)
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
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
import com.example.chiecnonkydieu.data.GameModel
import com.example.chiecnonkydieu.data.GameStatus
import com.example.chiecnonkydieu.data.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 com.example.chiecnonkydieu.model.QuestionAnswer
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlin.random.Random


val Context.dataStore: DataStore<Preferences> 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()
Expand Down Expand Up @@ -46,12 +64,20 @@ 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(),
currentPlayer = Player(name = binding.edtName.text.toString()),
letterCardList = getLetterCardListFromAnswer(questionAnswerList[0].DoiTuongInHoa),
previousQuestionAnswers = mutableListOf<QuestionAnswer>().apply {
add(questionAnswerList[1])
}
)
gameModel.playersList.add(Player(binding.edtName.text.toString()))
GameData.saveGameModel(
gameModel
)
saveCurrentPlayerToPreferencesDataStore(binding.edtName.text.toString())

}
goToWaitingRooom()
}
Expand All @@ -62,7 +88,26 @@ class CreateRoomActivity : AppCompatActivity() {
return true
}

fun goToWaitingRooom() {

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<LetterCard>{
val list = mutableListOf<LetterCard>()
for (i in answer.indices) {
list.add(LetterCard(answer[i].toString()))
}
return list
}
private fun goToWaitingRooom() {
val intent = Intent(this, WaitingRoomActivity::class.java)
intent.putExtra("room_id", binding.tvMaPhong.text.toString())
startActivity(intent)
Expand Down
Loading

0 comments on commit c23ad6e

Please sign in to comment.