feat(user-sessions): introduce GameLobby and GameUtil for session management and code generation

This commit is contained in:
2025-10-29 19:05:55 +01:00
parent 465004b9ce
commit f4f886727f
9 changed files with 60 additions and 58 deletions

View File

@@ -0,0 +1,29 @@
package util
import scala.util.Random
object GameUtil {
private val CharPool: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
private val CodeLength: Int = 6
private val MaxRepetition: Int = 2
private val random = new Random()
def generateCode(): String = {
val freq = Array.fill(CharPool.length)(0)
val code = new StringBuilder(CodeLength)
for (_ <- 0 until CodeLength) {
var index = random.nextInt(CharPool.length)
// Pick a new character if it's already used twice
while (freq(index) >= MaxRepetition) {
index = random.nextInt(CharPool.length)
}
freq(index) += 1
code.append(CharPool.charAt(index))
}
code.toString()
}
}