playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.45k stars 962 forks source link

Google cloud save returns one old value #1272

Open hgvyas123 opened 8 years ago

hgvyas123 commented 8 years ago

When ever i try to fetch data from cloud it always return me data which was set one time ago please let me know in case i am doing something wrong

scenario is like saving data to cloud "abc" then saving data again say "xyz" and now whenever i try to fetch data from cloud it returns me "abc" in case now i again set data "ghj" and fetch again from cloud it gives "xyz" why?

using UnityEngine; using System.Collections; using GooglePlayGames; using GooglePlayGames.BasicApi.Multiplayer; using System.Collections.Generic; using UnityEngine.SocialPlatforms; using GooglePlayGames.BasicApi; using GooglePlayGames.BasicApi.SavedGame; using System; using System.Text;

public class myFinalCloudSaveController {

region field declaration

private static myFinalCloudSaveController _instance = null;
public string myString = "blank";
bool isForReading;
ISavedGameMetadata myGameMetaData;
byte[] dataToWriteOnCloud;
private System.DateTime mLoadedTime;
ISavedGameClient savedGameClient;

#endregion

#region constructors
private myFinalCloudSaveController() {
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
        // registers a callback to handle game invitations.
        .EnableSavedGames()
        .Build();

    PlayGamesPlatform.InitializeInstance(config);
    mLoadedTime = DateTime.Now;
    // Activate the Google Play Games platform
    PlayGamesPlatform.Activate();

    PlayGamesPlatform.DebugLogEnabled = false;//to enable logs
    PlayGamesPlatform.Activate ();//initialize play service

    myString = "initialized";
}
#endregion 

#region get instance
public static myFinalCloudSaveController Instance {
    get {
        if (_instance == null) {
            _instance = new myFinalCloudSaveController();
        }
        return _instance;
    }
}
#endregion

#region authentication
public void SignInToGoogle() {
    if (! PlayGamesPlatform.Instance.localUser.authenticated) {
        PlayGamesPlatform.Instance.localUser.Authenticate((bool success) => {
            if (success) {
                Debug.Log ("We're signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
            } else {
                Debug.Log ("Oh... we're not signed in.");
            }
        });
    } else {
        Debug.Log ("You're already signed in.");
    }
}
#endregion

#region method to handle read and write from cloud
//to open file
void OpenSavedGame(string filename) {
    savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
        ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
}

public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {
    if (status == SavedGameRequestStatus.Success) {
        // handle reading or writing of saved game.
        if(isForReading){
            LoadGameData(game);
        }else{
            SaveGame(game);
        }
    } else {
        // handle error
        Debug.Log("error occurered at OnSavedGameOpened");
    }
}

//to load game

void LoadGameData (ISavedGameMetadata game) {
    //ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);
    Debug.Log("last modified timestamp is "+ game.LastModifiedTimestamp.ToString());

}

public void OnSavedGameDataRead (SavedGameRequestStatus status, byte[] data) {
    if (status == SavedGameRequestStatus.Success) {
        // handle processing the byte array data
        string byteToJson = Encoding.ASCII.GetString(data);
        myJsonClass readFromJson = JsonUtility.FromJson<myJsonClass>(byteToJson);
        Debug.Log("data from cloud are : "+readFromJson.playerName + "  "+readFromJson.level + "  "+readFromJson.timeElapsed);

    } else {
        Debug.Log("error occurered at OnSavedGameDataRead");
        // handle error
    }
}
//to save game
void SaveGame (ISavedGameMetadata game/*, byte[] savedData, TimeSpan totalPlaytime*/) {
    //ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;

    SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
    TimeSpan delta = DateTime.Now.Subtract(mLoadedTime);
    TimeSpan newTimeSpan = game.LastModifiedTimestamp.TimeOfDay.Add(delta);
    builder = builder
        .WithUpdatedPlayedTime(newTimeSpan)
        .WithUpdatedDescription("Saved game at " + DateTime.Now.ToString());
    Debug.Log("last save game at time span is : "+DateTime.Now.TimeOfDay.ToString());
    SavedGameMetadataUpdate updatedMetadata = builder.Build();
    savedGameClient.CommitUpdate(game, updatedMetadata, dataToWriteOnCloud, OnSavedGameWritten);

}

public void OnSavedGameWritten (SavedGameRequestStatus status, ISavedGameMetadata game) {
    if (status == SavedGameRequestStatus.Success) {
        // handle reading or writing of saved game.
        Debug.Log("saved ");
    } else {
        // handle error
        Debug.Log("error occurered at OnSavedGameWritten");
    }
}

public void ShowSelectUI() {
    uint maxNumToDisplay = 5;
    bool allowCreateNew = false;
    bool allowDelete = true;

    ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    savedGameClient.ShowSelectSavedGameUI("Select saved game",
        maxNumToDisplay,
        allowCreateNew,
        allowDelete,
        OnSavedGameSelected);
}

public void OnSavedGameSelected (SelectUIStatus status, ISavedGameMetadata game) {
    if (status == SelectUIStatus.SavedGameSelected) {
        // handle selected game save
    } else {
        // handle cancel or error
    }
}

#endregion

#region custom methods

public void readGameFromCloud(string fileName){
    isForReading = true;
    OpenSavedGame(fileName);
}

public void saveGameOnCloud(string fileName,byte[] data){
    isForReading = false;
    dataToWriteOnCloud = data;
    OpenSavedGame(fileName);
}

#endregion

}

FussenKuh commented 8 years ago

Unfortunately, I can't provide any help in resolving the issue. BUT, I want to chime in to say that I'm experiencing the exact same problem that you're describing. It's quite frustrating since, as far as I can tell, my code basically follows the Google examples.

If I go to App Manager on my phone, pull up the details for Google Play Services, go to Manage Storage, press the "Clear All Data" button and relaunch my game, the issue is temporarily "fixed." However, doing the above usually means I lose any old save game data. And, it's ultimately just a matter of time before the problem returns.

For me, the puzzling thing is that I never get any failure/negative status responses back from the Google Plugin. Whether it's opening a save file, loading the opened file, saving an opened file, etc, Google always send back success status values. If Google isn't telling me that it's failing (or why it's failing), I can't do much of anything to correct the failure.

