You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
2.6 KiB

7 years ago
5 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. "strconv"
  8. "github.com/gorilla/sessions"
  9. )
  10. type prob struct {
  11. Operation string
  12. First int
  13. Second int
  14. Score int
  15. }
  16. type solution struct {
  17. Status bool
  18. Score int
  19. }
  20. func getScore(session *sessions.Session) int {
  21. score := session.Values["Score"]
  22. var parsed_score int
  23. if score == nil {
  24. parsed_score = 0
  25. } else {
  26. parsed_score = score.(int)
  27. }
  28. return parsed_score
  29. }
  30. func addsub(w http.ResponseWriter, req *http.Request) {
  31. w.Header().Set("Content-Type", "application/json")
  32. operation := "+"
  33. if r := rand.Intn(2); r == 0 {
  34. operation = "-"
  35. }
  36. first := rand.Intn(MAX)
  37. var second int
  38. if operation == "-" {
  39. if first == 0 {
  40. second = 0
  41. } else {
  42. second = rand.Intn(first)
  43. }
  44. } else {
  45. second = rand.Intn(MAX)
  46. }
  47. session, _ := store.Get(req, "Score")
  48. score := getScore(session)
  49. r := prob{operation, first, second, score}
  50. err := json.NewEncoder(w).Encode(r)
  51. if err != nil {
  52. log.Fatal("issue with json marshalling", err)
  53. }
  54. }
  55. func mul(w http.ResponseWriter, req *http.Request) {
  56. w.Header().Set("Content-Type", "application/json")
  57. session, _ := store.Get(req, "Score")
  58. score := getScore(session)
  59. err := json.NewEncoder(w).Encode(
  60. prob{
  61. "x",
  62. rand.Intn(11),
  63. rand.Intn(11),
  64. score,
  65. },
  66. )
  67. if err != nil {
  68. log.Fatal("issue with json marshalling", err)
  69. }
  70. }
  71. func attempt(w http.ResponseWriter, req *http.Request) {
  72. w.Header().Set("Content-Type", "application/json")
  73. first, err := strconv.Atoi(req.FormValue("first"))
  74. if err != nil {
  75. log.Fatal("cannot parse first", err)
  76. }
  77. operation := req.FormValue("operation")
  78. second, err := strconv.Atoi(req.FormValue("second"))
  79. if err != nil {
  80. log.Fatal("cannot parse second", err)
  81. }
  82. var guess int
  83. answer := req.FormValue("answer")
  84. if answer == "" {
  85. guess = 0
  86. } else {
  87. guess, err = strconv.Atoi(answer)
  88. if err != nil {
  89. log.Fatal("cannot parser answer", err)
  90. }
  91. }
  92. var result bool
  93. switch operation {
  94. case "+":
  95. result = first+second == guess
  96. case "-":
  97. result = first-second == guess
  98. case "*", "x":
  99. result = first*second == guess
  100. }
  101. session, _ := store.Get(req, "Score")
  102. score := getScore(session)
  103. if result {
  104. score += 1
  105. } else {
  106. score -= 1
  107. }
  108. session.Values["Score"] = score
  109. session.Save(req, w)
  110. err = json.NewEncoder(w).Encode(
  111. solution{
  112. result,
  113. score,
  114. },
  115. )
  116. if err != nil {
  117. log.Fatal("cannot marshal solution", err)
  118. }
  119. }
  120. func reset(w http.ResponseWriter, req *http.Request) {
  121. session, _ := store.Get(req, "Score")
  122. session.Values["Score"] = 0
  123. session.Save(req, w)
  124. http.Redirect(w, req, "/", http.StatusFound)
  125. }