impl priceiswrong.Item
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"gitea/price-is-wrong/src/lib/db"
|
"gitea/price-is-wrong/src/lib/db"
|
||||||
priceiswrong "gitea/price-is-wrong/src/state/fsm/priceiswrong/internal"
|
priceiswrong "gitea/price-is-wrong/src/state/fsm/priceiswrong/internal"
|
||||||
|
"html"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,6 +26,10 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
|
|||||||
p.Contestants[i].Score += e.Score
|
p.Contestants[i].Score += e.Score
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case *priceiswrong.Item:
|
||||||
|
p.Item.ImageURL = e.ImageURL
|
||||||
|
p.Item.Title = html.EscapeString(e.Title)
|
||||||
|
p.Item.Description = html.EscapeString(e.Description)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -41,6 +46,14 @@ func (p *PriceIsWrong) Score(ctx context.Context, id, score int) error {
|
|||||||
return p.upsertEvent(ctx, &priceiswrong.Score{ID: id, Score: score})
|
return p.upsertEvent(ctx, &priceiswrong.Score{ID: id, Score: score})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PriceIsWrong) SetItem(ctx context.Context, imageURL, title, description string) error {
|
||||||
|
return p.upsertEvent(ctx, &priceiswrong.Item{
|
||||||
|
ImageURL: imageURL,
|
||||||
|
Title: title,
|
||||||
|
Description: description,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PriceIsWrong) upsertEvent(ctx context.Context, e priceiswrong.Event) error {
|
func (p *PriceIsWrong) upsertEvent(ctx context.Context, e priceiswrong.Event) error {
|
||||||
if err := upsertEvent(ctx, p.id, e); err != nil {
|
if err := upsertEvent(ctx, p.id, e); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -20,11 +20,18 @@ type Score struct {
|
|||||||
Score int
|
Score int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Item struct {
|
||||||
|
ImageURL string
|
||||||
|
Title string
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
func ParseEvent(b []byte) (Event, error) {
|
func ParseEvent(b []byte) (Event, error) {
|
||||||
typesToPointers := map[string]any{
|
typesToPointers := map[string]any{
|
||||||
"*priceiswrong.Players": &Players{},
|
"*priceiswrong.Players": &Players{},
|
||||||
"*priceiswrong.Host": &Host{},
|
"*priceiswrong.Host": &Host{},
|
||||||
"*priceiswrong.Score": &Score{},
|
"*priceiswrong.Score": &Score{},
|
||||||
|
"*priceiswrong.Item": &Item{},
|
||||||
}
|
}
|
||||||
t, err := event.Parse(b, typesToPointers)
|
t, err := event.Parse(b, typesToPointers)
|
||||||
return typesToPointers[t], err
|
return typesToPointers[t], err
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type PriceIsWrong struct {
|
|||||||
id int
|
id int
|
||||||
Host int
|
Host int
|
||||||
Contestants []Player
|
Contestants []Player
|
||||||
|
Item priceiswrong.Item
|
||||||
}
|
}
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
|
|||||||
@@ -39,4 +39,16 @@ func TestPriceIsWrong(t *testing.T) {
|
|||||||
} else if p.Contestants[1].Score != 0 {
|
} else if p.Contestants[1].Score != 0 {
|
||||||
t.Errorf("gave someone else points: %+v", p.Contestants)
|
t.Errorf("gave someone else points: %+v", p.Contestants)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := p.SetItem(ctx, "a", "b", "c"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if err := p.SetItem(ctx, "imageURL", "title", "description"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if p.Item.ImageURL != "imageURL" {
|
||||||
|
t.Errorf("wrong image url: %s", p.Item.ImageURL)
|
||||||
|
} else if p.Item.Title != "title" {
|
||||||
|
t.Errorf("wrong title: %s", p.Item.Title)
|
||||||
|
} else if p.Item.Description != "description" {
|
||||||
|
t.Errorf("wrong description: %s", p.Item.Description)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user