gamozome commented 8 years ago

I get the same strange behavior . Saved data doesn't save every time.

miracle7 commented 8 years ago

I have also experienced this issue, which is quite frustrating.

This doesn't happen consistently for me though, it only occasionally sends back old saves, which can result in a conflict on a single device (where what google gives us is not the same as the last one we uploaded).

Sounds like something is wrong with the google play services app, since the fix for this issue is 'Clear All Data' for Google Play Services as well: https://github.com/playgameservices/play-games-plugin-for-unity/issues/1262

Hopefully we hear something from the gpg unity team regarding this soon.

AliBenba commented 8 years ago

I have the same problem. Sending data to the cloud is always followed by a success event, but downloading data from the cloud, sometimes I get the old value. I have tried deleting cloud data before sending new data to the cloud. It did fix sending and loading data on one device but synchronization between devices is still corrupted.

ghost commented 8 years ago

I'm also having issues with cloud save. Everything worked great before but about a month ago the issues started. See also: #1254

This makes my game completely unplayable. If google is not fixing this soon, I will have to resort to other backend services. @claywilkinson any comments on the issue?

claywilkinson commented 8 years ago

I've passed along your experiences (and the frustration) - I'll keep the thread updated if I hear anything more. If you have a log of the problem happening that would be great for me to pass along as well and may help point to a solution.

ghost commented 8 years ago

I'll explain what happens in my case rather than providing a log because an explanation will be clearer. I have the following scenario in the game:

  1. A player opens up a save, creates a session ID and saves the game state (including the session ID) with DataSource.ReadNetworkOnly turned on
  2. A bit later the player opens up and loads the save also with DataSource.ReadNetworkOnly turned on
  3. Then the player checks whether the loaded sessionID equals the one created on step 1.

The problem is that on Android the save on step 1. does not go to the cloud. Then on step 2. the load happens from the cloud so an old value is loaded. As a result, the session ID:s differ on step 3 even though they should not.

The Playgameservices notifies that the save on step 1. was successful even though it did not go to the cloud. It does that even if the player has no connection to the internet.

It used to work before but about a month ago it broke on Android. On iOS the save properly goes to the cloud.

@claywilkinson I hope this points you to the right direction and the problem will be fixed soon.

gfaraj commented 8 years ago

I'm also experiencing this same behavior. No conflict is being reported by GPGS when saving yet during loading it's always giving me some old game's data. Is there any indication that this will be fixed soon?

claywilkinson commented 8 years ago

Internally the developers working on the issue are making progress, but I don't have any specific news yet. Stay Tuned!

sinmaplewing commented 8 years ago

We got the same problem in our project, and we made a very simple APP using this plugin (just save and load) to test the google cloud sync.

It works like this: Step 1. Load data => Get A Step 2. Save data => Save B Step 3. Immediately, load data => Get B Step 4. Wait a few second, load data => Get A

Sometimes we immediately load data but get A..... I think the google cloud sync does somthing to recover the new data to the old one...... But I don't know why...... Hope this information will points you to the right direction and fix the problem soon.....

patrickAdage commented 8 years ago

we got the same issue on our project. @claywilkinson is there any thread/forum you follow up on? any progress? users are really not happy about losing progress.

patrickAdage commented 7 years ago

Is there any news on this? We had to disable the cloud saving because too many users reported lost or reset data, and we don't have the resources to have our own cloud save system. Anyone any suggestions?

ghost commented 7 years ago

@patrickAdage: Check out what clayWilkinson's posted a week ago on #1262

patrickAdage commented 7 years ago

@TuuttitaR I read it already, but wasn't sure it is directly related. In either case, the players experienced the problem in the last days, so I guess that roll out is not finished yet.

ghost commented 7 years ago

@patrickAdage Yes the issues are directly related but Google Play Games app has not yet been updated. From my understanding of clayWilkinson's post, the update rolling out by the end of the month will prevent future issues from arising. Not sure what happens in case of existing conflict loops.

ghost commented 7 years ago

Not experiencing this issue anymore, the newest Google Drive update seemed to have fixed the problem!

claywilkinson commented 7 years ago

Thanks for the confirmation. I'm glad it is working again!

Maria-Angelova commented 7 years ago

So sorry to spoil the party but it's still not working for us and LIMBO..

ghost commented 7 years ago

@Maria-Angelova: Are you still experiencing the issue for completely new saved games or only existing cases?

Maria-Angelova commented 7 years ago

With a new account, new saved game from middle of last week on a fully updated Nexus 7 tablet, we have a conflict that seems unresolvable. (This is the only time we have managed to reproduce it).