src to pkg, impl lobby.Closed
This commit is contained in:
100
pkg/state/priceiswrong/events.go
Normal file
100
pkg/state/priceiswrong/events.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package priceiswrong
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitea/price-is-wrong/pkg/lib/db"
|
||||
priceiswrong "gitea/price-is-wrong/pkg/state/priceiswrong/internal"
|
||||
"html"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
|
||||
switch e := e.(type) {
|
||||
case *priceiswrong.Players:
|
||||
p.Contestants = p.Contestants[:0]
|
||||
for _, id := range e.IDs {
|
||||
p.Contestants = append(p.Contestants, Player{ID: id})
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
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)
|
||||
p.Item.Value = html.EscapeString(e.Value)
|
||||
case *priceiswrong.Guess:
|
||||
for i := range p.Contestants {
|
||||
if p.Contestants[i].ID == e.ID {
|
||||
p.Contestants[i].Guess = html.EscapeString(e.Guess)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PriceIsWrong) SetPlayers(ctx context.Context, ids []int) error {
|
||||
return p.upsertEvent(ctx, &priceiswrong.Players{IDs: ids})
|
||||
}
|
||||
|
||||
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) SetItem(ctx context.Context, imageURL, title, description, value string) error {
|
||||
return p.upsertEvent(ctx, &priceiswrong.Item{
|
||||
ImageURL: imageURL,
|
||||
Title: title,
|
||||
Description: description,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
p2, err := openID(ctx, p.id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*p = *p2
|
||||
return nil
|
||||
}
|
||||
|
||||
func upsertEvent(ctx context.Context, priceIsWrongID int, e priceiswrong.Event) error {
|
||||
b, err := priceiswrong.MarshalEvent(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.From(ctx).ExecContext(ctx, `
|
||||
INSERT INTO priceiswrong_events (priceiswrong_id, payload) VALUES (?, ?)
|
||||
`, priceIsWrongID, b)
|
||||
return err
|
||||
}
|
||||
49
pkg/state/priceiswrong/internal/event.go
Normal file
49
pkg/state/priceiswrong/internal/event.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package priceiswrong
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea/price-is-wrong/pkg/lib/event"
|
||||
)
|
||||
|
||||
type Event interface{}
|
||||
|
||||
type Players struct {
|
||||
IDs []int
|
||||
}
|
||||
|
||||
type Host struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
type Score struct {
|
||||
ID int
|
||||
Score int
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
ImageURL string
|
||||
Title string
|
||||
Description string
|
||||
Value 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
|
||||
}
|
||||
|
||||
func MarshalEvent(e Event) ([]byte, error) {
|
||||
return event.Serialize(fmt.Sprintf("%T", e), e)
|
||||
}
|
||||
118
pkg/state/priceiswrong/open.go
Normal file
118
pkg/state/priceiswrong/open.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package priceiswrong
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"gitea/price-is-wrong/pkg/lib/db"
|
||||
priceiswrong "gitea/price-is-wrong/pkg/state/priceiswrong/internal"
|
||||
)
|
||||
|
||||
type PriceIsWrong struct {
|
||||
id int
|
||||
Host int
|
||||
Contestants []Player
|
||||
Item priceiswrong.Item
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
ID int
|
||||
Score int
|
||||
Guess string
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, name string) (*PriceIsWrong, error) {
|
||||
if err := initialize(ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize priceiswrong: %w", err)
|
||||
}
|
||||
|
||||
if result, err := open(ctx, name); err != nil {
|
||||
return nil, err
|
||||
} else if result == nil {
|
||||
if err := create(ctx, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return mustOpen(ctx, name)
|
||||
}
|
||||
|
||||
func mustOpen(ctx context.Context, name string) (*PriceIsWrong, error) {
|
||||
result, err := open(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result == nil {
|
||||
return nil, fmt.Errorf("no priceiswrong found with name %s", name)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func open(ctx context.Context, name string) (*PriceIsWrong, error) {
|
||||
row := db.From(ctx).QueryRow(`SELECT id FROM priceiswrongs WHERE name=?`, name)
|
||||
if err := row.Err(); err != nil {
|
||||
return nil, fmt.Errorf("no priceiswrong found with name %s", name)
|
||||
}
|
||||
|
||||
var id sql.NullInt32
|
||||
if err := row.Scan(&id); err == sql.ErrNoRows || !id.Valid {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan id from priceiswrongs: %w", err)
|
||||
}
|
||||
|
||||
return openID(ctx, int(id.Int32))
|
||||
}
|
||||
|
||||
func openID(ctx context.Context, id int) (*PriceIsWrong, error) {
|
||||
rows, err := db.From(ctx).QueryContext(ctx, `
|
||||
SELECT payload
|
||||
FROM priceiswrong_events
|
||||
WHERE priceiswrong_events.priceiswrong_id=?
|
||||
ORDER BY id
|
||||
`, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query event payloads for id %d: %w", id, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := PriceIsWrong{id: id}
|
||||
for rows.Next() {
|
||||
var b []byte
|
||||
if err := rows.Scan(&b); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan event: %w", err)
|
||||
}
|
||||
event, err := priceiswrong.ParseEvent(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse event: %w", err)
|
||||
}
|
||||
if err := result.apply(event); err != nil {
|
||||
return nil, fmt.Errorf("failed to apply event %s: %w", b, err)
|
||||
}
|
||||
}
|
||||
|
||||
return &result, rows.Err()
|
||||
}
|
||||
|
||||
func create(ctx context.Context, name string) error {
|
||||
_, err := db.From(ctx).ExecContext(ctx, `
|
||||
INSERT INTO priceiswrongs (name) VALUES (?)
|
||||
`, name)
|
||||
return err
|
||||
}
|
||||
|
||||
func initialize(ctx context.Context) error {
|
||||
_, err := db.From(ctx).ExecContext(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS priceiswrongs (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS priceiswrong_events (
|
||||
id INTEGER PRIMARY KEY,
|
||||
priceiswrong_id NUMBER,
|
||||
payload TEXT,
|
||||
FOREIGN KEY (priceiswrong_id) REFERENCES priceiswrongs (id)
|
||||
);
|
||||
`)
|
||||
return err
|
||||
}
|
||||
62
pkg/state/priceiswrong/priceiswrong_test.go
Normal file
62
pkg/state/priceiswrong/priceiswrong_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package priceiswrong_test
|
||||
|
||||
import (
|
||||
"gitea/price-is-wrong/pkg/lib"
|
||||
"gitea/price-is-wrong/pkg/state/priceiswrong"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPriceIsWrong(t *testing.T) {
|
||||
ctx := lib.NewTestCtx(t)
|
||||
|
||||
p, err := priceiswrong.Open(ctx, "name")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := p.SetPlayers(ctx, []int{4, 5, 6}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(p.Contestants) != 3 {
|
||||
t.Errorf("bad contestants: %+v", p.Contestants)
|
||||
} else if p.Host < 4 || p.Host > 6 {
|
||||
t.Errorf("bad host: %+v", p.Host)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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", "d"); 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", "d"); 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user