Card.scala 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package models
  2. import anorm._
  3. import anorm.SqlParser._
  4. import play.api.db._
  5. import play.api.libs.json._
  6. import play.api.Play.current
  7. object Card {
  8. implicit val writes = new Writes[Card] {
  9. def writes(o: Card) = Json.obj(
  10. "word" -> o.word,
  11. "taboo" -> o.taboo
  12. )
  13. }
  14. def size = DB.withConnection { implicit c =>
  15. SQL("select count(*) from words").single(long("count"))
  16. }
  17. def add(card: Card) = DB.withTransaction { implicit c =>
  18. val id = SQL("insert into words values (default, {word})")
  19. .on('word -> card.word)
  20. .executeInsert()
  21. id.map { id =>
  22. card.taboo.map { word =>
  23. SQL("insert into taboo values (default, {id}, {word})")
  24. .on('id -> id, 'word -> word)
  25. .executeInsert()
  26. }
  27. }
  28. }
  29. def list() = DB.withConnection { implicit c =>
  30. val list = SQL("""
  31. select words.id as id, words.word as word, taboo.word as taboo
  32. from words left join taboo on word_id = words.id
  33. """)
  34. .list(int("id") ~ str("word") ~ str("taboo") map flatten)
  35. mapToCard(list)
  36. }
  37. def mapToCard(seq: Seq[(Int, String, String)]) = {
  38. seq.groupBy(_._1).map {
  39. case (id, taboos) => Card(taboos.map(_._2).head, taboos.map(_._3).toSet)
  40. }.toList
  41. }
  42. }
  43. case class Card(word: String, taboo: Set[String]) {
  44. lazy val tabooRegex = (taboo + word).map { word =>
  45. ("\\b"+word.toLowerCase+"\\b").r
  46. }
  47. def isTaboo(text: String) = {
  48. val lower = text.toLowerCase
  49. // check if text contains word or anything in taboo
  50. tabooRegex.map(!_.findFirstIn(lower).isEmpty).foldLeft(false)(_ || _)
  51. }
  52. def isCorrect(text: String) = {
  53. text.toLowerCase.indexOf(word.toLowerCase) >= 0
  54. }
  55. }
  56. object CardPool {
  57. import scala.util.Random
  58. def get() = DB.withConnection { implicit c =>
  59. val list = Card.list()
  60. CardPool(Random.shuffle(list))
  61. }
  62. }
  63. case class CardPool(list: List[Card]) {
  64. var index = 0
  65. def hasNext = list.size > index
  66. def next() = {
  67. val card = list(index)
  68. index += 1
  69. card
  70. }
  71. }