-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
174 lines (140 loc) · 3.91 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"github.com/woodworker/go-mud/game"
)
func main() {
workingdir, _ := os.Getwd()
log.Printf("Leveldir %s", workingdir+"/static/levels/")
server := game.NewServer(workingdir)
err := server.LoadLevels()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !server.HasDefaultLevel() {
log.Println("no default level set")
log.Println("please tag one of the levels as default")
os.Exit(1)
}
ln, err := net.Listen("tcp", server.Config.Interface)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
log.Printf("Listen on: %s", ln.Addr())
msgchan := make(chan string)
addchan := make(chan game.Client)
rmchan := make(chan game.Client)
go handleMessages(msgchan, addchan, rmchan)
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleConnection(conn, msgchan, addchan, rmchan, server)
}
}
func promptMessage(c net.Conn, bufc *bufio.Reader, message string) string {
for {
io.WriteString(c, message)
answer, _, _ := bufc.ReadLine()
if string(answer) != "" {
return string(answer)
}
}
}
func handleConnection(c net.Conn, msgchan chan<- string, addchan chan<- game.Client, rmchan chan<- game.Client, server *game.Server) {
bufc := bufio.NewReader(c)
defer c.Close()
log.Println("New connection open:", c.RemoteAddr())
io.WriteString(c, fmt.Sprintf("\033[1;30;41mWelcome to \"%s\" Go-MUD Server!\033[0m\n\r", server.GetName()))
io.WriteString(c, server.Config.Motd)
initialConnection := false
var nickname string
questions := 0
for {
if questions >= 3 {
io.WriteString(c, "See you\n\r")
return
}
nickname = promptMessage(c, bufc, "Whats your Nick?\n\r ")
isValidName := server.IsValidUsername(nickname);
if !isValidName {
questions++
io.WriteString(c, fmt.Sprintf("Username %s is not valid (0-9a-z_-).\n\r", nickname))
continue
}
ok := server.LoadPlayer(nickname)
if ok == false {
questions++
io.WriteString(c, fmt.Sprintf("Username %s does not exists.\n\r", nickname))
answer := promptMessage(c, bufc, "Do you want to create that user? [y|n] ")
if answer == "y" {
gameName := promptMessage(c, bufc, "Please enter your ingame Name: ")
playerType := promptMessage(c, bufc, "Please enter your character type: ")
server.CreatePlayer(nickname, gameName, playerType)
initialConnection = true
break
}
}
if ok == true {
break
}
}
player, playerLoaded := server.GetPlayerByNick(nickname)
if !playerLoaded {
log.Println("problem getting user object")
io.WriteString(c, "Problem getting user object\n")
return
}
client := game.NewClient(c, player)
if strings.TrimSpace(client.Nickname) == "" {
log.Println("invalid username")
io.WriteString(c, "Invalid Username\n")
return
}
// Register user
addchan <- client
defer func() {
msgchan <- fmt.Sprintf("User %s left the chat room.\n\r", client.Nickname)
log.Printf("Connection from %v closed.\n", c.RemoteAddr())
rmchan <- client
}()
io.WriteString(c, fmt.Sprintf("Welcome, %s!\n\n\r", client.Player.Gamename))
location, locationLoaded := server.GetRoom(client.Player.Position)
if locationLoaded {
if initialConnection {
client.WriteHelp(server)
}
location.OnEnterRoom(server, client)
}
// I/O
go client.ReadLinesInto(msgchan, server)
client.WriteLinesFrom(client.Ch)
}
func handleMessages(msgchan <-chan string, addchan <-chan game.Client, rmchan <-chan game.Client) {
clients := make(map[net.Conn]chan<- string)
for {
select {
case msg := <-msgchan:
log.Printf("New message: %s", msg)
for _, ch := range clients {
go func(mch chan<- string) { mch <- "\033[1;33;40m" + msg + "\033[m\n\r" }(ch)
}
case client := <-addchan:
log.Printf("New client: %v\n\r\n\r", client.Conn)
clients[client.Conn] = client.Ch
case client := <-rmchan:
log.Printf("Client disconnects: %v\n\r\n\r", client.Conn)
delete(clients, client.Conn)
}
}
}