Created
November 9, 2014 17:28
-
-
Save CptSpaceToaster/03c6692b6c5e75df95d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
..... | |
TraverseDirectories("../src/github.com/cptspacetoaster/adventurebot/rooms", HandleRooms) | |
} | |
func TraverseDirectories(dirloc string, handle func([]byte) error) { | |
names := getDirNames(dirloc) | |
for _, element := range names { | |
if isDir, _ := IsDirectory(dirloc + "/" + element); isDir { | |
if isLink, _ := IsSymlink(dirloc + "/" + element); isLink { | |
fmt.Println("Ignoring link: " + dirloc + "/" + element) | |
return | |
} else { | |
TraverseDirectories(dirloc+"/"+element, handle) | |
} | |
} else { | |
input, err := ioutil.ReadFile(dirloc + "/" + element) | |
if err != nil { | |
fmt.Println("Could not read " + element) | |
} else { | |
err = handle(input) | |
if err != nil { | |
fmt.Println("Warning: " + element + " was rejected.") | |
} | |
} | |
} | |
} | |
} | |
func HandleRooms(input []byte) error { | |
var r Room | |
json.Unmarshal(input, &r) | |
if r.ID != "" { | |
for _, s := range r.Adjectives { | |
Adjectives[strings.ToLower(s)] = append(Adjectives[strings.ToLower(s)], r.ID) | |
} | |
for _, s := range r.Names { | |
Nouns[strings.ToLower(s)] = append(Nouns[strings.ToLower(s)], r.ID) | |
} | |
Rooms[r.ID] = r | |
//fmt.Println("Loaded: " + r.ID) | |
} else { | |
return errors.New("Input could not be Unmarshalled into type Room") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment