Началась ересь.

This commit is contained in:
ElEgEv 2023-11-11 20:11:39 +04:00
parent c059f4fe77
commit 4ab7b39d8d
24 changed files with 201 additions and 19 deletions

Binary file not shown.

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" />
<bytecodeTargetLevel target="1.8" />
</component>
</project>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -49,6 +49,9 @@ android {
}
}
}
kotlin {
jvmToolchain(8)
}
dependencies {

View File

@ -1,5 +1,6 @@
package ru.ulstu.`is`.pmu.tank.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
@ -11,10 +12,10 @@ import ru.ulstu.`is`.pmu.tank.model.LevelWithTanks
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.User
@Dao
interface LevelDao {
@Query("select * from levels order by level collate nocase asc")
fun getAll(): Flow<List<Nation>>
fun getAll(): Flow<List<Level>>
//получить уровни с танками
@Transaction

View File

@ -1,5 +1,6 @@
package ru.ulstu.`is`.pmu.tank.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
@ -10,6 +11,7 @@ import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.Level
import ru.ulstu.`is`.pmu.tank.model.NationWithTanks
@Dao
interface NationDao {
@Query("select * from nations order by name collate nocase asc")
fun getAll(): Flow<List<Nation>>
@ -27,7 +29,7 @@ interface NationDao {
@Query(
"SELECT name FROM nations where nations.uid = :uid"
)
open fun getSimpleNationUid(uid: Long): Level
open fun getSimpleNationUid(uid: Long): Nation
@Insert
suspend fun insert(nation: Nation)

View File

@ -1,5 +1,6 @@
package ru.ulstu.`is`.pmu.tank.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
@ -10,13 +11,13 @@ import ru.ulstu.`is`.pmu.tank.model.LevelWithTanks
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.Tank
@Dao
interface TankDao {
@Query("select * from tanks order by name collate nocase asc")
fun getAll(): Flow<List<Tank>>
//получить конкретный уровень
@Query("select * from tanks where tanks.uid = :uid")
@Query("select * from tanks where tanks.tankId = :uid")
suspend fun getTankUid(uid: Long): Tank
@Insert

View File

@ -1,5 +1,7 @@
package ru.ulstu.`is`.pmu.tank.dao
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
@ -9,8 +11,9 @@ import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.NationWithTanks
import ru.ulstu.`is`.pmu.tank.model.User
import ru.ulstu.`is`.pmu.tank.model.UserTankCrossRef
import ru.ulstu.`is`.pmu.tank.model.UserWithTanks
@Dao
interface UserDao {
@Query("select * from users order by nickname collate nocase asc")
fun getAll(): Flow<List<User>>
@ -18,11 +21,17 @@ interface UserDao {
//получить нации с танками
@Transaction
@Query("SELECT * FROM users")
fun getNationsWithTanks(): List<UserTankCrossRef>
fun getUsersWithTanks(): LiveData<List<UserWithTanks>>
//получить конкретную нацию
@Query("select * from users where users.uid = :uid")
suspend fun getUserUid(uid: Long): UserTankCrossRef
@Transaction
@Query("select * from users where users.userId = :uid")
suspend fun getUserUid(uid: Long): UserWithTanks
//добавить танк в ангар пользователя
@Transaction
@Query("INSERT INTO UserTankCrossRef (userId, tankId) VALUES (:uid, :tid)")
suspend fun addTankToClient(uid: Long?, tid: Long?)
@Insert
suspend fun insert(user: User)

View File

@ -0,0 +1,144 @@
package ru.ulstu.`is`.pmu.tank.database
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import ru.ulstu.`is`.pmu.tank.dao.LevelDao
import ru.ulstu.`is`.pmu.tank.dao.NationDao
import ru.ulstu.`is`.pmu.tank.dao.TankDao
import ru.ulstu.`is`.pmu.tank.dao.UserDao
import ru.ulstu.`is`.pmu.tank.model.Level
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.Tank
import ru.ulstu.`is`.pmu.tank.model.User
import ru.ulstu.`is`.pmu.tank.model.UserTankCrossRef
//тут, собственно говоря, всё и мутится с БД :)))
@Database(entities = [Nation::class, Level::class, Tank::class, User::class, UserTankCrossRef::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun nationDao(): NationDao
abstract fun levelDao(): LevelDao
abstract fun tankDao() : TankDao
abstract fun userDao() : UserDao
companion object {
private const val DB_NAME: String = "first-db"
@Volatile
private var INSTANCE: AppDatabase? = null
private suspend fun populateDatabase() {
INSTANCE?.let { database ->
// Nations
val nationDao = database.nationDao()
val nation1 = Nation("СССР")
val nation2 = Nation("Германия")
val nation3 = Nation("США")
val nation4 = Nation("Великобритания")
val nation5 = Nation("Франция")
val nation6 = Nation("Чехословакия")
val nation7 = Nation( "Швеция")
val nation8 = Nation( "Китай")
val nation9 = Nation( "Япония")
val nation10 = Nation("Италия")
nationDao.insert(nation1)
nationDao.insert(nation2)
nationDao.insert(nation3)
nationDao.insert(nation4)
nationDao.insert(nation5)
nationDao.insert(nation6)
nationDao.insert(nation7)
nationDao.insert(nation8)
nationDao.insert(nation9)
nationDao.insert(nation10)
// Levels
val levelDao = database.levelDao()
val level1 = Level(1)
val level2 = Level(2)
val level3 = Level(3)
val level4 = Level(4)
val level5 = Level(5)
val level6 = Level(6)
val level7 = Level(7)
val level8 = Level(8)
val level9 = Level(9)
val level10 = Level(10)
levelDao.insert(level1)
levelDao.insert(level2)
levelDao.insert(level3)
levelDao.insert(level4)
levelDao.insert(level5)
levelDao.insert(level6)
levelDao.insert(level7)
levelDao.insert(level8)
levelDao.insert(level9)
levelDao.insert(level10)
//Tanks
val tankDao = database.tankDao()
val tank1 = Tank("МС-1", 1000, level1, nation1)
val tank2 = Tank("Т-34-85", 960000, level6, nation1)
val tank3 = Tank("ИС-2", 1230000, level7, nation1)
val tank4 = Tank("ИСУ-152", 2350000, level8, nation1)
val tank5 = Tank("Tiger 1", 1430000, level7, nation2)
val tank6 = Tank("Ferdinand", 2500000, level8, nation2)
val tank7 = Tank("Tiger 2", 2500000, level8, nation2)
val tank8 = Tank("Panther", 1350000, level7, nation2)
val tank9 = Tank("M4A2E3", 990000, level6, nation3)
val tank10 = Tank("Pershing", 1260000, level8, nation3)
val tank11 = Tank("Hellcat", 940000, level7, nation3)
tankDao.insert(tank1)
tankDao.insert(tank2)
tankDao.insert(tank3)
tankDao.insert(tank4)
tankDao.insert(tank5)
tankDao.insert(tank6)
tankDao.insert(tank7)
tankDao.insert(tank8)
tankDao.insert(tank9)
tankDao.insert(tank10)
tankDao.insert(tank11)
//Users
val userDao = database.userDao()
val user = User("3tankista73", "egor@mail.ru", "12032003", 10000000)
userDao.insert(user)
userDao.addTankToClient(user.userId, tank1.tankId)
userDao.addTankToClient(user.userId, tank3.tankId)
userDao.addTankToClient(user.userId, tank5.tankId)
userDao.addTankToClient(user.userId, tank7.tankId)
userDao.addTankToClient(user.userId, tank9.tankId)
}
}
fun getInstance(appContext: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
Room.databaseBuilder(
appContext,
AppDatabase::class.java,
DB_NAME
)
.addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
CoroutineScope(Dispatchers.IO).launch {
populateDatabase()
}
}
})
.build()
.also { INSTANCE = it }
}
}
}
}

View File

@ -10,14 +10,14 @@ import androidx.room.PrimaryKey
)
data class Tank(
@PrimaryKey(autoGenerate = true)
val uid: Long?,
val tankId: Long?,
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "price")
val price: Int,
@ColumnInfo(name = "levelId")
@ColumnInfo(name = "levelId", index = true)
val levelId: Long?,
@ColumnInfo(name = "nationId")
@ColumnInfo(name = "nationId", index = true)
val nationId: Long?
) {
@Ignore
@ -32,12 +32,12 @@ data class Tank(
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Tank
if (uid != other.uid) return false
if (tankId != other.tankId) return false
return true
}
override fun hashCode(): Int {
return (uid ?: -1) as Int
return (tankId ?: -1) as Int
}
}

View File

@ -0,0 +1,19 @@
package ru.ulstu.`is`.pmu.tank.model
import androidx.room.Embedded
import androidx.room.Junction
import androidx.room.Relation
import ru.ulstu.`is`.pmu.tank.model.Tank
import ru.ulstu.`is`.pmu.tank.model.User
import ru.ulstu.`is`.pmu.tank.model.UserTankCrossRef
data class TankWithUsers(
@Embedded val tank: Tank,
@Relation(
parentColumn = "tankId",
entityColumn = "userId",
associateBy = Junction(UserTankCrossRef::class)
)
val users: List<User>
)

View File

@ -10,7 +10,7 @@ import androidx.room.PrimaryKey
)
data class User (
@PrimaryKey(autoGenerate = true)
val uid: Long?,
val userId: Long?,
@ColumnInfo(name = "nickname")
val nickname: String,
@ColumnInfo(name = "email")
@ -32,11 +32,11 @@ data class User (
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Tank
if (uid != other.uid) return false
if (userId != other.tankId) return false
return true
}
override fun hashCode(): Int {
return (uid ?: -1) as Int
return (userId ?: -1) as Int
}
}

View File

@ -1,6 +1,7 @@
package ru.ulstu.`is`.pmu.tank.model
import androidx.room.Entity
import org.jetbrains.annotations.NotNull
//many to many for user and tank
@Entity(primaryKeys = ["userId", "tankId"])

View File

@ -7,6 +7,7 @@ import androidx.room.Relation
//для работы many to many для получения списка танков для пользователя
data class UserWithTanks(
@Embedded val user: User,
@Relation(
parentColumn = "userId",
entityColumn = "tankId",