Skip to content

Commit

Permalink
check if tmt2 match already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxW1337 committed Mar 21, 2024
1 parent 3516e10 commit 219d92e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/server/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func (w *Worker) processMatch(ctx context.Context, match *database.Match) error
slog.Error("error creating tmt2 match", "Error", err)
return err
}
slog.Info("created tmt2 match", "Match", match.MatchID, "Response", *createMatchResponse.JSON201)
slog.Info("created tmt2 match", "Match", match.MatchID, "Response", *createMatchResponse)

match.JobState = models.JOB_STATE_IN_PROGRESS
match.TMT2MatchId = createMatchResponse.JSON201.Id
match.TMT2MatchId = createMatchResponse.Id
_, err = w.dbClient.UpdateMatch(ctx, match)
if err != nil {
slog.Error("error updating match", "Error", err)
Expand All @@ -124,9 +124,45 @@ func (w *Worker) processMatch(ctx context.Context, match *database.Match) error
return nil
}

func (w *Worker) createTMT2Match(ctx context.Context, match *database.Match) (*tmt2_go.CreateMatchResponse, error) {
func (w *Worker) createTMT2Match(ctx context.Context, match *database.Match) (*tmt2_go.IMatch, error) {
slog.Debug("createTMT2Match", "Match", match.MatchID)

// check if match already exists
existingResponse, err := w.tmt2Client.GetMatchByExternalId(ctx, match.MatchID)
if err != nil {
slog.Error("error checking for existing tmt2 match", "Error", err)
}
if existingResponse != nil {
slog.Debug("tmt2 match already exists", "Match", match.MatchID)
return &tmt2_go.IMatch{
CanClinch: existingResponse.CanClinch,
CreatedAt: existingResponse.CreatedAt,
CurrentMap: existingResponse.CurrentMap,
Election: existingResponse.Election,
ElectionSteps: existingResponse.ElectionSteps,
GameServer: existingResponse.GameServer,
Id: existingResponse.Id,
IsStopped: existingResponse.IsStopped,
LogSecret: existingResponse.LogSecret,
Logs: existingResponse.Logs,
MapPool: existingResponse.MapPool,
MatchEndAction: existingResponse.MatchEndAction,
MatchMaps: existingResponse.MatchMaps,
Mode: existingResponse.Mode,
ParseIncomingLogs: existingResponse.ParseIncomingLogs,
Passthrough: existingResponse.Passthrough,
Players: existingResponse.Players,
RconCommands: existingResponse.RconCommands,
ServerPassword: existingResponse.ServerPassword,
State: existingResponse.State,
TeamA: existingResponse.TeamA,
TeamB: existingResponse.TeamB,
TmtLogAddress: existingResponse.TmtLogAddress,
TmtSecret: existingResponse.TmtSecret,
WebhookUrl: existingResponse.WebhookUrl,
}, nil
}

response, err := w.tmt2Client.CreateMatch(ctx, &match.MatchInfo)
if err != nil {
slog.Error("error creating tmt2 match", "Error", err)
Expand All @@ -139,7 +175,7 @@ func (w *Worker) createTMT2Match(ctx context.Context, match *database.Match) (*t
return nil, errors.New("error creating tmt2 match: " + response.Status())
}

return response, nil
return response.JSON201, nil
}

func (w *Worker) deleteTMT2Match(ctx context.Context, match *database.Match) error {
Expand Down
22 changes: 22 additions & 0 deletions src/tmt2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/GSH-LAN/Unwindia_common/src/go/matchservice"
"github.com/GSH-LAN/Unwindia_common/src/go/template"
tmt2_go "github.com/GSH-LAN/Unwindia_tmt2/pkg/tmt2-go"
"k8s.io/utils/ptr"
"log/slog"
"net/http"
"time"
Expand Down Expand Up @@ -120,6 +121,27 @@ func (t *TMT2ClientImpl) GetMatch(ctx context.Context, matchID string) (*tmt2_go
return t.tmt2Client.GetMatchWithResponse(ctx, matchID)
}

// GetMatchByExternalId returns current match state from TMT2 by external id
func (t *TMT2ClientImpl) GetMatchByExternalId(ctx context.Context, externalID string) (*tmt2_go.IMatchResponse, error) {
passthrough := []string{externalID}

allmatches, err := t.tmt2Client.GetAllMatchesWithResponse(ctx, &tmt2_go.GetAllMatchesParams{
Passthrough: &passthrough,
IsLive: ptr.To(true),
})
if err != nil {
return nil, err
}

if allmatches.JSON200 == nil || len(*allmatches.JSON200) == 0 {
return nil, fmt.Errorf("no match found with external id %s", externalID)
}

matches := *allmatches.JSON200

return &matches[0], nil
}

// DeleteMatch deletes a match from TMT2
func (t *TMT2ClientImpl) DeleteMatch(ctx context.Context, matchID string) error {
_, err := t.tmt2Client.DeleteMatchWithResponse(ctx, matchID)
Expand Down

0 comments on commit 219d92e

Please sign in to comment.