Ocean
Multiplayer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_MULTIPLAYER_H
9 #define META_OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_MULTIPLAYER_H
10 
13 
15 #include "ocean/base/Singleton.h"
16 
17 #include <OVR_Platform.h>
18 
19 namespace Ocean
20 {
21 
22 namespace Platform
23 {
24 
25 namespace Meta
26 {
27 
28 namespace Quest
29 {
30 
31 namespace PlatformSDK
32 {
33 
34 /**
35  * This class implements multi-player functionalities based on GroupPresence.
36  * @ingroup platformmetaquestplatformsdk
37  */
38 class OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_EXPORT Multiplayer :
39  public Singleton<Multiplayer>,
40  protected MessageHandler
41 {
42  friend class Singleton<Multiplayer>;
43 
44  public:
45 
46  /**
47  * Definition of a subscription object for an active GroupPresence configuration.
48  */
50 
51  /**
52  * This class holds the relevant information of a received join intent.
53  */
54  class JoinIntent
55  {
56  public:
57 
58  /**
59  * Default constructor.
60  */
61  JoinIntent() = default;
62 
63  /**
64  * Creates a new join intent object.
65  * @param deepLink The deep link of the join intent, can be empty
66  * @param destinationApiName The destination API name of the join intent, can be empty
67  * @param lobbySessionId The id of the lobby session of the join intent, can be empty
68  * @param matchSessionId The id of the match session of the join intent, can be empty
69  */
70  inline JoinIntent(std::string&& deepLink, std::string&& destinationApiName, std::string&& lobbySessionId, std::string&& matchSessionId);
71 
72  public:
73 
74  /// The deep link of the join intent, can be empty.
75  std::string deepLink_;
76 
77  /// The destination API name of the join intent, can be empty.
78  std::string destinationApiName_;
79 
80  /// The id of the lobby session of the join intent, can be empty.
81  std::string lobbySessionId_;
82 
83  /// The id of the match session of the join intent, can be empty.
84  std::string matchSessionId_;
85  };
86 
87  public:
88 
89  /**
90  * Ensures that the mulit-player functionalities are initialized.
91  */
93 
94  /**
95  * Sets the current GroupPresence and activates it (sets it as joinable).
96  * The group presence will be set and will be active until the scoped group presence object of the resulting future exists.<br>
97  * Only one active group presence can be set at the same time.
98  * @param destinationApiName The name of the destination API, must be valid
99  * @param lobbySessionId The lobby session id to set, can be empty
100  * @param matchSessionId The lobby session id to set, can be empty
101  */
102  [[nodiscard]] std::future<ScopedGroupPresence> setActiveGroupPresence(const std::string& destinationApiName, const std::string& lobbySessionId, const std::string& matchSessionId = std::string());
103 
104  /**
105  * Requests a list of invitable users.
106  * @return A future object which will provide the ids of all invitable users, if successful
107  */
108  std::future<Indices64> invitableUsers();
109 
110  /**
111  * Returns whether a latest result for the invitableUsers() function is available.
112  * @param result The result if available
113  * @return True, if so
114  */
115  inline bool hasLatestInvitableUsersResult(Indices64& result);
116 
117  /**
118  * Returns whether a joint intent has been received.
119  * @param joinIntent The resulting join intent object holding the relevant information
120  * @return True, if so
121  */
122  inline bool hasLatestJointIntent(JoinIntent& joinIntent);
123 
124  /**
125  * Invites several users.
126  * @param userIds The ids of the user to invite
127  * @return True, if succeeded
128  */
129  bool inviteUsers(const Indices64& userIds);
130 
131  /**
132  * Launches the panel to invite users.
133  * @return A future object which will provide whether at least one invite has been sent, if successful
134  */
135  std::future<bool> launchInvitePanel();
136 
137  /**
138  * Launches the roster panel showing all users with same group presence configuration.
139  * @return True, if succeeded
140  */
142 
143  protected:
144 
145  /**
146  * Creates a new object.
147  */
149 
150  /**
151  * The response function for setGroupPresence().
152  * @param message The response message, must be valid
153  * @param succeeded True, if the request succeeded
154  */
155  void onSetGroupPresence(ovrMessage* message, const bool succeeded);
156 
157  /**
158  * The response function for ovr_GroupPresence_Clear().
159  * @param message The response message, must be valid
160  * @param succeeded True, if the request succeeded
161  */
162  void onClearGroupPresence(ovrMessage* message, const bool succeeded);
163 
164  /**
165  * The response function for invitableUsers().
166  * @param message The response message, must be valid
167  * @param succeeded True, if the request succeeded
168  */
169  void onInvitableUsers(ovrMessage* message, const bool succeeded);
170 
171  /**
172  * The response function for inviteUsers().
173  * @param message The response message, must be valid
174  * @param succeeded True, if the request succeeded
175  */
176  void onInviteUsers(ovrMessage* message, const bool succeeded);
177 
178  /**
179  * The response function for launchInvitePanel().
180  * @param message The response message, must be valid
181  * @param succeeded True, if the request succeeded
182  */
183  void onLaunchInvitePanel(ovrMessage* message, const bool succeeded);
184 
185  /**
186  * The response function for launchRosterPanel().
187  * @param message The response message, must be valid
188  * @param succeeded True, if the request succeeded
189  */
190  void onLaunchRosterPanel(ovrMessage* message, const bool succeeded);
191 
192  /**
193  * The event function for notifications.
194  * @param message The response message, must be valid
195  * @param succeeded True, if the request succeeded
196  */
197  void onNotification(ovrMessage* message, const bool succeeded);
198 
199  /**
200  * The event function for join event received notifications.
201  * @param message The response message, must be valid
202  * @param succeeded True, if the request succeeded
203  */
204  void onNotificationJoinIntentReceived(ovrMessage* message, const bool succeeded);
205 
206  /**
207  * The event function called when the group presence subscription needs to be released.
208  * @param unusedValue An unused value
209  */
210  void onReleaseGroupPresenceSubscription(const bool& unusedValue);
211 
212  protected:
213 
214  /// The subscription objects for all messages.
216 
217  /// The result queue for invitableUsers().
219 
220  /// The promise for an active group presence configuration.
221  std::promise<ScopedGroupPresence> setGroupPresenceRequestPromise_;
222 
223  /// The promise for launch invite panel responses.
224  std::optional<std::promise<bool>> launchInvitePanelRequestPromise_;
225 
226  /// True, if currently a group presence configuration is active.
227  bool groupPresenceIsActive_ = false;
228 
229  /// The lock for group presence configurations.
231 
232  /// The result object for join intent notifications.
234 };
235 
236 inline Multiplayer::JoinIntent::JoinIntent(std::string&& deepLink, std::string&& destinationApiName, std::string&& lobbySessionId, std::string&& matchSessionId) :
237  deepLink_(std::move(deepLink)),
238  destinationApiName_(std::move(destinationApiName)),
239  lobbySessionId_(std::move(lobbySessionId)),
240  matchSessionId_(std::move(matchSessionId))
241 {
242  // nothing to do here
243 }
244 
246 {
247  return invitableUsersRequestQueue_.latestResponse(result);
248 }
249 
251 {
252  return joinIntentObject_.latestResponse(joinIntent);
253 }
254 
255 }
256 
257 }
258 
259 }
260 
261 }
262 
263 }
264 
265 #endif // META_OCEAN_PLATFORM_META_QUEST_PLATFORMSDK_MULTIPLAYER_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a helper object allowing to store responses for requests.
Definition: MessageHandler.h:117
This class implements a helper object allowing to queue responses of requests.
Definition: MessageHandler.h:162
This class is the base class for all objects needing to handle with messages.
Definition: MessageHandler.h:40
std::vector< MessageScopedSubscription > MessageScopedSubscriptions
Definition of a vector holding MessageScopedSubscription objects.
Definition: MessageHandler.h:220
This class holds the relevant information of a received join intent.
Definition: Multiplayer.h:55
std::string destinationApiName_
The destination API name of the join intent, can be empty.
Definition: Multiplayer.h:78
std::string matchSessionId_
The id of the match session of the join intent, can be empty.
Definition: Multiplayer.h:84
std::string deepLink_
The deep link of the join intent, can be empty.
Definition: Multiplayer.h:75
std::string lobbySessionId_
The id of the lobby session of the join intent, can be empty.
Definition: Multiplayer.h:81
This class implements multi-player functionalities based on GroupPresence.
Definition: Multiplayer.h:41
std::future< Indices64 > invitableUsers()
Requests a list of invitable users.
RequestObject< JoinIntent > joinIntentObject_
The result object for join intent notifications.
Definition: Multiplayer.h:233
bool hasLatestJointIntent(JoinIntent &joinIntent)
Returns whether a joint intent has been received.
Definition: Multiplayer.h:250
bool hasLatestInvitableUsersResult(Indices64 &result)
Returns whether a latest result for the invitableUsers() function is available.
Definition: Multiplayer.h:245
void onInvitableUsers(ovrMessage *message, const bool succeeded)
The response function for invitableUsers().
void onReleaseGroupPresenceSubscription(const bool &unusedValue)
The event function called when the group presence subscription needs to be released.
void onLaunchRosterPanel(ovrMessage *message, const bool succeeded)
The response function for launchRosterPanel().
std::future< bool > launchInvitePanel()
Launches the panel to invite users.
std::promise< ScopedGroupPresence > setGroupPresenceRequestPromise_
The promise for an active group presence configuration.
Definition: Multiplayer.h:221
void onLaunchInvitePanel(ovrMessage *message, const bool succeeded)
The response function for launchInvitePanel().
RequestQueue< Indices64 > invitableUsersRequestQueue_
The result queue for invitableUsers().
Definition: Multiplayer.h:218
MessageScopedSubscriptions messageScopedSubscriptions_
The subscription objects for all messages.
Definition: Multiplayer.h:215
void onClearGroupPresence(ovrMessage *message, const bool succeeded)
The response function for ovr_GroupPresence_Clear().
void onInviteUsers(ovrMessage *message, const bool succeeded)
The response function for inviteUsers().
bool inviteUsers(const Indices64 &userIds)
Invites several users.
void onNotification(ovrMessage *message, const bool succeeded)
The event function for notifications.
bool launchRosterPanel()
Launches the roster panel showing all users with same group presence configuration.
Lock groupPresenceLock_
The lock for group presence configurations.
Definition: Multiplayer.h:230
std::future< ScopedGroupPresence > setActiveGroupPresence(const std::string &destinationApiName, const std::string &lobbySessionId, const std::string &matchSessionId=std::string())
Sets the current GroupPresence and activates it (sets it as joinable).
void onSetGroupPresence(ovrMessage *message, const bool succeeded)
The response function for setGroupPresence().
void ensureInitialization()
Ensures that the mulit-player functionalities are initialized.
void onNotificationJoinIntentReceived(ovrMessage *message, const bool succeeded)
The event function for join event received notifications.
std::optional< std::promise< bool > > launchInvitePanelRequestPromise_
The promise for launch invite panel responses.
Definition: Multiplayer.h:224
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
std::vector< Index64 > Indices64
Definition of a vector holding 64 bit index values.
Definition: Base.h:108
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15