Card.scala 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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).flatMap(compoundWords).map { word =>
  45. ("\\b"+word.toLowerCase+"\\b").r
  46. }
  47. def compoundWords(text: String): Seq[String] = {
  48. text.split(" ") :+ text.replace(" ", "")
  49. }
  50. def isTaboo(text: String) = {
  51. val lower = text.toLowerCase
  52. // check if text contains word or anything in taboo
  53. tabooRegex.map(!_.findFirstIn(lower).isEmpty).foldLeft(false)(_ || _)
  54. }
  55. def isCorrect(text: String) = {
  56. text.toLowerCase.indexOf(word.toLowerCase) >= 0
  57. }
  58. }
  59. object CardPool {
  60. import scala.util.Random
  61. def get() = DB.withConnection { implicit c =>
  62. val list = Card.list()
  63. CardPool(Random.shuffle(list))
  64. }
  65. }
  66. case class CardPool(list: List[Card]) {
  67. var index = 0
  68. def hasNext = list.size > index
  69. def next() = {
  70. val card = list(index)
  71. index += 1
  72. card
  73. }
  74. }