-
Notifications
You must be signed in to change notification settings - Fork 0
/
MUD.go
519 lines (424 loc) · 10.3 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"fmt"
"log"
"bufio"
"os"
"strings"
"net"
)
// Struct :
type Zone struct {
ID int
Name string
Rooms []*Room
}
type Room struct {
ID int
Zone *Zone
Name string
Description string
Exits [6]Exit
}
type Exit struct {
To *Room
Description string
}
var Direction = map[string]int {
"n" : 0,
"e" : 1,
"w" : 2,
"s" : 3,
"u" : 4,
"d" : 5,
}
var ReDirection = map[int]string {
0 : "n",
1 : "e",
2 : "w",
3 : "s",
4 : "u",
5 : "d",
}
type Player struct {
Name string
ID int
world map[int]*Room
Location *Room
Connection net.Conn
Outputs chan OutputEvent
}
type InputEvent struct {
player *Player
Command string
Login bool
}
type OutputEvent struct {
mas string
}
var ALLROOM = make (map[int]*Room)
var ALLPLAYER = make (map[int]*Player)
var ConnToPlayer = make(map[net.Conn]*Player)
const listenAddress = ":3410"
// Part 1:
var allCommands = make(map[string]func(string, *Player))
// the MAKE bulid this type of data set in map and bulid the refrence
func commandLoop() error {
player := ALLPLAYER[0]
cmdwhere("", player)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
//log.Printf("The line was: %s", line)
doCommand(line, player)
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("in main command loop: %v", err)
}
return nil
}
func addCommand(cmd string, f func(string, *Player)) {
for i := range cmd {
if i == 0 {
continue
}
prefix := cmd[:i]
allCommands[prefix] = f
}
allCommands[cmd] = f
}
func initCommands() {
addCommand("look", cmdLook)
addCommand("north", cmdNorth)
addCommand("east", cmdEast)
addCommand("west", cmdWest)
addCommand("south", cmdSouth)
addCommand("up", cmdUp)
addCommand("down", cmdDown)
addCommand("where", cmdwhere)
addCommand("recall", cmdRecall)
addCommand("laugh", cmdLaugh)
addCommand("tell", cmdTell)
}
func doCommand(cmd string, player *Player) error {
words := strings.Fields(cmd)
if len(words) == 0 {
return nil
}
for i := 0; i < len(words) ; i++ {
if f, exists := allCommands[strings.ToLower(words[i])]; exists {
if strings.ToLower(words[i]) == "l" || strings.ToLower(words[i]) == "lo" || strings.ToLower(words[i]) == "loo" || strings.ToLower(words[i]) == "look"{
cmdLook(words[i + 1], player)
i++
} else {
f(cmd, player)
}
}
}
return nil
}
func cmdLaugh(s string, player *Player) {
if len(s) > 5 {
log.Println("HAHAHAHAAHA! You laught about", s[5:])
} else {
fmt.Printf("\n")
log.Println("HAHAHAHAAHA! You laught.")
}
}
func cmdTell(s string, player *Player) {
if len(s) > 4 {
log.Println("You tell", s[4:])
} else {
fmt.Printf("\n")
log.Println("You try to tell somebody about lsomething")
}
}
func cmdLook(s string, player *Player) {
if player.checkexit(string(s[0])) {
look_next_loca := player.Location.Exits[Direction[string(s[0])]].Description
fmt.Printf("\n")
fmt.Printf(look_next_loca)
} else {
fmt.Printf("\n")
fmt.Printf("There is nothing in that direcation.")
}
}
func cmdwhere(s string, player *Player) {
fmt.Printf(player.Location.Name)
fmt.Printf("\n")
fmt.Printf(player.Location.Description)
fmt.Printf("\n")
dir_list := []string{}
for d := 0; d < 6; d++ {
if player.Location.Exits[d].To != nil {
dir_list = append(dir_list, ReDirection[d])
}
}
fmt.Printf("[ Exits: %s ]\n", strings.Trim(fmt.Sprint(dir_list), "[]"))
}
func cmdRecall(s string, player *Player) {
fmt.Printf("You take out a wishbone and pull apart. You see a light coming to you...\n")
player.recall()
cmdwhere(s, player)
}
func cmdNorth(s string, player *Player) {
if player.checkexit("n") {
next_loca := player.Location.Exits[Direction["n"]].To
player = player.updatelocation(next_loca)
cmdwhere(s,player)
} else {
fmt.Printf("You can't go that direction!\n")
}
}
func cmdEast(s string, player *Player) {
if player.checkexit("e") {
player = player.updatelocation(player.Location.Exits[Direction["e"]].To)
fmt.Printf("\n")
cmdwhere(s,player)
} else {
fmt.Printf("You can't go that direction!\n")
}
}
func cmdWest(s string, player *Player) {
if player.checkexit("w") {
player = player.updatelocation(player.Location.Exits[Direction["w"]].To)
fmt.Printf("\n")
cmdwhere(s,player)
} else {
fmt.Printf("You can't go that direction!\n")
}
}
func cmdSouth(s string, player *Player) {
if player.checkexit("s") {
player = player.updatelocation(player.Location.Exits[Direction["s"]].To)
fmt.Printf("\n")
cmdwhere(s,player)
} else {
fmt.Printf("You can't go that direction!\n")
}
}
func cmdUp(s string, player *Player) {
if player.checkexit("u") {
player = player.updatelocation(player.Location.Exits[Direction["u"]].To)
fmt.Printf("\n")
cmdwhere(s,player)
} else {
fmt.Printf("You can't go that direction!\n")
}
}
func cmdDown(s string, player *Player) {
if player.checkexit("d") {
player = player.updatelocation(player.Location.Exits[Direction["d"]].To)
fmt.Printf("\n")
cmdwhere(s,player)
} else {
fmt.Printf("You can't go that direction!\n")
}
}
// Part 2:
func loadzone (transaction *sql.Tx) (map[int]*Zone, error) {
var id int
//var zone_id int
var name string
//var description string
var allzone = make(map[int]*Zone)
rows, err := transaction.Query(`SELECT id, name FROM zones ORDER BY id`)
if err != nil {
return nil , err
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&id, &name); err != nil {
return nil, err
}
new_zone := Zone{id,name, nil}
new_pointer := &new_zone
allzone[id] = new_pointer
}
return allzone, nil
}
func loadroom (Zone map[int]*Zone ,transaction *sql.Tx) (map[int]*Room, error) {
var room_id int
var room_name string
var room_description string
var allroom = make(map[int]*Room)
var zone_id int
rows, err := transaction.Query(`SELECT id, zone_id, name, description FROM rooms`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&room_id, &zone_id, &room_name, &room_description); err != nil {
return nil, err
}
zone_pointer := Zone[zone_id]
new_room := Room{room_id, zone_pointer, room_name, room_description, [6]Exit{}}
allroom[room_id] = &new_room
Zone[zone_id].Rooms = []*Room{&new_room}
}
return allroom, nil
}
func loadExit (Room map[int]*Room, transaction *sql.Tx) (map[int]*Room, error) {
var from_room int
var to_room int
var direction string
var description string
rows, err := transaction.Query(`SELECT * FROM exits`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&from_room, &to_room, &direction, &description); err != nil {
return nil, err
}
to_pointer := Room[to_room]
new_exit := Exit{to_pointer, description}
Room[from_room].Exits[Direction[direction]] = new_exit
}
return Room, nil
}
func loadalldata(tx *sql.Tx) error {
ALLzone, err := loadzone(tx)
if err != nil {
return err
}
ALLroom, err := loadroom(ALLzone, tx)
ALLROOM, err = loadExit(ALLroom, tx)
if err != nil {
return err
}
//for key, value := range ALLROOM {
// fmt.Printf("ROOM: ", key, "pointer: ", value)
// fmt.Printf("\n")
//}
return nil
}
func opentransaction(database *sql.DB) error {
tx, err := database.Begin()
if err != nil {
return fmt.Errorf("Starting Transaction: %v", err)
}
err = loadalldata(tx)
if err != nil {
tx.Rollback()
return fmt.Errorf("Loading world data: %v", err)
} else {
if err := tx.Commit(); err != nil {
return fmt.Errorf("Transaction failed while loading world data : %v", err)
}
}
return nil
}
// Player methods:
func initplayer(name string, world map[int]*Room , id int, conn net.Conn, Outputs chan OutputEvent) Player {
player := Player{name, id, world, ALLROOM[3001], conn, Outputs}
return player
}
func (player *Player) recall() {
player.Location = ALLROOM[3001]
}
func (player *Player) updatelocation(new_room *Room) *Player{
player.Location = new_room
return player
}
func (player *Player) checkexit(dir string) bool{
num_dir := Direction[dir]
return player.Location.Exits[num_dir].To != nil
}
func createplayer(conn net.Conn) error{
var Name string
fmt.Printf("Creating a player....\nType in your name?\n")
fmt.Scanln(&Name)
id := len(ALLPLAYER)
Outputs := make(chan OutputEvent)
// Create the target map
world := make(map[int]*Room)
// Copy from the original map to the target map
for key, value := range ALLROOM {
world[key] = value
}
player := initplayer(Name, world, id, conn, Outputs)
ALLPLAYER[id] = &player
ConnToPlayer[conn] = &player
return nil
}
func setupWorld () error{
// Part 2:
// the path to the database--this could be an absolute path
path := "world.db"
options :=
"?" + "_busy_timeout=10000" +
"&" + "_foreign_keys=ON" +
"&" + "_journal_mode=WAL" +
"&" + "mode=rw" +
"&" + "_synchronous=NORMAL"
db, err := sql.Open("sqlite3", path+options)
if err != nil {
// handle the error here
return err
}
defer db.Close()
err = opentransaction(db)
if err != nil {
return err
}
return nil
}
func setup() error{
initCommands()
err := setupWorld()
if err != nil {
return err
} else {
return nil
}
}
func main () {
err := setup()
if err != nil {
fmt.Printf("Setting up: %v", err)
}
inputChannel := make(chan InputEvent)
go listenForConnections(inputChannel)
mainLoop(inputChannel)
}
func mainLoop(inputChannel chan InputEvent) {
for event := range inputChannel {
}
}
func listenForConnections (inputChannel chan InputEvent) {
listen, err := net.Listen("tcp", listenAddress)
if err != nil {
log.Fatalf("listening: on %s: %v", listenAddress, err)
}
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
log.Fatalf("accept: %v", err)
}
go handleConnection(conn, inputChannel)
}
}
func handleConnection(conn net.Conn, inputChannel chan InputEvent) {
createplayer(conn)
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
line := scanner.Text()
event := InputEvent{player: ConnToPlayer[conn], Command: line, Login: true}
inputChannel <- event
}
if err := scanner.Err(); err != nil {
log.Printf("connection error: %v", err)
} else {
log.Printf("connection closed normally\n")
}
}
//func (p *Player) Printf (format string, a ...interface{}) {
// mas := fmt.Sprintf(format, a...)
// p.Outputs <- OutputEvent{Text: msg}
//}