Skip to content

Commit

Permalink
Refactor OnTimeUser
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstaeding committed May 18, 2024
1 parent 537ca4e commit 13d044d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import org.anvilpowered.anvil.core.db.DomainEntity
import java.time.LocalDateTime
import java.util.UUID

data class OnTimeUser(
override val id: UUID,
val username: String,
val createdUtc: LocalDateTime,
val playTime: Long,
val bonusTime: Long,
) : DomainEntity {
interface OnTimeUser : DomainEntity {
val username: String
val createdUtc: LocalDateTime
val playTime: Long
val bonusTime: Long

data class CreateDto(
val id: UUID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.anvilpowered.anvil.core.config.Registry
import org.anvilpowered.kbrig.tree.LiteralCommandNode
import org.anvilpowered.ontime.api.config.OnTimeKeys
import org.anvilpowered.ontime.core.command.OnTimeCommandFactory
import org.anvilpowered.ontime.core.db.OnTimeUserTable
import org.anvilpowered.ontime.core.db.OnTimeUsers
import org.anvilpowered.ontime.core.registrar.Registrar
import org.anvilpowered.ontime.core.task.LuckPermsSyncTaskService
import org.apache.logging.log4j.Logger
Expand Down Expand Up @@ -90,7 +90,7 @@ class OnTimePlugin(
logger.info("Finished connecting to database.")
logger.info("Creating tables...")
transaction {
SchemaUtils.createMissingTablesAndColumns(OnTimeUserTable)
SchemaUtils.createMissingTablesAndColumns(OnTimeUsers)
}
logger.info("Finished creating tables.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@ class OnTimeUserRepositoryImpl(
private val logger: Logger,
) : OnTimeUserRepository {
override suspend fun create(item: OnTimeUser.CreateDto): OnTimeUser = newSuspendedTransaction {
val user = OnTimeUserEntity.new(item.id) {
val user = DBOnTimeUser.new(item.id) {
username = item.username
createdUtc = LocalDateTime.now(ZoneId.of("UTC"))
playTime = 0
bonusTime = 0
}
logger.info("Created new OnTimeUser ${user.id.value} with data $item")
user.toOnTimeUser()
user
}

override suspend fun put(item: OnTimeUser.CreateDto): MutableRepository.PutResult<OnTimeUser> = newSuspendedTransaction {
val existingUser = getById(item.id)
if (existingUser == null) {
// create a new OnTimeUser
val newUser = OnTimeUserEntity.new(item.id) {
val newUser = DBOnTimeUser.new(item.id) {
username = item.username
createdUtc = LocalDateTime.now(ZoneId.of("UTC"))
playTime = 0
bonusTime = 0
}
logger.info("Created new OnTimeUser ${item.username} (${item.id}) with data $item")
MutableRepository.PutResult(newUser.toOnTimeUser(), created = true)
MutableRepository.PutResult(newUser, created = true)
} else {
logger.info(
"Loaded existing OnTime user ${existingUser.username} (${existingUser.id}) with" +
"Loaded existing OnTime user ${existingUser.username} (${existingUser.uuid}) with" +
"play time ${existingUser.playTimeFormatted}, " +
"bonus time ${existingUser.bonusTimeFormatted}, " +
"and total time ${existingUser.totalTimeFormatted}",
Expand All @@ -73,77 +73,77 @@ class OnTimeUserRepositoryImpl(
}

override suspend fun getAllUsernames(contains: String): SizedIterable<String> = newSuspendedTransaction {
OnTimeUserEntity.find { OnTimeUserTable.username.lowerCase() like "%${contains.lowercase()}%" }.mapLazy { it.username }
DBOnTimeUser.find { OnTimeUsers.username.lowerCase() like "%${contains.lowercase()}%" }.mapLazy { it.username }
}

override suspend fun getByUsername(username: String): OnTimeUser? = newSuspendedTransaction {
OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.toOnTimeUser()
DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()
}

private fun OnTimeUserEntity.addTime(time: Long) {
private fun DBOnTimeUser.addTime(time: Long) {
playTime += time
logger.debug("Added {} seconds to User {}", time, id.value)
}

override suspend fun addPlayTime(id: UUID, duration: Long): Long? = newSuspendedTransaction {
OnTimeUserEntity.findById(id)?.let { user ->
DBOnTimeUser.findById(id)?.let { user ->
user.addTime(duration)
user.playTime + user.bonusTime
}
}

private fun OnTimeUserEntity.setTime(time: Long) {
private fun DBOnTimeUser.setTime(time: Long) {
bonusTime = time - playTime
logger.info("Set total time of user $username (${id.value}) to $time")
}

override suspend fun setTotalTime(id: UUID, duration: Long): Boolean = newSuspendedTransaction {
OnTimeUserEntity.findById(id)?.setTime(duration) != null
DBOnTimeUser.findById(id)?.setTime(duration) != null
}

override suspend fun setTotalTime(username: String, duration: Long) = newSuspendedTransaction {
OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.setTime(duration) != null
DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()?.setTime(duration) != null
}

private fun OnTimeUserEntity.addBonusTime(time: Long) {
private fun DBOnTimeUser.addBonusTime(time: Long) {
bonusTime += time
logger.info("Added $time seconds to user $username (${id.value})")
}

override suspend fun addBonusTime(id: UUID, duration: Long): Boolean = newSuspendedTransaction {
OnTimeUserEntity.findById(id)?.addBonusTime(duration) != null
DBOnTimeUser.findById(id)?.addBonusTime(duration) != null
}

override suspend fun addBonusTime(username: String, duration: Long): Boolean = newSuspendedTransaction {
OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.addBonusTime(duration) != null
DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()?.addBonusTime(duration) != null
}

private fun OnTimeUserEntity.setBonusTime(time: Long) {
private fun DBOnTimeUser.setBonusTime(time: Long) {
bonusTime = time
logger.info("Set bonus time of user $username (${id.value}) to $time")
}

override suspend fun setBonusTime(id: UUID, duration: Long): Boolean = newSuspendedTransaction {
OnTimeUserEntity.findById(id)?.setBonusTime(duration) != null
DBOnTimeUser.findById(id)?.setBonusTime(duration) != null
}

override suspend fun setBonusTime(username: String, duration: Long): Boolean = newSuspendedTransaction {
OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.setBonusTime(duration) != null
DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()?.setBonusTime(duration) != null
}

override suspend fun getById(id: UUID): OnTimeUser? = newSuspendedTransaction {
OnTimeUserEntity.findById(id)?.toOnTimeUser()
DBOnTimeUser.findById(id)
}

override suspend fun countAll(): Long = newSuspendedTransaction {
OnTimeUserEntity.all().count()
DBOnTimeUser.all().count()
}

override suspend fun exists(id: UUID): Boolean = newSuspendedTransaction {
OnTimeUserEntity.findById(id) != null
DBOnTimeUser.findById(id) != null
}

override suspend fun deleteById(id: UUID): Boolean = newSuspendedTransaction {
OnTimeUserTable.deleteWhere { OnTimeUserTable.id eq id } > 0
OnTimeUsers.deleteWhere { OnTimeUsers.id eq id } > 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,22 @@ import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.javatime.datetime
import java.util.UUID

internal object OnTimeUserTable : UUIDTable("ontime_users") {
internal object OnTimeUsers : UUIDTable("ontime_users") {
val username = varchar("username", 16).uniqueIndex()
val createdUtc = datetime("created_utc")
val playTime = long("play_time")
val bonusTime = long("bonus_time")
}

internal class OnTimeUserEntity(id: EntityID<UUID>) : UUIDEntity(id) {
var username by OnTimeUserTable.username
var createdUtc by OnTimeUserTable.createdUtc
var playTime by OnTimeUserTable.playTime
var bonusTime by OnTimeUserTable.bonusTime
internal class DBOnTimeUser(id: EntityID<UUID>) : UUIDEntity(id), OnTimeUser {
override val uuid: UUID = id.value
override var username by OnTimeUsers.username
override var createdUtc by OnTimeUsers.createdUtc
override var playTime by OnTimeUsers.playTime
override var bonusTime by OnTimeUsers.bonusTime

companion object : UUIDEntityClass<OnTimeUserEntity>(OnTimeUserTable)
companion object : UUIDEntityClass<DBOnTimeUser>(OnTimeUsers)
}

internal fun OnTimeUserEntity.toOnTimeUser() = OnTimeUser(
id = this.id.value,
username = this.username,
createdUtc = this.createdUtc,
playTime = this.playTime,
bonusTime = this.bonusTime,
)

internal fun ResultRow.toOnTimeUser() = OnTimeUser(
id = this[OnTimeUserTable.id].value,
username = this[OnTimeUserTable.username],
createdUtc = this[OnTimeUserTable.createdUtc],
playTime = this[OnTimeUserTable.playTime],
bonusTime = this[OnTimeUserTable.bonusTime],
)

0 comments on commit 13d044d

Please sign in to comment.