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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
package game
import (
"errors"
"io"
"net/http"
"path"
"slices"
"strconv"
"strings"
"github.com/goccy/go-yaml"
"muhq.space/muhqs-game/go/assets"
"muhq.space/muhqs-game/go/log"
)
type CardType int
const (
undefinedCardType CardType = iota
unitType
bossType
spellType
artifactType
equipmentType
potionType
intentionType
)
func (ct CardType) String() string {
switch ct {
case undefinedCardType:
return "undefined"
case unitType:
return "unit"
case bossType:
return "boss"
case spellType:
return "spell"
case artifactType:
return "artifact"
case equipmentType:
return "equipment"
case potionType:
return "potion"
case intentionType:
return "intention"
}
log.Panicf("Switch not exhausting %d", ct)
return ""
}
var CardTypes = struct {
Unit CardType
Boss CardType
Spell CardType
Artifact CardType
Equipment CardType
Potion CardType
Intention CardType
}{
Unit: unitType,
Boss: bossType,
Spell: spellType,
Artifact: artifactType,
Equipment: equipmentType,
Potion: potionType,
Intention: intentionType,
}
var CardTypeNames = map[string]CardType{
"unit": unitType,
"boss": unitType,
"spell": spellType,
"artifact": artifactType,
"equipment": equipmentType,
"potion": potionType,
"intention": intentionType,
}
func (ct CardType) IsPermanent() bool {
switch ct {
case CardTypes.Unit, CardTypes.Boss, CardTypes.Artifact, CardTypes.Equipment, CardTypes.Potion:
return true
default:
return false
}
}
func (ct CardType) IsUnit() bool {
switch ct {
case CardTypes.Unit, CardTypes.Boss:
return true
default:
return false
}
}
func (ct CardType) IsArtifact() bool {
switch ct {
case CardTypes.Artifact, CardTypes.Equipment, CardTypes.Potion:
return true
default:
return false
}
}
type cardImplementation interface {
spawnTiles(*LocalState, *Player) []*Tile
fullActions(*Unit) []*FullAction
freeActions(Permanent) []*FreeAction
playTargets() TargetDesc
stateBasedActions(*LocalState, Permanent)
additionalPlayCosts(*PlayAction) ActionCostFunc
additionalSpawnsFor(Permanent, CardType) []*Tile
onEntering(*Tile)
onLeaving(*Tile)
onPlay(*PlayAction)
onETB(*LocalState, Permanent)
onPile(containing Permanent)
onUnpile(containing Permanent)
onDrop(dropped Permanent)
onUpkeep(Permanent)
}
// Default implementation of the cardImplementation interface.
//
// Card Implementations should embed this struct to "inherit" the
// default behavior.
type cardImplementationBase struct{}
func (*cardImplementationBase) spawnTiles(*LocalState, *Player) []*Tile { return nil }
func (*cardImplementationBase) fullActions(*Unit) []*FullAction { return nil }
func (*cardImplementationBase) freeActions(Permanent) []*FreeAction { return nil }
func (*cardImplementationBase) playTargets() TargetDesc { return INVALID_TARGET_DESC }
func (*cardImplementationBase) stateBasedActions(*LocalState, Permanent) {}
func (*cardImplementationBase) additionalPlayCosts(*PlayAction) ActionCostFunc { return nil }
func (*cardImplementationBase) additionalSpawnsFor(Permanent, CardType) []*Tile { return nil }
func (*cardImplementationBase) onEntering(*Tile) {}
func (*cardImplementationBase) onLeaving(*Tile) {}
func (*cardImplementationBase) onPlay(*PlayAction) {}
func (*cardImplementationBase) onETB(*LocalState, Permanent) {}
func (*cardImplementationBase) onPile(Permanent) {}
func (*cardImplementationBase) onUnpile(Permanent) {}
func (*cardImplementationBase) onDrop(Permanent) {}
func (*cardImplementationBase) onUpkeep(Permanent) {}
// Map of all card implementations
var cardImplementations map[string]cardImplementation
func getCardImplementation(c *Card) cardImplementation {
if impl, found := cardImplementations[c.Path()]; found {
return impl
}
impl := newParsedCardImplementation(c)
cardImplementations[c.Path()] = impl
return impl
}
const (
CARD_DATA_URL string = "data/cards"
)
type Card struct {
Name string
Set SetIdentifier
Type CardType
BuyCosts *ResourceCosts
PlayCosts *ResourceCosts
Values map[string]any
Impl cardImplementation
}
var cardDefinitions = map[string]map[string][]byte{}
func getCardDefinition(set, cardName string) ([]byte, error) {
cardDefinition, found := cardDefinitions[set][cardName]
if !found {
var err error
cardDefinition, err = retrieveCardDefinition(path.Join(set, cardName))
if err != nil {
return nil, err
}
if cardDefinitions[set] == nil {
cardDefinitions[set] = map[string][]byte{}
}
cardDefinitions[set][cardName] = cardDefinition
}
return cardDefinition, nil
}
var ErrUnknownCardPath = errors.New("unknown card path")
func retrieveCardDefinition(cardPath string) ([]byte, error) {
url := assets.PROTOCOL + path.Join(assets.BASE_URL, CARD_DATA_URL, cardPath+".yml")
log.Debug("loading card from", "url", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, ErrUnknownCardPath
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return data, nil
}
func (card *Card) parseDefinition(definition []byte) {
data := make(map[any]any)
err := yaml.Unmarshal(definition, &data)
if err != nil {
log.Panicf("parsing %s failed with: %v\n", definition, err)
}
name, found := data["name"]
if !found {
log.Panicf("no name in card yaml %s\n", definition)
}
delete(data, "name")
switch nameYml := name.(type) {
case string:
card.Name = nameYml
case map[string]any:
cardName := nameYml["en"]
card.Name = cardName.(string)
default:
log.Panicf("Unexpected type %T of yml card field 'Name'\n", name)
}
t, found := data["type"]
if !found {
log.Panicf("no type in card yaml %s\n", definition)
}
delete(data, "type")
card.Type = CardTypeNames[t.(string)]
bc, bcFound := data["buy"]
delete(data, "buy")
pc, pcFound := data["play"]
delete(data, "play")
for k, v := range data {
card.Values[k.(string)] = v
}
if bcFound {
card.BuyCosts = ParseCardResourceCosts(bc, card.getEffects())
}
if pcFound {
card.PlayCosts = ParseCardResourceCosts(pc, card.getEffects())
} else if card.Type == CardTypes.Unit {
card.PlayCosts = ParseCardResourceCosts(card.Values["upkeep"], card.getEffects())
}
card.Impl = getCardImplementation(card)
}
func (card *Card) getCanonicalValues(key string) []string {
var values []string
untypedValues, found := card.Values[key]
if !found {
return []string{}
}
switch typedValues := untypedValues.(type) {
case map[string]any:
enValues := typedValues["en"]
switch typedEnValues := enValues.(type) {
case string:
values = []string{typedEnValues}
case []string:
values = typedEnValues
case []any:
values = []string{}
for _, v := range typedEnValues {
values = append(values, v.(string))
}
}
case string:
values = []string{typedValues}
case uint64:
values = []string{strconv.Itoa(int(typedValues))}
case []string:
values = typedValues
}
for i, value := range values {
values[i] = strings.ToLower(value)
}
return values
}
func (card *Card) getEffects() []string {
effects := card.getCanonicalValues("effect")
for i, effect := range effects {
// Trim explanation from the effect
if effect[len(effect)-1:] != ")" {
continue
}
// Trim after " (..."
effects[i] = effect[0 : strings.IndexByte(effect, '(')-1]
}
return effects
}
func (card *Card) hasEffect(effect string) bool {
return slices.Contains(card.getEffects(), effect)
}
var ErrNoXEffect = errors.New("card has no xeffect")
func (card *Card) getXEffect(effect string) (xEffect, error) {
for _, e := range card.getEffects() {
if !strings.Contains(e, effect) {
continue
}
return parseXEffect(e)
}
return xEffect{}, ErrNoXEffect
}
func (card *Card) hasPlacementConstrain(effect string) bool {
if slices.Contains(card.getEffects(), effect) {
return true
} else if !card.Type.IsUnit() {
return false
}
movementDesc := card.getCanonicalValues("movement")[0]
return strings.Contains(movementDesc, effect)
}
func (card *Card) getAI() string {
aiDesc := card.getCanonicalValues("ai")
if len(aiDesc) == 0 {
return ""
}
return strings.Split(aiDesc[0], " ")[0]
}
// Create a new card from the path.
// Panic if the path is not valid.
func NewCard(cardPath string) *Card {
card, err := NewCardSafe(cardPath)
if err != nil {
log.Fatal(err)
}
return card
}
// NewCardSafe safely creates a new card from the path.
// Return an error if the path is invalid.
func NewCardSafe(cardPath string) (*Card, error) {
cardName := path.Base(cardPath)
set := path.Dir(cardPath)
cardDefinition, err := getCardDefinition(set, cardName)
if err != nil {
return nil, err
}
c := Card{
Name: "",
Set: SetNames[set],
Type: undefinedCardType,
BuyCosts: nil,
PlayCosts: nil,
Values: map[string]any{},
}
c.parseDefinition(cardDefinition)
return &c, err
}
// FileName returns the Name of a card kn lower case and replaces spaces with underscores.
func (c *Card) FileName() string {
return strings.ReplaceAll(strings.ToLower(c.Name), " ", "_")
}
// Path returns the canonical full path of a card.
// %he pa%h of a card consists of its set name followed by '/' and its file name.
func (c *Card) Path() string {
return path.Join(c.Set.String(), c.FileName())
}
func (c *Card) IsPermanent() bool {
return c.Type.IsPermanent()
}
func (c *Card) IsToken() bool {
if v, found := c.Values["token"]; found {
b, ok := v.(bool)
if !ok {
log.Panicf("Invalid value for %s token value: %v", c.Name, v)
}
return b
}
return false
}
func (c *Card) IsBuyable() bool {
return c.BuyCosts != nil
}
|