get-own-and-save
BACKND Guidelines > 5. Implementing Mail Functions > Step 4. Claiming and Saving Individual Pieces of Mail
Claiming and Saving Individual Pieces of Mail
1. Write the logic for the method of claiming and saving the mail
Add content to the PostReceive method in BackendPost.cs written in Pre-arrangements.
To call this method, the PostListGet method must be implemented in advance.
BackendPost.cs
Before editing
public void PostReceive(PostType postType, int index) {
// Step 4. Claiming and Saving Individual Pieces of Mail
}
After editing
public void PostReceive(PostType postType, int index) {
if(_postList.Count <= 0) {
Debug.LogWarning("There is no mail to receive. Or, call the mail list loading first.");
return;
}
if(index >= _postList.Count) {
Debug.LogError($"The mail does not exist: request index{index} / maximum mail count : {_postList.Count}");
return;
}
Debug.Log($"{_postList[index].inDate} of {postType.ToString()} Requesting mail claim.");
var bro = Backend.UPost.ReceivePostItem(postType, _postList[index].inDate);
if(bro.IsSuccess() == false) {
Debug.LogError($"{_postList[index].inDate} of {postType.ToString()} An error occurred while claiming the mail. : " + bro);
return;
}
Debug.Log($"{_postList[index].inDate} of {postType.ToString()} Successfully claimed mail. : " + bro);
_postList.RemoveAt(index);
if(bro.GetFlattenJSON()["postItems"].Count > 0) {
SavePostToLocal(bro.GetFlattenJSON()["postItems"]);
} else {
Debug.LogWarning("There is no mail item to be claimed.");
}
BackendGameData.Instance.GameDataUpdate();
}
2. Add method call to BackendManager.cs
BackendManager, which is automatically called when the game is executed, must be used to call this method.
Configure the method to be called after BACKND initialization, BACKND login, loading game information, and loading mail are complete.
BackendManager.cs
Before editing(After Step 3. Loading Mail)
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234"); // BACKND login method
BackendPost.Instance.PostListGet(PostType.Admin);
Debug.Log("Test complete.");
});
}
After editing
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234");
// Loads game data and saves it locally.(caching)
BackendGameData.Instance.GameDataGet();
// Loads the mail list and saves the inDate values locally.
BackendPost.Instance.PostListGet(PostType.Admin);
// Claims the mail by reading the position of the saved piece of mail. Here, the index is the order of the mail; 0 is the topmost and 1 is the next.
BackendPost.Instance.PostReceive(PostType.Admin,0);
Debug.Log("Test complete.");
});
}
3. Test in Unity
After editing the script, execute Unity debugging and check the console log of Unity.
The method has been successfully called when the log displays 'Successfully edited the item name and game information data attached to the mail. : statusCode : 204'.
When errors, such as statusCode : 400, 404, and 409, occur instead of the above log, you can check which errors caused the failure in ReceivePostItem error case.
4. Use the console to check if the mail has been looked up
When the user claims the mail, the date of the claim is displayed on Claimed Date.