-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (156 loc) · 3.85 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
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/gommon/log"
"golang.org/x/net/websocket"
"github.com/guregu/null"
)
func ConvertFizzBuzzMoi(number int) (answer string) {
if number%3 == 0 {
answer += "Fizz"
}
if number%5 == 0 {
answer += "Buzz"
}
if number%7 == 0 {
answer += "Moi"
}
if len(answer) == 0 {
answer = strconv.Itoa(number)
}
return
}
type FirstMessage struct {
Signal string `json:"signal"`
}
type QuestionMessage struct {
Number int `json:"number"`
Previous null.String `json:"previous"`
}
type ResultMessage struct {
Signal string `json:"signal"`
Result string `json:"success"`
Score int `json:"score"`
Message string `json:"message"`
}
type AnswerMessage struct {
Answer string `json:"answer"`
}
type CorrectType int
const (
NULL CorrectType = iota + 1
CORRECT
INCORRECT
)
var (
nullString = null.NewString("null", false)
successString = null.NewString("success", true)
failureString = null.NewString("failure", true)
)
func HandleWSConnForAnswer(c echo.Context) error {
logger := c.Logger()
if !c.IsWebSocket() {
logger.Info("it's NOT websocket connection.")
return nil
}
logger.Infof("ID: %s accessed.", c.ParamValues()[0])
websocket.Handler(func(ws *websocket.Conn) {
defer func() {
logger.Info("websocket connection stopped.")
ws.Close()
}()
// Send first message
var firstMessage FirstMessage
if err := websocket.JSON.Receive(ws, &firstMessage); err != nil {
logger.Fatal(err)
return
}
logger.Info("receive first message.")
// Process receive answer message
answerMsgChan := make(chan AnswerMessage)
go func() {
defer close(answerMsgChan)
var answerMessage AnswerMessage
for {
// Receive answer message
if err := websocket.JSON.Receive(ws, &answerMessage); err != nil {
break
}
logger.Info("receive answer message")
answerMsgChan <- answerMessage
}
}()
// Process send question message
questionMessage := QuestionMessage{}
preCorrect := NULL
correctNum := 0
questionNum := 0
quit := time.After(time.Second)
for cond := true; cond; {
// Build question message
questionMessage.Number = generateRand()
switch preCorrect {
case NULL:
questionMessage.Previous = nullString
case CORRECT:
questionMessage.Previous = successString
default:
questionMessage.Previous = failureString
}
// Send question message
if err := websocket.JSON.Send(ws, &questionMessage); err != nil {
logger.Error(err)
break
}
logger.Info("send question message")
// Receive answer message via channel until time out
select {
case <-quit:
logger.Info("time out.")
cond = false
case answerMessage := <-answerMsgChan:
questionNum++
if ConvertFizzBuzzMoi(questionMessage.Number) == answerMessage.Answer {
preCorrect = CORRECT
correctNum++
} else {
preCorrect = INCORRECT
}
}
}
// Send result message
resultMessage := ResultMessage{}
resultMessage.Signal = "end"
resultMessage.Score = correctNum
if correctNum >= 10 {
resultMessage.Result = "success"
resultMessage.Message = fmt.Sprintf("Challenge is successful! Your record is %d / %d 👏", correctNum, questionNum)
} else {
resultMessage.Result = "faild"
resultMessage.Message = fmt.Sprintf("faild. %d / %d", correctNum, questionNum)
}
websocket.JSON.Send(ws, &resultMessage)
logger.Info("send result message")
}).ServeHTTP(c.Response(), c.Request())
return nil
}
func generateRand() int {
return rand.Intn(100)
}
func main() {
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.Use(middleware.Logger())
e.Logger.SetLevel(log.ERROR)
e.Use(middleware.Recover())
e.GET("/websocket/:id", HandleWSConnForAnswer)
if err := e.Start(":12345"); err != nil {
e.Logger.Fatal(err)
}
}