aboutsummaryrefslogtreecommitdiff
path: root/go/game/set.go
blob: a85bf577d6bad5d3ae6ccba4580e84d950f0448a (plain)
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
package game

import (
	"errors"
	"net/http"
	"path"
	"strings"

	"golang.org/x/net/html"
	"muhq.space/muhqs-game/go/assets"
	"muhq.space/muhqs-game/go/log"
)

type SetIdentifier int

const (
	Base SetIdentifier = iota + 1
	Magic
	Equipments
	Misc
	Nautics
	Potions
	Kraken
	Tyrant
	Exp1
)

var ErrUnknownSet = errors.New("unknown set")

func (si SetIdentifier) String() string {
	switch si {
	case Base:
		return "base"
	case Magic:
		return "magic"
	case Equipments:
		return "equipments"
	case Misc:
		return "misc"
	case Nautics:
		return "nautics"
	case Potions:
		return "potions"
	case Kraken:
		return "kraken"
	case Tyrant:
		return "tyrant"
	case Exp1:
		return "exp1"
	}
	log.Panicf("Switch not exhausting %d", si)
	return ""
}

var Sets = struct {
	Base       SetIdentifier
	Magic      SetIdentifier
	Equipments SetIdentifier
	Misc       SetIdentifier
	Nautics    SetIdentifier
	Potions    SetIdentifier
	Kraken     SetIdentifier
	Tyrant     SetIdentifier
	Exp1       SetIdentifier
}{
	Base:       Base,
	Magic:      Magic,
	Equipments: Equipments,
	Misc:       Misc,
	Nautics:    Nautics,
	Potions:    Potions,
	Kraken:     Kraken,
	Tyrant:     Tyrant,
	Exp1:       Exp1,
}

var SetNames = map[string]SetIdentifier{
	"base":       Base,
	"magic":      Magic,
	"equipments": Equipments,
	"misc":       Misc,
	"nautics":    Nautics,
	"potions":    Potions,
	"kraken":     Kraken,
	"tyrant":     Tyrant,
	"exp1":       Exp1,
}

func (set SetIdentifier) CardPaths() []string {
	url := assets.PROTOCOL + path.Join(assets.BASE_URL, CARD_DATA_URL, set.String())
	log.Debug("loading set", "url", url)
	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}

	defer resp.Body.Close()

	doc, err := html.Parse(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	paths := []string{}
	var walk func(*html.Node)
	walk = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "a" {
			for _, a := range n.Attr {
				if a.Key == "href" == strings.HasSuffix(a.Val, ".yml") {
					paths = append(paths, path.Join(set.String(), strings.TrimSuffix(a.Val, ".yml")))
					break
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			walk(c)
		}
	}
	walk(doc)
	return paths
}

func SetListToSets(setList string) ([]SetIdentifier, error) {
	sets := []SetIdentifier{}
	setList = strings.ReplaceAll(setList, " ", "")
	for s := range strings.SplitSeq(setList, ",") {
		set, found := SetNames[s]
		if found {
			sets = append(sets, set)
		} else {
			return sets, ErrUnknownSet
		}
	}
	return sets, nil
}