Android - How to check if an achievement has been unlocked - android

I have some incremental achievements on my game and i want to check if one of them is already unlocked. How can i do that?
Originaly i used this method:
getGamesClient().incrementAchievement(achievement, increment);
Then i tried to use this:
mHelper.getGamesClient().incrementAchievementImmediate(new OnAchievementUpdatedListener()
{
#Override
public void onAchievementUpdated(int statusCode, String achievementId)
{
// TODO: Check if the achievement got unlocked
}
}, achievement, increment);
Is this the right way to do what i want? Is there a better way?
I only got two statusCode values on my tests.
Value: 5 = STATUS_NETWORK_ERROR_OPERATION_DEFERRED
Value: 0 = STATUS_OK
Can some one help me with this?
Thanks

Declare this inner class:
class AchievementClass implements ResultCallback<Achievements.LoadAchievementsResult> {
#Override
public void onResult(LoadAchievementsResult arg0) {
Achievement ach;
AchievementBuffer aBuffer = arg0.getAchievements();
Iterator<Achievement> aIterator = aBuffer.iterator();
while (aIterator.hasNext()) {
ach = aIterator.next();
if ("The Achievement Id you are checking".equals(ach.getAchievementId())) {
if (ach.getState() == Achievement.STATE_UNLOCKED) {
// it is unlocked
} else {
//it is not unlocked
}
aBuffer.close();
break;
}
}
}
}
And use it like so:
Games.Achievements.load(ApiClient, false).setResultCallback(new AchievementClass());
This will get achievement data for the currently signed in player, go through all the achievements, and when it reaches the achievement you're concerned with, it will check if it is unlocked.

Related

Unity Google Play Games Create game with invitation problem

