Skip to content

Commit d150d3d

Browse files
authored
feat(tab): show each player's voice state in the tab list (#108)
The proxy follows plugin-voice's per-server snapshots on voice.players.<env>.> and draws a voice-on or voice-off icon before the name for Java viewers.
1 parent b86c9d2 commit d150d3d

7 files changed

Lines changed: 190 additions & 2 deletions

File tree

‎velocity/src/main/kotlin/gg/grounds/proxy/velocity/GroundsProxyPlugin.kt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import gg.grounds.proxy.velocity.tab.BedrockRoster
4949
import gg.grounds.proxy.velocity.tab.PlayerDisplayQueryImpl
5050
import gg.grounds.proxy.velocity.tab.TabList
5151
import gg.grounds.proxy.velocity.tab.TabParties
52+
import gg.grounds.proxy.velocity.tab.VoicePresences
5253
import io.micrometer.core.instrument.MeterRegistry
5354
import io.nats.client.Subscription
5455
import java.time.Duration
@@ -217,6 +218,7 @@ constructor(private val proxy: ProxyServer, private val logger: Logger) {
217218
private var tabList: TabList? = null
218219
private var bedrockRoster: BedrockRoster? = null
219220
private val tabParties = TabParties()
221+
private val voicePresences = VoicePresences()
220222
private var partySubject: String? = null
221223

222224
/**
@@ -339,9 +341,14 @@ constructor(private val proxy: ProxyServer, private val logger: Logger) {
339341
serverQuery = { ProxyServiceRegistry.get(ServerDisplayQuery::class.java) },
340342
isBedrock = roster::isBedrock,
341343
partyOf = tabParties::partyOf,
344+
voiceOf = voicePresences::voiceOf,
342345
)
343346
tabList = tab
344347
partySubject = TabParties.subject(System.getenv("GROUNDS_ENVIRONMENT"))
348+
VoicePresences.subject(System.getenv("GROUNDS_ENVIRONMENT"))?.let { subject ->
349+
crossProxySubscriptions +=
350+
natsHandler.subscribe(subject) { if (voicePresences.receive(it)) tab.refreshAll() }
351+
}
345352
partySubject?.let { subject ->
346353
crossProxySubscriptions +=
347354
natsHandler.subscribe(subject) { if (tabParties.receive(it)) tab.refreshAll() }

‎velocity/src/main/kotlin/gg/grounds/proxy/velocity/tab/TabGlyphs.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ object TabGlyphs {
1111
const val BADGE_RIGHT = '\uE003'
1212
const val BEDROCK_ICON = '\uE004'
1313
const val BEDROCK_ADVANCE = 9
14+
/** resourcepacks `TabIcons`: white 7x7 glyphs in an 8px cell, tinted by the text colour. */
15+
const val VOICE_ON = '\uE005'
16+
const val VOICE_OFF = '\uE006'
17+
const val PARTY = '\uE007'
18+
const val ICON_ADVANCE = 8
1419
const val LEFT_PX = 3
1520
const val MIDDLE_PX = 1
1621
const val RIGHT_PX = 3

‎velocity/src/main/kotlin/gg/grounds/proxy/velocity/tab/TabList.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TabList(
2020
private val serverQuery: () -> ServerDisplayQuery?,
2121
private val isBedrock: (java.util.UUID) -> Boolean = { false },
2222
private val partyOf: (java.util.UUID) -> java.util.UUID? = { null },
23+
private val voiceOf: (java.util.UUID) -> Boolean? = { null },
2324
) {
2425
private val rankWarmup = RankWarmup()
2526

@@ -115,6 +116,7 @@ class TabList(
115116
isBedrock(entry.profile.id),
116117
bedrockViewer = isBedrock(viewer.uniqueId),
117118
partyMember = entry.profile.id in partyMembers,
119+
voice = voiceOf(entry.profile.id),
118120
)
119121
if (supplied != formatted) entry.setDisplayName(formatted)
120122
}

‎velocity/src/main/kotlin/gg/grounds/proxy/velocity/tab/TabName.kt‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ object TabName {
1717
bedrock: Boolean = false,
1818
bedrockViewer: Boolean = false,
1919
partyMember: Boolean = false,
20+
/** True for voice on, false for voice off, null for no voice; Java viewers only. */
21+
voice: Boolean? = null,
2022
): Component {
2123
val displayName = PlayerDisplayNames.displayName(name, bedrock)
2224
val colour = role?.colour?.let(TextColor::fromHexString) ?: Palette.TEXT
@@ -40,19 +42,38 @@ object TabName {
4042
)
4143
row.append(Component.text(TabSpaces.of(GAP)).font(TabGlyphs.FONT))
4244
}
45+
voice?.let { on ->
46+
row.append(
47+
Component.text(
48+
if (on) TabGlyphs.VOICE_ON else TabGlyphs.VOICE_OFF,
49+
if (on) Palette.SUCCESS else Palette.TEXT_FAINT,
50+
)
51+
.font(TabGlyphs.FONT)
52+
)
53+
row.append(Component.text(TabSpaces.of(GAP)).font(TabGlyphs.FONT))
54+
}
4355
row.append(nameText)
44-
val pad = TabGlyphs.HEADER_WIDTH - rowWidth(displayName, rank, bedrock, partyMember)
56+
val pad =
57+
TabGlyphs.HEADER_WIDTH -
58+
rowWidth(displayName, rank, bedrock, partyMember, voice != null)
4559
if (pad > 0) {
4660
row.append(Component.text(TabSpaces.of(pad)).font(TabGlyphs.FONT))
4761
}
4862
return row.build()
4963
}
5064

