card_id/common/game/card/card.go

48 lines
873 B
Go
Raw Normal View History

2022-04-13 23:09:58 +00:00
package card
2022-04-14 00:20:00 +00:00
import (
"strings"
"git.tuxpa.in/a/card_id/common/data"
"lukechampine.com/frand"
)
2022-04-13 23:09:58 +00:00
type Card struct {
2022-04-14 00:20:00 +00:00
Id int `json:"id"`
Name string `json:"name"`
CleanName string `json:"clean_name"`
2022-04-13 23:09:58 +00:00
Rate float64 `json:"rate"`
2022-04-14 00:20:00 +00:00
BonusLife bool `json:"bonus_life"`
2022-04-13 23:09:58 +00:00
}
func (c *Card) FromResult(d data.Result) Card {
c.Id = d.Id
c.Name = d.Name
c.Rate = d.Rate
2022-04-14 00:20:00 +00:00
c.CleanName = strings.ReplaceAll(strings.TrimSpace(stripNum(c.Name)), " ", " ")
2022-04-13 23:09:58 +00:00
return *c
}
2022-04-14 00:20:00 +00:00
func (c *Card) CopyWithBonus(chance float64) Card {
return Card{
Id: c.Id,
Rate: c.Rate,
Name: c.Name,
CleanName: c.CleanName,
BonusLife: (frand.Float64() <= chance),
}
}
func stripNum(s string) string {
var result strings.Builder
for i := 0; i < len(s); i++ {
b := s[i]
if !('0' <= b && b <= '9') {
result.WriteByte(b)
}
}
return result.String()
}