impl .Guess and clearing .Guess
parent
a7910adc26
commit
0ec74308a7
|
|
@ -27,9 +27,18 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
|
|||
}
|
||||
}
|
||||
case *priceiswrong.Item:
|
||||
for i := range p.Contestants {
|
||||
p.Contestants[i].Guess = ""
|
||||
}
|
||||
p.Item.ImageURL = e.ImageURL
|
||||
p.Item.Title = html.EscapeString(e.Title)
|
||||
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
|
||||
}
|
||||
|
|
@ -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 {
|
||||
if err := upsertEvent(ctx, p.id, e); err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -26,12 +26,18 @@ type Item struct {
|
|||
Description string
|
||||
}
|
||||
|
||||
type Guess struct {
|
||||
ID int
|
||||
Guess string
|
||||
}
|
||||
|
||||
func ParseEvent(b []byte) (Event, error) {
|
||||
typesToPointers := map[string]any{
|
||||
"*priceiswrong.Players": &Players{},
|
||||
"*priceiswrong.Host": &Host{},
|
||||
"*priceiswrong.Score": &Score{},
|
||||
"*priceiswrong.Item": &Item{},
|
||||
"*priceiswrong.Guess": &Guess{},
|
||||
}
|
||||
t, err := event.Parse(b, typesToPointers)
|
||||
return typesToPointers[t], err
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ type PriceIsWrong struct {
|
|||
type Player struct {
|
||||
ID int
|
||||
Score int
|
||||
Guess string
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, name string) (*PriceIsWrong, error) {
|
||||
|
|
|
|||
|
|
@ -40,8 +40,16 @@ func TestPriceIsWrong(t *testing.T) {
|
|||
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 {
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
} else if p.Item.ImageURL != "imageURL" {
|
||||
|
|
|
|||
Loading…
Reference in New Issue