-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
246 lines (222 loc) · 4.85 KB
/
main.go
File metadata and controls
246 lines (222 loc) · 4.85 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var ascii bool
func init() {
flag.BoolVar(&ascii, "a", false, "use ascii art instead of words")
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: rabbit [-a] [stats|check|catch|tag string]\n")
flag.PrintDefaults()
}
// Loads the directory forest from the file passed, if it doesn't exist
// a fresh directory forest is returned.
func loadDirectoryForest(filename string) *directoryForest {
file, err := os.Open(filename)
if err != nil {
if !os.IsNotExist(err) {
log.Fatal(err)
} else {
df := newDirectoryForest()
return &df
}
}
defer file.Close()
fz, err := gzip.NewReader(file)
if err != nil {
log.Fatal(err)
}
defer fz.Close()
bytes, err := ioutil.ReadAll(fz)
if err != nil {
log.Fatal(err)
}
var df directoryForest
err = json.Unmarshal(bytes, &df)
if err != nil {
log.Fatal(err)
}
return &df
}
// Saves the directory forest to a file.
func saveDirectoryForest(filename string, df *directoryForest) {
bs, err := json.Marshal(df)
if err != nil {
log.Fatal(err)
}
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(bs)
w.Close()
err = ioutil.WriteFile(filename, b.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
}
// Returns flavor for the number of spotted rabbits.
func spottedFlavor(count uint) string {
switch {
case count < 20: return ""
case count < 50: return ":)"
default: return ":D"
}
}
// Returns flavor for the number of caught rabbits.
func caughtFlavor(count uint) string {
switch {
case count < 5: return ""
case count < 20: return ":)"
default: return ":D"
}
}
// Returns flavor for the number of killed rabbits. The higher, the
// more dramatic the flavor.
func killedFlavor(count uint) string {
switch {
case count < 5: return ""
case count < 20: return ":("
case count < 50: return ";("
default: return "MONSTER!!"
}
}
// Prints the stats. Number of rabbits seen, caught, killed, etc.
func printStats(df *directoryForest) {
sflavor := spottedFlavor(df.spottedCount)
cflavor := caughtFlavor(df.caughtCount)
kflavor := killedFlavor(df.killedCount)
fmt.Printf("Rabbits\n");
fmt.Printf("...spotted: %d %s\n", df.spottedCount, sflavor)
fmt.Printf("...caught: %d %s\n", df.caughtCount, cflavor)
fmt.Printf("...killed: %d %s\n", df.killedCount, kflavor)
}
func printRabbit(state RabbitState) {
switch state {
case Wandering:
fmt.Printf(" ()_()\n")
fmt.Printf(" (-.-)\n")
fmt.Printf("'(\"|\")'\n")
case Spotted:
fmt.Printf("(_/ _#\n")
fmt.Printf("'.'_( )\n")
//fmt.Printf("/)/)\n")
//fmt.Printf("(o.o)\n")
//fmt.Printf("c(")(")\n")
case Fleeing:
fmt.Printf(" o __(\\\\\n")
fmt.Printf(" ) _ --\n")
fmt.Printf(" // \\\\\n")
case Caught:
fmt.Printf("_________\n")
fmt.Printf("| ()|() |\n")
fmt.Printf("+---+---+\n")
fmt.Printf("|(\")|(\")|\n")
fmt.Printf("---------\n")
case Dead:
fmt.Printf("(\\ /)\n")
fmt.Printf("(x.x)\n")
fmt.Printf("(> <)\n")
}
}
// Check the current directory for rabbits.
func check(df *directoryForest) {
spotted := df.PerformCheck()
if spotted != nil {
if spotted.Tag() != "" {
fmt.Printf("You see the %s rabbit!\n", spotted.Tag())
if ascii {
printRabbit(Spotted)
}
} else {
fmt.Printf("A rabbit is here!!\n")
if ascii {
printRabbit(Spotted)
}
}
} else {
here, track := df.GetTracksHere()
if here {
if track == TrackAscending {
fmt.Printf("You see rabbit tracks ascending...\n")
} else if track == TrackDescending {
fmt.Printf("You see rabbit tracks descending...\n")
}
if ascii {
fmt.Printf(" , , ,\n")
fmt.Printf("= = =\n")
fmt.Printf(" ` ` `\n")
}
}
}
}
// Try to catch a rabbit.
func catch(df *directoryForest) {
if df.IsRabbitHere() {
if df.PerformCatch() {
fmt.Printf("You caught the rabbit!\n")
if ascii {
printRabbit(Caught)
}
} else {
fmt.Printf("The rabbit got away...\n")
if ascii {
printRabbit(Fleeing)
}
}
} else {
fmt.Printf("Too slow or you're seeing things.\n")
}
}
// Try to tag a rabbit.
func tag(df *directoryForest, tag string) {
if df.IsRabbitHere() {
if df.PerformTag(tag) {
fmt.Printf("You successfully tagged the rabbit!\n")
if ascii {
printRabbit(Wandering)
}
} else {
fmt.Printf("The rabbit got away...\n")
if ascii {
printRabbit(Caught)
}
}
} else {
fmt.Printf("Too slow or you're seeing things.\n")
}
}
func main() {
flag.Parse()
savefile := filepath.Join(os.Getenv("HOME"), ".rabbit")
df := loadDirectoryForest(savefile)
defer saveDirectoryForest(savefile, df)
if flag.NArg() == 0 {
usage()
return
}
switch flag.Arg(0) {
case "stats":
printStats(df)
case "check":
check(df)
case "catch":
catch(df)
case "tag":
if flag.NArg() < 2 {
usage()
return
}
tag(df, flag.Arg(1))
case "debug":
fmt.Printf("%+v", df)
default: usage()
}
}