2022-04-13 07:48:22 +00:00
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
2022-04-14 00:57:28 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.tuxpa.in/a/card_id/common/errs"
|
2022-04-13 23:09:58 +00:00
|
|
|
"git.tuxpa.in/a/card_id/common/game/card"
|
2022-04-14 00:57:28 +00:00
|
|
|
"git.tuxpa.in/a/card_id/common/game/score"
|
2022-04-13 07:48:22 +00:00
|
|
|
"lukechampine.com/frand"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
2022-04-14 00:20:00 +00:00
|
|
|
Lives float64
|
|
|
|
Score int
|
|
|
|
|
|
|
|
Combo int
|
|
|
|
BestCombo int
|
|
|
|
CurrentTrial int
|
|
|
|
|
|
|
|
Trial *Trial
|
|
|
|
Trials []Trial
|
|
|
|
|
|
|
|
Player string
|
|
|
|
PlayerId string
|
|
|
|
|
|
|
|
Dealer *card.Dealer `json:"-"`
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:57:28 +00:00
|
|
|
func New(user string, dealer *card.Dealer) *Game {
|
2022-04-14 00:20:00 +00:00
|
|
|
return &Game{
|
|
|
|
Dealer: dealer,
|
|
|
|
Player: user,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:57:28 +00:00
|
|
|
func (g *Game) Resolve(action string, args []string) (resp any, err error) {
|
|
|
|
switch action {
|
|
|
|
case "click":
|
|
|
|
resp, err = g.ActionClick(args)
|
|
|
|
default:
|
|
|
|
err = errs.Invalid("cmd not found")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) ActionClick(args []string) (resp any, err error) {
|
|
|
|
if len(args) > 1 {
|
|
|
|
num, _ := strconv.Atoi(args[1])
|
|
|
|
_, err := g.Trial.SelectCard(num)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errs.Invalid("missing arg")
|
|
|
|
}
|
2022-04-14 00:20:00 +00:00
|
|
|
|
2022-04-14 00:57:28 +00:00
|
|
|
func (g *Game) Tick() (err error) {
|
|
|
|
sc, lives := g.Trial.CheckSelection()
|
|
|
|
if sc != 0 {
|
|
|
|
g.Combo = g.Combo + 1
|
|
|
|
g.Score = g.Score + sc*score.DefaultTable.Ratio(g.Combo)*g.CurrentTrial
|
|
|
|
} else {
|
|
|
|
g.Combo = 0
|
|
|
|
}
|
|
|
|
g.Lives = g.Lives + lives
|
|
|
|
return nil
|
2022-04-13 07:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
func (g *Game) CreateTrial(pairs int, extra int) *Trial {
|
|
|
|
if pairs > 10 {
|
|
|
|
pairs = 10
|
2022-04-13 07:48:22 +00:00
|
|
|
}
|
2022-04-13 23:09:58 +00:00
|
|
|
cards := make([]card.Card, 0, pairs*2+extra)
|
|
|
|
ids := map[int]struct{}{}
|
|
|
|
deck := g.Dealer.GetBrand().GetDeck()
|
2022-04-13 07:51:52 +00:00
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
for i := 0; i < pairs; {
|
|
|
|
card := deck.Draw()
|
|
|
|
if _, ok := ids[card.Id]; !ok {
|
|
|
|
ids[card.Id] = struct{}{}
|
|
|
|
cards = append(cards, card, card)
|
|
|
|
i = i + 1
|
|
|
|
}
|
2022-04-13 07:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
for i := 0; i < extra; {
|
|
|
|
card := deck.Draw()
|
|
|
|
if _, ok := ids[card.Id]; !ok {
|
|
|
|
ids[card.Id] = struct{}{}
|
|
|
|
cards = append(cards, card)
|
|
|
|
i = i + 1
|
2022-04-13 07:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
frand.Shuffle(len(cards), func(i, j int) { cards[i], cards[j] = cards[j], cards[i] })
|
2022-04-14 00:20:00 +00:00
|
|
|
trial := NewTrial()
|
2022-04-13 23:09:58 +00:00
|
|
|
for k, v := range cards {
|
|
|
|
trial.Cards[k+1] = v
|
2022-04-13 07:48:22 +00:00
|
|
|
}
|
2022-04-13 23:09:58 +00:00
|
|
|
return trial
|
2022-04-13 07:48:22 +00:00
|
|
|
}
|