-
Notifications
You must be signed in to change notification settings - Fork 4
/
message.go
121 lines (105 loc) · 2.8 KB
/
message.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
package main
import (
"encoding/json"
"strings"
"github.com/abdelq/lode-runner/game"
msg "github.com/abdelq/lode-runner/message"
)
type message msg.Message
// TODO Move sections to game package
func (m *message) parse(sender *client) {
switch evt := strings.ToLower(strings.TrimSpace(m.Event)); evt {
case "join":
parseJoin(m.Data, sender)
case "move":
parseMove(m.Data, sender)
case "dig":
parseDig(m.Data, sender)
case "list":
names := make([]string, 0, 16) // XXX
rooms.Range(func(name, room interface{}) bool {
names = append(names, name.(string))
return true
})
roomNames, _ := json.Marshal(names)
sender.out <- &msg.Message{"list", roomNames}
case "kill":
var roomName string
if err := json.Unmarshal(m.Data, &roomName); err != nil {
return
}
if r, ok := rooms.Load(roomName); ok {
if r, ok := r.(*room); ok {
r.game.Kill()
}
}
default:
sender.out <- msg.NewMessage("error", "invalid event")
}
}
func parseJoin(data json.RawMessage, sender *client) {
message := new(msg.JoinMessage)
if err := message.Parse(data); err != nil {
sender.out <- msg.NewMessage("error", err.Error())
return
}
// Find/Create room
r, _ := rooms.LoadOrStore(message.Room, newRoom(message.Room))
r.(*room).join <- &join{sender, game.NewPlayer(message, sender.out)}
}
// TODO Move to game package
func parseMove(data json.RawMessage, sender *client) {
message := new(msg.GameMessage)
if err := message.Parse(data); err != nil {
sender.out <- msg.NewMessage("error", err.Error())
return
}
// Find room name if none sent
if message.Room == "" {
if message.Room = findRoom(sender); message.Room == "" {
sender.out <- msg.NewMessage("error", "not in a room")
return
}
}
if r, ok := rooms.Load(message.Room); ok {
if r, ok := r.(*room); ok {
if !r.game.Started() {
sender.out <- msg.NewMessage("error", "game not started")
return
}
if player := r.clients[sender]; player != nil {
player.Move(message.Direction)
} else {
sender.out <- msg.NewMessage("error", "not a player")
}
}
}
}
// TODO Move to game package
func parseDig(data json.RawMessage, sender *client) {
message := new(msg.GameMessage)
if err := message.Parse(data); err != nil {
sender.out <- msg.NewMessage("error", err.Error())
return
}
// Find room name if none sent
if message.Room == "" {
if message.Room = findRoom(sender); message.Room == "" {
sender.out <- msg.NewMessage("error", "not in a room")
return
}
}
if r, ok := rooms.Load(message.Room); ok {
if r, ok := r.(*room); ok {
if !r.game.Started() {
sender.out <- msg.NewMessage("error", "game not started")
return
}
if runner, ok := r.clients[sender].(*game.Runner); ok {
runner.Dig(message.Direction)
} else {
sender.out <- msg.NewMessage("error", "not a runner")
}
}
}
}