no need for fsm since i didnt even fsm

main
Bel LaPointe 2025-02-12 16:21:58 -07:00
parent 56f994c837
commit 1f6b79aa3b
13 changed files with 14 additions and 192 deletions

View File

@ -1,46 +0,0 @@
package template
import (
"context"
"gitea/price-is-wrong/src/lib/db"
template "gitea/price-is-wrong/src/state/fsm/template/internal"
"io"
)
func (t *Template) TODO(ctx context.Context, id int) error {
return io.EOF
}
func (t *Template) upsertEvent(ctx context.Context, e template.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, templateID int, e template.Event) error {
b, err := template.MarshalEvent(e)
if err != nil {
return err
}
_, err = db.From(ctx).ExecContext(ctx, `
INSERT INTO template_events (template_id, payload) VALUES (?, ?)
`, templateID, b)
return err
}
func (t *Template) apply(e template.Event) error {
switch e := e.(type) {
case *int:
_ = e
}
return nil
}

View File

@ -1,20 +0,0 @@
package template
import (
"fmt"
"gitea/price-is-wrong/src/lib/event"
)
type Event interface{}
func ParseEvent(b []byte) (Event, error) {
typesToPointers := map[string]any{
"*any": &b,
}
t, err := event.Parse(b, typesToPointers)
return typesToPointers[t], err
}
func MarshalEvent(e Event) ([]byte, error) {
return event.Serialize(fmt.Sprintf("%T", e), e)
}

View File

@ -1,115 +0,0 @@
package template
import (
"context"
"database/sql"
"fmt"
"gitea/price-is-wrong/src/lib/db"
template "gitea/price-is-wrong/src/state/fsm/template/internal"
)
type Template struct {
id int
Host int
Contestants Player
}
type Player struct {
ID int
}
func Open(ctx context.Context, name string) (*Template, error) {
if err := initialize(ctx); err != nil {
return nil, fmt.Errorf("failed to initialize template: %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) (*Template, error) {
result, err := open(ctx, name)
if err != nil {
return nil, err
}
if result == nil {
return nil, fmt.Errorf("no template found with name %s", name)
}
return result, nil
}
func open(ctx context.Context, name string) (*Template, error) {
row := db.From(ctx).QueryRow(`SELECT id FROM templates WHERE name=?`, name)
if err := row.Err(); err != nil {
return nil, fmt.Errorf("no template 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 templates: %w", err)
}
return openID(ctx, int(id.Int32))
}
func openID(ctx context.Context, id int) (*Template, error) {
rows, err := db.From(ctx).QueryContext(ctx, `
SELECT payload
FROM template_events
WHERE template_events.template_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 := Template{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 := template.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 templates (name) VALUES (?)
`, name)
return err
}
func initialize(ctx context.Context) error {
_, err := db.From(ctx).ExecContext(ctx, `
CREATE TABLE IF NOT EXISTS templates (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE IF NOT EXISTS template_events (
id INTEGER PRIMARY KEY,
template_id NUMBER,
payload TEXT,
FOREIGN KEY (template_id) REFERENCES templates (id)
);
`)
return err
}

View File

@ -1,3 +1,3 @@
package fsm package state
//go:generate cp -r ./.template ./template //go:generate cp -r ./.template ./template

View File

@ -3,7 +3,7 @@ package lobby
import ( import (
"context" "context"
"gitea/price-is-wrong/src/lib/db" "gitea/price-is-wrong/src/lib/db"
lobby "gitea/price-is-wrong/src/state/fsm/lobby/internal" lobby "gitea/price-is-wrong/src/state/lobby/internal"
"slices" "slices"
) )

View File

@ -2,7 +2,7 @@ package lobby_test
import ( import (
"fmt" "fmt"
lobby "gitea/price-is-wrong/src/state/fsm/lobby/internal" lobby "gitea/price-is-wrong/src/state/lobby/internal"
"testing" "testing"
) )

View File

@ -2,7 +2,7 @@ package lobby_test
import ( import (
"gitea/price-is-wrong/src/lib" "gitea/price-is-wrong/src/lib"
"gitea/price-is-wrong/src/state/fsm/lobby" "gitea/price-is-wrong/src/state/lobby"
"testing" "testing"
) )

View File

@ -5,7 +5,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"gitea/price-is-wrong/src/lib/db" "gitea/price-is-wrong/src/lib/db"
lobby "gitea/price-is-wrong/src/state/fsm/lobby/internal" lobby "gitea/price-is-wrong/src/state/lobby/internal"
) )
type Lobby struct { type Lobby struct {

View File

@ -3,7 +3,7 @@ package priceiswrong
import ( import (
"context" "context"
"gitea/price-is-wrong/src/lib/db" "gitea/price-is-wrong/src/lib/db"
priceiswrong "gitea/price-is-wrong/src/state/fsm/priceiswrong/internal" priceiswrong "gitea/price-is-wrong/src/state/priceiswrong/internal"
"html" "html"
"math/rand" "math/rand"
) )
@ -33,6 +33,7 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
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)
p.Item.Value = html.EscapeString(e.Value)
case *priceiswrong.Guess: case *priceiswrong.Guess:
for i := range p.Contestants { for i := range p.Contestants {
if p.Contestants[i].ID == e.ID { if p.Contestants[i].ID == e.ID {
@ -55,11 +56,12 @@ func (p *PriceIsWrong) Score(ctx context.Context, id, score int) error {
return p.upsertEvent(ctx, &priceiswrong.Score{ID: id, Score: score}) return p.upsertEvent(ctx, &priceiswrong.Score{ID: id, Score: score})
} }
func (p *PriceIsWrong) SetItem(ctx context.Context, imageURL, title, description string) error { func (p *PriceIsWrong) SetItem(ctx context.Context, imageURL, title, description, value string) error {
return p.upsertEvent(ctx, &priceiswrong.Item{ return p.upsertEvent(ctx, &priceiswrong.Item{
ImageURL: imageURL, ImageURL: imageURL,
Title: title, Title: title,
Description: description, Description: description,
Value: value,
}) })
} }

View File

@ -24,6 +24,7 @@ type Item struct {
ImageURL string ImageURL string
Title string Title string
Description string Description string
Value string
} }
type Guess struct { type Guess struct {

View File

@ -5,7 +5,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"gitea/price-is-wrong/src/lib/db" "gitea/price-is-wrong/src/lib/db"
priceiswrong "gitea/price-is-wrong/src/state/fsm/priceiswrong/internal" priceiswrong "gitea/price-is-wrong/src/state/priceiswrong/internal"
) )
type PriceIsWrong struct { type PriceIsWrong struct {

View File

@ -2,7 +2,7 @@ package priceiswrong_test
import ( import (
"gitea/price-is-wrong/src/lib" "gitea/price-is-wrong/src/lib"
"gitea/price-is-wrong/src/state/fsm/priceiswrong" "gitea/price-is-wrong/src/state/priceiswrong"
"testing" "testing"
) )
@ -46,11 +46,11 @@ func TestPriceIsWrong(t *testing.T) {
t.Errorf("guess didnt persist: %+v", p.Contestants) 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", "d"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if p.Contestants[0].Guess != "" { } else if p.Contestants[0].Guess != "" {
t.Errorf("set item didnt clear guesses") 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", "d"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if p.Item.ImageURL != "imageURL" { } else if p.Item.ImageURL != "imageURL" {
t.Errorf("wrong image url: %s", p.Item.ImageURL) t.Errorf("wrong image url: %s", p.Item.ImageURL)