problème avec le aslivedata que j'arrive pas a import
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e60ed56936
commit
70564b5f03
@ -0,0 +1,8 @@
|
||||
package com.example.shakecraft
|
||||
|
||||
import android.app.Application
|
||||
import com.example.shakecraft.data.DataBase
|
||||
|
||||
class PlayerApplication: Application(){
|
||||
val database: DataBase by lazy { DataBase.getInstance(this) }
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.example.shakecraft
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.shakecraft.data.PlayerDao
|
||||
import com.example.shakecraft.model.Player
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class PlayerViewModel(private val playerDao: PlayerDao): ViewModel() {
|
||||
val player:LiveData<Player> = playerDao.getPlayer()
|
||||
.asLiveData()
|
||||
fun updatePlayer(
|
||||
Id: Int,
|
||||
pseudo: String,
|
||||
level: Int,
|
||||
xp: Int,
|
||||
image: Int,
|
||||
rank: String,
|
||||
){
|
||||
val updatePlayer= getUpdatePlayerEntry(Id, pseudo, level, xp, image, rank)
|
||||
updatePlayer(updatePlayer)
|
||||
}
|
||||
|
||||
private fun updatePlayer(player: Player) {
|
||||
viewModelScope.launch{
|
||||
playerDao.update(player)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUpdatePlayerEntry(
|
||||
Id: Int,
|
||||
pseudo: String,
|
||||
level: Int,
|
||||
xp: Int,
|
||||
image: Int,
|
||||
rank: String,
|
||||
): Player {
|
||||
return Player(
|
||||
id= Id,
|
||||
pseudo= pseudo,
|
||||
level= level,
|
||||
xp= xp,
|
||||
image= image,
|
||||
rank= rank,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class PlayerViewModelFactory(private val playerDao: PlayerDao): ViewModelProvider.Factory{
|
||||
fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
if (modelClass.isAssignableFrom(PlayerViewModel::class.java)){
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return PlayerViewModel(playerDao) as T
|
||||
}
|
||||
throw IllegalArgumentException("Unknown ViewModel class")
|
||||
}
|
||||
}
|
@ -1,14 +1,21 @@
|
||||
package com.example.shakecraft.data
|
||||
|
||||
import androidx.room.Query
|
||||
import androidx.room.*
|
||||
import com.example.shakecraft.model.Player
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.List
|
||||
|
||||
@Dao
|
||||
interface PlayerDao {
|
||||
@Query("SELECT * from Player ")
|
||||
fun getBoss(): Flow<List<Player>>
|
||||
|
||||
@Query("SELECT * from Player WHERE name = :name")
|
||||
fun getBoss(name : String): Flow<Player>
|
||||
@Query("SELECT * FROM Player")
|
||||
fun getPlayer(): Flow<Player>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insert(player: Player)
|
||||
|
||||
@Update
|
||||
suspend fun update(player: Player)
|
||||
|
||||
@Delete
|
||||
suspend fun delete(player: Player)
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package com.example.shakecraft.model
|
||||
|
||||
class Inventory() {
|
||||
var items: MutableList<Item> = mutableListOf()
|
||||
|
||||
fun addItem(item: Item) {
|
||||
val findItem = items.find { it.type.name == item.type.name }
|
||||
|
||||
if(findItem!= null){
|
||||
println("findItem n: "+findItem.stack+" item nb:"+item.stack)
|
||||
findItem.stack += item.stack
|
||||
}
|
||||
else{items.add(Item(type = item.type, stack = item.stack))}
|
||||
}
|
||||
|
||||
fun hasItem(item: Item) : Boolean{
|
||||
for (inventoryItem in items){
|
||||
if(inventoryItem.type.name == item.type.name && inventoryItem.stack >= item.stack){
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
fun craft(recipe: Recipe) : Boolean{
|
||||
println("test")
|
||||
for (ingredient in recipe.ingredients) {
|
||||
val searchedItem = items.find { it.type.name == ingredient.type.name }
|
||||
if(searchedItem != null) {
|
||||
searchedItem.stack -= ingredient.stack
|
||||
if (searchedItem.stack == 0){
|
||||
items.remove(searchedItem)
|
||||
}
|
||||
println(ingredient.type.name)
|
||||
if (recipe.item.type.name == "Open Treasure"){
|
||||
addItem(Generator.generateTreasure())
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
println("item:"+recipe.item.stack)
|
||||
addItem(recipe.item)
|
||||
return true
|
||||
}
|
||||
}
|
Loading…
Reference in new issue