mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
72 lines
2.6 KiB
Go
72 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
|
"github.com/cosmic-clash/cosmic-clash/server/matcher"
|
|
"github.com/cosmic-clash/cosmic-clash/server/migrations"
|
|
"github.com/cosmic-clash/cosmic-clash/server/store"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
)
|
|
|
|
func main() {
|
|
dsn := flag.String("dsn", os.Getenv("COSMIC_CLASH_POSTGRES_DSN"), "PostgreSQL connection string")
|
|
migrationDir := flag.String("migrations", "migrations", "directory containing numbered SQL migrations")
|
|
playlist := flag.String("playlist", string(domain.Casual), "playlist to match; ranked requires a provider-enabled role")
|
|
size := flag.Int("size", 4, "players per match")
|
|
interval := flag.Duration("interval", time.Second, "poll interval")
|
|
flag.Parse()
|
|
if *dsn == "" {
|
|
fatalf("--dsn or COSMIC_CLASH_POSTGRES_DSN is required")
|
|
}
|
|
if *playlist != string(domain.Casual) {
|
|
fatalf("unsupported playlist %q; only casual is currently enabled", *playlist)
|
|
}
|
|
db, err := sql.Open("pgx", *dsn)
|
|
if err != nil {
|
|
fatalf("open PostgreSQL: %v", err)
|
|
}
|
|
defer db.Close()
|
|
startupCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
if err := db.PingContext(startupCtx); err != nil {
|
|
fatalf("ping PostgreSQL: %v", err)
|
|
}
|
|
if err := migrations.Apply(startupCtx, db, *migrationDir); err != nil {
|
|
fatalf("apply migrations: %v", err)
|
|
}
|
|
now := func() time.Time { return time.Now().UTC() }
|
|
worker := matcher.Worker{
|
|
Source: func(ctx context.Context, at time.Time, playlist domain.Playlist, limit int) ([]domain.Candidate, error) {
|
|
return store.ListQueuedCandidates(ctx, db, playlist, at, limit)
|
|
},
|
|
Creator: matcher.ProposalCreatorFunc(func(ctx context.Context, proposal domain.Proposal, ticketIDs map[string]string, at time.Time) error {
|
|
return store.CreateProposal(ctx, db, proposal, ticketIDs, at)
|
|
}),
|
|
Playlist: domain.Casual, Size: *size, Now: now,
|
|
NextID: func() string { return fmt.Sprintf("proposal-%d", time.Now().UnixNano()) },
|
|
Prepare: func(id string, playlist domain.Playlist, formation domain.MatchFormation, at time.Time) (domain.PreparedProposal, error) {
|
|
return domain.PrepareProposal(id, playlist, formation, nil, domain.RankedArena{}, at)
|
|
},
|
|
}
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
if err := worker.Run(ctx, *interval); err != nil && ctx.Err() == nil {
|
|
fatalf("matcher stopped: %v", err)
|
|
}
|
|
}
|
|
|
|
func fatalf(format string, args ...any) {
|
|
log.Printf("matcher: "+format, args...)
|
|
os.Exit(1)
|
|
}
|