51-
private fun rowWidth(name: String, rank: String?, bedrock: Boolean, bold: Boolean): Int {
65+
private fun rowWidth(
66+
name: String,
67+
rank: String?,
68+
bedrock: Boolean,
69+
bold: Boolean,
70+
voice: Boolean,
71+
): Int {
5272
// Bold draws every glyph one pixel wider.
5373
var width = VanillaAdvances.width(name) + if (bold) name.length else 0
5474
rank?.let { width += TabBadge.width(it) + GAP }
5575
if (bedrock) width += TabGlyphs.BEDROCK_ADVANCE + GAP
76+
if (voice) width += TabGlyphs.ICON_ADVANCE + GAP
5677
return width
5778
}
5879
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package gg.grounds.proxy.velocity.tab
2+
3+
import com.google.gson.JsonParser
4+
import java.util.UUID
5+
import java.util.concurrent.ConcurrentHashMap
6+
7+
/**
8+
* Who has voice on, from plugin-voice's per-server snapshots on `voice.players.<env>.<zone>`:
9+
* `{"schemaVersion":1,"server":"<podIP:port>","players":{"<uuid>":"on"|"off"}}`. Each server sends
10+
* one after every change and every 5 s, so a snapshot older than [EXPIRY_MILLIS] belongs to a
11+
* server that is gone. Cosmetic: a wrong entry only draws the wrong icon.
12+
*/
13+
internal class VoicePresences(
14+
private val nowMillis: () -> Long = { System.nanoTime() / 1_000_000 }
15+
) {
16+
private data class Snapshot(val players: Map<UUID, Boolean>, val receivedAt: Long)
17+
18+
private val servers = ConcurrentHashMap<String, Snapshot>()
19+
20+
/** True for voice on, false for voice switched off, null for no voice (or no mod). */
21+
fun voiceOf(player: UUID): Boolean? {
22+
val now = nowMillis()
23+
return servers.values
24+
.filter { now - it.receivedAt < EXPIRY_MILLIS && player in it.players }
25+
.maxByOrNull { it.receivedAt }
26+
?.players
27+
?.get(player)
28+
}
29+
30+
/** Applies one snapshot and reports whether it changed what a tab list would show. */
31+
fun receive(raw: String): Boolean {
32+
if (raw.length > MAX_PAYLOAD) return false
33+
val (server, players) =
34+
runCatching {
35+
val root = JsonParser.parseString(raw).asJsonObject
36+
if (root.get("schemaVersion")?.asInt != 1) return false
37+
val entries = root.getAsJsonObject("players")
38+
if (entries.size() > MAX_PLAYERS) return false
39+
root.get("server").asString to
40+
entries.entrySet().associate { (id, state) ->
41+
UUID.fromString(id) to
42+
when (state.asString) {
43+
"on" -> true
44+
"off" -> false
45+
else -> return false
46+
}
47+
}
48+
}
49+
.getOrNull() ?: return false
50+
val now = nowMillis()
51+
servers.entries.removeIf { now - it.value.receivedAt >= EXPIRY_MILLIS }
52+
if (!servers.containsKey(server) && servers.size >= MAX_SERVERS) return false
53+
return servers.put(server, Snapshot(players, now))?.players != players
54+
}
55+
56+
companion object {
57+
const val EXPIRY_MILLIS = 20_000L
58+
private const val MAX_PAYLOAD = 256 * 1024
59+
private const val MAX_PLAYERS = 5_000
60+
private const val MAX_SERVERS = 1_024
61+
private val ENVIRONMENT = Regex("[A-Za-z0-9_-]{1,128}")
62+
63+
/** Every zone of [environment]; each server publishes under its own zone. */
64+
fun subject(environment: String?): String? =
65+
environment?.trim()?.takeIf { it.matches(ENVIRONMENT) }?.let { "voice.players.$it.>" }
66+
}
67+
}

‎velocity/src/test/kotlin/gg/grounds/proxy/velocity/tab/TabNameTest.kt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ class TabNameTest {
127127
)
128128
}
129129

