-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
205 lines (173 loc) · 5.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
type ImageGame struct {
CurrentImageSource string
Score int
Lives int
LivesAsHearts string
ConsecutiveWrongAnswers int
Answer string
StatusOfAnswer string
TotalGuesses int
Accuracy int
}
var myCache CacheItf
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
InitCache() // comment if want to use redis cache
fs := http.FileServer(http.Dir("resources/Semblance Game_files"))
http.Handle("/Semblance Game_files/", http.StripPrefix("/Semblance Game_files/", fs))
gameTemplate := template.Must(template.ParseFiles("resources/Semblance Game.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
resp, _ := http.Get("https://random.imagecdn.app/1024/600")
url := resp.Request.URL.String()
data :=
ImageGame{
StatusOfAnswer: "nonAnswer",
CurrentImageSource: url,
Score: 0,
Lives: 5,
LivesAsHearts: strings.Repeat("❤️", 5),
ConsecutiveWrongAnswers: 0,
TotalGuesses: 0,
}
gameTemplate.Execute(w, data)
} else if r.Method == "POST" {
r.ParseForm()
var totalGuesses int
if _, err := fmt.Sscanf(r.FormValue("totalGuesses"), "%d", &totalGuesses); err == nil {
fmt.Println(totalGuesses)
}
var score int
if _, err := fmt.Sscanf(r.FormValue("score"), "%d", &score); err == nil {
fmt.Println(score)
}
var consecutiveWrongAnswers int
if _, err := fmt.Sscanf(r.FormValue("consecutiveWrongAnswers"), "%d", &consecutiveWrongAnswers); err == nil {
fmt.Println(consecutiveWrongAnswers)
}
var lives int
if _, err := fmt.Sscanf(r.FormValue("lives"), "%d", &lives); err == nil {
fmt.Println(lives)
}
url := r.FormValue("currentImageSource")
statusOfAnswer := r.FormValue("statusOfAnswer")
guess := r.FormValue("guess")
var data ImageGame
if statusOfAnswer == "correctResponse" || statusOfAnswer == "incorrectResponsesExhausted" {
resp, _ := http.Get("https://random.imagecdn.app/1024/600")
newurl := resp.Request.URL.String()
data =
ImageGame{
CurrentImageSource: newurl,
Score: score,
StatusOfAnswer: "nonAnswer",
Lives: lives,
LivesAsHearts: strings.Repeat("❤️", lives),
ConsecutiveWrongAnswers: 0,
TotalGuesses: totalGuesses + 1,
}
} else {
tagsFromCache, err := myCache.Get(url)
var currentTags map[string]int
json.Unmarshal(tagsFromCache, ¤tTags)
if err != nil {
// error
log.Fatal(err)
}
if currentTags == nil {
currentTags = TagRemoteImage(CreateComputerVisionClient(), url)
myCache.Set(url, currentTags, -1)
}
guess = strings.ToLower(guess)
acceptableAnswers := getWordForms(guess)
answerFound := false
for _, acceptableAnswer := range acceptableAnswers {
if currentTags[acceptableAnswer] > 0 {
answerFound = true
break
}
}
if !answerFound {
for currentTag := range currentTags {
for _, acceptableAnswer := range acceptableAnswers {
if checkEitherIsSubString(currentTag, acceptableAnswer) {
answerFound = true
break
}
}
}
}
if answerFound {
data =
ImageGame{
CurrentImageSource: url,
Score: score + 1,
StatusOfAnswer: "correct",
Lives: lives,
LivesAsHearts: strings.Repeat("❤️", lives),
ConsecutiveWrongAnswers: 0,
TotalGuesses: totalGuesses + 1,
}
}
if !answerFound {
var answer string
if consecutiveWrongAnswers == 2 {
lives--
//Get answer
//get the first key, since that has the highest confidence
for key, pos := range currentTags {
if pos == 1 {
answer = key
break
}
}
}
if lives == 0 {
data =
ImageGame{
Score: score,
Lives: lives,
LivesAsHearts: strings.Repeat("❤️", lives),
TotalGuesses: totalGuesses,
Accuracy: int(float64(score) / float64(totalGuesses) * 100),
}
scoreTemplate := template.Must(template.ParseFiles("resources/Score.html"))
scoreTemplate.Execute(w, data)
return
}
data =
ImageGame{
CurrentImageSource: url,
Score: score,
StatusOfAnswer: "incorrectResponse",
Lives: lives,
LivesAsHearts: strings.Repeat("❤️", lives),
ConsecutiveWrongAnswers: consecutiveWrongAnswers + 1,
TotalGuesses: totalGuesses + 1,
Answer: answer,
}
}
}
gameTemplate.Execute(w, data)
}
})
http.ListenAndServe(":"+port, nil)
}