Play Fab - UQcsse3200/2024-studio-2 GitHub Wiki
1. Overview/Description
The PlayFab feature provides a comprehensive platform for managing user accounts, registering users, and handling leaderboard functionalities in the game. This feature enhances the overall player experience by allowing users to create accounts, log in, and view their rankings and scores in minigames.
2. Features:
There are some features of mini map, including:
- User Registration: Allows new players to create accounts with unique usernames and passwords.
- User Login: Enables existing players to log in to their accounts and retrieve their saved progress.
- Leaderboard Management: Allows for the retrieval and submission of player scores to a global leaderboard.
- Real-Time Score Submission: Scores are submitted to PlayFab, ensuring real-time updates to the leaderboard.
3. Implementation:
3.1 PlayFab SDK Implementation
Using the PlayFab Client SDK, we will implement methods to register and log in users. The SDK provides a straightforward interface to communicate with PlayFab’s backend, handling tasks like account creation, password resets, and guest login (in development). https://github.com/PlayFab/JavaSDK/tree/master/PlayFabSDK
3.2 Register
A function is provided for user registration that accepts a username and password, returning a response indicating success or failure. If you want to register with email also, you can set the request.RequireBothUsernameAndEmail
to true
public static Response registerUser(String username, String password) {
RegisterPlayFabUserRequest request = new RegisterPlayFabUserRequest();
request.Username = username;
request.Password = password;
request.DisplayName = username;
request.RequireBothUsernameAndEmail = false;
PlayFabResult<RegisterPlayFabUserResult> result = PlayFabClientAPI.RegisterPlayFabUser(request);
if (result.Result != null) {
String succeedMsg = result.Result.Username + " has successfully registered.";
return new Response(succeedMsg, true);
} else {
String errorMsg = result.Error.errorMessage;
logger.debug(errorMsg);
return new Response(errorMsg, false);
}
}
3.3 Login
Users can use the registered account to login and get the saved data.
public static Response loginUser(String username, String password) {
LoginWithPlayFabRequest request = new LoginWithPlayFabRequest();
request.Username = username;
request.Password = password;
PlayFabResult<LoginResult> result = PlayFabClientAPI.LoginWithPlayFab(request);
if (result.Result != null) {
String succeedMsg = "Welcome " + request.Username + ".";
playerName = username;
isLogin = true;
if (!minigameUsernames.contains(username)) {
minigameUsernames.add(username);
}
logger.debug(succeedMsg);
return new Response(succeedMsg, true);
} else {
String errorMsg = result.Error.errorMessage;
logger.debug(result.Error.errorMessage);
return new Response(errorMsg, false);
}
}
3.4 Update leaderboard
We implement functions to retrieve the leaderboard. There are 3 types of leaderboard: Snake, Bird, and Fish
public static void updateLeaderboard(String gameName){
GetLeaderboardRequest request = new GetLeaderboardRequest();
request.StatisticName = gameName;
request.MaxResultsCount = 10;
PlayFabResult<GetLeaderboardResult> result = PlayFabClientAPI.GetLeaderboard(request);
// Clean the arrays before updating them
minigameUsernames.clear();
minigameHighscores.clear();
if (result.Result != null) {
// Update UI with leaderboard data
List<PlayerLeaderboardEntry> leaderboard = result.Result.Leaderboard;
for (int i = 0; i < leaderboard.size(); i++) {
minigameUsernames.add(leaderboard.get(i).DisplayName);
minigameHighscores.add(String.valueOf(leaderboard.get(i).StatValue));
System.out.println("User " + i + ": " + minigameUsernames.get(i) + " - " + minigameHighscores.get(i));
}
} else {
logger.error("Failed to retrieve leaderboard: " + result.Error.errorMessage);
}
}
3.5 Submit Score
We implement functions for logged users to submit score to the leaderboard.
public static void submitScore(String gameName, int score) {
if (!isLogin) {
logger.info("You need to login to put your score to the leaderboard.");
return;
}
UpdatePlayerStatisticsRequest request = new UpdatePlayerStatisticsRequest();
StatisticUpdate statUpdate = new StatisticUpdate();
statUpdate.StatisticName = gameName;
statUpdate.Value = score;
ArrayList<StatisticUpdate> statisticUpdates = new ArrayList<>();
statisticUpdates.add(statUpdate);
request.Statistics = statisticUpdates;
PlayFabResult<UpdatePlayerStatisticsResult> result = PlayFabClientAPI.UpdatePlayerStatistics(request);
if (result.Result != null) {
logger.info("Player score submitted successfully.");
} else {
logger.error("Failed to submit score: " + result.Error.errorMessage);
}
}