I can create games with invites, I can invite and accept invitations, the thing mostly works fine. The problem appears when I make a game for 3 or more people:
user A creates game with 3 people or 2 people at least
user A chooses to invite someone and then adds another person to auto-pick for the room
User A invites user B
User B accepts
They both wait until the "Start now" button appears
User B presses the start now button and OnRoomConnected() is called 3 times for some reason and the game doesn't start (the room was also never left as far as I can tell, because this user can't receive invitations anymore)
Nothing changes from the perspective of user A, he still waits for the game to start or search for another auto pick opponent
I made sure that the problem is not from my code. I created a separate simple project, that I used only for testing purposes and it does exactly the same thing. So I was starting to think maybe it's not a problem from my side and I didn't see similar problems on the internet. So I decided to ask here. What should I do? What could be the problem?
That's basically it. Even if I restrict the number of players to 3 or 4 (min and max number of players are equal, 3 or 4), it still lets me start the game prematurely and I have the same problem with OnRoomConnected() being called multiple times and the game doesn't start.
Thanks in advance. If you have a link or something that would help me solve this problem, it would be greatly appreciated.
Here's the basic code I used for logging in the game and creating a room.
public class GPGM : MonoBehaviour, RealTimeMultiplayerListener{
public static GPGM instance;
public static int target;
private void Awake()
{
target = 60;
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = target;
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
}
// Use this for initialization
void Start () {
Login();
}
// Update is called once per frame
void Update () {
}
public void Login()
{
StartCoroutine(checkInternetConnection((isConnected) =>
{
LoginGPG();
}));
}
IEnumerator checkInternetConnection(Action<bool> action)
{
WWW www;
www = new WWW("http://google.com");
yield return www;
if (!String.IsNullOrEmpty(www.error))
{
Debug.Log("DebugM | no internet connection");
action(false);
}
else
{
Debug.Log("DebugM | There IS connection");
action(true);
}
}
public void LoginGPG()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().WithInvitationDelegate(OnInvitationReceived).Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
Debug.Log("DebugM | LoginGPG");
Auth();
}
public void Auth()
{
Debug.Log("DebugM | Auth");
try
{
//doesn't work sometimes for some reason. It gives null data if success is false
//reason for false success is unknown
Social.localUser.Authenticate((bool succes) =>
{
if (succes)
{
Debug.Log("DebugM | Logged in");
}else
{
Debug.Log("DebugM | authentication failed");
}
});
}
catch (Exception e)
{
Debug.Log("DebugM | Auth() has failed with error: " + e.Message);
}
}
public void OnInvitationReceived(Invitation invitation, bool shouldAutoAccept)
{
StartCoroutine(InvitationCo(invitation, shouldAutoAccept));
}
Invitation mIncomingInvitation;
IEnumerator InvitationCo(Invitation invitation, bool shouldAutoAccept)
{
yield return new WaitUntil(() => SceneManager.GetActiveScene().name == "Lobby");
Debug.Log("DebugM | Invitation has been received!!!");
//StartCoroutine(LM.LoadingAnim());
if (shouldAutoAccept)
{
Debug.Log("DebugM | Should auto accept: TRUE");
PlayGamesPlatform.Instance.RealTime.AcceptInvitation(invitation.InvitationId, instance);
}
else
{
// The user has not yet indicated that they want to accept this invitation.
// We should *not* automatically accept it. Rather we store it and
// display an in-game popup:
Debug.Log("DebugM | Should auto accept: FALSE");
Lobby LM = FindObjectOfType<Lobby>();
LM.invPanel.SetActive(true);
mIncomingInvitation = invitation;
}
}
public void AcceptGoogleInv(GameObject panel)
{
if (mIncomingInvitation != null)
{
// show the popup
//string who = (mIncomingInvitation.Inviter != null &&
// mIncomingInvitation.Inviter.DisplayName != null) ?
// mIncomingInvitation.Inviter.DisplayName : "Someone";
Debug.Log("DebugM | Invitation has been accepted");
PlayGamesPlatform.Instance.RealTime.AcceptInvitation(mIncomingInvitation.InvitationId, instance);
panel.SetActive(false);
}
}
public void CreateQuickRoom()
{
PlayGamesPlatform.Instance.RealTime.CreateWithInvitationScreen(1, 3, 1, instance );
}
public void OnRoomSetupProgress(float percent)
{
Debug.Log("OnRoomSetupProgress()");
PlayGamesPlatform.Instance.RealTime.ShowWaitingRoomUI();
}
public void OnRoomConnected(bool success)
{
SceneManager.LoadScene("Game");
Debug.Log("DebugM | Room conected");
}
public void OnLeftRoom()
{
throw new NotImplementedException();
}
public void OnParticipantLeft(Participant participant)
{
throw new NotImplementedException();
}
public void OnPeersConnected(string[] participantIds)
{
throw new NotImplementedException();
}
public void OnPeersDisconnected(string[] participantIds)
{
throw new NotImplementedException();
}
public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data)
{
throw new NotImplementedException();
}}
Edit (pictures):
This is when I wait for the last auto pick slot to fill (it works like this only when people were invited to the game)
The game goes to lobby for the person who pressed start and the others still wait for the last autopick even if 1 player practically left the room
You can do it like:
public void OnRoomConnected (bool success)
{
if (success)
{
//Start the game here
SceneManager.LoadScene("Game");
Debug.Log("DebugM | Room conected");
}
else
{
//Do somthing else.
}
}
or the best way to do it by checking connected participans count.
public void OnPeersConnected (string[] participantIds)
{
List<Participant> playerscount = PlayGamesPlatform.Instance.RealTime.GetConnectedParticipants();
if (playerscount != null && playerscount.Count > 1)//this condition should be decided by you.
{
//Start the game here
SceneManager.LoadScene("Game");
}
}

How can I know if an achievement is completed in android?

I'm developing an android app with achievements. Is it possible to know if the achievement is unlocked (or if the goal is reached on incremental ones)?
I've found solutions for old implementations which use Games.Achievements and add a callback function, but it is deprecated. This is the code I'm using for unlocking my achievements:
Games.getAchievementsClient(MyActivity.this, acct).unlockImmediate(my_achievement_id));
I'd like to add a listener to know if after the execution of my code the achievement is unlocked.
Solved using load method:
Games.getAchievementsClient(MyActivity.this, acct).load(true).addOnSuccessListener(new OnSuccessListener<AnnotatedData<AchievementBuffer>>() {
#Override
public void onSuccess(AnnotatedData<AchievementBuffer> achievementBufferAnnotatedData) {
AchievementBuffer buffer = achievementBufferAnnotatedData.get();
Achievement achievement;
Iterator<Achievement> iterator = buffer.iterator();
while (iterator.hasNext()) {
achievement = iterator.next();
if (achievement.getAchievementId().equals(myAchievementId) && achievement.getState() == Achievement.STATE_UNLOCKED) {
// The achievement is unlocked
} else {
// The achievement is locked
}
}
}
});

Sharing to Facebook shows error "We are Sorry, this post is no longer available. It may have been removed"

