diff --git a/velocity/src/main/kotlin/gg/grounds/proxy/velocity/GroundsProxyPlugin.kt b/velocity/src/main/kotlin/gg/grounds/proxy/velocity/GroundsProxyPlugin.kt index 50d76eb..98c6508 100644 --- a/velocity/src/main/kotlin/gg/grounds/proxy/velocity/GroundsProxyPlugin.kt +++ b/velocity/src/main/kotlin/gg/grounds/proxy/velocity/GroundsProxyPlugin.kt @@ -170,7 +170,7 @@ constructor(private val proxy: ProxyServer, private val logger: Logger) { // Looked up per invocation rather than captured: the registry is written during startup and // a reference taken now could be the one from before another plugin registered. val service = { ProxyServiceRegistry.get(ProxyService::class.java) } - val region = { System.getenv("REGION")?.trim()?.takeIf(String::isNotEmpty) } + val region = { RegionCatalog.currentCode() } proxy.commandManager.register("online", OnlineCommand(service)) proxy.commandManager.register("region", RegionCommand(catalog, region, service)) diff --git a/velocity/src/main/kotlin/gg/grounds/proxy/velocity/RegionCatalog.kt b/velocity/src/main/kotlin/gg/grounds/proxy/velocity/RegionCatalog.kt index 8df1124..afcbfc7 100644 --- a/velocity/src/main/kotlin/gg/grounds/proxy/velocity/RegionCatalog.kt +++ b/velocity/src/main/kotlin/gg/grounds/proxy/velocity/RegionCatalog.kt @@ -29,6 +29,18 @@ class RegionCatalog(val regions: List, val problems: List) { get() = regions.map { it.code } companion object { + /** + * Which catalogue code this proxy answers for. The catalogue is keyed by continent (`eu`), + * not by datacentre (`de-nbg1`), so `CONTINENT` is what a code compares against. `REGION` + * is the fallback for a catalogue still keyed by datacentre, where the two are the same. + */ + fun currentCode( + continent: String? = System.getenv("CONTINENT"), + region: String? = System.getenv("REGION"), + ): String? = + continent?.trim()?.takeIf(String::isNotEmpty) + ?: region?.trim()?.takeIf(String::isNotEmpty) + fun fromEnvironment( raw: String? = System.getenv("REGIONS"), defaultPort: Int = ProxyService.DEFAULT_MINECRAFT_PORT, diff --git a/velocity/src/test/kotlin/gg/grounds/proxy/velocity/RegionCatalogTest.kt b/velocity/src/test/kotlin/gg/grounds/proxy/velocity/RegionCatalogTest.kt index c76a963..00269ad 100644 --- a/velocity/src/test/kotlin/gg/grounds/proxy/velocity/RegionCatalogTest.kt +++ b/velocity/src/test/kotlin/gg/grounds/proxy/velocity/RegionCatalogTest.kt @@ -47,4 +47,16 @@ class RegionCatalogTest { assertTrue(catalog.problems.isEmpty()) assertNull(catalog["nl-ams1"]) } + + /** The catalogue is keyed by continent; comparing with the datacentre never matches. */ + @Test + fun `current code is the continent, not the datacentre`() { + assertEquals("eu", RegionCatalog.currentCode(continent = "eu", region = "de-nbg1")) + } + + @Test + fun `current code falls back to the region when no continent is set`() { + assertEquals("nl-ams1", RegionCatalog.currentCode(continent = " ", region = "nl-ams1")) + assertNull(RegionCatalog.currentCode(continent = null, region = null)) + } }