Exemplo n.º 1
0
        private static void FetchWorldData(VaultContext context, List <World> worlds, Dictionary <String, LocaleSettings> hostLocales, String hostname, bool overwrite)
        {
            var host = HostFor(hostname);

            var localeSettings = hostLocales.ContainsKey(host) ? hostLocales[host] : null;

            var fetchers = new IFetcher[]
            {
                new BuildingFetcher(),
                new ConfigFetcher()
                {
                    Overwrite         = overwrite,
                    DefaultTimeZoneId = localeSettings?.TimeZoneId ?? "Europe/London"
                },
                new UnitFetcher()
            };

            var world = worlds.Where(w => w.Hostname == hostname).SingleOrDefault();

            if (world == null)
            {
                short translationId;
                if (localeSettings != null)
                {
                    translationId = localeSettings.DefaultTranslationId;
                }
                else
                {
                    translationId = 1;
                    Console.WriteLine($"Warning: No default translation could be found for {hostname}, defaulting to primary English translation.");
                }

                world = new World
                {
                    Name                 = hostname.Split('.')[0],
                    Hostname             = hostname,
                    DefaultTranslationId = translationId
                };
                context.Add(world);
            }

            var pendingFetchers = fetchers.Where(f => f.NeedsUpdate(world)).ToList();

            if (pendingFetchers.Count > 0)
            {
                Console.WriteLine("Pulling for world {0}...", world.Hostname);
            }
            else
            {
                Console.WriteLine("World {0} is up to date", world.Hostname);
            }

            var baseUrl = $"https://{world.Hostname}";

            foreach (var fetcher in pendingFetchers)
            {
                var url = $"{baseUrl}{fetcher.Endpoint}";
                Console.Write("Fetching {0} ... ", url); Console.Out.Flush();

                var response = httpClient.GetAsync(url).Result;
                if ((int)response.StatusCode >= 300 && (int)response.StatusCode < 400)
                {
                    Console.WriteLine("Warning: server {0} seems to have ended (redirection occurred)", world.Hostname);
                    break;
                }

                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine("ERROR: Request failed with code {0}", response.StatusCode);
                    Environment.ExitCode = 1;
                    continue;
                }

                Console.Write("Processing... "); Console.Out.Flush();

                var data = response.Content.ReadAsStringAsync().Result;
                fetcher.Process(context, world, data);

                Console.WriteLine("Done.");
            }

            var    worldSettings = context.WorldSettings.Where(s => s.WorldId == world.Id).First();
            String timezoneId;

            if (localeSettings != null)
            {
                timezoneId = hostLocales[host].TimeZoneId;
            }
            else
            {
                Console.WriteLine($"Warning: No timezone ID could be found for {hostname}, please manually enter a timezone ID for the server.");
                Console.WriteLine("An exhaustive list of Timezone IDs can be found at: https://nodatime.org/TimeZones");
                Console.WriteLine("(The default for .net and .co.uk is 'Europe/London'.)");

                do
                {
                    Console.Write("Timezone ID: ");
                    timezoneId = Console.ReadLine().Trim();

                    if (NodaTime.TimeZones.TzdbDateTimeZoneSource.Default.ForId(timezoneId) == null)
                    {
                        Console.WriteLine("Invalid ID: " + timezoneId);
                        timezoneId = null;
                    }
                } while (timezoneId == null);

                hostLocales.Add(host, new LocaleSettings {
                    DefaultTranslationId = world.DefaultTranslationId, TimeZoneId = timezoneId
                });
            }

            worldSettings.TimeZoneId = timezoneId;
            context.SaveChanges();
        }