-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
69 lines (60 loc) · 1.65 KB
/
player.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
package main
import (
"encoding/json"
"time"
"github.com/gorilla/websocket"
)
// Player stores state for a codecrack player and enables two-way communication via websocket
type Player struct {
Conn *websocket.Conn
Username string
Number int
Combi [5]Colour
Board *Board
ReadPump chan Packet[json.RawMessage]
WritePump chan any
}
// StartReadPump starts the read pump, which allows client websocket packets to be read via the channel
func (p *Player) StartReadPump() {
for {
var pack Packet[json.RawMessage]
// Read next client packet and parse it
if err := p.Conn.ReadJSON(&pack); err != nil {
return
}
p.ReadPump <- pack
}
}
// StartWritePump starts the write pump, which enables the server to send packets to the client and avoiding errors if two packets are sent at the same time
func (p *Player) StartWritePump() {
for {
pack, ok := <-p.WritePump
if !ok {
return
} else if err := p.Conn.WriteJSON(pack); err != nil {
return
}
}
}
// ReadDeadline waits the provided duration for the client to send a packet, and checks the opcode to see if it is the correct one
func ReadDeadline[T any](p *Player, opcode Opcode, deadline time.Duration) (T, bool) {
timeout := make(chan struct{})
var data T
// Send timeout message if the deadline is reached
go func() {
time.Sleep(deadline * time.Second)
timeout <- struct{}{}
}()
select {
case <-timeout:
// If no message was received, return false
return data, false
case pack := <-p.ReadPump:
// If message is received in time, parse it and return true
json.Unmarshal(pack.Data, &data)
if pack.Opcode != opcode {
return data, false
}
return data, true
}
}