~nromdotcom/gemif

ref: f1ef4d543cc010bb97d0d825503ff0ece1841ed7 gemif/pkg/gamemanager/dataloader.go -rw-r--r-- 1.3 KiB
f1ef4d54Norm MacLennan README updates for new features 1 year, 1 month ago
                                                                                
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
package gamemanager

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"gopkg.in/yaml.v2"
)

type config struct {
	Stories map[string]Story `yaml:"stories" json:"stories"`
}

// Story contains all information about a loaded story.
type Story struct {
	Metadata StoryMetadata `yaml:"metadata" json:"metadata"`
	Rooms    []Room        `yaml:"rooms" json:"rooms"`
}

// StoryMetadata contains story metadata - authorship information, etc.
type StoryMetadata struct {
	ID          string `yaml:"id"`
	Name        string `yaml:"name"`
	Description string `yaml:"description"`
	Author      string `yaml:"author"`
}

func (c *config) getStories(root string) error {
	var files []string

	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		files = append(files, path)

		return nil
	})
	if err != nil {
		panic(err)
	}

	c.Stories = make(map[string]Story)

	for _, file := range files {
		yamlFile, err := ioutil.ReadFile(file)
		if err != nil {
			return fmt.Errorf("couldn't load story: %w", err)
		}

		var thisStory *Story
		if err := yaml.Unmarshal(yamlFile, &thisStory); err != nil {
			fmt.Println(err.Error())

			return fmt.Errorf("couldn't load story: %w", err)
		}

		c.Stories[thisStory.Metadata.ID] = *thisStory
	}

	return nil
}