We have extended the Microsoft sample game Platformer to elaborate the features of App42 Cloud API. In platformer, user has to collect the maximum gold coins and has to reach the exit point in minimum time.
Following features of App42 Cloud API have been included in this sample:
- Global Leaderboard
- Facebook friends leaderboard
- Send messages to Facebook friends
- Share score using App42 Cloud API
Prerequisites:
Facebook OAuth access token is required to access these APIs. You will have to create a Facebook app and use windows phone Facebook SDK to get user login and the access token for the same.
All the methods of App42 API are executed asynchronously and have a common type of callback “App42Callback”. So, we have extended it to setup the interaction with UICallbacks
public class App42ApiCallback : App42Callback
{
App42ApiResultCallback mShowResultCallback = null;
public App42ApiCallback(App42ApiResultCallback callBack) {
mShowResultCallback = callBack;
}
public void OnException(App42Exception exception) {
Deployment.Current.Dispatcher.BeginInvoke(new App42ApiResultCallback(mShowResultCallback), exception, true);
}
public void OnSuccess(object response) {
Deployment.Current.Dispatcher.BeginInvoke(new App42ApiResultCallback(mShowResultCallback), response, false);
}
}
Global Scoreboard
List of global user’s scores can be fetched by using ScoreBoardService of App42 API.
private static ScoreBoardService mScoreBoardService = null;
public static void GetTopNGlobalScores(int count, App42ApiResultCallback callBack) {
try {
App42ApiCallback _sCallback = new App42ApiCallback(callBack);
if (mScoreBoardService == null) {
mScoreBoardService = GlobalContext.SERVICE_API.BuildScoreBoardService();
}
mScoreBoardService.GetTopNRankers(GlobalContext.gameName, count, _sCallback);
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
private void GetTopNRankersCallback(object response, bool IsException) {
if (IsException) {
App42Exception exception = (App42Exception) response;
//showMessage("Exception,Please try again later");
} else {
List userIds = new List();
com.shephertz.app42.paas.sdk.windows.game.Game game = (com.shephertz.app42.paas.sdk.windows.game.Game) response;
if (game.IsResponseSuccess()) {
for (int i = 0; i< game.GetScoreList().Count; i++) {
ScoreListItem item = new ScoreListItem();
item.Rank = (i + 1).ToString();
item.Score = game.GetScoreList()[i].GetValue().ToString();
item.UserName = game.GetScoreList()[i].GetUserName();
_scorelist.Add(item);
userIds.Add(game.GetScoreList()[i].GetUserName());
}
App42Api.GetFacebookProfilesFromIds(userIds, GetUserFacebookProfileFromIdsCallback);
} else {
lbxGlobalScoremessageTB.Text = "Error,Please try again later";
}
}
}
Facebook friends Scoreboard
To get top Facebook friends scores of user, you need to pass game/level name and access token to App42 API's function.
public static void GetTopNFacebookFriendsScores(int count, App42ApiResultCallback callBack) {
try {
App42ApiCallback _sCallback = new App42ApiCallback(callBack);
if (mScoreBoardService == null) {
mScoreBoardService = GlobalContext.SERVICE_API.BuildScoreBoardService();
}
mScoreBoardService.GetTopNRankersFromFacebook(GlobalContext.gameName, GlobalContext.AccessToken, count, _sCallback);
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
private void GetTopNRankersFacebookFriendsCallback(object response, bool IsException) {
if (IsException) {
App42Exception exception = (App42Exception) response;
//showMessage("Exception,Please try again later");
} else {
com.shephertz.app42.paas.sdk.windows.game.Game game = (com.shephertz.app42.paas.sdk.windows.game.Game) response;
if (game.IsResponseSuccess()) {
for (int i = 0; i< game.GetScoreList().Count; i++) {
ScoreListItem item = new ScoreListItem();
item.Rank = (i + 1).ToString();
item.Score = game.GetScoreList()[i].GetValue().ToString();
item.UserId = game.GetScoreList()[i].GetFacebookProfile().GetId();
item.UserName = game.GetScoreList()[i].GetFacebookProfile().GetName();
_friendscorelist.Add(item);
}
lbxFriendsScore.ItemsSource = _friendscorelist;
if (_friendscorelist.Count == 0) {
lbxFriendsScoremessageTB.Text = "There is no item in list";
} else {
IsLoaded_friendscorelist = true;
lbxFriendsScoreMessagePopup.Visibility = Visibility.Collapsed;
}
} else {
lbxFriendsScoremessageTB.Text = "Error,Please try again later";
// showMessage("Error,Please try again later");
}
}
}
Send messages to facebook friends
Messaging feature uses Storage service to implement it. Here are the steps of implementation for the same.
1.Encapsulate the message in JSON object.
StringBuilder sbJson1 = new StringBuilder();
StringWriter sw = new StringWriter(sbJson1);
JsonWriter itemObj = new JsonTextWriter(sw);
itemObj.WriteStartObject();
itemObj.WritePropertyName("SenderName");
itemObj.WriteValue(GlobalContext.g_UserProfile.Name);
itemObj.WritePropertyName("SenderID");
itemObj.WriteValue(GlobalContext.g_UserProfile.UserID);
itemObj.WritePropertyName("Picture");
itemObj.WriteValue(GlobalContext.g_UserProfile.Picture);
itemObj.WritePropertyName("Message");
itemObj.WriteValue(tbxMessage.Text);
itemObj.WritePropertyName("RecepientID");
itemObj.WriteValue(tblToMessage.Tag.ToString());
itemObj.WriteEndObject();
App42Api.SendMessage(sbJson1.ToString(), SendMessagesCallback);
2. Insert message json object in App42 storage as a document
public static void SendMessage(String message, App42ApiResultCallback requestCallback) {
try {
App42ApiCallback _sCallback = new App42ApiCallback(requestCallback);
if (mStorageService == null) {
mStorageService = GlobalContext.SERVICE_API.BuildStorageService();
}
mStorageService.InsertJSONDocument(GlobalContext.databaseName, GlobalContext.collectionName, message, _sCallback);
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
public void SendMessagesCallback(object response, bool IsException) {
if (IsException) {
App42Exception exception = (App42Exception) response;
sendMessagePopupTB.Text = "Error,Please try again later";
} else {
Storage storage = (Storage) response;
sendMessagePopupTB.Text = "Message sent successfully";
}
}
3. At recipient retrieve message json object from App42 Storage by "RecipientID"
public static void GetMessages(App42ApiResultCallback requestCallback) {
try {
App42ApiCallback _sCallback = new App42ApiCallback(requestCallback);
if (mStorageService == null) {
mStorageService = GlobalContext.SERVICE_API.BuildStorageService();
}
mStorageService.FindDocumentByKeyValue(GlobalContext.databaseName, GlobalContext.collectionName, "RecepientID", GlobalContext.g_UserProfile.UserID, _sCallback);
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
public void GetMessagesCallback(object response, bool IsException) {
if (IsException) {
App42Exception exception = (App42Exception) response;
if (exception.GetAppErrorCode() == 2601) {
lbxMessageMyProfileMessageTB.Text = "No Messages";
} else {
lbxMessageMyProfileMessageTB.Text = "Error,Please try again later";
}
} else {
Storage storage = (Storage) response;
_messagelist.Clear();
for (int i = 0; i
Share score using App42 API
This sample uses feature of sharing user score on Facebook. Following API has been used to implement this feature:
1.Link user Facebook account to App42 API using "LinkUserFacebookAccount" method.It takes UserName and AccessToken
public static void LinkUserFacebookAccount(App42ApiResultCallback callBack) {
App42ApiCallback _sCallback = new App42ApiCallback(callBack);
if (mSocialService == null) {
mSocialService = GlobalContext.SERVICE_API.BuildSocialService();
}
mSocialService.LinkUserFacebookAccount(GlobalContext.g_UserProfile.UserID, GlobalContext.AccessToken, _sCallback);
}
2. Once user access token is linked with the user name, you can call UpdateFacebookStatus method using SocialService
public static void ShareStatus(String message, App42ApiResultCallback requestCallback) {
try {
App42ApiCallback _sCallback = new App42ApiCallback(requestCallback);
if (mSocialService == null) {
mSocialService = GlobalContext.SERVICE_API.BuildSocialService();
}
mSocialService.UpdateFacebookStatus(GlobalContext.g_UserProfile.UserID, message, _sCallback);
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
Source Code can be downloaded or viewed from our Git repo. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.
Leave A Reply