-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved idle phrases to database. add unified reload source command
- Loading branch information
1 parent
2902935
commit dce5d13
Showing
11 changed files
with
121 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
package dao | ||
|
||
import net.dv8tion.jda.core.entities.Game | ||
import org.jetbrains.exposed.sql.Table | ||
|
||
object DAOUsers : Table("users") { | ||
val discordId = long("discord_id").primaryKey() | ||
val name = varchar("name", 255) | ||
val classification = varchar("classification", 255) | ||
val ssn = integer("ssn") | ||
} | ||
|
||
object DAOPhrases : Table("idle_phrases") { | ||
val type = enumerationByName("type", | ||
Game.GameType.values().map { it.name.length }.max() ?: 9, | ||
Game.GameType::class.java) | ||
val phrase = text("phrase") | ||
val weight = float("weight") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package sources | ||
|
||
import dao.DAOPhrases | ||
import net.dv8tion.jda.core.entities.Game | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import utils.WeightedList | ||
import utils.toWeightedList | ||
import java.io.File | ||
|
||
|
||
typealias GameList = List<Game> | ||
typealias GameList = WeightedList<Game> | ||
|
||
object GameListSource : BasicExternalSource<GameList>() { | ||
|
||
|
||
override suspend fun load(): GameList = File("idlePhrases.txt").useLines { f -> | ||
f.filter { it[0] != '#' } | ||
.map { it.split(":", limit = 2) } | ||
.map { (type, text) -> Game.of(Game.GameType.valueOf(type), text) } | ||
.toList() | ||
override suspend fun load(): GameList = transaction { | ||
DAOPhrases.selectAll() | ||
.map { it[DAOPhrases.weight] to Game.of(it[DAOPhrases.type], it[DAOPhrases.phrase]) } | ||
.toWeightedList() | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package utils | ||
|
||
import kotlin.random.Random | ||
|
||
class WeightedList<A> { | ||
|
||
private val list = mutableListOf<Pair<Float, A>>() | ||
|
||
constructor(iterable: Iterable<Pair<Float, A>>) { | ||
addAll(iterable) | ||
} | ||
|
||
private val accumulatedWeight: Float | ||
get() = list.lastOrNull()?.first ?: 0.0f | ||
|
||
fun add(weight: Float, item: A) = | ||
list.add(accumulatedWeight + weight to item) | ||
|
||
fun addAll(iterable: Iterable<Pair<Float, A>>) { | ||
iterable.forEach { (weight, item) -> add(weight, item) } | ||
} | ||
|
||
fun random(): A = (Random.nextDouble() * accumulatedWeight).let { | ||
list.first { x -> x.first >= it }.second | ||
} | ||
|
||
} | ||
|
||
fun <A> List<Pair<Float, A>>.toWeightedList(): WeightedList<A> = | ||
WeightedList(this) |