commit 45b71573b1d078e8d1ecf7bea8f7a23c564aa36c Author: Jonas Date: Tue Nov 17 07:27:46 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a12dd3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +game +*.swp diff --git a/game.go b/game.go new file mode 100644 index 0000000..7a2d986 --- /dev/null +++ b/game.go @@ -0,0 +1,85 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "encoding/json" + "log" + "net/http" + "math/rand" + "strconv" +) +type Phrase struct { + Phrase string `json:"phrase"` + Mode bool `json:"mode"` + Answer string `json:"answer"` +} +type Phrases struct { + Phrases []Phrase `json:"phrases"` +} +var phrases Phrases +func ParseJSON() { + jsonFile, err := os.Open("phrases.json") + if err != nil { + log.Fatal(err) + } + defer jsonFile.Close() + byteValue, _ := ioutil.ReadAll(jsonFile) + err = json.Unmarshal(byteValue, &phrases) + if err != nil { + log.Fatal(err) + } +} +func requestHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + i := rand.Intn(len(phrases.Phrases)) + fmt.Fprintf(w, ` + %s +
+ + +
+ + `, phrases.Phrases[i].Phrase, i) +} +func answerHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + i, err := strconv.Atoi(r.URL.Path[len("/answer/"):]) + if err != nil { + log.Fatal(err) + } + p := phrases.Phrases[i] + pageContent := "" + p.Phrase + "
tu respuesta estaba " + if r.FormValue("answer") == p.Answer { + pageContent += "correcta!. Buen hecho
=i { + j++ + } + pageContent+= "
frase nueva: " + phrases.Phrases[j].Phrase + `
+
+ + +
` + pageContent += "" + fmt.Fprintf(w,pageContent) +} +func main() { + ParseJSON() +/* for _, phrase := range phrases.Phrases { + fmt.Println(phrase.Phrase) + }*/ + http.HandleFunc("/", requestHandler) + http.HandleFunc("/answer/", answerHandler) + log.Fatal(http.ListenAndServe(":8080", nil)) +} diff --git a/phrases.json b/phrases.json new file mode 100644 index 0000000..295773a --- /dev/null +++ b/phrases.json @@ -0,0 +1,24 @@ +{ + "phrases": [ + { + "phrase": "Quiero que (saber, tú) toda la verdad", + "mode": true, + "answer": "sepas" + }, + { + "phrase": "Me gusta que (ir, vosotr@s) siempre al teatro", + "mode": true, + "answer": "vayáis" + }, + { + "phrase": "A la madre de Osvaldo le alegra que él (estudiar, él) todos los días", + "mode": true, + "answer": "estudie" + }, + { + "phrase": "ojalá (llover)", + "mode": true, + "answer": "llueva" + } + ] +}