impl .Guess and clearing .Guess

This commit is contained in:
Bel LaPointe
2025-02-12 16:05:55 -07:00
parent a7910adc26
commit 0ec74308a7
4 changed files with 32 additions and 0 deletions

View File

@@ -27,9 +27,18 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
} }
} }
case *priceiswrong.Item: case *priceiswrong.Item:
for i := range p.Contestants {
p.Contestants[i].Guess = ""
}
p.Item.ImageURL = e.ImageURL p.Item.ImageURL = e.ImageURL
p.Item.Title = html.EscapeString(e.Title) p.Item.Title = html.EscapeString(e.Title)
p.Item.Description = html.EscapeString(e.Description) p.Item.Description = html.EscapeString(e.Description)
case *priceiswrong.Guess:
for i := range p.Contestants {
if p.Contestants[i].ID == e.ID {
p.Contestants[i].Guess = e.Guess
}
}
} }
return nil return nil
} }
@@ -54,6 +63,14 @@ func (p *PriceIsWrong) SetItem(ctx context.Context, imageURL, title, description
}) })
} }
func (p *PriceIsWrong) Guess(ctx context.Context, id int, guess string) error {
return p.upsertEvent(ctx, &priceiswrong.Guess{
ID: id,
Guess: guess,
})
}
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

View File

@@ -26,12 +26,18 @@ type Item struct {
Description string Description string
} }
type Guess struct {
ID int
Guess 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{}, "*priceiswrong.Item": &Item{},
"*priceiswrong.Guess": &Guess{},
} }
t, err := event.Parse(b, typesToPointers) t, err := event.Parse(b, typesToPointers)
return typesToPointers[t], err return typesToPointers[t], err

View File

@@ -18,6 +18,7 @@ type PriceIsWrong struct {
type Player struct { type Player struct {
ID int ID int
Score int Score int
Guess string
} }
func Open(ctx context.Context, name string) (*PriceIsWrong, error) { func Open(ctx context.Context, name string) (*PriceIsWrong, error) {

View File

@@ -40,8 +40,16 @@ func TestPriceIsWrong(t *testing.T) {
t.Errorf("gave someone else points: %+v", p.Contestants) t.Errorf("gave someone else points: %+v", p.Contestants)
} }
if err := p.Guess(ctx, 4, "guess"); err != nil {
t.Fatal(err)
} else if p.Contestants[0].Guess != "guess" {
t.Errorf("guess didnt persist: %+v", p.Contestants)
}
if err := p.SetItem(ctx, "a", "b", "c"); err != nil { if err := p.SetItem(ctx, "a", "b", "c"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if p.Contestants[0].Guess != "" {
t.Errorf("set item didnt clear guesses")
} else if err := p.SetItem(ctx, "imageURL", "title", "description"); err != nil { } else if err := p.SetItem(ctx, "imageURL", "title", "description"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if p.Item.ImageURL != "imageURL" { } else if p.Item.ImageURL != "imageURL" {