Пример #1
0
        public WalletData InitWallet()
        {
            if (Wallet != null)
            {
                return(Wallet);
            }
            TempUserInfo temp;

            try {
                string playerData =
                    File.ReadAllText(Application.streamingAssetsPath + "/defaultPlayerData.json");
                temp = JsonHelper.getObjectFromJson <TempUserInfo>(playerData);
            }
            catch (FileNotFoundException e) {
                SpilLogging.Log("defaultPlayerData.json not found. Creating a placeholder!" + e);
                string placeholder =
                    "{\"wallet\":{\"currencies\":[],\"offset\": 0,\"logic\": \"CLIENT\"},\"inventory\":{\"items\":[],\"offset\":0,\"logic\": \"\"}}";
                temp = JsonHelper.getObjectFromJson <TempUserInfo>(placeholder);
            }

            // Currencies and items loaded from playerdata don't have fields like initialValue because those are defined in the gamedata. Add the missing information.
            // TODO: Should playerdata ever be used to access these fields or only gamedata? A. Check where these fields are being used (they shouldnt be? see SendUpdatePlayerDataEvent tho) and B. Consider maybe this is the wrong inheritance structure / we're exposing fields that shouldn't be exposed for playerdata items/currencies?
            // TODO: Make this prettier? Load both defaultPlayerData and defaultGameData.json and create/initialise wallet+inventory by combining data (instead of deserialising playerdata and adding missing data from gamedata afterwards)?
            if (temp != null && temp.wallet != null && temp.wallet.currencies != null)
            {
                if (SpilUnityEditorImplementation.gData == null)
                {
                    throw new NotImplementedException("GameData must be initialised before calling this method.");
                }

                foreach (PlayerCurrencyData currency in temp.wallet.currencies)
                {
                    SpilCurrencyData gameDataCurrency = SpilUnityEditorImplementation.gData.currencies.FirstOrDefault(a => a.id == currency.id);
                    if (gameDataCurrency != null)
                    {
                        currency.displayDescription = gameDataCurrency.displayDescription;
                        currency.displayName        = gameDataCurrency.displayName;
                        currency.imageUrl           = gameDataCurrency.imageUrl;
                        currency.initialValue       = gameDataCurrency.initialValue;
                        currency.name = gameDataCurrency.name;
                        currency.type = gameDataCurrency.type;
                    }
                    else
                    {
                        // TODO: Playerdata contains a currency that is not defined in the gamedata, should this throw an exception?
                        // TODO: Remove the currency from the list or keep it with missing data (as it is now)?
                    }
                }
            }

            return(temp.wallet);
        }
        public static void ProcessGameDataResponse(ResponseEvent response)
        {
            if (response.data.HasField("currencies"))
            {
                if (SpilUnityEditorImplementation.gData.currencies == null)
                {
                    SpilUnityEditorImplementation.gData.currencies = new List <SpilCurrencyData>();
                }

                if (SpilUnityEditorImplementation.gData.currencies.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.currencies.Clear();
                }

                JSONObject currenciesJSON = response.data.GetField("currencies");

                for (int i = 0; i < currenciesJSON.Count; i++)
                {
                    SpilCurrencyData currency =
                        JsonHelper.getObjectFromJson <SpilCurrencyData>(currenciesJSON.list[i].Print(false));
                    SpilUnityEditorImplementation.gData.currencies.Add(currency);
                }
            }

            if (response.data.HasField("items"))
            {
                if (SpilUnityEditorImplementation.gData.items == null)
                {
                    SpilUnityEditorImplementation.gData.items = new List <SpilItemData>();
                }

                if (SpilUnityEditorImplementation.gData.items.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.items.Clear();
                }

                JSONObject itemsJSON = response.data.GetField("items");

                for (int i = 0; i < itemsJSON.Count; i++)
                {
                    SpilItemData item = JsonHelper.getObjectFromJson <SpilItemData>(itemsJSON.list[i].Print(false));
                    SpilUnityEditorImplementation.gData.items.Add(item);
                }
            }

            if (response.data.HasField("bundles"))
            {
                if (SpilUnityEditorImplementation.gData.bundles == null)
                {
                    SpilUnityEditorImplementation.gData.bundles = new List <SpilBundleData>();
                }

                if (SpilUnityEditorImplementation.gData.bundles.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.bundles.Clear();
                }

                JSONObject bundlesJSON = response.data.GetField("bundles");

                for (int i = 0; i < bundlesJSON.Count; i++)
                {
                    SpilBundleData bundle =
                        JsonHelper.getObjectFromJson <SpilBundleData>(bundlesJSON.list[i].Print(false));
                    SpilUnityEditorImplementation.gData.bundles.Add(bundle);
                }
            }

            if (response.data.HasField("shop"))
            {
                if (SpilUnityEditorImplementation.gData.shop == null)
                {
                    SpilUnityEditorImplementation.gData.shop = new List <SpilShopTabData>();
                }

                if (SpilUnityEditorImplementation.gData.shop.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.shop.Clear();
                }

                JSONObject shopJSON = response.data.GetField("shop");

                for (int i = 0; i < shopJSON.Count; i++)
                {
                    SpilShopTabData tab = JsonHelper.getObjectFromJson <SpilShopTabData>(shopJSON.list[i].Print(false));

                    SpilUnityEditorImplementation.gData.shop.Add(tab);
                }
            }

            GameDataManager.updatedFromServer = true;

            Spil.GameData.RefreshData(Spil.Instance);
            SpilUnityImplementationBase.fireSpilGameDataAvailable();
        }