Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package yobu.christophpickl.github.com.yobu.logic
  2.  
  3. import org.json.JSONArray
  4. import org.json.JSONObject
  5. import yobu.christophpickl.github.com.yobu.*
  6. import yobu.christophpickl.github.com.yobu.common.readString
  7. import java.io.InputStream
  8.  
  9.  
  10. class QuestionsLoader {
  11.  
  12. private val boGenerator = BoPunctGenerator()
  13.  
  14.  
  15. // * wer liegt nicht auf KG? => Lu, Di, Mi, Ni, Gb, Le
  16. // * wer liegt zwischen brustwarzen? => Pk (KG17)
  17. // * wer liegt (als einziger) (2C) seitlich vom nabel? => Di (Ma25)
  18. fun load(): List<Question> {
  19. return listOf(
  20. Question( // wer auf der 11./12. rippe? => Mi (Le13), Ni (Gb25)
  21. id = "BoRippen",
  22. text = "Welche zwei Bo Punkte liegen auf den Rippenenden?",
  23. answers = listOf(
  24. Answer("MP, Ni", isRight = true),
  25. Answer("MP, KG"),
  26. Answer("Gb, Le"),
  27. Answer("Ni, Bl")
  28. )
  29. ),
  30. Question( // wer in 6./7. ICR? => Le (Le14, 6.), Gb (Gb24, 7.)
  31. id = "BoICR",
  32. text = "Welche zwei Bo Punkte liegen in den ICRs?",
  33. answers = listOf(
  34. Answer("Le, Gb", isRight = true),
  35. Answer("MP, Ni"),
  36. Answer("Di, Le"),
  37. Answer("Mi, Gb")
  38. )
  39. ),
  40. Question( // wer liegt auf seinem eigenen meridian? => Lu(1), Gb(24), Le(14)
  41. id = "BoOwnMerid",
  42. text = "Welcher Bo Punkt liegt auf seinem eigenen Meridian?",
  43. answers = listOf(
  44. // or maybe reduce to one single right answer containing "Lu, Gb, Le"?
  45. Answer("Lu", isRight = true),
  46. Answer("Gb", isRight = true),
  47. Answer("Le", isRight = true),
  48. Answer("MP"),
  49. Answer("Ma"),
  50. Answer("Bl"),
  51. Answer("Ni")
  52. )
  53. )
  54. )
  55. // return JsonQuestionReader().read(questionsStream).questions.map { question ->
  56. // Question(question.id, question.text, transformAnswers(question))
  57. // }
  58. // .plus(generateDefaultQuestions())
  59. // return generateDefaultQuestions()
  60. }
  61.  
  62.  
  63. private fun transformAnswers(question: JsonQuestion): List<Answer> {
  64. // if (question.flags.contains(QuestionFlag.RANDOM_BO_PUNCT)) {
  65. // if (question.answers.size != 1) {
  66. // throw RuntimeException("Expected only 1 answer if RANDOM_BO_PUNCT is set, but was: ${question.answers.size}!")
  67. // }
  68. // val answer = question.answers[0]
  69. // val list = mutableListOf(Answer(answer.text, isRight = true))
  70. // // TODO this has to be done on level higher up, each time we display the question
  71. // list.addAll(boGenerator.generateRandomAnswers(PunctCoordinate.parse(answer.text)))
  72. // return list
  73. // }
  74. return question.answers.mapIndexed { i, answer ->
  75. Answer(answer.text, isRight = i == 0)
  76. }
  77. }
  78. }
  79.  
  80. enum class QuestionFlag {
  81. // RANDOM_BO_PUNCT
  82. }
  83.  
  84. data class JsonAnswer(val text: String)
  85. data class JsonQuestion(
  86. val id: String,
  87. val text: String,
  88. val flags: List<QuestionFlag>,
  89. val answers: List<JsonAnswer>)
  90.  
  91. data class JsonCatalog(val questions: List<JsonQuestion>)
  92.  
  93. class JsonQuestionReader {
  94.  
  95. companion object {
  96. private val LOG = yobu.christophpickl.github.com.yobu.common.LOG(JsonQuestionReader::class.java)
  97. }
  98.  
  99. // val questionsInputStream = resources.openRawResource(R.raw.questions_catalog)
  100. // val rawJson = Resources.getSystem().getString(R.raw.questions_catalog)
  101. fun read(questionsStream: InputStream): JsonCatalog {
  102. val rawJson = questionsStream.readString()
  103. val json = JSONObject(rawJson)
  104. LOG.d { "Read JSON: $json" }
  105.  
  106. return JsonCatalog(json.getJSONArray("questions").map {
  107. toJsonQuestion(it)
  108. })
  109. }
  110.  
  111. private fun toJsonQuestion(json: JSONObject): JsonQuestion {
  112. return JsonQuestion(
  113. json.getString("id"),
  114. json.getString("text"),
  115. json.optJSONArray("flags")?.let { toFlags(it) } ?: emptyList(),
  116. json.getJSONArray("answers").map { toJsonAnswer(it) }
  117. )
  118. }
  119.  
  120. private fun toFlags(json: JSONArray): List<QuestionFlag> {
  121. return json.mapStrings { QuestionFlag.valueOf(it) }
  122. }
  123.  
  124. private fun toJsonAnswer(json: JSONObject): JsonAnswer {
  125. return JsonAnswer(json.getString("text"))
  126. }
  127. }
  128.  
  129. fun <T> JSONArray.mapStrings(transform: (String) -> T): List<T> {
  130. return 1.rangeTo(this.length()).map {
  131. transform(this.getString(it - 1))
  132. }
  133. }
  134.  
  135. fun <T> JSONArray.map(transform: (JSONObject) -> T): List<T> {
  136. return 1.rangeTo(this.length()).map {
  137. transform(getJSONObject(it - 1))
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement