Add Discord event announcement functionality to backend
This commit introduces a new Discord event announcer to the backend, allowing for event announcements via Discord. It includes the addition of new environment variables for Discord configuration in `.env.example` and `compose.yaml`. The `main.go` file has been updated to initialize the announcer, and a new `discord` package has been created, containing the announcer logic and tests. Additionally, the service layer has been modified to support bracket draft management, enhancing the overall event workflow. Integration tests have been updated to ensure proper functionality of the new features.
This commit is contained in:
@@ -371,6 +371,40 @@ func (s *Store) ResetRoster(ctx context.Context, eventID string) error {
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) SaveBracketDraft(ctx context.Context, draft domain.BracketDraft, expectedVersion int) (domain.BracketDraft, error) {
|
||||
body, _ := json.Marshal(draft)
|
||||
if expectedVersion < 0 {
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO event_bracket_drafts(event_id,body,version,confirmed) VALUES($1,$2,$3,$4)
|
||||
ON CONFLICT(event_id) DO UPDATE SET body=excluded.body,version=excluded.version,confirmed=excluded.confirmed,updated_at=now()`,
|
||||
draft.EventID, body, draft.Version, draft.Confirmed)
|
||||
return draft, err
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `UPDATE event_bracket_drafts SET body=$2,version=$3,confirmed=$4,updated_at=now()
|
||||
WHERE event_id=$1 AND version=$5`, draft.EventID, body, draft.Version, draft.Confirmed, expectedVersion)
|
||||
if err == nil && tag.RowsAffected() == 0 {
|
||||
err = domain.ErrConflict
|
||||
}
|
||||
return draft, err
|
||||
}
|
||||
|
||||
func (s *Store) GetBracketDraft(ctx context.Context, eventID string) (domain.BracketDraft, error) {
|
||||
var body []byte
|
||||
var version int
|
||||
var confirmed bool
|
||||
var draft domain.BracketDraft
|
||||
err := s.pool.QueryRow(ctx, `SELECT body,version,confirmed FROM event_bracket_drafts WHERE event_id=$1`, eventID).Scan(&body, &version, &confirmed)
|
||||
if err == nil {
|
||||
err = json.Unmarshal(body, &draft)
|
||||
draft.Version, draft.Confirmed = version, confirmed
|
||||
}
|
||||
return draft, mapError(err)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteBracketDraft(ctx context.Context, eventID string) error {
|
||||
_, err := s.pool.Exec(ctx, `DELETE FROM event_bracket_drafts WHERE event_id=$1`, eventID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) StartScrim(ctx context.Context, event domain.Event, expectedVersion int, series []domain.Series, tournament *domain.Tournament) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -386,8 +420,8 @@ func (s *Store) StartScrim(ctx context.Context, event domain.Event, expectedVers
|
||||
}
|
||||
if tournament != nil {
|
||||
body, _ := json.Marshal(tournament)
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO tournaments(id,event_id,body) VALUES($1,$2,$3)`,
|
||||
tournament.ID, tournament.EventID, body); err != nil {
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO tournaments(id,event_id,body,version) VALUES($1,$2,$3,$4)`,
|
||||
tournament.ID, tournament.EventID, body, tournament.Version); err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
}
|
||||
@@ -600,8 +634,12 @@ func (s *Store) GetSeries(ctx context.Context, id string) (domain.Series, error)
|
||||
|
||||
func (s *Store) SaveTournament(ctx context.Context, t domain.Tournament) (domain.Tournament, error) {
|
||||
body, _ := json.Marshal(t)
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO tournaments(id,event_id,body) VALUES($1,$2,$3)
|
||||
ON CONFLICT(id) DO UPDATE SET body=excluded.body`, t.ID, t.EventID, body)
|
||||
tag, err := s.pool.Exec(ctx, `INSERT INTO tournaments(id,event_id,body,version) VALUES($1,$2,$3,$4)
|
||||
ON CONFLICT(id) DO UPDATE SET body=excluded.body,version=excluded.version
|
||||
WHERE excluded.version=0 OR tournaments.version=excluded.version-1`, t.ID, t.EventID, body, t.Version)
|
||||
if err == nil && tag.RowsAffected() == 0 {
|
||||
err = domain.ErrConflict
|
||||
}
|
||||
return t, err
|
||||
}
|
||||
|
||||
@@ -652,6 +690,24 @@ func (s *Store) hydrateTournament(ctx context.Context, tournament *domain.Tourna
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(tournament.Matches) > 0 {
|
||||
for index := range tournament.Matches {
|
||||
match := &tournament.Matches[index]
|
||||
if series, ok := live[match.SeriesID]; ok {
|
||||
match.TeamAID, match.TeamBID, match.WinnerTeamID = series.TeamAID, series.TeamBID, series.WinnerTeamID
|
||||
match.Series = &series
|
||||
} else if series, ok := live[match.ID]; ok {
|
||||
match.SeriesID = series.ID
|
||||
match.TeamAID, match.TeamBID, match.WinnerTeamID = series.TeamAID, series.TeamBID, series.WinnerTeamID
|
||||
match.Series = &series
|
||||
}
|
||||
}
|
||||
if err := tournament.Resolve(); err != nil {
|
||||
return err
|
||||
}
|
||||
tournament.BuildRounds()
|
||||
return nil
|
||||
}
|
||||
for round := range tournament.Rounds {
|
||||
for match := range tournament.Rounds[round] {
|
||||
if series, ok := live[tournament.Rounds[round][match].ID]; ok {
|
||||
|
||||
Reference in New Issue
Block a user