Skip to content

Commit

Permalink
chore: Updated button handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
renja-g committed Jul 26, 2024
1 parent 6da4c02 commit 396848a
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 197 deletions.
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func main() {
h.SlashCommand("/history", commands.HistoryHandler(b))
h.SlashCommand("/list", commands.ListHandler(b))

h.Component("/reshuffle_button", components.ReshuffleComponent)
h.Component("/start_match_button", components.StartMatchComponent)
h.Component("/team1_wins_button", components.SetWinnerComponent)
h.Component("/team2_wins_button", components.SetWinnerComponent)
h.Component("/cancel_match_button", components.CancelMatchComponent)
h.ButtonComponent("/reshuffle_button", components.ReshuffleComponent())
h.ButtonComponent("/start_match_button", components.StartMatchComponent())
h.ButtonComponent("/team1_wins_button", components.SetWinnerComponent())
h.ButtonComponent("/team2_wins_button", components.SetWinnerComponent())
h.ButtonComponent("/cancel_match_button", components.CancelMatchComponent())

b.SetupBot(h, bot.NewListenerFunc(b.OnReady))

Expand Down
60 changes: 31 additions & 29 deletions components/cancel_match_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@ import (
"github.com/renja-g/Barkeeper/utils"
)

func CancelMatchComponent(e *handler.ComponentEvent) error {
matches, err := utils.GetMatches()
if err != nil {
return err
}
func CancelMatchComponent() handler.ButtonComponentHandler {
return func(data discord.ButtonInteractionData, e *handler.ComponentEvent) error {
matches, err := utils.GetMatches()
if err != nil {
return err
}

matchID, err := utils.ParseMatchID(e.ComponentInteraction.Message)
if err != nil {
return err
}
matchID, err := utils.ParseMatchID(e.Message)
if err != nil {
return err
}

// Remove the match
newMatchArr := make([]constants.Match, 0)
for i, match := range matches {
if match.MatchID != *matchID {
newMatchArr = append(newMatchArr, matches[i])
// Remove the match
newMatchArr := make([]constants.Match, 0)
for i, match := range matches {
if match.MatchID != *matchID {
newMatchArr = append(newMatchArr, matches[i])
}
}

err = utils.SaveMatches(newMatchArr)
if err != nil {
return err
}
}

err = utils.SaveMatches(newMatchArr)
if err != nil {
return err
}

embed := e.ComponentInteraction.Message.Embeds[0]
embed.Title = "Match Cancelled"
embed.Color = 0xff0000
embed := e.Message.Embeds[0]
embed.Title = "Match Cancelled"
embed.Color = 0xff0000

return e.UpdateMessage(discord.NewMessageUpdateBuilder().
SetEmbeds(embed).
ClearContainerComponents().
Build(),
)
}
return e.UpdateMessage(discord.NewMessageUpdateBuilder().
SetEmbeds(embed).
ClearContainerComponents().
Build(),
)
}
}
66 changes: 34 additions & 32 deletions components/reshuffle_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,41 @@ import (
"github.com/renja-g/Barkeeper/utils"
)

func ReshuffleComponent(e *handler.ComponentEvent) error {
team1, team2 := utils.ParseTeamMessage(e.Message)
participantIds := append(team1, team2...)
participants := make([]*constants.Rating, 0)

ratings, err := utils.GetRatings()
if err != nil {
return err
}
func ReshuffleComponent() handler.ButtonComponentHandler {
return func(data discord.ButtonInteractionData, e *handler.ComponentEvent) error {
team1, team2 := utils.ParseTeamMessage(e.Message)
participantIds := append(team1, team2...)
participants := make([]*constants.Rating, 0)

ratings, err := utils.GetRatings()
if err != nil {
return err
}

for _, rating := range ratings {
for _, id := range participantIds {
if rating.UserID == *id {
ratingCopy := rating
participants = append(participants, &ratingCopy)
break
for _, rating := range ratings {
for _, id := range participantIds {
if rating.UserID == *id {
ratingCopy := rating
participants = append(participants, &ratingCopy)
break
}
}
}
}

newTeam1, newTeam2 := utils.GenerateTeams(participants)
newTeam1Rating, newTeam2Rating := utils.CalculateTeamRating(newTeam1), utils.CalculateTeamRating(newTeam2)

// Create the embed
embed := discord.NewEmbedBuilder().
SetTitle("Teams").
SetColor(0x3498db).
AddField(fmt.Sprintf("Blue (%d)", newTeam1Rating), utils.FormatTeam(newTeam1), false).
AddField(fmt.Sprintf("Red (%d)", newTeam2Rating), utils.FormatTeam(newTeam2), false).
SetFooter("Teams reshuffled", "").
Build()

return e.UpdateMessage(discord.MessageUpdate{
Embeds: &[]discord.Embed{embed},
})
}
newTeam1, newTeam2 := utils.GenerateTeams(participants)
newTeam1Rating, newTeam2Rating := utils.CalculateTeamRating(newTeam1), utils.CalculateTeamRating(newTeam2)

// Create the embed
embed := discord.NewEmbedBuilder().
SetTitle("Teams").
SetColor(0x3498db).
AddField(fmt.Sprintf("Blue (%d)", newTeam1Rating), utils.FormatTeam(newTeam1), false).
AddField(fmt.Sprintf("Red (%d)", newTeam2Rating), utils.FormatTeam(newTeam2), false).
SetFooter("Teams reshuffled", "").
Build()

return e.UpdateMessage(discord.MessageUpdate{
Embeds: &[]discord.Embed{embed},
})
}
}
134 changes: 68 additions & 66 deletions components/set_winner_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,88 @@ import (
"github.com/renja-g/Barkeeper/utils"
)

func SetWinnerComponent(e *handler.ComponentEvent) error {
winner := "team1"
if e.ComponentInteraction.Data.CustomID() == "team2_wins_button" {
winner = "team2"
}
func SetWinnerComponent() handler.ButtonComponentHandler {
return func(data discord.ButtonInteractionData, e *handler.ComponentEvent) error {
winner := "team1"
if e.ComponentInteraction.Data.CustomID() == "team2_wins_button" {
winner = "team2"
}

matches, err := utils.GetMatches()
if err != nil {
return err
}
matches, err := utils.GetMatches()
if err != nil {
return err
}

matchID, err := utils.ParseMatchID(e.ComponentInteraction.Message)
if err != nil {
return err
}
matchID, err := utils.ParseMatchID(e.ComponentInteraction.Message)
if err != nil {
return err
}

// Update the match with the winner
for i, match := range matches {
if match.MatchID == *matchID {
matches[i].Winner = winner
break
// Update the match with the winner
for i, match := range matches {
if match.MatchID == *matchID {
matches[i].Winner = winner
break
}
}
}

err = utils.SaveMatches(matches)
if err != nil {
return err
}
err = utils.SaveMatches(matches)
if err != nil {
return err
}

// Update the participants
team1Ptr, team2Ptr := utils.ParseTeamMessage(e.Message)
// Update the participants
team1Ptr, team2Ptr := utils.ParseTeamMessage(e.Message)

ratings, err := utils.GetRatings()
if err != nil {
return err
}
ratings, err := utils.GetRatings()
if err != nil {
return err
}

// Update the stats
for i, rating := range ratings {
for _, player := range team1Ptr {
if rating.UserID == *player {
if winner == "team1" {
ratings[i].Wins += 1
} else {
ratings[i].Losses += 1
// Update the stats
for i, rating := range ratings {
for _, player := range team1Ptr {
if rating.UserID == *player {
if winner == "team1" {
ratings[i].Wins += 1
} else {
ratings[i].Losses += 1
}
break
}
break
}
}
for _, player := range team2Ptr {
if rating.UserID == *player {
if winner == "team2" {
ratings[i].Wins += 1
} else {
ratings[i].Losses += 1
for _, player := range team2Ptr {
if rating.UserID == *player {
if winner == "team2" {
ratings[i].Wins += 1
} else {
ratings[i].Losses += 1
}
break
}
break
}
}
}

err = utils.SaveRatings(ratings)
if err != nil {
return err
}
err = utils.SaveRatings(ratings)
if err != nil {
return err
}

// Update the message with the winner
winnnerTeam := "Blue"
if winner == "team2" {
winnnerTeam = "Red"
}
// Update the message with the winner
winnnerTeam := "Blue"
if winner == "team2" {
winnnerTeam = "Red"
}

embed := e.ComponentInteraction.Message.Embeds[0]
embed.Title = "Match Finished"
embed.Description = winnnerTeam + " wins the match! 🎉"
embed.Color = 0x00ff00
embed := e.ComponentInteraction.Message.Embeds[0]
embed.Title = "Match Finished"
embed.Description = winnnerTeam + " wins the match! 🎉"
embed.Color = 0x00ff00

return e.UpdateMessage(discord.NewMessageUpdateBuilder().
SetEmbeds(embed).
ClearContainerComponents().
Build(),
)
}
return e.UpdateMessage(discord.NewMessageUpdateBuilder().
SetEmbeds(embed).
ClearContainerComponents().
Build(),
)
}
}
Loading

0 comments on commit 396848a

Please sign in to comment.