Example #1
0
func PrepareHandler(w http.ResponseWriter, r *http.Request) {
	playData := game.NewPlayData()

	r.ParseForm()
	if val, ok := r.Form["words[]"]; ok {
		for _, rawWord := range val {
			defaultWord := german.Default{}

			err := json.Unmarshal([]byte(rawWord), &defaultWord)
			if err != nil {
				log.Println(err)
				continue
			}

			playData.Question = defaultWord.GetEnglish()
			playData.DataStore = defaultWord
		}
	}

	outputBytes, err := json.Marshal(playData)
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write(outputBytes)
}
Example #2
0
func FinalHandler(w http.ResponseWriter, r *http.Request) {
	word := german.Default{}
	answer := ""

	r.ParseForm()
	if val, ok := r.Form["data"]; ok {
		json.Unmarshal([]byte(val[0]), &word)
	} else {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte{})
		return
	}

	if val, ok := r.Form["answer"]; ok {
		answer = val[0]
	} else {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte{})
		return
	}

	if answer == word.GetWord() {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("100"))
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte("0"))
}