-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.go
141 lines (113 loc) · 3.32 KB
/
room.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
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// Room is an interface for all rooms.
type Room interface {
Name() string
DisplayName() string
Description() string
Exits() []string
Items() []string
}
// DisplayRoom writes a depiction of the Room to the io.Writer.
func DisplayRoom(r Room, out io.Writer) {
printHeader(r.Name(), out)
fmt.Fprintf(out, r.Description())
items := r.Items()
if len(items) > 0 {
fmt.Fprint(out, "Items:\n")
for _, item := range items {
fmt.Fprintf(out, "\t%s\n", item)
}
}
fmt.Fprint(out, "Exits:\n")
for _, exit := range r.Exits() {
fmt.Fprintf(out, "\t%s\n", exit)
}
}
func printHeader(str string, out io.Writer) {
fmt.Fprintf(out, "%s\n", str)
fmt.Fprintf(out, "%s\n\n", strings.Repeat("=", len(str)))
}
// DirRoom is a dungeon room that represents a filesystem directory.
type DirRoom struct {
path string
}
// Name returns the path of the directory.
func (r *DirRoom) Name() string {
return r.path
}
// DisplayName returns a short title to be used for display.
func (r *DirRoom) DisplayName() string {
return filepath.Base(r.path)
}
// Description returns a description of the Room.
func (r *DirRoom) Description() string {
desc := "You stand in a dusty dungeon chamber, not much different from the rest. The\n" +
"room is full of cobwebs and everything is coated in an undisturbed layer of\n" +
"dust.\n"
pathDesc := "On one wall there is a metal plaque rusted with age. It reads:"
return fmt.Sprintf("%s\n%s\n\t%s\n\n", desc, pathDesc, r.path)
}
// Exits returns a list of the child directories as exits.
func (r *DirRoom) Exits() []string {
var dirs []string
_ = filepath.Walk(r.path, func(path string, info os.FileInfo, err error) error {
// Do nothing with current dir
if path == r.path {
return nil
}
// don't recurse into child directories
if filepath.Join(r.path, info.Name()) != path {
return filepath.SkipDir
}
if info.IsDir() {
if os.IsPermission(err) {
dirs = append(dirs, fmt.Sprintf("%s (locked)", info.Name()))
return filepath.SkipDir
}
if err != nil {
return filepath.SkipDir
}
dirs = append(dirs, info.Name())
}
return nil
})
return dirs
}
// Items returns the files in the directory as objects.
func (r *DirRoom) Items() []string {
var files []string
_ = filepath.Walk(r.path, func(path string, info os.FileInfo, err error) error {
// Do nothing with current dir
if path == r.path {
return nil
}
// don't recurse into child directories
if filepath.Join(r.path, info.Name()) != path {
return filepath.SkipDir
}
if info.IsDir() {
return filepath.SkipDir
} else {
files = append(files, info.Name())
}
return nil
})
return files
}