-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
230 lines (194 loc) · 5.2 KB
/
main.go
File metadata and controls
230 lines (194 loc) · 5.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bufio"
"errors"
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
"example.com/mud/config"
"example.com/mud/lua_runtime"
"example.com/mud/parser/commands"
"example.com/mud/world"
"example.com/mud/world/entities"
"example.com/mud/world/player"
lua "github.com/yuin/gopher-lua"
)
func handleConnection(conn net.Conn, gameWorld *world.World, cfg *config.Config) {
defer conn.Close()
reader := bufio.NewReader(conn)
var name string
for {
if _, err := fmt.Fprint(conn, "What is your name, weary adventurer? "); err != nil {
return
}
name, _ = reader.ReadString('\n')
name = strings.TrimSpace(name)
vdn := player.NameValidation(name)
if vdn != "" {
fmt.Fprint(conn, vdn)
continue
}
break
}
inbox := make(chan string, 64)
player, err := gameWorld.AddPlayer(name, inbox)
if err != nil {
err := fmt.Errorf("error adding player: %w", err)
fmt.Println(err.Error())
fmt.Fprintln(conn, err.Error())
return
}
// start consuming incoming messages
go handleConnectionIncoming(conn, inbox)
// run init function for player entity
player.Init()
// notify when outgoing messages end
done := make(chan struct{})
go func() {
handleConnectionOutgoing(conn, gameWorld, player, cfg)
close(done)
}()
// Wait until the outgoing loop ends
<-done
}
func handleConnectionIncoming(conn net.Conn, inbox chan string) {
go func() {
for msg := range inbox {
// Use CRLF for telnet clients
fmt.Fprint(conn, msg+"\r\n")
}
}()
}
func handleConnectionOutgoing(conn net.Conn, gameWorld *world.World, player *player.Player, cfg *config.Config) {
scanner := bufio.NewScanner(conn)
for {
if !scanner.Scan() {
break
}
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if strings.ToLower(line) == "quit" {
break
}
// check if player has pending multi-part messages
if p := player.Pending; p != nil {
n, nerr := strconv.Atoi(line)
if nerr == nil {
slot := p.Ambiguity.Slots[p.StepIndex]
if n >= 1 && n <= len(slot.Matches) {
p.Selected[slot.Role] = n - 1
p.StepIndex++
if p.StepIndex < len(p.Ambiguity.Slots) {
// continue prompting current slot
promptCurrentSlot(conn, p)
} else {
chosen := make(map[string]*entities.Entity, len(p.Selected))
for _, s := range p.Ambiguity.Slots {
idx := p.Selected[s.Role]
chosen[s.Role] = s.Matches[idx].Entity
}
out, execErr := p.Ambiguity.Execute(chosen)
player.Pending = nil
if execErr != nil {
fmt.Fprintln(conn, execErr.Error())
} else if out != "" {
fmt.Fprintln(conn, out)
}
}
}
continue
}
// non-number input falls through and removes pending action
player.Pending = nil
}
if coolDownTime := player.CooldownRemaining(); coolDownTime > 0 {
fmt.Fprintf(conn, "You need to catch your breath. Try again in %.1fs\r\n", coolDownTime.Seconds())
continue
}
message, err := gameWorld.Parse(player, line)
if err != nil {
var amb *entities.AmbiguityError
if errors.As(err, &amb) {
player.Pending = &entities.PendingAction{
Ambiguity: amb,
StepIndex: 0,
Selected: map[string]int{},
}
promptCurrentSlot(conn, player.Pending)
continue
}
err := fmt.Errorf("error received: %w", err)
fmt.Println(err.Error())
fmt.Fprintln(conn, err.Error())
} else if message != "" {
fmt.Fprintln(conn, message)
}
player.StartCooldown(time.Duration(cfg.PlayerRateLimit) * time.Millisecond)
}
if err := scanner.Err(); err != nil {
fmt.Println("Connection error:", err)
}
gameWorld.DisconnectPlayer(player)
fmt.Printf("Connection closed\n")
}
func promptCurrentSlot(conn net.Conn, p *entities.PendingAction) {
slot := p.Ambiguity.Slots[p.StepIndex]
fmt.Fprintln(conn, slot.Prompt)
for i, opt := range slot.Matches {
fmt.Fprintf(conn, " %d) %s\r\n", i+1, opt.Text)
}
}
func main() {
// load configuration file
cfg, err := config.Load("config.yaml")
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
// lua
lr := lua_runtime.NewLuaRuntime()
defer lr.Close()
// enables require/package
lua.OpenPackage(lr.L)
// add ./data to Lua's search path
if err := lr.L.DoString(`package.path = package.path .. ';./data/build/?.lua;./data/?/init.lua'`); err != nil {
panic(err)
}
entityMap, cmds, err := lr.LoadFile("data/build/src/index.lua")
if err != nil {
panic(err)
}
// validate starting room exists in entity map
if _, ok := entityMap[cfg.StartingRoom]; !ok {
log.Fatalf("room '%s' does not exist in world.", cfg.StartingRoom)
}
if err := commands.RegisterBuiltInCommands(); err != nil {
log.Fatalf("failed to register built-in commands: %v", err)
}
if err := commands.RegisterCommands(cmds); err != nil {
log.Fatalf("failed to register DSL commands: %v", err)
}
gameWorld := world.NewWorld(entityMap, cfg.StartingRoom)
err = gameWorld.Init()
if err != nil {
panic(err)
}
listener, err := net.Listen("tcp", ":4000")
if err != nil {
panic(err)
}
defer listener.Close()
fmt.Println("MUD server listening on port 4000...")
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
go handleConnection(conn, gameWorld, cfg)
}
}