feat(user-sessions): introduce GameLobby and GameUtil for session management and code generation
This commit is contained in:
29
knockoutwhistweb/app/util/GameUtil.scala
Normal file
29
knockoutwhistweb/app/util/GameUtil.scala
Normal 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()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user