public static void UpdateUserDataVersions()
        {
            bool check = false;

            for (int i = 0; i < userDataVersions.Count; i++)
            {
                if (userDataVersions[i].deviceId.Equals(SystemInfo.deviceUniqueIdentifier))
                {
                    check = true;
                }
            }

            if (!check)
            {
                UserDataVersion userDataVersion = new UserDataVersion();
                userDataVersion.deviceId = SystemInfo.deviceUniqueIdentifier;
                userDataVersion.version  = 0;

                userDataVersions.Add(userDataVersion);
            }

            for (int i = 0; i < userDataVersions.Count; i++)
            {
                if (userDataVersions[i].deviceId.Equals(SystemInfo.deviceUniqueIdentifier))
                {
                    userDataVersions[i].version++;
                }
            }
        }
        public static JSONObject GenerateUserDataVersionsJSON(List <UserDataVersion> userDataVersions)
        {
            if (userDataVersions.Count == 0)
            {
                UserDataVersion localUserDataVersion = new UserDataVersion();
                localUserDataVersion.deviceId = SystemInfo.deviceUniqueIdentifier;
                localUserDataVersion.version  = 0;

                userDataVersions.Add(localUserDataVersion);
            }

            JSONObject userDataVersionsJSON = new JSONObject();

            foreach (UserDataVersion userDataVersion in userDataVersions)
            {
                userDataVersionsJSON.AddField(userDataVersion.deviceId, userDataVersion.version);
            }

            return(userDataVersionsJSON);
        }
        public static void ProcessUserDataResponse(ResponseEvent responseEvent)
        {
            WalletData             receivedWallet    = null;
            InventoryData          receivedInventory = null;
            string                 gameStateData     = null;
            List <UserDataVersion> userDataVersions  = null;
            UserDataMeta           userDataMeta      = null;
            bool externalChange = false;

            if (responseEvent.data.HasField("playerData"))
            {
                JSONObject playerDataJSON = responseEvent.data.GetField("playerData");
                if (playerDataJSON.HasField("wallet"))
                {
                    JSONObject walletJSON = playerDataJSON.GetField("wallet");

                    receivedWallet = new WalletData();

                    if (walletJSON.HasField("currencies"))
                    {
                        receivedWallet.currencies = new List <PlayerCurrencyData>();

                        JSONObject currenciesJSON = walletJSON.GetField("currencies");

                        for (int i = 0; i < currenciesJSON.Count; i++)
                        {
                            PlayerCurrencyData currency = new PlayerCurrencyData();

                            currency.id             = (int)currenciesJSON.list[i].GetField("id").n;
                            currency.currentBalance = (int)currenciesJSON.list[i].GetField("currentBalance").n;
                            currency.delta          = (int)currenciesJSON.list[i].GetField("delta").n;

                            receivedWallet.currencies.Add(currency);
                        }
                    }

                    receivedWallet.offset = (long)walletJSON.GetField("offset").n;
                    receivedWallet.logic  = walletJSON.GetField("logic").str;
                }

                if (playerDataJSON.HasField("inventory"))
                {
                    JSONObject inventoryJSON = playerDataJSON.GetField("inventory");

                    receivedInventory = new InventoryData();

                    if (inventoryJSON.HasField("items"))
                    {
                        receivedInventory.items = new List <PlayerItemData>();

                        JSONObject itemsJSON = inventoryJSON.GetField("items");

                        for (int i = 0; i < itemsJSON.Count; i++)
                        {
                            PlayerItemData item = new PlayerItemData();

                            item.id     = (int)itemsJSON.list[i].GetField("id").n;
                            item.amount = (int)itemsJSON.list[i].GetField("amount").n;
                            item.delta  = (int)itemsJSON.list[i].GetField("delta").n;

                            receivedInventory.items.Add(item);
                        }
                    }

                    receivedInventory.offset = (long)inventoryJSON.GetField("offset").n;
                    receivedInventory.logic  = inventoryJSON.GetField("logic").str;
                }
            }

            if (responseEvent.data.HasField("gameState"))
            {
                gameStateData = responseEvent.data.GetField("gameState").Print();
            }

            if (responseEvent.data.HasField("externalChange"))
            {
                externalChange = responseEvent.data.GetField("externalChange").b;
            }

            if (responseEvent.data.HasField("deviceVersions"))
            {
                JSONObject deviceVersionsJSON = responseEvent.data.GetField("deviceVersions");
                userDataVersions = new List <UserDataVersion>();

                if (deviceVersionsJSON.Count > 0)
                {
                    for (int i = 0; i < deviceVersionsJSON.Count; i++)
                    {
                        UserDataVersion userDataVersion = new UserDataVersion();
                        userDataVersion.deviceId = deviceVersionsJSON.keys[i];
                        userDataVersion.version  = (int)deviceVersionsJSON.list[i].i;

                        userDataVersions.Add(userDataVersion);
                    }
                }
            }

            if (responseEvent.data.HasField("metaData"))
            {
                JSONObject metaDataJSON = responseEvent.data.GetField("metaData");

                userDataMeta = new UserDataMeta();

                if (metaDataJSON.HasField("serverTime"))
                {
                    userDataMeta.serverTime = metaDataJSON.GetField("serverTime").i;
                }

                if (metaDataJSON.HasField("clientTime"))
                {
                    userDataMeta.clientTime = metaDataJSON.GetField("clientTime").i;
                }

                if (metaDataJSON.HasField("timezoneOffset"))
                {
                    userDataMeta.timezoneOffset = (int)metaDataJSON.GetField("timezoneOffset").i;
                }

                if (metaDataJSON.HasField("deviceModel"))
                {
                    userDataMeta.deviceModel = metaDataJSON.GetField("deviceModel").str;
                }

                if (metaDataJSON.HasField("appVersion"))
                {
                    userDataMeta.appVersion = metaDataJSON.GetField("appVersion").str;
                }
            }

            if (responseEvent.action.ToLower().Trim().Equals("request"))
            {
                UserDataManager.ProcessRequestUserData(receivedWallet, receivedInventory, externalChange, gameStateData, userDataVersions, userDataMeta);
            }
            else if (responseEvent.action.ToLower().Trim().Equals("mergeconflict"))
            {
                UserDataManager.ProcessMergeConflict(receivedWallet, receivedInventory, gameStateData, userDataVersions, userDataMeta);
            }
            else if (responseEvent.action.ToLower().Trim().Equals("merge"))
            {
                UserDataManager.ProcessMergeUserData(userDataVersions);
            }
            else if (responseEvent.action.ToLower().Trim().Equals("lockerror"))
            {
                UserDataManager.ProcessLockError();
            }
            else if (responseEvent.action.ToLower().Trim().Equals("syncerror"))
            {
                UserDataManager.ProcessSyncError();
            }
        }