/
button_lfg_dungeon_mode_story.go
155 lines (133 loc) · 5.11 KB
/
button_lfg_dungeon_mode_story.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package message_component
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"cloud.google.com/go/pubsub"
"github.com/bwmarrin/discordgo"
_const "github.com/rmb938/gw2groups/discord/const"
gw2Api "github.com/rmb938/gw2groups/pkg/api_clients/gw2"
playfab2 "github.com/rmb938/gw2groups/pkg/api_clients/playfab"
"github.com/rmb938/gw2groups/playfab"
"k8s.io/utils/pointer"
)
type ButtonLFGDungeonModeStory struct{}
func (c *ButtonLFGDungeonModeStory) Handle(ctx context.Context, session *discordgo.Session, interaction *discordgo.Interaction, data discordgo.MessageComponentInteractionData) error {
playFabClient := playfab2.NewPlayFabClient()
loginResponse, err := playFabClient.LoginWithCustomID(ctx, &playfab2.ServerLoginWithCustomIDRequest{
ServerCustomId: interaction.User.ID,
InfoRequestParameters: &playfab2.PlayerCombinedInfoRequestParams{
GetUserData: pointer.Bool(true),
},
})
if err != nil {
if err != nil {
return fmt.Errorf("error logging in with playfab customid: %w", err)
}
}
var dungeonsList []string
queuedDungeons := make([]string, 0)
err = json.Unmarshal([]byte(loginResponse.InfoResultPayload.UserData["lfg_dungeon"].Value), &queuedDungeons)
if err != nil {
return fmt.Errorf("error parsing lfg_dungeon from player: %w", err)
}
for _, id := range _const.DungeonIDs {
for _, selectedOption := range queuedDungeons {
if id == selectedOption {
dungeonsList = append(dungeonsList, _const.DungeonsIDsToName[id])
}
}
}
_, err = playFabClient.UpdateUserData(ctx, loginResponse.PlayFabId, &playfab2.ServerUpdateUserDataRequest{
Data: map[string]string{
"lfg_dungeon_mode": "story",
},
})
if err != nil {
return fmt.Errorf("error updating playfab user data: %w", err)
}
// See https://learn.microsoft.com/en-us/gaming/playfab/features/multiplayer/matchmaking/config-examples#hostsearcher-or-role-based-requirements when doing Raids
// Specifically the "Games may have role requirements" part for selecting healer, support, tank, ect.. (quickness, alacrity, ect..)
gw2Client := gw2Api.NewGW2APIClient(loginResponse.InfoResultPayload.UserData["gw2-api-key"].Value)
gw2Account, err := gw2Client.GetAccount(ctx)
if err != nil {
// TODO: what if gw2 api key is invalid
return fmt.Errorf("error getting gw2 account: %w", err)
}
gw2WorldId := gw2Account.World
gw2WorldLocation := gw2WorldId
for gw2WorldLocation >= 10 {
gw2WorldLocation = gw2WorldLocation / 10
}
// TODO: some sort of keep-alive
// if we want pure 100% discord it'll need to be a button the user
// clicks every now and then
// but there won't be a way to tell them "their time is running out"
// We also need something to check if their ticket has given up automatically
// unsure how to do that without having a server constantly running and checking :(
// to do this effectively we would need to spin up something for every match making request
// TODO: start helper method to do match making ticket
_, err = playFabClient.UpdateUserData(ctx, loginResponse.PlayFabId, &playfab2.ServerUpdateUserDataRequest{
Data: map[string]string{
"lfg_last_ack_time": strconv.FormatInt(time.Now().Unix(), 10),
},
})
if err != nil {
return fmt.Errorf("error updating playfab user data: %w", err)
}
matchMakingTicketResponse, err := playFabClient.CreateMatchMakingTicket(ctx, loginResponse.EntityToken, &playfab2.CreateMatchMakingTicketRequest{
Creator: playfab2.MatchmakingPlayer{
Entity: loginResponse.EntityToken.Entity,
Attributes: playfab2.MatchmakingPlayerAttributes{
DataObject: map[string]interface{}{
"lfg_world_location": strconv.Itoa(gw2WorldLocation),
"lfg_player_count": 1, // used for playerCount expansion rule
},
},
},
GiveUpAfterSeconds: 3599,
QueueName: "dungeons",
})
if err != nil {
return fmt.Errorf("error creating matchmaking ticket: %w", err)
}
// TODO: end helper method to do match making ticket
_, err = session.FollowupMessageEdit(interaction, interaction.Message.ID, &discordgo.WebhookEdit{
Content: pointer.String(fmt.Sprintf("> Category: **Dungeons**\n> Dungeon: **%s**\n> Mode: **Story** __*When in story mode you are expected to watch all cutscenes and be beginner friendly*__\n \nNow matchmaking for Dungeons", strings.Join(dungeonsList, ", "))),
Components: &[]discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Stop Matchmaking",
CustomID: "button_reset_lfg_selection",
Style: discordgo.DangerButton,
},
},
},
},
})
if err != nil {
return err
}
message := &playfab.MatchMakingTicketMessage{
QueueName: "dungeons",
TicketId: matchMakingTicketResponse.TicketId,
}
messageBytes, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("error marshaling MatchMakingTicketMessage: %w", err)
}
pubsubTopic := _const.GetPubsubTopic(ctx, os.Getenv("PUBSUB_PLAYFAB_MATCHMAKING_TICKETS_TOPIC_ID"))
result := pubsubTopic.Publish(ctx, &pubsub.Message{
Data: messageBytes,
})
_, err = result.Get(ctx)
if err != nil {
return fmt.Errorf("error publishing message: %w", err)
}
return nil
}