195 lines
6.5 KiB
Kotlin
195 lines
6.5 KiB
Kotlin
package nl.voidcorp.ottobot.database
|
|
|
|
import org.jetbrains.exposed.dao.IntEntity
|
|
import org.jetbrains.exposed.dao.IntEntityClass
|
|
import org.jetbrains.exposed.dao.LongEntity
|
|
import org.jetbrains.exposed.dao.LongEntityClass
|
|
import org.jetbrains.exposed.dao.id.EntityID
|
|
import org.jetbrains.exposed.sql.and
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
|
|
|
class GuildStore(id: EntityID<Long>) : LongEntity(id) {
|
|
companion object : LongEntityClass<GuildStore>(GuildStores)
|
|
|
|
var defaultVerified by GuildStores.defaultVerified
|
|
var guildId by GuildStores.guildId
|
|
var prefix by GuildStores.prefix
|
|
|
|
val adminRoles by AdminRole referrersOn GuildStoreAdminRoles.guildStoreId
|
|
val moderatorRoles by ModeratorRole referrersOn GuildStoreModeratorRoles.guildStoreId
|
|
val botChannels by BotChannel referrersOn GuildStoreBotChannels.guildStoreId
|
|
val roles by RoleMap referrersOn GuildStoreRoleMap.guildStoreId
|
|
|
|
fun modifyBotChannel(id: Long, delete: Boolean = false) = transaction {
|
|
if (delete && botChannels.map { it.botchannel }.contains(id)) {
|
|
BotChannel.find { GuildStoreBotChannels.botChannels eq id }.forEach { it.delete() }
|
|
true
|
|
} else if (!delete && !botChannels.map { it.botchannel }.contains(id)) {
|
|
BotChannel.new {
|
|
guildStoreId = this@GuildStore
|
|
botchannel = id
|
|
}
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
fun modifyAdminRole(id: Long, delete: Boolean = false) = transaction {
|
|
if (delete && adminRoles.map { it.adminRoleId }.contains(id)) {
|
|
AdminRole.find { GuildStoreAdminRoles.adminRoles eq id }.forEach { it.delete() }
|
|
true
|
|
} else if (!delete && !adminRoles.map { it.adminRoleId }.contains(id)) {
|
|
AdminRole.new {
|
|
guildStoreId = this@GuildStore
|
|
adminRoleId = id
|
|
}
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
fun modifyModeratorRole(id: Long, delete: Boolean = false) = transaction {
|
|
if (delete && moderatorRoles.map { it.moderatorRoleId }.contains(id)) {
|
|
ModeratorRole.find { GuildStoreModeratorRoles.moderatorRoles eq id }.forEach { it.delete() }
|
|
true
|
|
} else if (!delete && !moderatorRoles.map { it.moderatorRoleId }.contains(id)) {
|
|
ModeratorRole.new {
|
|
guildStoreId = this@GuildStore
|
|
moderatorRoleId = id
|
|
}
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
val roleMap: MutableMap<String, Long> = SufferMap(this)
|
|
|
|
}
|
|
|
|
class SufferMap(val guildStore: GuildStore) : MutableMap<String, Long> {
|
|
override val size: Int
|
|
get() = transaction { guildStore.roles.count().toInt() }
|
|
|
|
override fun containsKey(key: String) = !transaction {
|
|
RoleMap.find {
|
|
(GuildStoreRoleMap.guildStoreId eq guildStore.id.value) and
|
|
(GuildStoreRoleMap.roleMapKey eq key)
|
|
}
|
|
}.empty()
|
|
|
|
override fun containsValue(value: Long) = !transaction {
|
|
RoleMap.find {
|
|
(GuildStoreRoleMap.guildStoreId eq guildStore.id.value) and
|
|
(GuildStoreRoleMap.roleMap eq value)
|
|
}
|
|
}.empty()
|
|
|
|
override fun get(key: String) = transaction {
|
|
RoleMap.find {
|
|
(GuildStoreRoleMap.guildStoreId eq guildStore.id.value) and
|
|
(GuildStoreRoleMap.roleMapKey eq key)
|
|
}.firstOrNull()?.roleId
|
|
}
|
|
|
|
override fun isEmpty() = size == 0
|
|
|
|
// Not implemented... too much effort already...
|
|
override val entries: MutableSet<MutableMap.MutableEntry<String, Long>>
|
|
get() = emptySet<MutableMap.MutableEntry<String, Long>>().toMutableSet()
|
|
|
|
override val keys: MutableSet<String>
|
|
get() = transaction {
|
|
RoleMap.find {
|
|
GuildStoreRoleMap.guildStoreId eq guildStore.id.value
|
|
}.map { it.roleName }
|
|
}.toMutableSet()
|
|
|
|
override val values: MutableCollection<Long>
|
|
get() = transaction {
|
|
RoleMap.find {
|
|
GuildStoreRoleMap.guildStoreId eq guildStore.id.value
|
|
}.map { it.roleId }
|
|
}.toMutableSet()
|
|
|
|
override fun clear() {
|
|
transaction {
|
|
RoleMap.find {
|
|
GuildStoreRoleMap.guildStoreId eq guildStore.id.value
|
|
}.forEach { it.delete() }
|
|
}
|
|
}
|
|
|
|
override fun put(key: String, value: Long): Long? {
|
|
val old = transaction {
|
|
RoleMap.find {
|
|
(GuildStoreRoleMap.guildStoreId eq guildStore.id.value) and (GuildStoreRoleMap.roleMapKey eq key)
|
|
}.firstOrNull()
|
|
}
|
|
val valOld = transaction { old?.roleId }
|
|
|
|
if (old != null) {
|
|
transaction {
|
|
old.roleId = value
|
|
}
|
|
} else {
|
|
RoleMap.new {
|
|
guildStoreId = guildStore
|
|
roleName = key
|
|
roleId = value
|
|
}
|
|
}
|
|
|
|
return valOld
|
|
}
|
|
|
|
override fun putAll(from: Map<out String, Long>) = from.forEach(this::put)
|
|
|
|
override fun remove(key: String): Long? {
|
|
val old = transaction {
|
|
RoleMap.find {
|
|
GuildStoreRoleMap.guildStoreId eq guildStore.id.value
|
|
}.firstOrNull()
|
|
}
|
|
return if (old == null) {
|
|
null
|
|
} else {
|
|
transaction { old.delete() }
|
|
old.roleId
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
class AdminRole(id: EntityID<Int>) : IntEntity(id) {
|
|
companion object : IntEntityClass<AdminRole>(GuildStoreAdminRoles)
|
|
|
|
var guildStoreId by GuildStore referencedOn GuildStoreAdminRoles.guildStoreId
|
|
var adminRoleId by GuildStoreAdminRoles.adminRoles
|
|
}
|
|
|
|
class BotChannel(id: EntityID<Int>) : IntEntity(id) {
|
|
companion object : IntEntityClass<BotChannel>(GuildStoreBotChannels)
|
|
|
|
var guildStoreId by GuildStore referencedOn GuildStoreBotChannels.guildStoreId
|
|
var botchannel by GuildStoreBotChannels.botChannels
|
|
|
|
}
|
|
|
|
class ModeratorRole(id: EntityID<Int>) : IntEntity(id) {
|
|
companion object : IntEntityClass<ModeratorRole>(GuildStoreModeratorRoles)
|
|
|
|
var guildStoreId by GuildStore referencedOn GuildStoreModeratorRoles.guildStoreId
|
|
var moderatorRoleId by GuildStoreModeratorRoles.moderatorRoles
|
|
}
|
|
|
|
class RoleMap(id: EntityID<Int>) : IntEntity(id) {
|
|
companion object : IntEntityClass<RoleMap>(GuildStoreRoleMap)
|
|
|
|
var guildStoreId by GuildStore referencedOn GuildStoreRoleMap.guildStoreId
|
|
var roleId by GuildStoreRoleMap.roleMap
|
|
var roleName by GuildStoreRoleMap.roleMapKey
|
|
} |