mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
fix(multiplayer): make match promotion replay lifecycle-safe
This commit is contained in:
@@ -45,7 +45,7 @@ const AcceptedMatchInsertSQL = `INSERT INTO matches
|
||||
VALUES ($1, $2, 'ALLOCATING', $3, $4, NULLIF($5, ''))
|
||||
ON CONFLICT (match_id) DO NOTHING`
|
||||
|
||||
const AcceptedMatchSelectSQL = `SELECT playlist, state, region, protocol_version, arena_path, server_id
|
||||
const AcceptedMatchSelectSQL = `SELECT playlist, region, protocol_version, arena_path
|
||||
FROM matches
|
||||
WHERE match_id = $1
|
||||
FOR UPDATE`
|
||||
@@ -99,6 +99,9 @@ func PromoteStoredAcceptedProposal(ctx context.Context, db *sql.DB, proposalID s
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return CreateMatchFromAcceptedProposal(ctx, db, plan, now)
|
||||
}
|
||||
|
||||
@@ -206,6 +209,9 @@ func acceptedProposalParticipants(ctx context.Context, tx *sql.Tx, plan Accepted
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(participants) != len(plan.Players) {
|
||||
return nil, fmt.Errorf("proposal participants do not match accepted plan")
|
||||
}
|
||||
@@ -218,14 +224,16 @@ func acceptedProposalParticipants(ctx context.Context, tx *sql.Tx, plan Accepted
|
||||
}
|
||||
|
||||
func verifyAcceptedMatchReplay(ctx context.Context, tx *sql.Tx, plan AcceptedMatchPlan, playlist domain.Playlist, tickets map[string]string) error {
|
||||
var existingPlaylist, state, region string
|
||||
var existingPlaylist, region string
|
||||
var protocol int
|
||||
var arenaPath sql.NullString
|
||||
var serverID sql.NullString
|
||||
if err := tx.QueryRowContext(ctx, AcceptedMatchSelectSQL, plan.MatchID).Scan(&existingPlaylist, &state, ®ion, &protocol, &arenaPath, &serverID); err != nil {
|
||||
if err := tx.QueryRowContext(ctx, AcceptedMatchSelectSQL, plan.MatchID).Scan(&existingPlaylist, ®ion, &protocol, &arenaPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if existingPlaylist != string(playlist) || state != string(domain.Allocating) || region != plan.Region || protocol != plan.Protocol || arenaPath.String != plan.ArenaPath || arenaPath.Valid != (plan.ArenaPath != "") || serverID.Valid {
|
||||
// Match state and server ownership are intentionally absent: allocation may
|
||||
// advance immediately after the first promotion commits. A retry after a
|
||||
// lost API response is valid whenever the immutable topology still matches.
|
||||
if existingPlaylist != string(playlist) || region != plan.Region || protocol != plan.Protocol || arenaPath.String != plan.ArenaPath || arenaPath.Valid != (plan.ArenaPath != "") {
|
||||
return domain.ErrConflict
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, AcceptedMatchParticipantsSQL, plan.MatchID)
|
||||
|
||||
Reference in New Issue
Block a user