-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
65 lines (55 loc) · 1.1 KB
/
client.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
package main
import (
"fmt"
"math"
"github.com/gorilla/websocket"
)
type client struct {
conn *websocket.Conn
nick string
room *room
data chan []byte
progress int
}
func (c *client) readMessages() {
for {
msg := Message{}
err := c.conn.ReadJSON(&msg)
fmt.Printf("\n%+v", msg)
if err != nil {
c.room.leave <- c
c.conn.Close()
break
}
switch msg.Event {
case PROGRESS_ADVANCE:
floatProgress := (float64(len(msg.Data["doneText"].(string))) / float64(len(c.room.text))) * 100
intProgress := math.Floor(floatProgress)
//CHECK IF PROGRESS == 100 ( WIN )
if intProgress == 100 {
c.winGame()
break
}
msg.Data = map[string]interface{}{
"progress": intProgress,
"player": c.nick,
}
c.room.forward <- msg
}
}
}
func (c *client) sendMsg(msg Message) {
if err := c.conn.WriteJSON(msg); err != nil {
fmt.Printf("unable to write message, err: %s", err.Error())
c.conn.Close()
}
}
func (c *client) winGame() {
msg := Message{
Event: VICTORY,
Data: map[string]interface{}{
"player": c.nick,
},
}
c.room.forward <- msg
}