.Score, .SetHost, .SetPlayers
parent
53ca14a1bb
commit
fa46313386
|
|
@ -9,8 +9,6 @@ import (
|
|||
|
||||
func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
|
||||
switch e := e.(type) {
|
||||
case *priceiswrong.Host:
|
||||
p.Host = e.ID
|
||||
case *priceiswrong.Players:
|
||||
p.Contestants = p.Contestants[:0]
|
||||
for _, id := range e.IDs {
|
||||
|
|
@ -19,6 +17,14 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
|
|||
if n := len(p.Contestants); n > 0 {
|
||||
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
|
||||
}
|
||||
|
|
@ -31,6 +37,10 @@ func (p *PriceIsWrong) SetHost(ctx context.Context, id int) error {
|
|||
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 {
|
||||
if err := upsertEvent(ctx, p.id, e); err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -15,10 +15,16 @@ type Host struct {
|
|||
ID int
|
||||
}
|
||||
|
||||
type Score struct {
|
||||
ID int
|
||||
Score int
|
||||
}
|
||||
|
||||
func ParseEvent(b []byte) (Event, error) {
|
||||
typesToPointers := map[string]any{
|
||||
"*priceiswrong.Players": &Players{},
|
||||
"*priceiswrong.Host": &Host{},
|
||||
"*priceiswrong.Score": &Score{},
|
||||
}
|
||||
t, err := event.Parse(b, typesToPointers)
|
||||
return typesToPointers[t], err
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ type PriceIsWrong struct {
|
|||
}
|
||||
|
||||
type Player struct {
|
||||
ID int
|
||||
ID int
|
||||
Score int
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, name string) (*PriceIsWrong, error) {
|
||||
|
|
|
|||
|
|
@ -20,9 +20,23 @@ func TestPriceIsWrong(t *testing.T) {
|
|||
t.Errorf("bad contestants: %+v", p.Contestants)
|
||||
} else if p.Host < 4 || p.Host > 6 {
|
||||
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)
|
||||
} else if p.Host != 5 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue