From a7910adc2659fa1489a6f9d6a71d568accf747e2 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 12 Feb 2025 15:59:39 -0700 Subject: [PATCH] impl priceiswrong.Item --- src/state/fsm/priceiswrong/events.go | 13 +++++++++++++ src/state/fsm/priceiswrong/internal/event.go | 7 +++++++ src/state/fsm/priceiswrong/open.go | 1 + src/state/fsm/priceiswrong/priceiswrong_test.go | 12 ++++++++++++ 4 files changed, 33 insertions(+) diff --git a/src/state/fsm/priceiswrong/events.go b/src/state/fsm/priceiswrong/events.go index 1a4976b..15c2cce 100644 --- a/src/state/fsm/priceiswrong/events.go +++ b/src/state/fsm/priceiswrong/events.go @@ -4,6 +4,7 @@ import ( "context" "gitea/price-is-wrong/src/lib/db" priceiswrong "gitea/price-is-wrong/src/state/fsm/priceiswrong/internal" + "html" "math/rand" ) @@ -25,6 +26,10 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error { 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 } @@ -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}) } +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 { if err := upsertEvent(ctx, p.id, e); err != nil { return err diff --git a/src/state/fsm/priceiswrong/internal/event.go b/src/state/fsm/priceiswrong/internal/event.go index 168b72a..f4923bf 100644 --- a/src/state/fsm/priceiswrong/internal/event.go +++ b/src/state/fsm/priceiswrong/internal/event.go @@ -20,11 +20,18 @@ type Score struct { Score int } +type Item struct { + ImageURL string + Title string + Description string +} + func ParseEvent(b []byte) (Event, error) { typesToPointers := map[string]any{ "*priceiswrong.Players": &Players{}, "*priceiswrong.Host": &Host{}, "*priceiswrong.Score": &Score{}, + "*priceiswrong.Item": &Item{}, } t, err := event.Parse(b, typesToPointers) return typesToPointers[t], err diff --git a/src/state/fsm/priceiswrong/open.go b/src/state/fsm/priceiswrong/open.go index b4628de..b069900 100644 --- a/src/state/fsm/priceiswrong/open.go +++ b/src/state/fsm/priceiswrong/open.go @@ -12,6 +12,7 @@ type PriceIsWrong struct { id int Host int Contestants []Player + Item priceiswrong.Item } type Player struct { diff --git a/src/state/fsm/priceiswrong/priceiswrong_test.go b/src/state/fsm/priceiswrong/priceiswrong_test.go index 88eb677..c34635d 100644 --- a/src/state/fsm/priceiswrong/priceiswrong_test.go +++ b/src/state/fsm/priceiswrong/priceiswrong_test.go @@ -39,4 +39,16 @@ func TestPriceIsWrong(t *testing.T) { } else if p.Contestants[1].Score != 0 { 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) + } }