130+
@Test
131+
fun `the voice icon sits before the name and is counted in the padding`() {
132+
val name = "Steve"
133+
val text =
134+
PlainTextComponentSerializer.plainText()
135+
.serialize(TabName.format(name, null, voice = false))
136+
assertTrue(text.startsWith(TabGlyphs.VOICE_OFF.toString()), text)
137+
assertTrue(
138+
text.endsWith(
139+
TabSpaces.of(
140+
TabGlyphs.HEADER_WIDTH -
141+
VanillaAdvances.width(name) -
142+
TabGlyphs.ICON_ADVANCE -
143+
2
144+
)
145+
),
146+
text,
147+
)
148+
}
149+
150+
@Test
151+
fun `bedrock viewers get no voice icon`() {
152+
val text =
153+
PlainTextComponentSerializer.plainText()
154+
.serialize(TabName.format("Steve", null, bedrockViewer = true, voice = true))
155+
assertEquals("Steve", text)
156+
}
157+
130158
@Test
131159
fun `a name that is already wider than the wordmark is not padded`() {
132160
val name = "A".repeat(40)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package gg.grounds.proxy.velocity.tab
2+
3+
import java.util.UUID
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Assertions.assertFalse
6+
import org.junit.jupiter.api.Assertions.assertNull
7+
import org.junit.jupiter.api.Assertions.assertTrue
8+
import org.junit.jupiter.api.Test
9+
10+
class VoicePresencesTest {
11+
private var now = 1_000L
12+
private val presences = VoicePresences { now }
13+
private val alice = UUID(0, 1)
14+
private val bob = UUID(0, 2)
15+
16+
private fun snapshot(server: String, vararg players: Pair<UUID, String>) =
17+
"""{"schemaVersion":1,"server":"$server","players":{${
18+
players.joinToString(",") { "\"${it.first}\":\"${it.second}\"" }
19+
}}}"""
20+
21+
@Test
22+
fun `a snapshot tells voice on from voice off and leaves others without an icon`() {
23+
assertTrue(presences.receive(snapshot("10.0.0.1:24460", alice to "on", bob to "off")))
24+
assertEquals(true, presences.voiceOf(alice))
25+
assertEquals(false, presences.voiceOf(bob))
26+
assertNull(presences.voiceOf(UUID(0, 3)))
27+
assertFalse(presences.receive(snapshot("10.0.0.1:24460", alice to "on", bob to "off")))
28+
}
29+
30+
@Test
31+
fun `the freshest server wins after a player switched servers`() {
32+
presences.receive(snapshot("10.0.0.1:24460", alice to "on"))
33+
now += 1_000
34+
presences.receive(snapshot("10.0.0.2:24460", alice to "off"))
35+
assertEquals(false, presences.voiceOf(alice))
36+
}
37+
38+
@Test
39+
fun `a server that stopped sending is forgotten`() {
40+
presences.receive(snapshot("10.0.0.1:24460", alice to "on"))
41+
now += VoicePresences.EXPIRY_MILLIS
42+
assertNull(presences.voiceOf(alice))
43+
}
44+
45+
@Test
46+
fun `malformed snapshots are ignored`() {
47+
assertFalse(presences.receive("nope"))
48+
assertFalse(presences.receive("""{"schemaVersion":2,"server":"x","players":{}}"""))
49+
assertFalse(presences.receive(snapshot("x", alice to "loud")))
50+
assertNull(presences.voiceOf(alice))
51+
}
52+
53+
@Test
54+
fun `the subject covers every zone of the environment`() {
55+
assertEquals("voice.players.stage.>", VoicePresences.subject("stage"))
56+
assertNull(VoicePresences.subject("stage.*"))
57+
}
58+
}

0 commit comments

Comments
 (0)