Skip to content

Commit

Permalink
Make home tile and exp rate settings dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Jan 10, 2025
1 parent 8bb82f6 commit 84585c5
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 54 deletions.
43 changes: 19 additions & 24 deletions engine/src/main/kotlin/world/gregs/voidps/engine/EngineModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,28 @@ val engineModule = module {
single {
SaveQueue(get(), SafeStorage(File(Settings["storage.players.errors"])))
}
single {
val homeTile = Tile(
x = Settings["world.home.x", 0],
y = Settings["world.home.y", 0],
level = Settings["world.home.level", 0]
)
AccountManager(get(), get(), get(), get(), get(), get(), homeTile, get(), get(), get(), get(), Settings["world.experienceRate", 1.0])
}
single { AccountManager(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
// IO
single { Yaml(YamlReaderConfiguration(2, 8, VERY_FAST_LOAD_FACTOR)) }
single { if (Settings["storage.type", "files"] == "database") {
DatabaseStorage.connect(
Settings["storage.database.username"],
Settings["storage.database.password"],
Settings["storage.database.driver"],
Settings["storage.database.jdbcUrl"],
Settings["storage.database.poolSize", 2],
)
DatabaseStorage()
} else {
val saves = File(Settings["storage.players.path"])
if (!saves.exists()) {
saves.mkdir()
single {
if (Settings["storage.type", "files"] == "database") {
DatabaseStorage.connect(
Settings["storage.database.username"],
Settings["storage.database.password"],
Settings["storage.database.driver"],
Settings["storage.database.jdbcUrl"],
Settings["storage.database.poolSize", 2],
)
DatabaseStorage()
} else {
val saves = File(Settings["storage.players.path"])
if (!saves.exists()) {
saves.mkdir()
}
FileStorage(get(), saves)
}
FileStorage(get(), saves, get(), Settings["world.experienceRate", 1.0])
} }
single { PlayerAccountLoader(get(), get(), get(), get(), get(), Contexts.Game, Settings["world.experienceRate", 1.0]) }
}
single { PlayerAccountLoader(get(), get(), get(), get(), get(), Contexts.Game) }
// Map
single { ZoneBatchUpdates() }
single { DynamicZones(get(), get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.withContext
import world.gregs.voidps.engine.data.AccountManager
import world.gregs.voidps.engine.data.AccountStorage
import world.gregs.voidps.engine.data.SaveQueue
import world.gregs.voidps.engine.data.Settings
import world.gregs.voidps.engine.data.definition.AccountDefinitions
import world.gregs.voidps.engine.entity.World
import world.gregs.voidps.engine.entity.character.player.Player
Expand All @@ -29,8 +30,7 @@ class PlayerAccountLoader(
private val accounts: AccountManager,
private val saveQueue: SaveQueue,
private val accountDefinitions: AccountDefinitions,
private val gameContext: CoroutineDispatcher,
private val experienceRate: Double
private val gameContext: CoroutineDispatcher
) : AccountLoader {
private val logger = InlineLogger()

Expand All @@ -52,7 +52,7 @@ class PlayerAccountLoader(
client.disconnect(Response.ACCOUNT_ONLINE)
return null
}
val player = storage.load(username)?.toPlayer(experienceRate) ?: accounts.create(username, passwordHash)
val player = storage.load(username)?.toPlayer(Settings["world.experienceRate", 1.0]) ?: accounts.create(username, passwordHash)
logger.info { "Player $username loaded and queued for login." }
connect(player, client, displayMode)
return player.instructions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ class AccountManager(
private val accountDefinitions: AccountDefinitions,
private val collisionStrategyProvider: CollisionStrategyProvider,
private val variableDefinitions: VariableDefinitions,
private val homeTile: Tile,
private val saveQueue: SaveQueue,
private val connectionQueue: ConnectionQueue,
private val players: Players,
private val areaDefinitions: AreaDefinitions,
private val experienceRate: Double
private val areaDefinitions: AreaDefinitions
) {
private val validItems = ValidItemRestriction(itemDefinitions)
private val homeTile: Tile
get() = Tile(Settings["world.home.x", 0], Settings["world.home.y", 0], Settings["world.home.level", 0])

fun create(name: String, passwordHash: String): Player {
return Player(tile = homeTile, accountName = name, passwordHash = passwordHash, experience = Experience(rate = experienceRate)).apply {
return Player(tile = homeTile, accountName = name, passwordHash = passwordHash, experience = Experience(rate = Settings["world.experienceRate", 1.0])).apply {
this["creation"] = System.currentTimeMillis()
this["new_player"] = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
import world.gregs.voidps.engine.data.AccountStorage
import world.gregs.voidps.engine.data.PlayerSave
import world.gregs.voidps.engine.data.config.AccountDefinition
import world.gregs.voidps.engine.data.definition.ItemDefinitions
import world.gregs.voidps.engine.data.yaml.PlayerYamlReaderConfig
import world.gregs.voidps.engine.data.yaml.PlayerYamlWriterConfig
import world.gregs.voidps.engine.entity.character.player.chat.clan.Clan
Expand All @@ -20,9 +19,7 @@ import java.io.File
@Suppress("UNCHECKED_CAST")
class FileStorage(
private val yaml: Yaml,
private val directory: File,
itemDefinitions: ItemDefinitions,
experienceRate: Double
private val directory: File
) : AccountStorage {
private val config = object : YamlReaderConfiguration() {
override fun set(map: MutableMap<String, Any>, key: String, value: Any, indent: Int, parentMap: String?) {
Expand Down Expand Up @@ -82,7 +79,7 @@ class FileStorage(
}

private val writeConfig = PlayerYamlWriterConfig()
private val readerConfig = PlayerYamlReaderConfig(itemDefinitions, experienceRate)
private val readerConfig = PlayerYamlReaderConfig()

override fun save(accounts: List<PlayerSave>) {
for (account in accounts) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package world.gregs.voidps.engine.data.yaml

import world.gregs.voidps.engine.data.definition.ItemDefinitions
import world.gregs.voidps.engine.data.Settings
import world.gregs.voidps.engine.entity.character.player.chat.clan.ClanRank
import world.gregs.voidps.engine.entity.character.player.skill.Skill
import world.gregs.voidps.engine.entity.character.player.skill.exp.Experience
Expand All @@ -9,10 +9,7 @@ import world.gregs.voidps.engine.entity.item.Item
import world.gregs.voidps.type.Tile
import world.gregs.yaml.read.YamlReaderConfiguration

internal class PlayerYamlReaderConfig(
private val itemDefinitions: ItemDefinitions,
private val experienceRate: Double
) : YamlReaderConfiguration() {
internal class PlayerYamlReaderConfig : YamlReaderConfiguration() {
override fun add(list: MutableList<Any>, value: Any, parentMap: String?) {
if (value is Map<*, *> && value.containsKey("id")) {
val id = value["id"] as String
Expand All @@ -37,7 +34,7 @@ internal class PlayerYamlReaderConfig(
val exp = Experience(
experience = (value["experience"] as List<Double>).toDoubleArray(),
blocked = (value["blocked"] as List<Skill>).toMutableSet(),
rate = experienceRate
rate = Settings["world.experienceRate", 1.0]
)
super.set(map, key, exp, indent, parentMap)
} else if (key == "levels") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class PlayerAccountLoaderTest : KoinMock() {
saveQueue = SaveQueue(storage, scope = TestScope())
definitions = AccountDefinitions(mutableMapOf("name" to AccountDefinition("name", "oldName", "", "hash")))
accounts = mockk(relaxed = true)
loader = PlayerAccountLoader(queue, storage, accounts, saveQueue, definitions, UnconfinedTestDispatcher(), 1.0)
loader = PlayerAccountLoader(queue, storage, accounts, saveQueue, definitions, UnconfinedTestDispatcher())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package world.gregs.voidps.engine.data
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.verify
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -53,19 +54,18 @@ class AccountManagerTest : KoinMock() {

override fun load(accountName: String): PlayerSave? = null
}
Settings.load(mapOf("world.home.x" to "1234", "world.home.y" to "5432", "world.experienceRate" to "1.0"))
manager = AccountManager(
interfaceDefinitions = get(),
inventoryDefinitions = inventoryDefinitions,
itemDefinitions = get(),
accountDefinitions = AccountDefinitions(),
collisionStrategyProvider = CollisionStrategyProvider(),
variableDefinitions = VariableDefinitions(),
homeTile = Tile(1234, 5432),
saveQueue = SaveQueue(storage),
connectionQueue = connectionQueue,
areaDefinitions = get(),
players = Players(),
experienceRate = 1.0
players = Players()
)
}

Expand Down Expand Up @@ -120,4 +120,9 @@ class AccountManagerTest : KoinMock() {
client.disconnect()
}
}

@AfterEach
fun teardown() {
Settings.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ class FileStorageTest : AccountStorageTest() {

override lateinit var storage: AccountStorage

private val itemDefinitions = ItemDefinitions(Array(30000) { ItemDefinition.EMPTY }).apply {
ids = emptyMap()
}

@BeforeEach
fun setup() {
storage = FileStorage(Yaml(), directory, itemDefinitions, 1.0)
storage = FileStorage(Yaml(), directory)
}

@Test
fun `Invalid directory returns no names`() {
val storage = FileStorage(Yaml(), directory.resolve("invalid"), itemDefinitions, 1.0)
val storage = FileStorage(Yaml(), directory.resolve("invalid"))
assertTrue(storage.names().isEmpty())
}

@Test
fun `Invalid directory returns no clans`() {
val storage = FileStorage(Yaml(), directory.resolve("invalid"), itemDefinitions, 1.0)
val storage = FileStorage(Yaml(), directory.resolve("invalid"))
assertTrue(storage.clans().isEmpty())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ val Character.damageDealers: MutableMap<Character, Int>
get() = getOrPut("damage_dealers") { mutableMapOf() }

val respawnTile: Tile
get() = Tile(Settings["world.home.x", 0], Settings["world.home.y", 0], Settings["world.home.level", 0])
get() = Tile(Settings["world.home.x", 0], Settings["world.home.y", 0], Settings["world.home.level", 0])

playerDeath { player ->
player.dead = true
Expand Down

0 comments on commit 84585c5

Please sign in to comment.