Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class DatabaseStorage : Storage {
AccountsTable
.leftJoin(display) {
AccountsTable.id eq display[VariablesTable.playerId] and
(display[VariablesTable.name] eq stringLiteral("display_name"))
(display[VariablesTable.name] eq stringLiteral("display_name"))
}
.leftJoin(history) {
AccountsTable.id eq history[VariablesTable.playerId] and
(history[VariablesTable.name] eq stringLiteral("name_history"))
(history[VariablesTable.name] eq stringLiteral("name_history"))
}
.select(
AccountsTable.name,
Expand Down Expand Up @@ -76,7 +76,7 @@ class DatabaseStorage : Storage {
}
}

override fun offers(days: Int): OpenOffers {
override fun offers(days: Int): OpenOffers = transaction {
val buyByItem: MutableMap<String, TreeMap<Int, MutableList<OpenOffer>>> = mutableMapOf()
val sellByItem: MutableMap<String, TreeMap<Int, MutableList<OpenOffer>>> = mutableMapOf()
val offers = OpenOffers(sellByItem, buyByItem)
Expand All @@ -102,13 +102,13 @@ class DatabaseStorage : Storage {
}
val maxId = OffersTable.id.max()
for (row in OffersTable.select(maxId)) {
val max = row[maxId]!!
offers.counter = max
val max = row[maxId]
offers.counter = max ?: 0
}
return offers
offers
}

override fun saveClaims(claims: Map<Int, Claim>) {
override fun saveClaims(claims: Map<Int, Claim>): Unit = transaction {
ClaimsTable.deleteAll()
ClaimsTable.batchUpsert(claims.toList(), ClaimsTable.offerId) { (id, claim) ->
this[ClaimsTable.offerId] = id
Expand All @@ -117,15 +117,13 @@ class DatabaseStorage : Storage {
}
}

override fun savePriceHistory(history: Map<String, PriceHistory>) {
override fun savePriceHistory(history: Map<String, PriceHistory>) = transaction {
ItemHistoryTable.deleteAll()
transaction {
for ((item, itemHistory) in history) {
insertAggregate(item, itemHistory.day, "day")
insertAggregate(item, itemHistory.week, "week")
insertAggregate(item, itemHistory.month, "month")
insertAggregate(item, itemHistory.year, "year")
}
for ((item, itemHistory) in history) {
insertAggregate(item, itemHistory.day, "day")
insertAggregate(item, itemHistory.week, "week")
insertAggregate(item, itemHistory.month, "month")
insertAggregate(item, itemHistory.year, "year")
}
}

Expand All @@ -149,14 +147,16 @@ class DatabaseStorage : Storage {
}
}

override fun claims(): Map<Int, Claim> = ClaimsTable.selectAll().associate { row ->
val id = row[ClaimsTable.offerId]
val amount = row[ClaimsTable.amount]
val coins = row[ClaimsTable.coins]
id to Claim(amount, coins)
override fun claims(): Map<Int, Claim> = transaction {
ClaimsTable.selectAll().associate { row ->
val id = row[ClaimsTable.offerId]
val amount = row[ClaimsTable.amount]
val coins = row[ClaimsTable.coins]
id to Claim(amount, coins)
}
}

override fun priceHistory(): Map<String, PriceHistory> {
override fun priceHistory(): Map<String, PriceHistory> = transaction {
val history = mutableMapOf<String, PriceHistory>()
ItemHistoryTable.selectAll().forEach { row ->
val item = row[ItemHistoryTable.item]
Expand All @@ -182,24 +182,22 @@ class DatabaseStorage : Storage {
}
frame[timestamp] = Aggregate(open = open, high = high, low = low, close = close, volume = volume, count = count, averageHigh = averageHigh, averageLow = averageLow, volumeHigh = volumeHigh, volumeLow = volumeLow)
}
return history
history
}

override fun save(accounts: List<PlayerSave>) {
transaction {
saveAccounts(accounts)
val names = accounts.map { it.name }
val playerIds = AccountsTable
.select(AccountsTable.id, AccountsTable.name)
.where { LowerCase(AccountsTable.name) inList names.map { it.lowercase() } }
.associate { it[AccountsTable.name].lowercase() to it[AccountsTable.id] }
saveExperience(accounts, playerIds)
saveLevels(accounts, playerIds)
saveVariables(accounts, playerIds)
saveInventories(accounts, playerIds)
saveOffers(accounts, playerIds)
saveHistories(accounts, playerIds)
}
override fun save(accounts: List<PlayerSave>): Unit = transaction {
saveAccounts(accounts)
val names = accounts.map { it.name }
val playerIds = AccountsTable
.select(AccountsTable.id, AccountsTable.name)
.where { LowerCase(AccountsTable.name) inList names.map { it.lowercase() } }
.associate { it[AccountsTable.name].lowercase() to it[AccountsTable.id] }
saveExperience(accounts, playerIds)
saveLevels(accounts, playerIds)
saveVariables(accounts, playerIds)
saveInventories(accounts, playerIds)
saveOffers(accounts, playerIds)
saveHistories(accounts, playerIds)
}

override fun exists(accountName: String): Boolean = transaction {
Expand Down Expand Up @@ -260,8 +258,8 @@ class DatabaseStorage : Storage {

private fun saveOffers(accounts: List<PlayerSave>, playerIds: Map<String, Int>) {
OffersTable.deleteWhere { playerId inList playerIds.values }
val offerData = accounts.flatMap { save -> save.offers.withIndex().map { (index, offer) -> Triple(save.name, index, offer) } }
OffersTable.batchUpsert(offerData, OffersTable.playerId, OffersTable.id, OffersTable.index) { (id, index, offer) ->
val offerData = accounts.flatMap { save -> save.offers.filter { !it.isEmpty() }.withIndex().map { (index, offer) -> Triple(save.name, index, offer) } }
OffersTable.batchUpsert(offerData, OffersTable.playerId, OffersTable.index, OffersTable.id) { (id, index, offer) ->
this[OffersTable.playerId] = playerIds.getValue(id.lowercase())
this[OffersTable.id] = offer.id
this[OffersTable.index] = index
Expand All @@ -275,7 +273,7 @@ class DatabaseStorage : Storage {
}
}

override fun saveOffers(offers: OpenOffers) {
override fun saveOffers(offers: OpenOffers) = transaction {
val playerIds = AccountsTable
.select(AccountsTable.id, AccountsTable.name)
.associate { it[AccountsTable.name].lowercase() to it[AccountsTable.id] }
Expand Down Expand Up @@ -567,7 +565,7 @@ class DatabaseStorage : Storage {
}
}

internal val tables = arrayOf(AccountsTable, ExperienceTable, LevelsTable, VariablesTable, InventoriesTable, OffersTable, PlayerHistoryTable)
internal val tables = arrayOf(AccountsTable, ExperienceTable, LevelsTable, VariablesTable, InventoriesTable, OffersTable, ActiveOffersTable, PlayerHistoryTable, ClaimsTable, ItemHistoryTable)

private const val TYPE_STRING = 0.toByte()
private const val TYPE_INT = 1.toByte()
Expand Down
8 changes: 6 additions & 2 deletions database/src/main/kotlin/world/gregs/voidps/storage/Tables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ internal object OffersTable : Table("grand_exchange_offers") {
val coins = integer("coins").default(0)

init {
index(true, playerId, id, index)
index(true, playerId, index, id)
}

override val primaryKey = PrimaryKey(id, name = "pk_offer_id")
}

internal object ActiveOffersTable : Table("grand_exchange_active_offers") {
Expand All @@ -134,6 +136,8 @@ internal object ActiveOffersTable : Table("grand_exchange_active_offers") {
init {
index(true, playerId, id)
}

override val primaryKey = PrimaryKey(OffersTable.id, name = "pk_active_offer_id")
}

internal object PlayerHistoryTable : Table("player_exchange_history") {
Expand All @@ -148,7 +152,7 @@ internal object PlayerHistoryTable : Table("player_exchange_history") {
}

internal object ClaimsTable : Table("grand_exchange_claims") {
val offerId = integer("offer_id").references(OffersTable.id)
val offerId = integer("offer_id").references(OffersTable.id).uniqueIndex()
val amount = integer("amount")
val coins = integer("coins")
}
Expand Down
1 change: 1 addition & 0 deletions game/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<logger name="org.jetbrains.exposed" level="WARN"/>
<logger name="com.zaxxer.hikari.pool" level="WARN"/>
<logger name="com.zaxxer.hikari.HikariConfig" level="WARN"/>
<logger name="Exposed" level="WARN"/>
</configuration>
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#org.gradle.java.home=C:/Users/<name>/.jdks/openjdk-21.0.1/
org.gradle.jvmargs=-Xmx7g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx7g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8
#includeDb = true
Loading