add counter, counter handlers

This commit is contained in:
dalbodeule
2024-06-13 14:43:49 +09:00
parent 4da72f194e
commit 20f6d84040
16 changed files with 285 additions and 41 deletions

View File

@@ -9,12 +9,13 @@ import space.mori.chzzk_bot.models.Commands
import space.mori.chzzk_bot.models.User
object CommandService {
fun saveCommand(user: User, command: String, content: String): Command {
fun saveCommand(user: User, command: String, content: String, failContent: String): Command {
return transaction {
return@transaction Command.new {
this.user = user
this.command = command
this.content = content
this.failContent = failContent
}
}
}
@@ -26,31 +27,32 @@ object CommandService {
commandRow ?: throw RuntimeException("Command not found! $command")
commandRow.delete()
return@transaction commandRow
commandRow
}
}
fun updateCommand(user: User, command: String, content: String): Command {
fun updateCommand(user: User, command: String, content: String, failContent: String): Command {
return transaction {
val updated = Commands.update({Commands.user eq user.id and(Commands.command eq command)}) {
it[Commands.content] = content
it[Commands.failContent] = failContent
}
if(updated == 0) throw RuntimeException("Command not found! $command")
return@transaction Command.find(Commands.user eq user.id and(Commands.command eq command)).first()
Command.find(Commands.user eq user.id and(Commands.command eq command)).first()
}
}
fun getCommand(id: Int): Command? {
return transaction {
return@transaction Command.findById(id)
Command.findById(id)
}
}
fun getCommands(user: User): List<Command> {
return transaction {
return@transaction Command.find(Commands.user eq user.id)
Command.find(Commands.user eq user.id)
.toList()
}
}

View File

@@ -0,0 +1,105 @@
package space.mori.chzzk_bot.services
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction
import space.mori.chzzk_bot.models.*
import java.time.LocalDate
object CounterService {
fun getCounterValue(name: String, user: User): Int {
return transaction {
Counter.find {
(Counters.name eq name) and (Counters.user eq user.id)
}.singleOrNull()?.value ?: 0
}
}
fun updateCounterValue(name: String, increment: Int, user: User): Int {
return transaction {
val counter = Counter.find {
(Counters.name eq name) and (Counters.user eq user.id) }.singleOrNull()
return@transaction if (counter != null) {
counter.value += increment
counter.value
} else {
val newCounter = Counter.new {
this.name = name
this.value = increment
this.user = user
}
newCounter.value
}
}
}
fun getPersonalCounterValue(name: String, userId: String, user: User): Int {
return transaction {
PersonalCounter.find {
(PersonalCounters.name eq name) and (PersonalCounters.userId eq userId) and (PersonalCounters.user eq user.id)
}.singleOrNull()?.value ?: 0
}
}
fun updatePersonalCounterValue(name: String, userId: String, increment: Int, user: User): Int {
return transaction {
val counter = PersonalCounter.find {
(PersonalCounters.name eq name) and (PersonalCounters.userId eq userId) and (PersonalCounters.user eq user.id)
}.singleOrNull()
return@transaction if (counter != null) {
counter.value += increment
counter.value
} else {
val newCounter = PersonalCounter.new {
this.name = name
this.value = increment
this.userId = userId
this.user = user
}
newCounter.value
}
}
}
fun getDailyCounterValue(name: String, userId: String, user: User): Pair<Int, Boolean> {
val today = LocalDate.now()
return transaction {
val counter = DailyCounter.find {
(DailyCounters.name eq name) and (DailyCounters.userId eq userId) and (DailyCounters.user eq user.id)
}.singleOrNull()
Pair(counter?.value ?: 0, counter?.updatedAt != today)
}
}
fun updateDailyCounterValue(name: String, userId: String, increment: Int, user: User): Pair<Int, Boolean> {
val today = LocalDate.now()
return transaction {
val counter = DailyCounter.find {
(DailyCounters.name eq name) and (DailyCounters.userId eq userId) and (DailyCounters.user eq user.id)
}.singleOrNull()
println("$counter")
if(counter == null) {
val newCounter = DailyCounter.new {
this.name = name
this.value = increment
this.userId = userId
this.updatedAt = today
this.user = user
}
return@transaction Pair(newCounter.value, true)
}
return@transaction if(counter.updatedAt == today)
Pair(counter.value, false)
else {
counter.value += increment
Pair(counter.value, true)
}
}
}
}

View File

@@ -8,7 +8,7 @@ import space.mori.chzzk_bot.models.Users
object UserService {
fun saveUser(username: String, token: String, discordID: Long): User {
return transaction {
return@transaction User.new {
User.new {
this.username = username
this.token = token
this.discord = discordID
@@ -18,7 +18,7 @@ object UserService {
fun getUser(id: Int): User? {
return transaction {
return@transaction User.findById(id)
User.findById(id)
}
}
@@ -26,7 +26,7 @@ object UserService {
return transaction {
val users = User.find(Users.discord eq discordID)
return@transaction users.firstOrNull()
users.firstOrNull()
}
}
@@ -34,13 +34,13 @@ object UserService {
return transaction {
val users = User.find(Users.token eq chzzkID)
return@transaction users.firstOrNull()
users.firstOrNull()
}
}
fun getAllUsers(): List<User> {
return transaction {
return@transaction User.all().toList()
User.all().toList()
}
}
}