lila/modules/clas/src/main/NameGenerator.scala

19 lines
553 B
Scala
Raw Normal View History

2020-01-17 20:45:05 -07:00
package lila.clas
2020-01-18 08:40:03 -07:00
import scala.concurrent.ExecutionContext
2020-01-17 20:45:05 -07:00
2021-03-05 01:55:21 -07:00
import lila.common.CuteNameGenerator
2020-01-17 20:45:05 -07:00
2021-03-06 04:58:13 -07:00
final class NameGenerator(userRepo: lila.user.UserRepo)(implicit ec: ExecutionContext) {
2021-03-05 01:55:21 -07:00
def apply(maxSize: Int = 17, triesLeft: Int = 20): Fu[Option[String]] = {
CuteNameGenerator.make(maxSize) ?? { name =>
userRepo.nameExists(name) flatMap {
case true if triesLeft > 0 => apply(maxSize, triesLeft - 1)
case true => fuccess(none)
case _ => fuccess(name.some)
}
2020-01-18 08:40:03 -07:00
}
2020-01-17 20:45:05 -07:00
}
}