aboutsummaryrefslogtreecommitdiff
path: root/go/game/costs.go
blob: daa2c2136fe2ff683697b7033ce5174060bec889 (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
package game

import (
	"strconv"
	"strings"

	"muhq.space/muhqs-game/go/log"
)

type ResourceCosts struct {
	variadicConstraintFunc func(*LocalState) int
	variadicConstraint     string
	fixed                  int
	variadicComponents     int
}

func (c *ResourceCosts) IsVariadic() bool {
	return c.variadicConstraintFunc != nil || c.variadicComponents > 0
}

func ParseResourceCosts(costsStr string) *ResourceCosts {
	costs := ResourceCosts{}
	for _, c := range costsStr {
		if c == 'X' {
			costs.variadicComponents += 1
		}
	}
	costs.fixed, _ = strconv.Atoi(strings.ReplaceAll(costsStr, "X", ""))
	return &costs
}

func (c *ResourceCosts) Costs(s *LocalState, choosenVariadicCosts ...int) int {
	if !c.IsVariadic() {
		return c.fixed
	}

	if c.variadicConstraintFunc != nil {
		return c.fixed + c.variadicConstraintFunc(s)
	}

	costs := c.fixed
	if len(choosenVariadicCosts) == 1 {
		vc := choosenVariadicCosts[0]
		if vc >= 0 {
			costs = costs + c.variadicComponents*vc
		} else {
			log.Debug("Invalid variadic costs choosen")
		}
	} else {
		log.Debug("Unambigous variadic costs choosen")
	}
	return costs
}

func ParseCardResourceCosts(_costs any, effects []string) *ResourceCosts {
	switch c := _costs.(type) {
	case uint64:
		return &ResourceCosts{fixed: int(c)}
	case string:
		costs := ParseResourceCosts(c)
		if !costs.IsVariadic() {
			return costs
		}

		for _, effect := range effects {
			if strings.Contains(effect, "X is") {
				costs.variadicConstraint = effect
			}
		}
		return costs
	default:
		return nil
	}
}