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 } }