diff --git a/database/src/main/kotlin/world/gregs/voidps/storage/DatabaseStorage.kt b/database/src/main/kotlin/world/gregs/voidps/storage/DatabaseStorage.kt index e3b522c186..10876043ec 100644 --- a/database/src/main/kotlin/world/gregs/voidps/storage/DatabaseStorage.kt +++ b/database/src/main/kotlin/world/gregs/voidps/storage/DatabaseStorage.kt @@ -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, @@ -76,7 +76,7 @@ class DatabaseStorage : Storage { } } - override fun offers(days: Int): OpenOffers { + override fun offers(days: Int): OpenOffers = transaction { val buyByItem: MutableMap>> = mutableMapOf() val sellByItem: MutableMap>> = mutableMapOf() val offers = OpenOffers(sellByItem, buyByItem) @@ -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) { + override fun saveClaims(claims: Map): Unit = transaction { ClaimsTable.deleteAll() ClaimsTable.batchUpsert(claims.toList(), ClaimsTable.offerId) { (id, claim) -> this[ClaimsTable.offerId] = id @@ -117,15 +117,13 @@ class DatabaseStorage : Storage { } } - override fun savePriceHistory(history: Map) { + override fun savePriceHistory(history: Map) = 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") } } @@ -149,14 +147,16 @@ class DatabaseStorage : Storage { } } - override fun claims(): Map = 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 = 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 { + override fun priceHistory(): Map = transaction { val history = mutableMapOf() ItemHistoryTable.selectAll().forEach { row -> val item = row[ItemHistoryTable.item] @@ -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) { - 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): 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 { @@ -260,8 +258,8 @@ class DatabaseStorage : Storage { private fun saveOffers(accounts: List, playerIds: Map) { 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 @@ -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] } @@ -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() diff --git a/database/src/main/kotlin/world/gregs/voidps/storage/Tables.kt b/database/src/main/kotlin/world/gregs/voidps/storage/Tables.kt index b56653d5d8..e5a00a2914 100644 --- a/database/src/main/kotlin/world/gregs/voidps/storage/Tables.kt +++ b/database/src/main/kotlin/world/gregs/voidps/storage/Tables.kt @@ -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") { @@ -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") { @@ -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") } diff --git a/game/src/main/resources/logback.xml b/game/src/main/resources/logback.xml index 38dc1d5f7c..0a7e34fbd0 100644 --- a/game/src/main/resources/logback.xml +++ b/game/src/main/resources/logback.xml @@ -13,4 +13,5 @@ + \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index ba572b16ff..f16ff424f6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,3 @@ #org.gradle.java.home=C:/Users//.jdks/openjdk-21.0.1/ -org.gradle.jvmargs=-Xmx7g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8 \ No newline at end of file +org.gradle.jvmargs=-Xmx7g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8 +#includeDb = true \ No newline at end of file