Example #1
0
        //process the login result and all user data retrieved from PlayFab, such as
        //user inventory with purchased products, virtual currency balances and title data
        private static void OnLoggedIn(PlayFab.ClientModels.LoginResult result)
        {
            instance.loginResult = null;
            userId = result.PlayFabId;
            bool skipPayload = result.InfoResultPayload == null ? true : false;

            if (IAPManager.isDebug)
            {
                Debug.Log("Got PlayFabID: " + userId + (result.NewlyCreated ? " (new account)" : " (existing account)"));
            }

            #if PLAYFAB_VALIDATION
            skipPayload = true;
            #endif

            if (skipPayload)
            {
                IAPManager.GetInstance().Initialize();
                if (loginSucceededEvent != null)
                {
                    loginSucceededEvent();
                }

                return;
            }

            instance.serverData = new JSONClass();

            List <ItemInstance> inventory = result.InfoResultPayload.UserInventory;
            if (inventory != null && inventory.Count != 0)
            {
                string itemId = null;
                for (int i = 0; i < inventory.Count; i++)
                {
                    itemId = inventory[i].ItemId;
                    IAPObject obj = IAPManager.GetIAPObject(itemId);

                    if (obj != null)
                    {
                        switch (obj.type)
                        {
                        case ProductType.Consumable:
                            if (obj.editorType == IAPType.Virtual)
                            {
                                instance.serverData[DBManager.playerKey][obj.id] = new SimpleJSON.JSONData((int)inventory[i].RemainingUses);
                            }
                            break;

                        default:
                            instance.serverData[DBManager.contentKey][obj.id].AsInt = 1;
                            break;
                        }
                    }
                }
            }

            Dictionary <string, int> virtualCurrency = result.InfoResultPayload.UserVirtualCurrency;
            if (virtualCurrency != null && virtualCurrency.Count != 0)
            {
                Dictionary <string, int> currency = DBManager.GetCurrencies();
                foreach (KeyValuePair <string, int> pair in virtualCurrency)
                {
                    //update local data in memory
                    foreach (KeyValuePair <string, int> cur in currency)
                    {
                        if (cur.Key.StartsWith(pair.Key, StringComparison.OrdinalIgnoreCase))
                        {
                            instance.serverData[DBManager.currencyKey][cur.Key].AsInt = pair.Value;
                            break;
                        }
                    }
                }
            }

            Dictionary <string, UserDataRecord> userData = result.InfoResultPayload.UserData;
            if (userData != null && userData.Count != 0)
            {
                string[] userKey = instance.accountParams.UserDataKeys.ToArray();
                for (int i = 0; i < userKey.Length; i++)
                {
                    if (userData.ContainsKey(userKey[i]) && !string.IsNullOrEmpty(userData[userKey[i]].Value))
                    {
                        JSONNode node = JSON.Parse(userData[userKey[i]].Value);
                        foreach (string groupKey in node.AsObject.Keys)
                        {
                            JSONArray array = node[groupKey].AsArray;

                            //it's a simple value, not an array (usually Player data)
                            if (array == null)
                            {
                                instance.serverData[userKey[i]][groupKey] = node[groupKey].Value;
                                continue;
                            }

                            //it's an array of values (usually Selected group data)
                            for (int j = 0; j < array.Count; j++)
                            {
                                instance.serverData[userKey[i]][groupKey][j] = array[j].Value;
                            }
                        }
                    }
                }
            }

            /*
             * //try to resolve easy conflicts automatically in case of empty data,
             * //but forward more complex conflicts to the user for resolution,
             * //or use pre-defined resolution scenario assigned in the inspector
             * if (string.IsNullOrEmpty(DBManager.Read()))
             * {
             *  //local data is empty: get and overwrite with server data
             *  GetInstance().HandleDataConflict(MergeConflictRule.UseServer);
             * }
             * else if (string.IsNullOrEmpty(instance.serverData.ToString()))
             * {
             *  //server data is empty: keep local and upload to server
             *  GetInstance().HandleDataConflict(MergeConflictRule.UseLocal);
             * }
             * else if (DBManager.Read() != instance.serverData.ToString())
             * {
             *
             *  Debug.Log("On Local: " + DBManager.Read());
             *  Debug.Log("On Server: " + instance.serverData.ToString());
             *  Debug.LogWarning("Merge Conflict detected.");
             *
             *  //conflict detected: local and server data is different
             *  //use pre-defined resolution rule, if any
             *  if (instance.mergeRule != MergeConflictRule.UserPrompt)
             *      GetInstance().HandleDataConflict(instance.mergeRule);
             *  else if (dataConflictEvent != null)
             *      dataConflictEvent();
             *
             *  return;
             * }
             */

            DBManager.Overwrite(instance.serverData.ToString());
            IAPManager.GetInstance().Initialize();

            if (loginSucceededEvent != null)
            {
                loginSucceededEvent();
            }
        }