/// <summary>
        /// Returns a <see cref="Village"/> that will be deserialize from the specified
        /// JSON string with the specified <see cref="Data.AssetManager"/> instance.
        /// </summary>
        ///
        /// <param name="value">
        /// JSON string which represents the <see cref="Village"/>.
        /// </param>
        /// <param name="level">
        /// <see cref="Data.AssetManager"/> from which data of <see cref="VillageObject"/> in the <see cref="Village"/> will be populated.
        /// </param>
        /// <returns>A <see cref="Village"/> that is deserialized from the specified JSON string.</returns>
        ///
        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null or whitespace.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="level"/> is null.</exception>
        /// <exception cref="InvalidOperationException">
        /// <paramref name="level"/> has not loaded <see cref="CsvData"/> type of either of the following;
        /// <see cref="BuildingData"/>, <see cref="ObstacleData"/>, <see cref="TrapData"/> or <see cref="DecorationData"/>.
        /// </exception>
        public static Village FromJson(string value, Level level)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (level == null)
            {
                throw new ArgumentNullException(nameof(level));
            }

            var village = new Village(level);

            var textReader = new StringReader(value);

            using (var jsonReader = new JsonTextReader(textReader))
            {
                while (jsonReader.Read())
                {
                    if (jsonReader.TokenType == JsonToken.PropertyName)
                    {
                        var propertyName = (string)jsonReader.Value;
                        switch (propertyName)
                        {
                        case "exp_ver":
                            village.ExperienceVersion = jsonReader.ReadAsInt32().Value;
                            break;

                        case "buildings":
                            ReadBuildingArray(jsonReader, village);
                            break;

                        case "obstacles":
                            ReadObstacleArray(jsonReader, village);
                            break;

                        case "traps":
                            ReadTrapArray(jsonReader, village);
                            break;

                        case "decos":
                            ReadDecorationArray(jsonReader, village);
                            break;
                        }
                    }
                }
            }

            if (village._townhall == null)
            {
                throw new InvalidOperationException("Village does not contain a Town Hall.");
            }

            // Tick once to update/set VillageObject values and
            // finish construction that are finished.
            village.Update(0);

            return(village);
        }
Example #2
0
        /// <summary>
        /// Ticks all <see cref="VillageObject"/> in the <see cref="Level"/>.
        /// </summary>
        public void Tick(int tick)
        {
            // In case the level is being processed by more than 1 thread.
            lock (Village)
            {
                _lastTick      = DateTime.UtcNow;
                _lastTickValue = tick;

                Village.LastTickTime = _lastTick;
                Village.Update(tick);
            }
        }