mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
feat: enable guarded ranked matcher role
This commit is contained in:
@@ -22,9 +22,10 @@ import (
|
||||
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")
|
||||
playlist := flag.String("playlist", string(domain.Casual), "playlist to match")
|
||||
size := flag.Int("size", 4, "players per match")
|
||||
interval := flag.Duration("interval", time.Second, "poll interval")
|
||||
rankedRandomArena := flag.Bool("ranked-random-arena", false, "enable ranked matching only when the selected arena is random and non-elevated")
|
||||
redisAddr := flag.String("redis-addr", os.Getenv("COSMIC_CLASH_REDIS_ADDR"), "optional Redis candidate projection address")
|
||||
redisPrefix := flag.String("redis-prefix", envOrDefault("COSMIC_CLASH_REDIS_PREFIX", "cosmic-clash"), "Redis key prefix")
|
||||
redisTTL := flag.Duration("redis-ttl", 60*time.Second, "TTL for transient candidate projection entries")
|
||||
@@ -32,8 +33,15 @@ func main() {
|
||||
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)
|
||||
if *playlist != string(domain.Casual) && *playlist != string(domain.Ranked) {
|
||||
fatalf("unsupported playlist %q", *playlist)
|
||||
}
|
||||
selectedPlaylist := domain.Playlist(*playlist)
|
||||
if selectedPlaylist == domain.Ranked && *size != 6 {
|
||||
fatalf("ranked matching requires --size=6")
|
||||
}
|
||||
if selectedPlaylist == domain.Casual && *size < 2 || selectedPlaylist == domain.Casual && *size > 6 {
|
||||
fatalf("casual matching requires --size between 2 and 6")
|
||||
}
|
||||
if *redisTTL <= 0 {
|
||||
fatalf("--redis-ttl must be positive")
|
||||
@@ -60,7 +68,7 @@ func main() {
|
||||
candidateProjection := store.CandidateProjection{
|
||||
Index: store.RedisCandidateIndex{Client: redisClient, Prefix: *redisPrefix, TTL: *redisTTL},
|
||||
Source: func(ctx context.Context, at time.Time) ([]domain.Candidate, error) {
|
||||
return store.ListQueuedCandidates(ctx, db, domain.Casual, at, 1000)
|
||||
return store.ListQueuedCandidates(ctx, db, selectedPlaylist, at, 1000)
|
||||
},
|
||||
}
|
||||
projection = &candidateProjection
|
||||
@@ -88,9 +96,20 @@ func main() {
|
||||
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,
|
||||
Playlist: selectedPlaylist, 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) {
|
||||
if playlist == domain.Ranked {
|
||||
playerIDs := make([]string, 0, len(formation.Selection.Players))
|
||||
for _, player := range formation.Selection.Players {
|
||||
playerIDs = append(playerIDs, player.PlayerID)
|
||||
}
|
||||
participants, err := store.LoadRankedParticipants(context.Background(), db, playerIDs)
|
||||
if err != nil {
|
||||
return domain.PreparedProposal{}, err
|
||||
}
|
||||
return domain.PrepareProposal(id, playlist, formation, participants, domain.RankedArena{RandomEnabled: *rankedRandomArena}, at)
|
||||
}
|
||||
return domain.PrepareProposal(id, playlist, formation, nil, domain.RankedArena{}, at)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -44,6 +44,40 @@ WHERE state = 'QUEUED' AND playlist = $1 AND expires_at > $2
|
||||
ORDER BY enqueued_at, ticket_id
|
||||
LIMIT $3`
|
||||
|
||||
const RankedParticipantSQL = `SELECT player_id, steam_id
|
||||
FROM identities
|
||||
WHERE player_id = ANY($1)
|
||||
ORDER BY player_id`
|
||||
|
||||
// LoadRankedParticipants resolves the verified identity metadata required by
|
||||
// ranked admission. The caller must compare the returned set with the formed
|
||||
// candidate set; a partial lookup is not a valid ranked roster.
|
||||
func LoadRankedParticipants(ctx context.Context, db *sql.DB, playerIDs []string) ([]domain.RankedParticipant, error) {
|
||||
if db == nil || len(playerIDs) != 6 {
|
||||
return nil, fmt.Errorf("ranked admission requires six players")
|
||||
}
|
||||
rows, err := db.QueryContext(ctx, RankedParticipantSQL, playerIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
participants := make([]domain.RankedParticipant, 0, len(playerIDs))
|
||||
for rows.Next() {
|
||||
var participant domain.RankedParticipant
|
||||
if err := rows.Scan(&participant.PlayerID, &participant.SteamID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
participants = append(participants, participant)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(participants) != len(playerIDs) {
|
||||
return nil, fmt.Errorf("ranked identity metadata is incomplete")
|
||||
}
|
||||
return participants, nil
|
||||
}
|
||||
|
||||
// ListQueuedCandidates is an authoritative, expiry-filtered source for the
|
||||
// matcher projection. It deliberately does not claim rows; CreateProposal is
|
||||
// the transaction that performs the competing claim with SKIP LOCKED fences.
|
||||
|
||||
@@ -8,13 +8,14 @@ import (
|
||||
|
||||
func TestQueueSQLUsesDurableIdempotencyAndOwnerScopedRecovery(t *testing.T) {
|
||||
for query, fragments := range map[string][]string{
|
||||
QueueIdempotencyInsertSQL: {"idempotency_keys", "ON CONFLICT (scope, idempotency_key) DO NOTHING", "payload_digest"},
|
||||
QueueIdempotencySelectSQL: {"scope = $1", "idempotency_key = $2", "FOR UPDATE"},
|
||||
QueueTicketSelectSQL: {"ticket_id = $1", "player_id = $2"},
|
||||
QueueTicketInsertSQL: {"player_id", "playlist", "client_build", "protocol_version"},
|
||||
QueueTicketHeartbeatSQL: {"player_id = $2", "revision = $3", "expires_at > $4", "RETURNING"},
|
||||
QueueTicketCancelSQL: {"player_id = $2", "revision = $3", "state NOT IN", "RETURNING"},
|
||||
QueueIdempotencyInsertSQL: {"idempotency_keys", "ON CONFLICT (scope, idempotency_key) DO NOTHING", "payload_digest"},
|
||||
QueueIdempotencySelectSQL: {"scope = $1", "idempotency_key = $2", "FOR UPDATE"},
|
||||
QueueTicketSelectSQL: {"ticket_id = $1", "player_id = $2"},
|
||||
QueueTicketInsertSQL: {"player_id", "playlist", "client_build", "protocol_version"},
|
||||
QueueTicketHeartbeatSQL: {"player_id = $2", "revision = $3", "expires_at > $4", "RETURNING"},
|
||||
QueueTicketCancelSQL: {"player_id = $2", "revision = $3", "state NOT IN", "RETURNING"},
|
||||
QueueCandidateProjectionSQL: {"playlist = $1", "predicted_rtt", "expires_at > $2", "LIMIT $3"},
|
||||
RankedParticipantSQL: {"steam_id", "player_id = ANY($1)", "ORDER BY player_id"},
|
||||
} {
|
||||
for _, fragment := range fragments {
|
||||
if !contains(query, fragment) {
|
||||
@@ -24,6 +25,12 @@ func TestQueueSQLUsesDurableIdempotencyAndOwnerScopedRecovery(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRankedParticipantsRejectsNonSixPlayerLookupsWithoutDatabase(t *testing.T) {
|
||||
if _, err := LoadRankedParticipants(nil, nil, []string{"player-1"}); err == nil {
|
||||
t.Fatal("partial ranked identity lookup was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListQueuedCandidatesRejectsUnscopedOrUnboundedReads(t *testing.T) {
|
||||
now := time.Unix(1000, 0)
|
||||
for _, playlist := range []domain.Playlist{"", "invalid"} {
|
||||
|
||||
Reference in New Issue
Block a user