I'm trying to implement Facebook sharing in my game using Unity 3D + Facebook Unity SDK. But when I tried testing to post to my wall, this error shows up: "We are Sorry, this post is no longer available. It may have been removed." Can anybody help me? Thanks in advance.
BTW, here's my code:
using UnityEngine;
using System.Collections;
public class FacebookController : MonoBehaviour {
public bool isUsingFacebook = true; //Are we using Facebook SDK? This variable is only
//Feed parameters.
private string link = "market://details?id=com.LaserCookie.Queue"; //The link that will show the user the game's google play store address
private string linkName = "Queue"; //The link name
private string linkCaption = "Wow this game is great! 10/10 must play!"; // The caption of the link
private string linkDescription = "I achieved the score of " + PlayerController.instance.score.ToString() + "! Try to beat me if you can!"; //The description of the link
private string picture = "http://www.drycreekequestriancenter.com/testImage.jpeg"; //This is the image / game icon for the link. For now, it's shamelessly got from a random source. Thank you, random citizen...
void Awake()
{
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//Init FB
private void Init()
{
if (!FB.IsInitialized)
{
FB.Init(OnInitComplete, OnHideUnity);
}
}
//Callback that will be called when the initialization is completed
private void OnInitComplete()
{
Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
//Check if we are logged in.
//If not, we will log in.
//If we are, post status.
if (!FB.IsLoggedIn)
{
LoginWithPublish();
}
else
{
PostImpl();
}
}
//Callback that will be called when the game is shown / not
private void OnHideUnity(bool isGameShown)
{
Debug.Log("Is game showing? " + isGameShown);
}
//Post to Facebook. This is the only exposed method because we only use this to post to Facebook.
//The initialization and login will be called when needed.
//It will first detect whether or not we have been initialized. And will init if we haven't.
//Then it will check whether or not we have been logged in with publish. And will log in if not.
public void PostToFacebook()
{
//Are we using facebook SDK?
if (isUsingFacebook)
{
if (!FB.IsInitialized) //Check for initialization
{
Init();
}
else if (!FB.IsLoggedIn) //Check for login
{
LoginWithPublish();
}
else //Post if we are already initia;ized and logged in
{
PostImpl();
}
}
}
//The true implementation of the posting
private void PostImpl()
{
FB.Feed("",link, linkName,linkCaption,linkDescription,picture);
}
//Login to Facebook with publish
private void LoginWithPublish()
{
// It is generally good behavior to split asking for read and publish
// permissions rather than ask for them all at once.
//
// In your own game, consider postponing this call until the moment
// you actually need it.
FB.Login("publish_actions", LoginCallback);
}
//Login callback
void LoginCallback(FBResult result)
{
if (result.Error != null)
{
Debug.Log( "Error Response:\n" + result.Error );
//TODO: Retry login if we have error? Or do we display a pop up?
}
else if (!FB.IsLoggedIn)
{
Debug.Log( "Login cancelled by Player" );
//TODO: Do we display a pop up?
}
else
{
Debug.Log( "Login was successful!" );
PostImpl();
}
}
}
You need to add Key Hash for FB application.
Go to My Apps, select you application, open Setting tab, add platform for android, and add you key hash.
check this link out
Setting a Release Key Hash
I've fixed the issue. It turns out it's because I used my still in development google store address as the link. I thought it would be automatically recognized regardless of my app is live or not. Thank you anyway. :)

How to retrieve user score from leaderboard

I am using google play services leaderboard to upload the score of a user. Is there a way to retrieve the score of a user programmatically?
My use case is that I want to run multiplier game where users have level which goes up and down based on their wins/loses (sort of like stackoverflow reputation). I need to retrieve the old score to upload the new modified score.
How can I do that? I didn't see this anywhere on the tutorials.
Thanks
In theory, you can by doing something like that:
gameHelper.getGamesClient().loadLeaderboardMetadata(this, false);
/**
* Called when the data is loaded
*/
#Override
public void onLeaderboardMetadataLoaded(int statusCode, LeaderboardBuffer leaderboards) {
// If the fragment is currently added to an activity
if (isAdded()) {
// There's been an error on leaderboards loading
if (statusCode != GamesClient.STATUS_OK) {
if (leaderboards != null) leaderboards.close();
// Error
return;
}
ArrayList<LeaderboardVariant> variants;
Iterator<Leaderboard> it = leaderboards.iterator();
while (it.hasNext()) {
Leaderboard leaderboard = it.next();
final String id = leaderboard.getLeaderboardId();
variants = leaderboard.getVariants();
// For each leaderboard
for (LeaderboardVariant variant: variants) {
if (variant.getCollection() == LeaderboardVariant.COLLECTION_PUBLIC &&
variant.getTimeSpan() == LeaderboardVariant.TIME_SPAN_ALL_TIME) {
int rawScore = (int) variant.getRawPlayerScore();
}
}
}
}
if (leaderboards != null) leaderboards.close();
}
... but the reality is that you can't because the data is outdated, see here: Play games loadLeaderboardMetadata() returns outdated data
It probably is a bug, but as there's no place to submit play-games bugs...
Anyway, why don't you keep a local copy of the scores you submitted? Wouldn't it be the easier way to do it?
[UPDATE]
It seems that you can achieve what you want by using the loadCurrentPlayerLeaderboardScore instead of the loadLeaderboardMetadata method. That one doesn't seem to return outdated data. Check it here
In case someone is still looking for the solution, according to the latest android releases,the method described in the above answer is deprecated now.
The Latest way to do it is as follows:
private LeaderboardsClient mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
private void updateLeaderboards(final String leaderboardId) {
mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(
leaderboardId,
LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_PUBLIC
).addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
#Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
if (leaderboardScoreAnnotatedData.get() == null)
mLeaderboardsClient.submitScore(leaderboardId, 1);
else {
long currentscore = leaderboardScoreAnnotatedData.get().getRawScore();
mLeaderboardsClient.submitScore(leaderboardId, currentscore + 1);
}
}
});
}

