Improved the GE database access to use a basic reference counter for the connection that automatically closes the connection when no one is using it anymore

Grand Exchange now uses the GE sqlite database's price index table to track price changes
Removed the old price index (confusingly called GrandExchangeDatabase.java)
Grand Exchange prices are now dynamic and influenced by trades (weighted stepping average price)
Fixed GE interfaces not loading on login
This commit is contained in:
Ceikry
2022-05-20 11:23:30 +00:00
committed by Ryan
parent 01a4292123
commit 334bc77d06
20 changed files with 422 additions and 475 deletions
+38 -4
View File
@@ -8,18 +8,20 @@ import rs09.game.ge.GEDB
import rs09.game.ge.GrandExchange
import rs09.game.ge.GrandExchangeOffer
import rs09.game.system.SystemLogger
import java.io.File
import kotlin.random.Random
@TestInstance(TestInstance.Lifecycle.PER_CLASS) class ExchangeTests {
companion object {
private const val TEST_DB_PATH = "ge_test.db"
init {
TestUtils.preTestSetup()
GEDB.init(TEST_DB_PATH)
}
fun generateOffer(itemId: Int, amount: Int, price: Int, sale: Boolean) : GrandExchangeOffer {
fun generateOffer(itemId: Int, amount: Int, price: Int, sale: Boolean, username: String = "test ${System.currentTimeMillis()}") : GrandExchangeOffer {
val offer = GrandExchangeOffer()
val uid = "test ${System.currentTimeMillis()}".hashCode()
val uid = username.hashCode()
offer.offerState = OfferState.REGISTERED
offer.itemID = itemId
offer.offeredValue = price
@@ -32,14 +34,18 @@ import kotlin.random.Random
offer.writeNew()
return offer
}
@AfterAll fun cleanup() {
File(TEST_DB_PATH).delete()
}
}
@Test fun testPlaceOffer() {
val offer = generateOffer(4151, 1, 100000, true)
val uid = offer.playerUID
with (GEDB.connect()) {
val stmt = this.createStatement()
GEDB.run { conn ->
val stmt = conn.createStatement()
val result = stmt.executeQuery("select * from player_offers where player_uid = $uid")
val thisOffer = if(result.next()) GrandExchangeOffer.fromQuery(result) else fail("Offer did not exist!")
Assertions.assertEquals(offer.itemID, thisOffer.itemID)
@@ -66,4 +72,32 @@ import kotlin.random.Random
GrandExchange.exchange(sellOffer,buyOffer)
Assertions.assertEquals(null, buyOffer.withdraw[0]) //Buyer should not get any items, sell offer is higher
}
@Test fun manyCompletedOffersAboveDefaultPriceShouldInfluencePriceUpwards() {
val defaultPrice = GrandExchange.getRecommendedPrice(4151)
val modifiedPrice = defaultPrice * 1.15
for(i in 0 until 100) {
val sellOffer = generateOffer(4151, 1, modifiedPrice.toInt(), true, "test1")
val buyOffer = generateOffer(4151, 1, modifiedPrice.toInt(), false, "test2")
GrandExchange.exchange(sellOffer,buyOffer)
}
val newPrice = GrandExchange.getRecommendedPrice(4151)
Assertions.assertEquals(true, newPrice > defaultPrice, "Price was not influenced in the expected way! New Price: $newPrice, default: $defaultPrice")
}
@Test fun playerTradingWithThemselvesShouldNotBeAbleToInfluencePrices() {
val defaultPrice = GrandExchange.getRecommendedPrice(4151)
val modifiedPrice = defaultPrice * 1.15
for(i in 0 until 100) {
val sellOffer = generateOffer(4151, 1, modifiedPrice.toInt(), true, "test")
val buyOffer = generateOffer(4151, 1, modifiedPrice.toInt(), false, "test")
GrandExchange.exchange(sellOffer,buyOffer)
}
Assertions.assertEquals(defaultPrice, GrandExchange.getRecommendedPrice(4151))
}
}
+37
View File
@@ -0,0 +1,37 @@
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import rs09.game.ge.GEDB
import rs09.game.ge.PriceIndex
class PriceIndexTests {
companion object {init {TestUtils.preTestSetup(); GEDB.init("ge_test.db")}}
@Test fun shouldAllowCheckingIfItemCanBeSoldOnGE() {
PriceIndex.allowItem(4151)
val canSellWhip = PriceIndex.canTrade(4151)
Assertions.assertEquals(true, canSellWhip)
}
@Test fun shouldAllowBanningItems() {
PriceIndex.allowItem(1333)
Assertions.assertEquals(true, PriceIndex.canTrade(1333))
PriceIndex.banItem(1333)
Assertions.assertEquals(false, PriceIndex.canTrade(1333))
}
@Test fun shouldAllowValueToBeInfluenced() {
PriceIndex.allowItem(1333)
val defaultValue = PriceIndex.getValue(1333)
PriceIndex.addTrade(1333, 100, (defaultValue * 1.15).toInt())
Assertions.assertNotEquals(defaultValue, PriceIndex.getValue(1333))
}
@Test fun shouldAllowValueToGoDownWithHighVolumeOfLowerValueTrades() {
PriceIndex.allowItem(1334)
val defaultValue = PriceIndex.getValue(1334)
PriceIndex.addTrade(1334, 1000, (defaultValue * 1.15).toInt())
Assertions.assertEquals(true, PriceIndex.getValue(1334) > defaultValue)
PriceIndex.addTrade(1334, 2000, (defaultValue * 0.85).toInt())
Assertions.assertEquals(true, PriceIndex.getValue(1334) < defaultValue, "Old: $defaultValue, New: ${PriceIndex.getValue(1334)}")
}
}