feat(game): add a chat command system with /help, /pos, /testbattle and /catch (#125)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-09 09:11:19 +02:00
1 parent abce0d6eb4
commit 70c746fdc6
24 files changed
+662 -142

No files matched your search

@@ -102,9 +102,11 @@ private class ConcurrentAttributes : SessionAttributes {
@Suppress("UNCHECKED_CAST")
override fun <T : Any> remove(key: SessionAttribute<T>): T? = map.remove(key) as T?
// Not getOrPut. That is a get then a put, so racing threads each build one and the loser's
// value is dropped while still in use.
@Suppress("UNCHECKED_CAST")
override fun <T : Any> getOrPut(key: SessionAttribute<T>, default: () -> T): T =
map.getOrPut(key) { default() } as T
map.computeIfAbsent(key) { default() } as T
override fun contains(key: SessionAttribute<*>): Boolean = map.containsKey(key)
}
@@ -12,6 +12,9 @@ import io.kotest.matchers.shouldBe
import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
private data class Ping(val n: Int)
@@ -121,6 +124,35 @@ class TypedProtocolHandlerTest :
session.attributes.contains(key) shouldBe true
}
test("getOrPut builds one value however many threads race for it") {
val handler = ServerHandler()
val channel = channelWithHandler(handler)
val session = channel.attr(SESSION_KEY).get()
val key = SessionAttribute.of<Any>("scope")
val built = AtomicInteger()
val threads = 16
val start = CountDownLatch(1)
val pool = Executors.newFixedThreadPool(threads)
val results =
(1..threads)
.map {
pool.submit<Any> {
start.await()
session.attributes.getOrPut(key) {
built.incrementAndGet()
Any()
}
}
}
.also { start.countDown() }
.map { it.get() }
pool.shutdown()
built.get() shouldBe 1
results.all { it === results.first() } shouldBe true
}
test("OutgoingPacket goes through ProtocolHandler.write") {
val handler = ServerHandler()
val channel = channelWithHandler(handler)