Google Game Services onPeersDisconnected/onP2PDisconnected not being called

As in the title I've been able to connect to Google Game Services, exchange data between two devices and everything is running fine, except one thing: disconnection callbacks.
I tried to intercept both onPeersDisconnected and onP2PDisconnected without any success. The onP2PDisconnected method is being called in the device that get disconnected from Internet but not into device that is still online (so there is no way to tell the player that the other one got disconnected).
After the match is started it seems that the second device is never notified of the accidental disconnection. If the user close the game properly the onPeersLeft method is being called thought.
Is a ping between the two devices really necessary to overcome this "bug"? Am I doing something wrong?
Here is the code I use:
void startQuickGame() {
// quick-start a game with 1 randomly selected opponent
final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
MAX_OPPONENTS, 0);
RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
rtmConfigBuilder.setMessageReceivedListener(this);
rtmConfigBuilder.setRoomStatusUpdateListener(this);
rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
mListener.switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
getGamesClient().createRoom(rtmConfigBuilder.build());
}
And here the simple listeners:
#Override
public void onPeersDisconnected(Room room, List<String> peers) {
Log.d(TAG, "onPeersDisconnected");
updateRoom(room);
}
void updateRoom(Room room) {
Log.d(TAG, "UpdateRoom: "+room.getParticipants().size());
mParticipants = room.getParticipants();
}
#Override
public void onP2PDisconnected(String participantId) {
Log.d(TAG, "onP2PDisconnected");
}
public int getPartecipantsInRooom(){
if(mRoom != null)
return mRoom.getParticipants().size();
else
return -123456;
}
Note that calling getPartecipantsInRooom() after one of the two devices disconnects always return 2, and updateRoom never get called.
Just to be sure this might not work for you, for my applications I use this to let me know when another Participant has left the Room, and it is called immediately :
#Override
public void onPeerLeft(Room room, final List<String> participantIds) {
this.mRoomCurrent = room;
this.mRoomId = this.mRoomCurrent.getRoomId();
this.mParticipants = this.mRoomCurrent.getParticipants();
int connected = 0;
for (Participant p : room.getParticipants()) {
if(p.getStatus() == Participant.STATUS_JOINED) {
connected += 1;
}
}
final int fconnected = connected;
for (String s : listIgnoreTheseIDs) {
//checkint to see if we care anymore about this ID.. if out of game already.. nope
if(s.equals(participantIds.get(0))){
return;
}
}
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
mGHInterface.onPeerLeft(fconnected, participantIds.size());
}
});
}
No idea why there are two items, but like you, I realized the onPeersDisconnected() isn't that reliable, but onPeerLeft() normally gets back to the other devices in under 1 second.
onPeerDisconnected() handles disconnects. So if somebody is still in the application but the network connection is lost, this is called for him.
onPeerLeft() handles participants who leave a room. This is called when somebody explizit leaves the room in the application or the application is minimized, and the room is left on the androids onStop() or onDestroy() callback.
I'm making two player game. So I use this approach
#Override
public void onPeerLeft(Room room, List<String> peersWhoLeft) {
updateRoom(room);
Toast.makeText(MyLauncherActivity.this, "Other player left the game", Toast.LENGTH_LONG).show();
quitGame();
}

Categories

Resources