I was looking into adding friends, i understand how its done on facebook. I also found plenty of api refernce on listing and messaging, but i didnt find a single friend request, is that even possible with GameSparks? I was also wondering if the facebook issue where you could only retrieve friends who have authorized the app as i noticed the post was about 8 months ago.
Thnx,
Cheers
Best Answer
C
Customer Support
said
over 8 years ago
The facebook issue hasn't been resolved yet and we are unsure when it will be, this is due to changes made to Facebook's rules about getting user information.
At the moment you'd have to do it with teams through Cloud Code, on the RegistrationResponse you'd create a team for the User with the teamId being the same as the userId. Then you could create a challenge called friendRequest, depending on who you send it to, if they accept it both players are adding to the respective teams, if they decline, neither are.
I'll be working on a document to help people do this themselves in the near future.
The facebook issue hasn't been resolved yet and we are unsure when it will be, this is due to changes made to Facebook's rules about getting user information.
At the moment you'd have to do it with teams through Cloud Code, on the RegistrationResponse you'd create a team for the User with the teamId being the same as the userId. Then you could create a challenge called friendRequest, depending on who you send it to, if they accept it both players are adding to the respective teams, if they decline, neither are.
I'll be working on a document to help people do this themselves in the near future.
Shane
N
Niels Jørgensen
said
about 8 years ago
Would the above method (using teams) mark the players as "friends" in GameSparks?
Specifically, if I retrieve a leaderboard with "social=true" - will it include the scores of the people on the team?
In my particular use-case I cannot use Facebook, but have to use other social media. I'm thinking I can get the players ID on the given social media and store that with the player and create a team with that ID also.
Then for each player I will attempt to join the "teams" of all their friends based on the same IDs.
I would like to handle it silently without user interaction as a way of getting the "friends" feature to work on other social networks than the ones support natively by GameSparks.
Would this work?
I suppose it's best implemented server-side to avoid a massive amount of calls.
Customer Support
said
about 8 years ago
Hi Niels,
So long as the team itself is marked as "Social" they will be considered friends by the platform. With social = true on the leaderboardDataRequest, the leaderboard will show just your friends and their rankings will be relative to the friends list, so instead of
friend 1 - 50551,
friend 2, - 30
friend 3 - 100
it will be
friend 2 - 1
friend 3 - 2
friend 1 - 3
The rest sounds very feasible!
Shane
N
Niels Jørgensen
said
about 8 years ago
Thanks, I should maybe have replied to my own question since I already went ahead and implemented it :)... Seems to work fine...
N
Niels Jørgensen
said
about 8 years ago
Well, it doesn't really work...
The problem with teams is that everyone on the team becomes friends so, in effect, teams assume that all my friends are also friends of each other which is obviously not the case.
The platform really needs a SetFriends API. Preferably it would be implemented as a one-way connection so I can become a friend of someone else and thus compete against him/her on leaderboards, without him/her having to have me as a friend. (Think popular you-tubers who might be followed by lots, but don't follow anywhere near as many - a popular gamer might be popular to compete against, but might not want to compete against everyone in the world).
Right now I've implemented this function using a team of "followed" players, but had to switch off the social option to avoid the friend-of-friends problem, which means that e.g. push notifications doesn't work properly.
Another issue is that the vast majority of our players don't use FB but other social media that GameSpark does not have native support for. I have no way of managing friends properly in this case.
If there's a way to fix this already, I'd love to hear about it :)
Customer Support
said
about 8 years ago
Hi Niels,
I think I have come up with a solution for you. It will give every player their own "Friends List", a team with a uniqueId based on their playerId. This will be a social team but only friends who have been approved by the user will be added to it.
We will also set up another Team which will allow any user to "Follow" said player. This wont be a social team, so anyone who joins it wont be included in leaderboards etc.
The way we have teams set up at the moment allows users to create their own set of rules relating to how a friends systems should be set up. A lot of people have their own idea's about how rules should be implemented, so instead of enforcing any one or two types, we let you create them.
1) Create the Friend Team:
Friends List:
shortCode: friendsList
name: Friends List
social: on
max members: 0
max membership: 0
max ownership: 1 (Mandatory)
2) Create Cloud Code for RegistrationResponse:
var userName = Spark.getPlayer().getDisplayName();
var userId = Spark.getPlayer().getPlayerId();
//We are going to have a collection of all players so we can search it easily and return userIds
var playerCollection = Spark.runtimeCollection("playerDirectory")
//Insert a new document containing the playerId and playerName, this way we can search by name and return an Id we can use
playerCollection.insert({"playerId" : userId, "playerName" : userName});
//Create a new friendList team with an Id equal to that of the user + "friends"
var friendsSuccess = Spark.sendRequest({
"@class": ".CreateTeamRequest",
"teamId": userId + "friends",
"teamType": "friendsList",
"teamName": "Friends List"
});
//We will be adding more here later on if you also wish to implement the followers/following way of things
3) Create the "Friend Request" Challenge:
shortCode: friendRequest
name: Friend Request
description: Asks someone to be friend
turn based: off
turn / attempt consumbers: none
leaderboard: Scripted Outcome
global: off
first to achievement: not configured
Now we can use the CreateChallengeRequest to send a Friend Request to a user using the following JSON Request:
{
"@class": ".CreateChallengeRequest",
"challengeMessage": "Hey, lets to be friends!",
"challengeShortCode": "friendRequest",
"endTime": "2014-12-31T12:00Z",
"usersToChallenge": "userIdToSendFriendRequestTo"
}
4) Accept the friend request:
In Cloud Code -> Events, create a new event
shortCode: acceptFriendRequest
name: Accept Friend Request
description: Accepts a Friend Request Challenge
Add a new attribute:
name: Friend Request Id
shortCode: friendRequestId
dataType: String
default value:
default calc: Used In Script
Then add this to the Cloud Code:
//This will take the challengeInstaneId from our previously created Friend Request challenge
var friendRequestId = Spark.data.friendRequestId;
//We'll load the friend request challenge for easier access to it's variables
var friendRequest = Spark.getChallenge(friendRequestId);
//We are now going to add both players to each other's friends list
//Send a JoinTeamRequest as the player who logged this event
//The teamId will the same as the one we created on the RegistrationResponse
var success1 = Spark.sendRequestAs({
"@class": ".JoinTeamRequest",
"teamId": friendRequest.getChallengerId()+"friends"},
Spark.getPlayer().getPlayerId());
//Send a JoinTeamRequest as the player who sent the Friend Request
//The teamId will their playerId + "friends" same as the one we created on the RegistrationResponse
var success2 = Spark.sendRequestAs({
"@class": ".JoinTeamRequest",
"teamId": Spark.getPlayer().getPlayerId()+"friends",
"teamType": "friendsList"},
friendRequest.getChallengerId());
//We are only using this challenge as a vessal for the friend request, so we'll withdraw it as it is no longer useful
Spark.sendRequestAs({"@class" : ".WithdrawChallengeRequest", "challengeInstanceId" : friendRequestId}, friendRequest.getChallengerId());
Now if we receive a Friend Request Challenge, we can use the challengeInstanceId as the friendRequestId and add both players to each other's friends list. We no longer have use for the challenge so we withdraw it.
5) Decline Friend Request
Just like Accept Friend Request create the event:
shortCode: declineFriendRequest
name: Decline Friend Request
description: Declines a Friend Request Challenge
Add a new attribute:
name: Friend Request Id
shortCode: friendRequestId
dataType: String
default value:
default calc: Used In Script
Then add this to the Cloud Code:
//This will take the challengeInstaneId from our previously created Friend Request challenge
var friendRequestId = Spark.data.friendRequestId;
//We'll load the friend request challenge for easier access to it's variables
var friendRequest = Spark.getChallenge(friendRequestId);
//Decline the challenge, you can use the message that accompanies this to inform the player who sent the friend request
Spark.sendRequestAs({"@class" : ".DeclineChallengeRequest", "challengeInstanceId" : friendRequestId}, friendRequest.getChallengerId());
6) Remove a friend
This will remove a friend from your personal player list.
Create a new event:
shortCode: removeFriendRequest
name: Remove A Friend Request
description: Removes a friend from your friends list
Add a new attribute:
name: PlayerId To Remove
shortCode: playerIdToRemove
dataType: String
default value:
default calc: Used In Script
In the Cloud Code
//The playerId of the person you wish to unfriend
var playerIdToRemove = Spark.data.playerIdToRemove;
//We'll use a LeaveTeaRequest to remove both players from each others respective friends list
var success1 = Spark.sendRequestAs(
{"@class" : ".LeaveTeamRequest",
"teamId": playerIdToRemove +"friends"},
Spark.getPlayer().getPlayerId());
var success2 = Spark.sendRequestAs(
{"@class" : ".LeaveTeamRequest",
"teamId": Spark.getPlayer().getPlayerId()+"friends"},
playerIdToRemove);
This will remove both players from each others list.
7) Suppress the ChallengeWithdrawnMessage:
In Cloud Code -> Global Messages -> ChallengeWithdrawnMessage, add the following code:
if(Spark.getData().challenge.shortCode == "friendRequest"){
Spark.setScriptError("dontshow", "no point in showing it withdrawn")
}
This checks the shortCode included in the message and sets an error, effectively preventing the message from sending without affecting anything else.
That effectively closes the "standard" way of doing friends for a lot of people. However, we still need to be able to follow/unfollow people and search the playerDirectory for specific people.
Customer Support
said
about 8 years ago
Following/Followers system:
This can be used on it's own for a twitter style system or in conjunction with the "traditional" friends system above. This lets a user curate their friends and allows a system of random users to keep updated with their progress
1) Create the Followers and Following Teams:
Followers:
shortCode: followers
name: Followers
social: off
max members: 0
max membership: 0
max ownership: 1 (Mandatory)
Following:
shortCode: following
name: Following
social: on
max members: 0
max membership: 0
max ownership: 1 (Mandatory)
Depending on your preference, if a user chooses to follow someone, this could be a social team to include the person in all their social leaderboards etc, that way if the person they are following isn't following them, they don't know they exists other than they'll be on their followers team.
2) Update RegistrationResponse Cloud Code:
//Create a new followers team with an id equal to theat of the user + "followers"
var followersSuccess = Spark.sendRequest({
"@class": ".CreateTeamRequest",
"teamId": userId + "followers",
"teamType": "followers",
"teamName": "Followers"
});
var followingSuccess = Spark.sendRequest({
"@class": ".CreateTeamRequest",
"teamId": userId + "following",
"teamType": "following",
"teamName": "Following"
});
This creates the two separate teams on registration.
3)Follow a user
Create a new event called follow user:
shortCode: FollowUser
name: Follow a user
description: Adds a user to your following list
Add a new attribute:
name: PlayerId To Follow
shortCode: playerIdToFollow
dataType: String
default value:
default calc: Used In Script
In Cloud Code add this to the event:
//The playerId of the person you wish to unfriend
var playerIdToFollow = Spark.data.playerIdToFollow;
//We will add ourselves to the person we want to follow's followers list
var success1 = Spark.sendRequestAs({
"@class": ".JoinTeamRequest",
"teamId": playerIdToFollow+"followers"},
Spark.getPlayer().getPlayerId());
//We'll add the person we want to follow to our following list
var success2 = Spark.sendRequestAs({
"@class": ".JoinTeamRequest",
"teamId": Spark.getPlayer().getPlayerId()+"following",
"teamType": "friendsList"},
playerIdToFollow);
//Inform the person we are following they have a new follower
Spark.sendMessage({"type" : "newFollower", "message" : Spark.getPlayer().getDisplayName() + " is now following you."}, [Spark.loadPlayer(playerIdToFollow)]);
This will add any user to our "following team and us to their "followers" team. It will also send a message to inform who we are following that they have a new follower.
4) Unfollow a user
Create an event:
shortCode: UnfollowUser
name: Unfollow a user
description: Removes a user to your following list
Add a new attribute:
name: PlayerId To Unfollow
shortCode: playerIdToUnfollow
dataType: String
default value:
default calc: Used In Script
Add this to the Cloud Code:
//The playerId of the person you wish to unfriend
var playerIdToUnfollow = Spark.data.playerIdToUnfollow;
//We will remove ourselves to the person we want to unfollow's followers list
var success1 = Spark.sendRequestAs(
{"@class" : ".LeaveTeamRequest",
"teamId": playerIdToUnfollow +"followers"},
Spark.getPlayer().getPlayerId());
//We'll remove the person we want to unfollow from our following list
var success2 = Spark.sendRequestAs(
{"@class" : ".LeaveTeamRequest",
"teamId": Spark.getPlayer().getPlayerId()+"following"},
playerIdToUnfollow);
Finally we will create an event that will let us find a user by their display name:
Create an event:
shortCode: findUser
name: Find User
description: Finds a user by displayName
Add a new attribute:
name: displayName
shortCode: displayName
dataType: String
default value:
default calc: Used In Script
Add this to Cloud Code:
//Load our playerDirectory collection
var playerCollection = Spark.runtimeCollection("playerDirectory");
//Set the response scriptData to return all documents found with the same or similar displayName as the one we entered
Spark.setScriptData("player", playerCollection.find({displayName : {$regex : "(?i)^" + Spark.getData().displayName}}, {_id : 1, displayName : 1});
Sorry for the long post, I got a bit carried away, hopefully you can get use of the actual code above and if not hopefully it shows you that anything is possible and you can tweak it to suit your needs.
Shane
N
Niels Jørgensen
said
about 8 years ago
WOA!
I'll need a little time to digest that :) But thanks a lot for the detailed reply - if we must, we can live with our current implementation of "follow", so it's not a showstopper at the moment, but I'll certainly "steal" the example above if I can make it work for our use-case :)
If nothing else, you've opened Pandoras box :)
Customer Support
said
about 8 years ago
You're very welcome! I had a lot fun coming up with it!
Shane
S
Stephen Swan
said
about 7 years ago
Hi Shane,
I am looking to do something similar above, in particular finding and connecting to other players via their email rather than just their social network. Before I try to implement your system above has it been improved upon or further documented over the last year?
Thanks
Stephen
Customer Support
said
about 7 years ago
Hi Stephen,
I would still recommend the system described by Shane for something like this. We are currently in the process of updating all our documentation and hope to have more relating to this area in the coming months.
Oisin
D
Dimitri Tarasenko
said
about 7 years ago
friendRequest have to be declined and accepted by the challenged (not by challenger itself). =>
var wrongDeclineFriendlistRequest = Spark.sendRequestAs({"@class": ".DeclineChallengeRequest", "challengeInstanceId": friendRequestId}, friendRequest.getChallengerId());
var rightDeclineFriendlistRequest = Spark.sendRequestAs({"@class": ".DeclineChallengeRequest", "challengeInstanceId": friendRequestId}, Spark.getPlayer().getPlayerId());
D
David Reilly
said
almost 6 years ago
Hey,
Just wanted to check if this is still the recommended approach for implementing a friend system in GameSparks?
Thanks.
Customer Support
said
almost 6 years ago
Hi David,
Yes this approach is still valid. If you have any issues going through it just let us know, we'll be happy to answer any questions you may have.
Test Account
Hi,
I was looking into adding friends, i understand how its done on facebook. I also found plenty of api refernce on listing and messaging, but i didnt find a single friend request, is that even possible with GameSparks? I was also wondering if the facebook issue where you could only retrieve friends who have authorized the app as i noticed the post was about 8 months ago.
Thnx,
Cheers
The facebook issue hasn't been resolved yet and we are unsure when it will be, this is due to changes made to Facebook's rules about getting user information.
At the moment you'd have to do it with teams through Cloud Code, on the RegistrationResponse you'd create a team for the User with the teamId being the same as the userId. Then you could create a challenge called friendRequest, depending on who you send it to, if they accept it both players are adding to the respective teams, if they decline, neither are.
I'll be working on a document to help people do this themselves in the near future.
Shane
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
The facebook issue hasn't been resolved yet and we are unsure when it will be, this is due to changes made to Facebook's rules about getting user information.
At the moment you'd have to do it with teams through Cloud Code, on the RegistrationResponse you'd create a team for the User with the teamId being the same as the userId. Then you could create a challenge called friendRequest, depending on who you send it to, if they accept it both players are adding to the respective teams, if they decline, neither are.
I'll be working on a document to help people do this themselves in the near future.
Shane
Niels Jørgensen
Would the above method (using teams) mark the players as "friends" in GameSparks?
Specifically, if I retrieve a leaderboard with "social=true" - will it include the scores of the people on the team?
In my particular use-case I cannot use Facebook, but have to use other social media. I'm thinking I can get the players ID on the given social media and store that with the player and create a team with that ID also.
Then for each player I will attempt to join the "teams" of all their friends based on the same IDs.
I would like to handle it silently without user interaction as a way of getting the "friends" feature to work on other social networks than the ones support natively by GameSparks.
Would this work?
I suppose it's best implemented server-side to avoid a massive amount of calls.
Customer Support
Hi Niels,
So long as the team itself is marked as "Social" they will be considered friends by the platform. With social = true on the leaderboardDataRequest, the leaderboard will show just your friends and their rankings will be relative to the friends list, so instead of
friend 1 - 50551,
friend 2, - 30
friend 3 - 100
it will be
friend 2 - 1
friend 3 - 2
friend 1 - 3
The rest sounds very feasible!
Shane
Niels Jørgensen
Thanks, I should maybe have replied to my own question since I already went ahead and implemented it :)... Seems to work fine...
Niels Jørgensen
Well, it doesn't really work...
The problem with teams is that everyone on the team becomes friends so, in effect, teams assume that all my friends are also friends of each other which is obviously not the case.
The platform really needs a SetFriends API. Preferably it would be implemented as a one-way connection so I can become a friend of someone else and thus compete against him/her on leaderboards, without him/her having to have me as a friend. (Think popular you-tubers who might be followed by lots, but don't follow anywhere near as many - a popular gamer might be popular to compete against, but might not want to compete against everyone in the world).
Right now I've implemented this function using a team of "followed" players, but had to switch off the social option to avoid the friend-of-friends problem, which means that e.g. push notifications doesn't work properly.
Another issue is that the vast majority of our players don't use FB but other social media that GameSpark does not have native support for. I have no way of managing friends properly in this case.
If there's a way to fix this already, I'd love to hear about it :)
Customer Support
Hi Niels,
I think I have come up with a solution for you. It will give every player their own "Friends List", a team with a uniqueId based on their playerId. This will be a social team but only friends who have been approved by the user will be added to it.
We will also set up another Team which will allow any user to "Follow" said player. This wont be a social team, so anyone who joins it wont be included in leaderboards etc.
The way we have teams set up at the moment allows users to create their own set of rules relating to how a friends systems should be set up. A lot of people have their own idea's about how rules should be implemented, so instead of enforcing any one or two types, we let you create them.
1) Create the Friend Team:
Friends List:
2) Create Cloud Code for RegistrationResponse:
3) Create the "Friend Request" Challenge:
Now we can use the CreateChallengeRequest to send a Friend Request to a user using the following JSON Request:
4) Accept the friend request:
In Cloud Code -> Events, create a new event
Then add this to the Cloud Code:
Now if we receive a Friend Request Challenge, we can use the challengeInstanceId as the friendRequestId and add both players to each other's friends list. We no longer have use for the challenge so we withdraw it.
5) Decline Friend Request
Just like Accept Friend Request create the event:
Then add this to the Cloud Code:
6) Remove a friend
This will remove a friend from your personal player list.
Create a new event:
In the Cloud Code
This will remove both players from each others list.
7) Suppress the ChallengeWithdrawnMessage:
In Cloud Code -> Global Messages -> ChallengeWithdrawnMessage, add the following code:
This checks the shortCode included in the message and sets an error, effectively preventing the message from sending without affecting anything else.
That effectively closes the "standard" way of doing friends for a lot of people. However, we still need to be able to follow/unfollow people and search the playerDirectory for specific people.
Customer Support
Following/Followers system:
This can be used on it's own for a twitter style system or in conjunction with the "traditional" friends system above. This lets a user curate their friends and allows a system of random users to keep updated with their progress
1) Create the Followers and Following Teams:
Followers:
Following:
Depending on your preference, if a user chooses to follow someone, this could be a social team to include the person in all their social leaderboards etc, that way if the person they are following isn't following them, they don't know they exists other than they'll be on their followers team.
2) Update RegistrationResponse Cloud Code:
This creates the two separate teams on registration.
3)Follow a user
Create a new event called follow user:
In Cloud Code add this to the event:
This will add any user to our "following team and us to their "followers" team. It will also send a message to inform who we are following that they have a new follower.
4) Unfollow a user
Create an event:
Add this to the Cloud Code:
Finally we will create an event that will let us find a user by their display name:
Create an event:
Add this to Cloud Code:
Sorry for the long post, I got a bit carried away, hopefully you can get use of the actual code above and if not hopefully it shows you that anything is possible and you can tweak it to suit your needs.
Shane
Niels Jørgensen
WOA!
I'll need a little time to digest that :) But thanks a lot for the detailed reply - if we must, we can live with our current implementation of "follow", so it's not a showstopper at the moment, but I'll certainly "steal" the example above if I can make it work for our use-case :)
If nothing else, you've opened Pandoras box :)
Customer Support
You're very welcome! I had a lot fun coming up with it!
Shane
Stephen Swan
Hi Shane,
I am looking to do something similar above, in particular finding and connecting to other players via their email rather than just their social network. Before I try to implement your system above has it been improved upon or further documented over the last year?
Thanks
Stephen
Customer Support
Hi Stephen,
I would still recommend the system described by Shane for something like this.
We are currently in the process of updating all our documentation and hope to have more relating to this area in the coming months.
Oisin
Dimitri Tarasenko
friendRequest have to be declined and accepted by the challenged (not by challenger itself). =>
var wrongDeclineFriendlistRequest = Spark.sendRequestAs({
"@class"
:
".DeclineChallengeRequest"
,
"challengeInstanceId"
: friendRequestId}, friendRequest.getChallengerId());
var rightDeclineFriendlistRequest =
Spark.sendRequestAs({"@class" : ".DeclineChallengeRequest", "challengeInstanceId" : friendRequestId}, Spark.getPlayer().getPlayerId());David Reilly
Hey,
Just wanted to check if this is still the recommended approach for implementing a friend system in GameSparks?
Thanks.
Customer Support
Hi David,
Yes this approach is still valid. If you have any issues going through it just let us know, we'll be happy to answer any questions you may have.
Regards,
Liam
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics