.Score, .SetHost, .SetPlayers

main
Bel LaPointe 2025-02-12 15:54:25 -07:00
parent 53ca14a1bb
commit fa46313386
4 changed files with 35 additions and 4 deletions

View File

@ -9,8 +9,6 @@ import (
func (p *PriceIsWrong) apply(e priceiswrong.Event) error { func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
switch e := e.(type) { switch e := e.(type) {
case *priceiswrong.Host:
p.Host = e.ID
case *priceiswrong.Players: case *priceiswrong.Players:
p.Contestants = p.Contestants[:0] p.Contestants = p.Contestants[:0]
for _, id := range e.IDs { for _, id := range e.IDs {
@ -19,6 +17,14 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
if n := len(p.Contestants); n > 0 { if n := len(p.Contestants); n > 0 {
p.Host = p.Contestants[rand.Intn(n)].ID p.Host = p.Contestants[rand.Intn(n)].ID
} }
case *priceiswrong.Host:
p.Host = e.ID
case *priceiswrong.Score:
for i := range p.Contestants {
if p.Contestants[i].ID == e.ID {
p.Contestants[i].Score += e.Score
}
}
} }
return nil return nil
} }
@ -31,6 +37,10 @@ func (p *PriceIsWrong) SetHost(ctx context.Context, id int) error {
return p.upsertEvent(ctx, &priceiswrong.Host{ID: id}) return p.upsertEvent(ctx, &priceiswrong.Host{ID: id})
} }
func (p *PriceIsWrong) Score(ctx context.Context, id, score int) error {
return p.upsertEvent(ctx, &priceiswrong.Score{ID: id, Score: score})
}
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

@ -15,10 +15,16 @@ type Host struct {
ID int ID int
} }
type Score struct {
ID int
Score int
}
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{},
} }
t, err := event.Parse(b, typesToPointers) t, err := event.Parse(b, typesToPointers)
return typesToPointers[t], err return typesToPointers[t], err

View File

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

View File

@ -20,9 +20,23 @@ func TestPriceIsWrong(t *testing.T) {
t.Errorf("bad contestants: %+v", p.Contestants) t.Errorf("bad contestants: %+v", p.Contestants)
} else if p.Host < 4 || p.Host > 6 { } else if p.Host < 4 || p.Host > 6 {
t.Errorf("bad host: %+v", p.Host) t.Errorf("bad host: %+v", p.Host)
} else if err := p.SetHost(ctx, 5); err != nil { }
if err := p.SetHost(ctx, 5); err != nil {
t.Fatal(err) t.Fatal(err)
} else if p.Host != 5 { } else if p.Host != 5 {
t.Errorf("bad host: %+v", p.Host) t.Errorf("bad host: %+v", p.Host)
} }
if err := p.Score(ctx, 4, 9); err != nil {
t.Fatal(err)
} else if p.Contestants[0].Score != 9 {
t.Errorf("players changed order or didnt give 4 its points: %+v", p.Contestants)
} else if err := p.Score(ctx, 4, 9); err != nil {
t.Fatal(err)
} else if p.Contestants[0].Score != 18 {
t.Errorf("players changed order or didnt give 4 more points: %+v", p.Contestants)
} else if p.Contestants[1].Score != 0 {
t.Errorf("gave someone else points: %+v", p.Contestants)
}
} }