-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParty.hpp
More file actions
4730 lines (4099 loc) · 162 KB
/
Party.hpp
File metadata and controls
4730 lines (4099 loc) · 162 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MIT License
//
// Copyright (c) 2019 Stormancer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "gamefinder/GameFinder.hpp"
#include "Users/ClientAPI.hpp"
#include "Users/Users.hpp"
#include "stormancer/IClient.h"
#include "stormancer/Event.h"
#include "stormancer/msgpack_define.h"
#include "stormancer/Scene.h"
#include "stormancer/StormancerTypes.h"
#include "stormancer/Tasks.h"
#include "stormancer/Utilities/Macros.h"
#include "stormancer/Utilities/PointerUtilities.h"
#include "stormancer/Utilities/StringUtilities.h"
#include "stormancer/Utilities/TaskUtilities.h"
#include "stormancer/cpprestsdk/cpprest/json.h"
#include <bitset>
#include <stdio.h>
#include <string>
#include <unordered_map>
namespace Stormancer
{
namespace Party
{
struct PartyUserDto;
struct PartySettings;
struct PartyInvitation;
struct PartyCreationOptions;
struct PartyGameFinderFailure;
struct MembersUpdate;
struct JoinPartyFromSystemArgs;
enum class PartyUserStatus
{
NotReady = 0,
Ready = 1
};
enum class PartyGameFinderStatus
{
SearchStopped = 0,
SearchInProgress = 1
};
enum class MemberDisconnectionReason
{
Left = 0,
Kicked = 1
};
/// <summary>
/// Errors of the party system.
/// </summary>
/// <remarks>
/// An instance of this class represents a specific error.
/// This class also contains static helpers to parse error strings.
/// </remarks>
struct PartyError
{
/// <summary>
/// Represents well-known causes of error.
/// </summary>
enum Value
{
UnspecifiedError,
InvalidInvitation, /* You tried to perform an operation on an invitation that is no longer valid. */
AlreadyInParty, /* You tried to join a party while already being in a party. Call leaveParty() before joining the other party. */
NotInParty, /* An operation that requires you to be in a party could not be performed because you are not in a party. */
PartyNotReady, /* The party cannot enter the GameFinder yet because no GameFinder has been set in the party settings. */
Unauthorized, /* A party operation failed because you do not have the required privileges. */
StormancerClientDestroyed, /* An operation could not complete because the Stormancer client has been destroyed. */
UnsupportedPlatform /* An operation could not be performed because of missing platform-specific support. */
};
/// <summary>
/// Represents the different methods of PartyApi that can emit a PartyError object.
/// </summary>
enum class Api
{
JoinParty
};
struct Str
{
static constexpr const char* InvalidInvitation = "party.invalidInvitation";
static constexpr const char* AlreadyInParty = "party.alreadyInParty";
static constexpr const char* AlreadyInSameParty = "party.alreadyInSameParty";
static constexpr const char* NotInParty = "party.notInParty";
static constexpr const char* PartyNotReady = "party.partyNotReady";
static constexpr const char* Unauthorized = "unauthorized";
static constexpr const char* StormancerClientDestroyed = "party.clientDestroyed";
static constexpr const char* UnsupportedPlatform = "party.unsupportedPlatform";
Str() = delete;
};
static Value fromString(const char* error)
{
if (std::strcmp(error, Str::AlreadyInParty) == 0) { return AlreadyInParty; }
if (std::strcmp(error, Str::InvalidInvitation) == 0) { return InvalidInvitation; }
if (std::strcmp(error, Str::NotInParty) == 0) { return NotInParty; }
if (std::strcmp(error, Str::PartyNotReady) == 0) { return PartyNotReady; }
if (std::strcmp(error, Str::Unauthorized) == 0) { return Unauthorized; }
if (std::strcmp(error, Str::StormancerClientDestroyed) == 0) { return StormancerClientDestroyed; }
if (std::strcmp(error, Str::UnsupportedPlatform) == 0) { return UnsupportedPlatform; }
return UnspecifiedError;
}
/// <summary>
/// The API call that failed
/// </summary>
Api apiCalled;
/// <summary>
/// The reason for the failure
/// </summary>
std::string error;
/// <summary>
/// Get the error code for this particular <c>error</c>.
/// </summary>
/// <remarks>If the error has no particular code associated to it, this method will return <c>UnspecifiedError</c>.
/// <returns>The error code (member of the <c>PartyError::Value</c> enum) corresponding to the <c>error</c> member.</returns>
Value getErrorCode() const
{
return fromString(error.c_str());
}
/// <summary>
/// Construct a PartyError, specifying the API (PartyApi method) that failed, and the error string.
/// </summary>
/// <param name="api">The PartyApi method that failed</param>
/// <param name="error">The error string. This is a const char* because this value often comes from <c>exception.what()</c>. We avoid creating a temporary string.</param>
PartyError(Api api, const char* error)
: apiCalled(api)
, error(error)
{
}
};
struct LocalPlayerInfos
{
std::string stormancerUserId;
std::string platform;
std::string pseudo;
std::string platformId;
std::string customData;
int localPlayerIndex;
MSGPACK_DEFINE(platform, stormancerUserId, pseudo, platformId, customData, localPlayerIndex)
};
struct PartyUserDto
{
std::string userId;
PartyUserStatus partyUserStatus;
std::vector<byte> userData;
Stormancer::SessionId sessionId;
std::vector<LocalPlayerInfos> localPlayers;
bool isLeader = false; // Computed locally
PartyUserDto(std::string userId) : userId(userId) {}
PartyUserDto() = default;
MSGPACK_DEFINE(userId, partyUserStatus, userData, sessionId, localPlayers);
};
/// <summary>
/// Abstraction for a party identifier.
/// </summary>
/// <remarks>
/// Could be a stormancer scene Id, a platform-specific session Id, and more.
/// </remarks>
struct PartyId
{
/// <summary>
/// Platform-specific type of the PartyId.
/// </summary>
std::string type;
/// <summary>
/// Identifier for a party.
/// </summary>
std::string id;
/// <summary>
/// Platform of this PartyId. Can be empty if type is scene Id or connection token.
/// </summary>
std::string platform;
MSGPACK_DEFINE(type, id, platform);
static constexpr const char* TYPE_SCENE_ID = "stormancer.sceneId";
static constexpr const char* TYPE_PARTY_ID = "stormancer.partyId";
static constexpr const char* TYPE_CONNECTION_TOKEN = "stormancer.connectionToken";
static constexpr const char* STRING_PLATFORM_FIELD = "platform";
static constexpr const char* STRING_TYPE_FIELD = "type";
static constexpr const char* STRING_ID_FIELD = "id";
static constexpr const char* STRING_SEP_1 = ", ";
static constexpr const char* STRING_SEP_2 = ": ";
std::string toJson() const
{
auto jsonObject = web::json::value::object();
jsonObject[utility::conversions::to_string_t(STRING_ID_FIELD)] = web::json::value(utility::conversions::to_string_t(id));
jsonObject[utility::conversions::to_string_t(STRING_TYPE_FIELD)] = web::json::value(utility::conversions::to_string_t(type));
jsonObject[utility::conversions::to_string_t(STRING_PLATFORM_FIELD)] = web::json::value(utility::conversions::to_string_t(platform));
return utility::conversions::to_utf8string(jsonObject.serialize());
}
static PartyId fromJson(const std::string& jsonString)
{
PartyId partyId;
auto jsonValue = web::json::value::parse(utility::conversions::to_string_t(jsonString));
if (jsonValue.is_object())
{
auto jsonObject = jsonValue.as_object();
auto idIt = jsonObject.find(utility::conversions::to_string_t(STRING_ID_FIELD));
if (idIt != jsonObject.end() && idIt->second.is_string())
{
partyId.id = utility::conversions::to_utf8string(idIt->second.as_string());
}
auto typeIt = jsonObject.find(utility::conversions::to_string_t(STRING_TYPE_FIELD));
if (typeIt != jsonObject.end() && typeIt->second.is_string())
{
partyId.type = utility::conversions::to_utf8string(typeIt->second.as_string());
}
auto platformIt = jsonObject.find(utility::conversions::to_string_t(STRING_PLATFORM_FIELD));
if (platformIt != jsonObject.end() && platformIt->second.is_string())
{
partyId.type = utility::conversions::to_utf8string(platformIt->second.as_string());
}
}
return partyId;
}
std::string toString() const
{
std::stringstream ss;
ss << STRING_PLATFORM_FIELD << STRING_SEP_2 << platform
<< STRING_SEP_1
<< STRING_TYPE_FIELD << STRING_SEP_2 << type
<< STRING_SEP_1
<< STRING_ID_FIELD << STRING_SEP_2 << id;
return ss.str();
}
static PartyId fromString(const std::string& partyIdStr)
{
PartyId partyId;
auto parts = stringSplit(partyIdStr, STRING_SEP_1);
if (parts.size() == 3)
{
auto platform = stringSplit(parts[0], STRING_SEP_2);
if (platform[0] == STRING_PLATFORM_FIELD)
{
partyId.platform = platform[1];
}
auto type = stringSplit(parts[1], STRING_SEP_2);
if (type[1] == STRING_TYPE_FIELD)
{
partyId.type = type[1];
}
auto id = stringSplit(parts[2], STRING_SEP_2);
if (id[2] == STRING_ID_FIELD)
{
partyId.id = id[1];
}
}
return partyId;
}
bool operator==(const PartyId& right)
{
return !((*this) != right);
}
bool operator!=(const PartyId& right)
{
return (id != right.id || type != right.type || (!platform.empty() && !right.platform.empty() && platform != right.platform));
}
};
/// <summary>
/// Contains information about a party that the current user can join.
/// </summary>
struct AdvertisedParty
{
/// <summary>
/// A friend of the current user.
/// </summary>
struct Friend
{
/// <summary>
/// Stormancer user Id of the friend. May be empty.
/// </summary>
std::string stormancerId;
/// <summary>
/// Platform-specific user Id of the friend. May be empty.
/// </summary>
std::string platformId;
/// <summary>
/// Username of the friend. May be empty.
/// </summary>
std::string username;
/// <summary>
/// Additional data for this friend.
/// </summary>
std::unordered_map<std::string, std::string> data;
MSGPACK_DEFINE(stormancerId, platformId, username, data);
};
/// <summary>
/// Abstract party Id, possibly platform-specific.
/// </summary>
PartyId partyId;
/// <summary>
/// Stormancer user Id of the party leader. May be empty.
/// </summary>
std::string leaderUserId;
/// <summary>
/// List of friends who are in the party.
/// </summary>
std::vector<Friend> friends;
/// <summary>
/// Additional metadata for the party.
/// </summary>
std::unordered_map<std::string, std::string> metadata;
MSGPACK_DEFINE(partyId, leaderUserId, friends, metadata);
};
struct PartyDocument
{
std::string id;
std::string content;
MSGPACK_DEFINE(id, content)
};
struct SearchResult
{
Stormancer::uint32 total;
std::vector<PartyDocument> hits;
MSGPACK_DEFINE(total, hits)
};
class PartyApi
{
public:
virtual ~PartyApi() = default;
/// <summary>
/// Create and join a new party.
/// </summary>
/// <remarks>
/// If the local player is currently in a party, the operation fails.
/// The local player will be the leader of the newly created party.
/// </remarks>
/// <param name="partyRequest">Party creation parameters</param>
/// <returns>A task that completes when the party has been created and joined.</returns>
virtual pplx::task<void> createParty(const PartyCreationOptions& partyRequest, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Creates a party if the user is not connected to one.
/// </summary>
/// <param name="partyRequest">Party creation parameters.</param>
/// <param name="ct">Cancellation token that cancels party creation.</param>
/// <returns></returns>
virtual pplx::task<void> createPartyIfNotJoined(const PartyCreationOptions& partyRequest, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Join an existing party using a connection token provided by the server
/// </summary>
/// <param name="connectionToken">Token required to connect to the party.</param>
/// <returns>A task that completes once the party has been joined.</returns>
virtual pplx::task<void> joinParty(const std::string& connectionToken, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Join a party using an abstract PartyId.
/// </summary>
/// <param name="partyId">Abstract PartyId.</param>
/// <returns>A task that completes once the party has been joined.</returns>
virtual pplx::task<void> joinParty(const PartyId& partyId, const std::vector<byte>& userData, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Join an existing party using its unique scene Id.
/// </summary>
/// <param name="sceneId">Id of the party scene.</param>
/// <returns>A task that completes once the party has been joined.</returns>
virtual pplx::task<void> joinPartyBySceneId(const std::string& sceneId, const std::vector<byte>& userData, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Join an existing party using an invitationCode.
/// </summary>
/// <param name="invitationCode"></param>
/// <param name="userData">custom data associated with the party member on join.</param>
/// <param name="ct"></param>
/// <returns></returns>
virtual pplx::task<void> joinPartyByInvitationCode(const std::string& invitationCode, const std::vector<byte>& userData = {}, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Gets a boolean indicating if the party is currently in a gamesession.
/// </summary>
/// <returns></returns>
virtual bool isInGameSession() = 0;
/// <summary>
/// If the party is in a gamesession, gets a token to connect to it.
/// </summary>
/// <param name="ct"></param>
/// <returns></returns>
virtual pplx::task<std::string> getCurrentGameSessionConnectionToken(pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Leave the party
/// </summary>
/// <returns>A task that completes with the operation.</returns>
virtual pplx::task<void> leaveParty(pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Check if you are currently in a party.
/// </summary>
/// <returns>
/// <c>true</c> if you are in a party, <c>false</c> otherwise.
/// Note that if you are in the process of joining or creating a party, but are not finished yet, this method will also return <c>false</c>.
/// </returns>
virtual bool isInParty() const noexcept = 0;
/// <summary>
/// Get the party scene.
/// </summary>
/// <returns>The party scene.</returns>
virtual std::shared_ptr<Scene> getPartyScene() const = 0;
/// <summary>
/// Get the member list of the currently joined party.
/// </summary>
/// <remarks>
/// It is invalid to call this method while not in a party.
/// Call <c>isInParty()</c> to check.
/// </remarks>
/// <returns>A vector of structs that describe every user who is currently in the party.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual std::vector<PartyUserDto> getPartyMembers() const = 0;
/// <summary>
/// Get the local member's party data.
/// </summary>
/// <remarks>
/// This method is a shortcut for calling <c>getPartyMembers()</c> and iterating over the list to find the local member.
/// </remarks>
/// <remarks>
/// It is invalid to call this method while not in a party.
/// Call <c>isInParty()</c> to check.
/// </remarks>
/// <returns>The struct containing the local player's party data.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual PartyUserDto getLocalMember() const = 0;
/// <summary>
/// Set the local player's status (ready/not ready).
/// </summary>
/// <remarks>
/// By default, a GameFinder request (matchmaking group queuing) is automatically started when all players in the party are ready.
/// This behavior can be controlled server-side. See the Party documentation for details.
/// </remarks>
/// <param name="playerStatus">Ready or not ready</param>
/// <returns>A task that completes when the update has been sent.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual pplx::task<void> updatePlayerStatus(PartyUserStatus playerStatus) = 0;
/// <summary>
/// Get the settings of the current party.
/// </summary>
/// <returns>The settings of the current party, if the current user is currently in a party.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual PartySettings getPartySettings() const = 0;
/// <summary>
/// Get the partyId of the current party.
/// </summary>
/// <returns>The partyId of the current party, if the current user is currently in a party.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual PartyId getPartyId() const = 0;
/// <summary>
/// Get the User Id of the party leader.
/// </summary>
/// <returns>The Stormancer User Id of the party leader.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual std::string getPartyLeaderId() const = 0;
/// <summary>
/// Update the party settings
/// </summary>
/// <remarks>
/// Party settings can only be set by the party leader.
/// Party settings are automatically replicated to other players. The current value is available
/// in the current party object. Subscribe to the onUpdatedPartySettings event to listen to update events.
/// </remarks>
/// <param name="partySettings">New settings</param>
/// <returns>A task that completes when the settings have been updated and replicated to other players.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual pplx::task<void> updatePartySettings(PartySettings partySettings) = 0;
/// <summary>
/// Update the data associated with the local player
/// </summary>
/// <remarks>
/// player data are automatically replicated to other players. The current value is available
/// in the current party members list. Subscribe to the OnUpdatedPartyMembers event to listen to update events.
/// </remarks>
/// <param name="data">New player data</param>
/// <param name="localPlayers">The local players </param>
/// <returns>A task that completes when the data has been updated and replicated to other players.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual pplx::task<void> updatePlayerData(std::vector<byte> data, std::vector<LocalPlayerInfos> localPlayers) = 0;
/// <summary>
/// Update the data associated with the local player
/// </summary>
/// <remarks>
/// player data are automatically replicated to other players. The current value is available
/// in the current party members list. Subscribe to the OnUpdatedPartyMembers event to listen to update events.
/// </remarks>
/// <param name="data">New player data</param>
/// <returns>A task that completes when the data has been updated and replicated to other players.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
pplx::task<void> updatePlayerData(std::vector<byte> data)
{
return updatePlayerData(data, this->getLocalMember().localPlayers);
}
/// <summary>
/// Check if the local user is the leader of the party.
/// </summary>
/// <returns><c>true</c> if the local user is the leader, <c>false</c> otherwise.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual bool isLeader() const = 0;
/// <summary>
/// Promote the specified user as leader
/// </summary>
/// <remarks>
/// The caller must be the leader of the party
/// The new leader must be in the party
/// </remarks>
/// <param name="userId">The id of the player to promote</param>
/// <returns>A task that completes when the underlying RPC (remote procedure call) has returned.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual pplx::task<void> promoteLeader(std::string userId) = 0;
/// <summary>
/// Kick the specified user from the party
/// </summary>
/// <remarks>
/// The caller must be the leader of the party
/// If the user has already left the party, the operation succeeds.
/// </remarks>
/// <param name="userId">The id of the player to kick</param>
/// <returns>A task that completes when the underlying RPC (remote procedure call) has returned.</returns>
/// <exception cref="std::exception">If you are not in a party.</exception>
virtual pplx::task<void> kickPlayer(std::string userId) = 0;
/// <summary>
/// Creates an invitation code that can be used by users to join the party.
/// </summary>
virtual pplx::task<std::string> createInvitationCode(pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<void> cancelInvitationCode(pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Get pending party invitations for the player.
/// </summary>
/// <remarks>
/// Call <c>subscribeOnInvitationReceived()</c> in order to be notified when an invitation is received.
/// </remarks>
/// <returns>A vector of invitations that have been received and have not yet been accepted.</returns>
virtual std::vector<PartyInvitation> getPendingInvitations() = 0;
/// <summary>
/// Get the list of invitations the player has sent for the current party.
/// </summary>
/// <remarks>
/// This list will only contain invitations that support cancellation.
/// Invitations that are backed by a system which doesn't support cancellation, like most platform-specific invitation systems, will not appear in the list.
/// If your game needs cancelable invitations as a feature, you should always set <c>forceStormancerInvite</c> to <c>true</c> when calling <c>sendInvitation()</c>.
/// </remarks>
/// <returns>A vector of user ids to whom invitations have been sent but not yet accepted or declined.</returns>
virtual std::vector<std::string> getSentPendingInvitations() = 0;
/// <summary>
/// Check whether the local player can send invitations with <c>sendInvitation()</c>.
/// </summary>
/// <returns><c>true</c> if the local player is in a party, and is authorized to send invitations, <c>false</c> otherwise.</returns>
/// <remarks>
/// By default, invitations can only be sent by the leader of the party.
/// This restriction can be lifted by setting <c>PartyCreationOptions::onlyLeaderCanInvite</c> to <c>false</c> when creating a party,
/// or later on by changing the party settings with <c>updatePartySettings()</c>.
/// </remarks>
virtual bool canSendInvitations() const = 0;
/// <summary>
/// Send an invitation to another player.
/// </summary>
/// <param name="recipient">Stormancer Id of the player to be invited.</param>
/// <param name="forceStormancerInvite">If <c>true</c>, always send a Stormancer invitation, even if a platform-specific invitation system is available.</param>
/// <remarks>
/// The stormancer server determines the kind of invitation that should be sent according to the sender and the recipient's platform.
/// Unless <paramref name="forceStormancerInvite" /> is set to <c>true</c>, stormancer will prioritize platform-specific invitation systems where possible.
/// If your game needs cancelable invitations as a feature, you should always set <paramref name="forceStormancerInvite" /> to <c>true</c>.
/// </remarks>
/// <returns>A task that completes when the invitation has been sent.</returns>
virtual pplx::task<void> sendInvitation(const std::string& recipient, bool forceStormancerInvite = false) = 0;
/// <summary>
/// Show the system UI to send invitations to the current party, if the current platform supports it.
/// </summary>
/// <returns><c>true</c> if we were able to show the UI, <c>false</c> otherwise.</returns>
virtual bool showSystemInvitationUI() = 0;
/// <summary>
/// Cancel an invitation that was previously sent.
/// </summary>
/// <param name="recipient">Stormancer Id of the player who was previously invited through <c>sendInvitation()</c>.</param>
/// <remarks>
/// This call will only have an effect if the invitation is backed by a system which supports canceling an invitation, such as Stormancer invitations,
/// and the invitation has not yet been accepted or declined by the recipient.
/// In all other circumstances, it will have no effect.
/// </remarks>
virtual void cancelInvitation(const std::string& recipient) = 0;
/// <summary>
/// Get advertised parties.
/// </summary>
/// <returns>A list of advertised parties.</returns>
virtual pplx::task<std::vector<AdvertisedParty>> getAdvertisedParties(pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
/// <summary>
/// Get the PartyApi's DependencyScope.
/// </summary>
/// <returns>The DependencyScope of the PartyApi instance.</returns>
virtual const DependencyScope& dependencyScope() const = 0;
/// <summary>
/// Register a callback to be notified when the list of sent invitations changes.
/// </summary>
/// <param name="callback">Callable object taking a vector if strings as parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
/// <remarks>
/// The vector of strings passed to <paramref name="callback" /> is the list of stormancer Ids to which you have sent an invitation to the current party.
/// Only invitations that are cancelable, and have not yet been accepted or declined by their recipient, will appear in the list.
/// </remarks>
virtual Subscription subscribeOnSentInvitationsListUpdated(std::function<void(std::vector<std::string>)> callback) = 0;
/// <summary>
/// Register a callback to be notified when an invitation that you previously sent has been declined by its recipient.
/// </summary>
/// <param name="callback">Callable object taking the stormancer Id of the recipient of the ivitation as parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
/// <remarks>
/// An invtitation system may have the notion of a user declining an invitation that they received. The Stormancer invitation system does.
/// When an invitation that was sent through such a system is declined, and said system supports notifying the sender about the declination, this event will be triggered on the sender's side.
/// </remarks>
virtual Subscription subscribeOnSentInvitationDeclined(std::function<void(std::string)> callback) = 0;
/// <summary>
/// Register a callback to be run when the party leader changes the party settings.
/// </summary>
/// <param name="callback">Callable object taking a <c>PartySettings</c> struct as parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Event<PartySettings>::Subscription subscribeOnUpdatedPartySettings(std::function<void(PartySettings)> callback) = 0;
/// <summary>
/// Register a callback to be run when the party member list changes.
/// </summary>
/// <remarks>
/// This event is triggered for any kind of change to the list:
/// - Member addition and removal
/// - Member data change
/// - Member status change
/// - Leader change
/// The list of <c>PartyUserDto</c> passed to the callback contains only the entries that have changed.
/// To retrieve the updated full list of members, call <c>getPartyMembers()</c> (it is safe to call from inside the callback too).
/// </remarks>
/// <param name="callback">Callable object taking a vector of <c>PartyUserDto</c> structs as parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
STORM_DEPRECATED("Use subscribeOnPartyMembersUpdated() instead")
virtual Event<std::vector<PartyUserDto>>::Subscription subscribeOnUpdatedPartyMembers(std::function<void(std::vector<PartyUserDto>)> callback) = 0;
/// <summary>
/// Register a callback to be run when there is a change to any party member.
/// </summary>
/// <remarks>
/// This event is triggered for any kind of change to the party members:
/// - Member joining, leaving or being kicked
/// - Member data change
/// - Member status change
/// - Leader change
/// A single event can contain multiple kinds of changes for multiple party members.
/// The <c>MembersUpdate</c> object passed to the callback contains the details of every change.
/// To retrieve the updated full list of members, call <c>getPartyMembers()</c> (it is safe to call from inside the callback too).
/// </remarks>
/// <param name="callback">Callable object taking a <c>MembersUpdate</c> struct as parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Subscription subscribeOnPartyMembersUpdated(std::function<void(MembersUpdate)> callback) = 0;
/// <summary>
/// Register a callback to be run when the local player has joined a party.
/// </summary>
/// <param name="callback">Callable object.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Event<>::Subscription subscribeOnJoinedParty(std::function<void()> callback) = 0;
/// <summary>
/// Register a callback to be run when the local player has left the party.
/// </summary>
/// <remarks>
/// The callback parameter <c>MemberDisconnectionReason</c> will be set to <c>Kicked</c> if you were kicked by the party leader.
/// In any other case, it will be set to <c>Left</c>.
/// </remarks>
/// <param name="callback">Callable object taking a <c>MemberDisconnectionReason</c> parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Event<MemberDisconnectionReason>::Subscription subscribeOnLeftParty(std::function<void(MemberDisconnectionReason)> callback) = 0;
/// <summary>
/// Register a callback to be run when the local player receives an invitation to a party from a remote player.
/// </summary>
/// <remarks>
/// To accept the invitation, call <c>joinParty(PartyInvitation)</c>.
/// To retrieve the list of all pending invitations received by the local player, call <c>getPendingInvitations()</c>.
/// </remarks>
/// <param name="callback">Callable object taking a <c>PartyInvitation</c> parameter.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Event<PartyInvitation>::Subscription subscribeOnInvitationReceived(std::function<void(PartyInvitation)> callback) = 0;
/// <summary>
/// Register a callback to be run when an invitation sent to the local player was canceled by the sender.
/// </summary>
/// <param name="callback">Callable object taking the Id of the user who canceled the invitation.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Event<std::string>::Subscription subscribeOnInvitationCanceled(std::function<void(std::string)> callback) = 0;
/// <summary>
/// Register a callback to be run when the status of the GameFinder for this party is updated.
/// </summary>
/// <remarks>
/// Monitoring the status of the GameFinder can be useful to provide visual feedback to the player.
/// </remarks>
/// <param name="callback">Callable object taking a <c>GameFinderStatus</c>.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Subscription subscribeOnGameFinderStatusUpdate(std::function<void(PartyGameFinderStatus)> callback) = 0;
/// <summary>
/// Register a callback to be run when a game session has been found for this party.
/// </summary>
/// <remarks>
/// This event happens as a result of a successful GameFinder request. Call <c>subscribeOnGameFinderStatusUpdate()</c> to monitor the state of the request.
/// </remarks>
/// <param name="callback">Callable object taking a <c>GameFinder::GameFinderResponse</c> containing the information needed to join the game session.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Subscription subscribeOnGameFound(std::function<void(GameFinder::GameFoundEvent)> callback) = 0;
/// <summary>
/// Register a callback to be run when an error occurs while looking for a game session.
/// </summary>
/// <remarks>
/// This event is triggered when an ongoing GameFinder request for this party fails for any reason.
/// GameFinder failure conditions are fully customizable on the server side ; please see the GameFinder documentation for details.
/// </remarks>
/// <param name="callback">Callable object taking a <c>PartyGameFinderFailure</c> containing details about the failure.</param>
/// <returns>A <c>Subscription</c> object to track the lifetime of the subscription.</returns>
virtual Subscription subscribeOnGameFinderFailure(std::function<void(PartyGameFinderFailure)> callback) = 0;
/// <summary>
/// Register a callback to be run when an error occurs in the party system.
/// </summary>
/// <param name="callback">Callable object taking a <c>const PartyError&</c> that holds data about the error.</param>
/// <returns>A <c>Subscription</c> object to control the lifetime of the subscription.</returns>
virtual Subscription subscribeOnPartyError(std::function<void(const PartyError&)> callback) = 0;
virtual pplx::task<SearchResult> searchParties(const std::string& jsonQuery, Stormancer::uint32 skip, Stormancer::uint32 size, pplx::cancellation_token cancellationToken) = 0;
};
/// <summary>
/// Arguments passed to the callback set by <c>setJoinPartyFromSystemHandler()</c> when a join party from system event occurs.
/// </summary>
struct JoinPartyFromSystemArgs
{
std::shared_ptr<IClient> client;
std::shared_ptr<PartyApi> party;
std::shared_ptr<Users::PlatformUserId> user;
PartyId partyId;
pplx::cancellation_token cancellationToken = pplx::cancellation_token::none();
std::vector<byte> userData;
};
/// <summary>
/// Party creation settings.
/// </summary>
/// <remarks>
/// Some of these settings can be changed by the party leader after the party has been created, by calling <c>PartyApi::updatePartySettings()</c>.
/// </remarks>
struct PartyCreationOptions
{
/// <summary>
/// Optional: Set this if you want to force the party's scene Id to a specific value.
/// </summary>
/// <remarks>
/// This should be left empty, unless you have very specific needs.
/// For instance, it could be used if you wanted to bypass stormancer's built-in platform-specific session and invitation integration.
/// This cannot be changed after the party has been created.
/// </remarks>
std::string platformSessionId;
/// <summary>
/// Required: Name of the GameFinder that the party will use.
/// </summary>
/// <remarks>
/// This GameFinder must exist and be accessible from the party on the server application.
/// This setting can be changed after the party has been created.
/// </remarks>
std::string GameFinderName;
/// <summary>
/// Optional: Game-specific, party-wide custom data.
/// </summary>
/// <remarks>
/// This is the custom data for the whole party. After the party has been created, it can be changed by the party leader using <c>PartyApi::updatePartySettings()</c>.
/// This must not be confused with per-player custom data, which can be set using <c>PartyApi::updatePlayerData()</c>.
/// </remarks>
std::string CustomData;
/// <summary>
/// Optional: Settings for server-side extensions of the Party system.
/// </summary>
/// <remarks>
/// If you are using any Party extensions that require settings at party creation time, these settings should be put in this map.
/// These settings cannot be changed after the party has been created.
/// </remarks>
std::unordered_map<std::string, std::string> serverSettings;
/// <summary>
/// Optional: If true, only the party leader can send invitations to other players. If false, all party members can send invitations.
/// </summary>
/// <remarks>
/// By default, the party leader is the player who created the party. It can be changed later by calling <c>PartyApi::promoteLeader()</c>.
/// This setting can be changed after the party has been created.
/// </remarks>
bool onlyLeaderCanInvite = true;
/// <summary>
/// Optional: Whether the party can be joined by other players, including players who have been invited.
/// </summary>
/// <remarks>
/// When this is <c>false</c>, nobody can join the party.
/// This setting can be changed after the party has been created.
/// </remarks>
bool isJoinable = true;
/// <summary>
/// Whether the party is public or private.
/// </summary>
/// <remarks>
/// A public party is always visible to other players.
/// A private party is visible only to players who have received an invitation.
/// On some platforms, only public parties can be advertised.
/// </remarks>
bool isPublic = false;
/// <summary>
/// Gets or sets binary member data to associate the party leader with on party join.
/// </summary>
std::vector<byte> userData;
MSGPACK_DEFINE(platformSessionId, GameFinderName, CustomData, serverSettings, onlyLeaderCanInvite, isJoinable, isPublic, userData);
};
namespace details
{
class IPartyInvitationInternal
{
public:
virtual ~IPartyInvitationInternal() = default;
virtual std::string getSenderId() = 0;
virtual std::string getSenderPlatformId() = 0;
virtual pplx::task<void> acceptAndJoinParty(const std::vector<byte>& userData, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual void decline() = 0;
virtual bool isValid() = 0;
};
}
struct PartyInvitation
{
PartyInvitation(std::shared_ptr<details::IPartyInvitationInternal> invite)
: _internal(invite)
{
}
#ifdef __clang__
// Avoid clang warnings with implicit default constructors. Note: the same solution cannot be applied with MSVC (and isn't needed).
STORM_WARNINGS_PUSH;
STORM_CLANG_DIAGNOSTIC("clang diagnostic ignored \"-Wdeprecated-declarations\"")
PartyInvitation(const PartyInvitation& other) = default;
PartyInvitation(PartyInvitation&& other) = default;
PartyInvitation& operator=(const PartyInvitation& other) = default;
STORM_WARNINGS_POP;
#endif
/// <summary>
/// Get the stormancer Id of the user who sent the invitation.
/// </summary>
/// <returns>The stormancer Id of the player who sent the invitation.</returns>
std::string getSenderId() const { return _internal->getSenderId(); }
std::string getSenderPlatformId() const { return _internal->getSenderPlatformId(); }
/// <summary>
/// Accept the invitation and join the corresponding party.
/// </summary>
/// <returns>A task that completes once the party has been joined.</returns>
pplx::task<void> acceptAndJoinParty(const std::vector<byte>& userData = {}, const std::unordered_map<std::string, std::string>& userMetadata = {}, pplx::cancellation_token ct = pplx::cancellation_token::none()) { return _internal->acceptAndJoinParty(userData, userMetadata, ct); }
/// <summary>
/// Decline the invitation.
/// </summary>
/// <remarks>
/// This will remove the invitation from the list obtained via <c>PartyApi::getPendingInvitations()</c>,
/// and, if the underlying invitation system supports it, send a declination message.
/// </remarks>
void decline() { _internal->decline(); }
/// <summary>
/// Check whether this invitation is still valid.
/// </summary>
/// <remarks>
/// An invitation becomes invalid once it has been accepted or denied.
/// </remarks>
/// <returns><c>true</c> if the invitation is valid, <c>false</c> otherwise.</returns>
bool isValid() const { return _internal->isValid(); }
private:
std::shared_ptr<details::IPartyInvitationInternal> _internal;