mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-16 03:52:03 +00:00
feat(multiplayer): dispatch completed result events
This commit is contained in:
@@ -35,6 +35,18 @@ WHERE published_at IS NULL AND event_type = 'proposal_changed'
|
||||
ORDER BY created_at, event_id
|
||||
LIMIT $1`
|
||||
|
||||
const OutboxUnpublishedResultSelectSQL = `SELECT event_id, aggregate_type, aggregate_id, revision,
|
||||
event_type, payload, created_at, published_at
|
||||
FROM outbox
|
||||
WHERE published_at IS NULL AND event_type = 'match_completed'
|
||||
ORDER BY created_at, event_id
|
||||
LIMIT $1`
|
||||
|
||||
const MatchParticipantIDsSQL = `SELECT player_id
|
||||
FROM match_participants
|
||||
WHERE match_id = $1
|
||||
ORDER BY player_id`
|
||||
|
||||
const OutboxMarkPublishedSQL = `UPDATE outbox
|
||||
SET published_at = $2
|
||||
WHERE event_id = $1 AND published_at IS NULL`
|
||||
@@ -106,6 +118,36 @@ func ReadUnpublishedProposalOutbox(ctx context.Context, db *sql.DB, limit int) (
|
||||
return readUnpublishedOutbox(ctx, db, limit, OutboxUnpublishedProposalSelectSQL)
|
||||
}
|
||||
|
||||
// ReadUnpublishedResultOutbox returns only durable match-completion events.
|
||||
// Proposal and result consumers acknowledge separate event types so one
|
||||
// transient fan-out outage cannot hide rows owned by another consumer.
|
||||
func ReadUnpublishedResultOutbox(ctx context.Context, db *sql.DB, limit int) ([]OutboxEvent, error) {
|
||||
return readUnpublishedOutbox(ctx, db, limit, OutboxUnpublishedResultSelectSQL)
|
||||
}
|
||||
|
||||
func ReadMatchParticipantIDs(ctx context.Context, db *sql.DB, matchID string) ([]string, error) {
|
||||
if db == nil || matchID == "" {
|
||||
return nil, fmt.Errorf("invalid match participant read arguments")
|
||||
}
|
||||
rows, err := db.QueryContext(ctx, MatchParticipantIDsSQL, matchID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var players []string
|
||||
for rows.Next() {
|
||||
var playerID string
|
||||
if err := rows.Scan(&playerID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
players = append(players, playerID)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return players, nil
|
||||
}
|
||||
|
||||
func readUnpublishedOutbox(ctx context.Context, db *sql.DB, limit int, query string) ([]OutboxEvent, error) {
|
||||
if db == nil || limit < 1 || limit > 1000 {
|
||||
return nil, fmt.Errorf("invalid outbox read arguments")
|
||||
|
||||
Reference in New Issue
Block a user