This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mud.go
159 lines (140 loc) · 3.71 KB
/
mud.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
/*
Lighthouse
Copyright (C) 2021 Nathanael Bracy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"net"
"strings"
"sync"
)
// MUD facilitates interaction with its underlying components.
type MUD interface {
// BroadcastAll sends a message to all Players.
BroadcastAll(msg []byte)
// ListenAndServe starts the MUD on the specified port.
ListenAndServe(port string) error
// Players returns a slice of online Players.
Players() []Player
// Process receives a Message for processing.
Process(msg Message)
// RemovePlayer removes a Player from the MUD's Player list.
RemovePlayer(Player)
}
// MUD implementation.
type iMUD struct {
sync.RWMutex
msgqueue chan Message
// Listener.
server net.Listener
// Player store.
players []Player
}
// NewMud creates a new MUD.
func NewMud() MUD {
return &iMUD{msgqueue: make(chan Message)}
}
// BroadcastAll sends a message to all Players.
func (m *iMUD) BroadcastAll(msg []byte) {
for _, p := range m.Players() {
p.Send(msg)
}
}
// Handle handles a command.
func (m *iMUD) Handle(msg Message) {
tokens := strings.Split(msg.Message(), " ")
if handler, ok := CommandMap[strings.ToLower(tokens[0])]; ok {
handler(tokens, msg.Player(), m)
} else {
msg.Player().Send(UNKNOWN_CMD_MSG)
}
}
// ListenAndServe starts a MUD up.
func (m *iMUD) ListenAndServe(port string) (err error) {
// Start the listener.
m.Lock()
m.server, err = net.Listen("tcp", port)
if err != nil {
m.Unlock()
return
}
m.Unlock()
// Accept connections.
go func() {
var conn net.Conn
for {
// Accept a new connection.
conn, err = m.server.Accept()
if err != nil { return }
// Add a new player to the player pool.
m.Lock()
m.players = append(m.players, NewPlayer(conn, m))
m.Unlock()
}
}()
// Process messages.
var msg Message
for {
msg = <- m.msgqueue
if msg.Error() != nil {
go m.RemovePlayer(msg.Player())
continue
}
// TODO(Nate): Delegate command handlers.
if msg.Player().Name() != "" {
go m.Handle(msg)
} else {
if name := msg.Message(); name != "" {
msg.Player().SetName(name)
msg.Player().Send([]byte("\n"))
go m.BroadcastAll([]byte(fmt.Sprintf("%s has entered the lighthouse.\n", name)))
} else {
msg.Player().Send([]byte("You must choose a valid name!\n\n"))
msg.Player().Send(WELCOME_PROMPT)
}
}
}
}
// Players returns a slice of players currently online.
func (m *iMUD) Players() []Player {
m.RLock()
defer m.RUnlock()
return m.players
}
// Process receives a Messsage for processing.
func (m *iMUD) Process(msg Message) {
go func() {
m.msgqueue <- msg
}()
}
// RemovePlayer removes a Player from the MUD's player list.
func (m *iMUD) RemovePlayer(p Player) {
// Shutdown the underlying connection.
p.Shutdown()
// Send a disconnect message.
if p.Name() != "" {
go m.BroadcastAll([]byte(fmt.Sprintf("%s has left the lighthouse.\n", p.Name())))
}
// Delete the Player from the player list.
players := m.Players()
for k, v := range players {
if v == p {
m.Lock()
// Remove the Player by plucking him out of the list.
m.players = append(m.players[:k], m.players[k+1:]...)
m.Unlock()
return
}
}
}