Application.scala 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package controllers
  2. import java.util.concurrent.ThreadLocalRandom
  3. import scala.concurrent.{ExecutionContext, Future}
  4. import play.api.libs.json.{JsObject, JsValue, Json}
  5. import play.api.mvc._
  6. import reactivemongo.api.{Cursor, ReadPreference}
  7. import reactivemongo.play.json.collection.JSONCollection
  8. import play.modules.reactivemongo.ReactiveMongoApi
  9. import reactivemongo.play.json._
  10. class Application (val controllerComponents: ControllerComponents, reactiveMongoApi: ReactiveMongoApi)(implicit ec: ExecutionContext)
  11. extends BaseController {
  12. private def worldCollection: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection[JSONCollection]("world"))
  13. private def fortuneCollection: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection[JSONCollection]("fortune"))
  14. private val projection = Json.obj("_id" -> 0)
  15. def getRandomWorlds(queries: Int): Future[Seq[Option[JsObject]]] = {
  16. val futureWorlds: Seq[Future[Option[JsObject]]] = for {
  17. _ <- 1 to queries
  18. } yield { worldCollection.map(_
  19. .find(Json.obj("_id" -> getNextRandom), Option(projection))
  20. .one[JsObject]).flatten
  21. }
  22. Future.sequence(futureWorlds)
  23. }
  24. def getRandomWorld = {
  25. worldCollection.map(_
  26. .find(Json.obj("id" -> getNextRandom), Option(projection))
  27. .one[JsValue]).flatten
  28. }
  29. def getFortunes: Future[List[JsObject]] = {
  30. fortuneCollection.map(_.find(Json.obj(), Option.empty[JsObject])
  31. .cursor[JsObject](ReadPreference.primaryPreferred, false).collect[List](Int.MaxValue, (v, t) => Cursor.Fail(t))).flatten
  32. }
  33. def updateWorlds(queries: Int): Future[Seq[JsObject]] = {
  34. getRandomWorlds(queries)
  35. .map(_.flatten)
  36. .map(_.map(oldWorld => {
  37. val newWorld = oldWorld ++ Json.obj("randomNumber" -> getNextRandom)
  38. worldCollection.map(_.update(oldWorld, newWorld).map(result => newWorld)).flatten
  39. }))
  40. .map(Future.sequence(_))
  41. .flatten
  42. }
  43. def getNextRandom: Int = {
  44. ThreadLocalRandom.current().nextInt(TestDatabaseRows) + 1
  45. }
  46. // Semi-Common code between Scala database code
  47. protected val TestDatabaseRows = 10000
  48. def db = Action.async {
  49. getRandomWorld.map { worlds =>
  50. Ok(Json.toJson(worlds.head))
  51. }
  52. }
  53. def queries(countString: String) = Action.async {
  54. val n = parseCount(countString)
  55. getRandomWorlds(n).map { worlds =>
  56. Ok(Json.toJson(worlds))
  57. }
  58. }
  59. private def byMessage(item: JsValue): String = {
  60. (item \ "message").as[String]
  61. }
  62. def fortunes() = Action.async {
  63. getFortunes.map { dbFortunes =>
  64. val appendedFortunes = Json.obj("_id" -> 0, "message" -> "Additional fortune added at request time.") :: dbFortunes
  65. val sorted = appendedFortunes.sortBy(byMessage(_))
  66. Ok(views.html.fortune(sorted))
  67. }
  68. }
  69. def update(queries: String) = Action.async {
  70. val n = parseCount(queries)
  71. updateWorlds(n).map { worlds =>
  72. Ok(Json.toJson(worlds))
  73. }
  74. }
  75. private def parseCount(s: String): Int = {
  76. try {
  77. val parsed = java.lang.Integer.parseInt(s, 10)
  78. parsed match {
  79. case i if i < 1 => 1
  80. case i if i > 500 => 500
  81. case i => i
  82. }
  83. } catch {
  84. case _: NumberFormatException => 1
  85. }
  86. }
  87. }