-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhai.go
167 lines (158 loc) · 5.63 KB
/
hai.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
package main
import (
"fmt"
"net/http"
)
/*Each letter is 5 units high and 3 units wide.
BubbleLetter stores 5 numbers between 0 and 7,
that, when converted to binary, represent the
existence or absence of a face in each unit.
example "e","E":
7: 1 1 1
4: 1 0 0
7: 1 1 1
4: 1 0 0
7: 1 1 1
*/
type BubbleLetter struct {
First int
Second int
Third int
Fourth int
Fifth int
}
func bubbleLetterField(letter string) BubbleLetter {
fmt.Println("LETTER:", letter)
switch letter {
case "a", "A":
return BubbleLetter{First: 2, Second: 5, Third: 7, Fourth: 5, Fifth: 5}
case "b", "B":
return BubbleLetter{First: 7, Second: 5, Third: 6, Fourth: 5, Fifth: 7}
case "c", "C":
return BubbleLetter{First: 7, Second: 4, Third: 4, Fourth: 4, Fifth: 7}
case "d", "D":
return BubbleLetter{First: 6, Second: 5, Third: 5, Fourth: 5, Fifth: 6}
case "e", "E":
return BubbleLetter{First: 7, Second: 4, Third: 7, Fourth: 4, Fifth: 7}
case "f", "F":
return BubbleLetter{First: 7, Second: 4, Third: 7, Fourth: 4, Fifth: 4}
case "g", "G":
return BubbleLetter{First: 7, Second: 4, Third: 5, Fourth: 5, Fifth: 7}
case "h", "H":
return BubbleLetter{First: 5, Second: 5, Third: 7, Fourth: 5, Fifth: 5}
case "i", "I":
return BubbleLetter{First: 7, Second: 2, Third: 2, Fourth: 2, Fifth: 7}
case "j", "J":
return BubbleLetter{First: 1, Second: 1, Third: 1, Fourth: 5, Fifth: 7}
case "k", "K":
return BubbleLetter{First: 5, Second: 6, Third: 4, Fourth: 6, Fifth: 5}
case "l", "L":
return BubbleLetter{First: 4, Second: 4, Third: 4, Fourth: 4, Fifth: 7}
case "m", "M":
return BubbleLetter{First: 5, Second: 7, Third: 5, Fourth: 5, Fifth: 5}
case "n", "N":
return BubbleLetter{First: 5, Second: 7, Third: 7, Fourth: 7, Fifth: 5}
case "o", "O":
return BubbleLetter{First: 7, Second: 5, Third: 5, Fourth: 5, Fifth: 7}
case "p", "P":
return BubbleLetter{First: 7, Second: 5, Third: 7, Fourth: 4, Fifth: 4}
case "q", "Q":
return BubbleLetter{First: 7, Second: 5, Third: 5, Fourth: 7, Fifth: 1}
case "r", "R":
return BubbleLetter{First: 7, Second: 5, Third: 6, Fourth: 5, Fifth: 5}
case "s", "S":
return BubbleLetter{First: 7, Second: 4, Third: 7, Fourth: 1, Fifth: 7}
case "t", "T":
return BubbleLetter{First: 7, Second: 2, Third: 2, Fourth: 2, Fifth: 2}
case "u", "U":
return BubbleLetter{First: 5, Second: 5, Third: 5, Fourth: 5, Fifth: 7}
case "v", "V":
return BubbleLetter{First: 5, Second: 5, Third: 5, Fourth: 5, Fifth: 2}
case "w", "W":
return BubbleLetter{First: 5, Second: 5, Third: 5, Fourth: 7, Fifth: 5}
case "x", "X":
return BubbleLetter{First: 5, Second: 5, Third: 2, Fourth: 5, Fifth: 5}
case "y", "Y":
return BubbleLetter{First: 5, Second: 5, Third: 2, Fourth: 2, Fifth: 2}
case "z", "Z":
return BubbleLetter{First: 7, Second: 1, Third: 2, Fourth: 4, Fifth: 7}
case " ":
return BubbleLetter{First: 0, Second: 0, Third: 0, Fourth: 0, Fifth: 0}
default:
return BubbleLetter{First: 5, Second: 0, Third: 2, Fourth: 0, Fifth: 7}
}
}
func bubbleWordField(word string) []BubbleLetter {
wordField := make([]BubbleLetter, len(word))
for i, _ := range wordField {
wordField[i] = bubbleLetterField(string(word[i]))
}
return wordField
}
func fieldToString(i int, emoji string) string {
switch i {
case 0:
return " "
case 1:
return " :" + emoji + ":"
case 2:
return " :" + emoji + ": "
case 3:
return " :" + emoji + "::" + emoji + ":"
case 4:
return ":" + emoji + ": "
case 5:
return ":" + emoji + ": :" + emoji + ":"
case 6:
return ":" + emoji + "::" + emoji + ": "
case 7:
return ":" + emoji + "::" + emoji + "::" + emoji + ":"
default:
return "Somehow, the integer passed to fieldToString was not 0-7..."
}
}
func wordFieldToString(wordField []BubbleLetter, emoji string) string {
var firstString []byte
var secondString []byte
var thirdString []byte
var fourthString []byte
var fifthString []byte
for i, _ := range wordField {
firstString = append(firstString, fieldToString(wordField[i].First, emoji)...)
secondString = append(secondString, fieldToString(wordField[i].Second, emoji)...)
thirdString = append(thirdString, fieldToString(wordField[i].Third, emoji)...)
fourthString = append(fourthString, fieldToString(wordField[i].Fourth, emoji)...)
fifthString = append(fifthString, fieldToString(wordField[i].Fifth, emoji)...)
//add 4 spaces after each letter for... spacing
firstString = append(firstString, " "...)
secondString = append(secondString, " "...)
thirdString = append(thirdString, " "...)
fourthString = append(fourthString, " "...)
fifthString = append(fifthString, " "...)
}
//add newlines to each finished string
firstString = append(firstString, "\n"...)
secondString = append(secondString, "\n"...)
thirdString = append(thirdString, "\n"...)
fourthString = append(fourthString, "\n"...)
//append the 5 string into a single multi-line string
responseString := append(firstString, secondString...)
responseString = append(responseString, thirdString...)
responseString = append(responseString, fourthString...)
responseString = append(responseString, fifthString...)
return string(responseString)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.FormValue("text"))
fmt.Println(r.FormValue("emoji"))
if r.FormValue("emoji") == "" {
fmt.Fprintf(w, "%s", wordFieldToString(bubbleWordField(r.FormValue("text")), "simple_smile"))
} else {
fmt.Fprintf(w, "%s", wordFieldToString(bubbleWordField(r.FormValue("text")), r.FormValue("emoji")))
}
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("serving on localhost:8080")
http.ListenAndServe(":8080", nil)
}