Skip to content

Commit 2ea3df9

Browse files
committed
Make guest names great again! Fix #55
1 parent ddac365 commit 2ea3df9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

games-server/src/main/kotlin/net/zomis/games/server2/AuthorizationSystem.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ import com.fasterxml.jackson.databind.ObjectMapper
44
import com.github.kittinunf.fuel.Fuel
55
import net.zomis.core.events.EventSystem
66
import org.slf4j.LoggerFactory
7+
import java.net.URL
78
import java.util.UUID
89

910
data class ClientLoginEvent(val client: Client, val providerId: String, val loginName: String, val provider: String, val token: String)
1011

12+
fun URL.lines(): List<String> {
13+
return this.readText(Charsets.UTF_8).split("\n")
14+
.map { it.trim() }
15+
.filter { it.isNotEmpty() }.distinct()
16+
.map { it[0].toUpperCase() + it.substring(1) }
17+
}
18+
1119
class AuthorizationSystem(private val events: EventSystem) {
1220
val router = MessageRouter(this)
1321
.handler("guest", this::handleGuest)
@@ -18,9 +26,15 @@ class AuthorizationSystem(private val events: EventSystem) {
1826
private val mapper = ObjectMapper()
1927

2028
private val guestRandom = kotlin.random.Random.Default
29+
private val guestAdjectives = this.javaClass.classLoader.getResource("lists/adjectives.txt")?.lines()
30+
private val guestAnimals = this.javaClass.classLoader.getResource("lists/animals.txt")?.lines()
31+
2132
private fun handleGuest(message: ClientJsonMessage) {
2233
val client = message.client
23-
val token: String = guestRandom.nextInt(100000).toString()
34+
val token: String = guestAdjectives?.random()
35+
?.plus(guestAnimals?.random()?:"")
36+
?.plus(guestRandom.nextInt(10, 99))
37+
?: guestRandom.nextInt(100000).toString()
2438
this.handleGuest(client, token, UUID.randomUUID())
2539
}
2640
fun handleGuest(client: Client, token: String, uuid: UUID) {

0 commit comments

Comments
 (0)