fix(multiplayer): advance tickets on allocation bind

This commit is contained in:
Josh Creek
2026-09-01 10:42:10 +01:00
parent 25fdc2c2c8
commit 013eb0778b
5 changed files with 41 additions and 24 deletions
+31 -19
View File
@@ -36,13 +36,27 @@ JOIN match_participants mp ON mp.ticket_id = q.ticket_id AND mp.player_id = q.pl
WHERE mp.match_id = $1
ORDER BY q.client_build`
const BindAllocatedMatchSQL = `UPDATE matches
SET server_id = $3
WHERE match_id = $1 AND state = 'ALLOCATING' AND allocation_id = $2 AND server_id IS NULL
AND EXISTS (
SELECT 1 FROM allocations
WHERE allocation_id = $2 AND match_id = $1 AND server_id = $3 AND state = 'ALLOCATED'
)`
const BindAllocatedMatchParticipantsSQL = `WITH bound AS (
UPDATE matches
SET server_id = $3
WHERE match_id = $1 AND state = 'ALLOCATING' AND allocation_id = $2 AND server_id IS NULL
AND EXISTS (
SELECT 1 FROM allocations
WHERE allocation_id = $2 AND match_id = $1 AND server_id = $3 AND state = 'ALLOCATED'
)
RETURNING match_id
), participants AS (
SELECT mp.ticket_id, mp.player_id
FROM match_participants mp
JOIN bound ON bound.match_id = mp.match_id
), advanced AS (
UPDATE queue_tickets q
SET state = 'ALLOCATING', revision = revision + 1
FROM participants p
WHERE q.ticket_id = p.ticket_id AND q.player_id = p.player_id AND q.state = 'ACCEPTED'
RETURNING q.ticket_id
)
SELECT (SELECT count(*) FROM participants), (SELECT count(*) FROM advanced)`
const ReleaseAllocatedMatchClaimSQL = `UPDATE matches
SET allocation_id = NULL, allocation_claimed_at = NULL
@@ -126,18 +140,16 @@ func BindAllocatedMatch(ctx context.Context, db *sql.DB, allocation domain.Alloc
if db == nil || allocation.MatchID == "" || allocation.AllocationID == "" || allocation.ServerID == "" || allocation.State != domain.ServerAllocated {
return fmt.Errorf("invalid allocated match binding")
}
result, err := db.ExecContext(ctx, BindAllocatedMatchSQL, allocation.MatchID, allocation.AllocationID, allocation.ServerID)
if err != nil {
return err
}
changed, err := result.RowsAffected()
if err != nil {
return err
}
if changed != 1 {
return domain.ErrConflict
}
return nil
return RunSerializable(ctx, db, DefaultSerializableAttempts, func(ctx context.Context, tx *sql.Tx) error {
var participants, advanced int
if err := tx.QueryRowContext(ctx, BindAllocatedMatchParticipantsSQL, allocation.MatchID, allocation.AllocationID, allocation.ServerID).Scan(&participants, &advanced); err != nil {
return err
}
if participants == 0 || participants != advanced {
return domain.ErrConflict
}
return nil
})
}
func ReleaseAllocatedMatchClaim(ctx context.Context, db *sql.DB, matchID, allocationID string) error {