public static async Task <UserUploadHistory> GetOrCreateUserUploadHistory(VaultContext context, int uid) { var userUploadHistory = await context.UserUploadHistory.Where(h => h.Uid == uid).FirstOrDefaultAsync(); if (userUploadHistory == null) { userUploadHistory = new UserUploadHistory(); userUploadHistory.Uid = uid; context.Add(userUploadHistory); } return(userUploadHistory); }
public static async Task <CurrentPlayer> GetOrCreateCurrentPlayer(VaultContext context, long playerId, short worldId, int accessGroupId) { var currentPlayer = await context.CurrentPlayer.FromAccessGroup(accessGroupId).FromWorld(worldId).Where(p => p.PlayerId == playerId).FirstOrDefaultAsync(); if (currentPlayer == null) { currentPlayer = new CurrentPlayer(); currentPlayer.PlayerId = playerId; currentPlayer.WorldId = worldId; currentPlayer.AccessGroupId = accessGroupId; context.Add(currentPlayer); } return(currentPlayer); }
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(); }
public override void Process(VaultContext context, World world, string fetchedContents) { var xml = ParseXml(fetchedContents); var newSettings = Parse(xml); var oldSettings = world.WorldSettings; if (oldSettings == null) { Console.WriteLine("Got world settings for new world, storing"); world.WorldSettings = newSettings; context.Add(world.WorldSettings); context.SaveChanges(); } else if (Overwrite) { var props = typeof(WorldSettings).GetProperties(); var differingValues = new Dictionary <String, object[]>(); foreach (var prop in props) { if (!ComparableSettingPropertyTypes.Contains(prop.PropertyType) || prop.Name == "WorldId") { continue; } var newValue = prop.GetValue(newSettings); var oldValue = prop.GetValue(oldSettings); if (!newValue.Equals(oldValue)) { differingValues.Add(prop.Name, new[] { oldValue, newValue }); } } if (differingValues.Count == 0) { Console.WriteLine("Got updated world settings for existing world, but stored config matches latest. No action necessary."); return; } Console.WriteLine("Got updated world settings for existing world, and new settings do not match the old:"); foreach (var prop in differingValues.Keys) { var values = differingValues[prop]; var oldValue = values[0]; var newValue = values[1]; Console.WriteLine("- {0}: Changed from {1} (old) to {2} (new)", prop, oldValue, newValue); } bool?confirmOverwrite = null; while (!confirmOverwrite.HasValue) { Console.Write("Overwrite? [y/n]: "); var response = Console.ReadLine(); if (response.ToLower().Trim() == "y") { confirmOverwrite = true; } if (response.ToLower().Trim() == "n") { confirmOverwrite = false; } } if (confirmOverwrite.Value) { Console.WriteLine("Copying new config..."); CopyTo(newSettings, oldSettings); context.SaveChanges(); } else { Console.WriteLine("Ignoring new config, keeping old."); } } }