public void Migrate(IRavenfallDbContextProvider db, IEntityRestorePoint restorePoint) { try { var sw = new Stopwatch(); sw.Start(); var queryBuilder = new QueryBuilder(); var users = restorePoint.Get <User>(); var characters = restorePoint.Get <Character>(); var skills = restorePoint.Get <Skills>(); var marketplaceItems = restorePoint.Get <MarketItem>(); var syntyAppearance = restorePoint.Get <SyntyAppearance>(); var appearances = restorePoint.Get <Appearance>(); var resources = restorePoint.Get <Resources>(); var charStates = restorePoint.Get <CharacterState>(); var charStats = restorePoint.Get <Statistics>(); var invItems = restorePoint.Get <InventoryItem>(); var villages = restorePoint.Get <Village>(); var villageHouses = restorePoint.Get <VillageHouse>(); var failedCount = 0; using (var con = db.GetConnection()) { con.Open(); logger.LogInformation("Truncating db..."); var restoreTypes = restorePoint.GetEntityTypes(); foreach (var restoreType in restoreTypes) { if (restoreType == typeof(ItemCraftingRequirement)) { continue; } var table = $"[DB_A3551F_ravenfall].[dbo].[{restoreType.Name}]"; try { using (var cmd = con.CreateCommand()) { cmd.CommandText = $"truncate table {table};"; cmd.ExecuteNonQuery(); } logger.LogInformation($"Migrating {table} data..."); var entities = restorePoint.Get(restoreType); var queries = BuildInsertQuery(queryBuilder, entities); foreach (var q in queries) { using (var cmd = con.CreateCommand()) { cmd.CommandText = q; cmd.ExecuteNonQuery(); } } } catch (Exception exc) { logger.LogError($"Failed to truncate table {table}! Aborting migration!! Exc: " + exc); return; } } con.Close(); } sw.Stop(); logger.LogInformation($"Data migration inserted {characters.Count} characters, {failedCount} failed inserts and took a total of: " + sw.Elapsed); } catch (Exception exc) { logger.LogError($"Data migration failed!! " + exc); } }
public void MigrateTest(IEntityRestorePoint restorePoint) { try { var sw = new Stopwatch(); sw.Start(); var queryBuilder = new QueryBuilder(); var users = restorePoint.Get <User>(); var characters = restorePoint.Get <Character>(); var skills = restorePoint.Get <Skills>(); var marketplaceItems = restorePoint.Get <MarketItem>(); var syntyAppearance = restorePoint.Get <SyntyAppearance>(); var appearances = restorePoint.Get <Appearance>(); var resources = restorePoint.Get <Resources>(); var charStates = restorePoint.Get <CharacterState>(); var charStats = restorePoint.Get <Statistics>(); var iitems = restorePoint.Get <InventoryItem>(); IReadOnlyList <InventoryItem> invItems = Merge(iitems); var villages = restorePoint.Get <Village>(); var villageHouses = restorePoint.Get <VillageHouse>(); var failedCount = 0; logger?.LogInformation("Migrating user data..."); var index = 0; foreach (var character in characters) { if (!string.IsNullOrEmpty(character.Identifier) && int.TryParse(character.Name, out var twitchId)) { var user = users.FirstOrDefault(x => x.UserId == character.Name); if (user != null) { var otherChars = characters.Where(x => x.UserId == user.Id).ToList(); if (otherChars.Any(x => x.Id != character.Id)) { continue; } } else { continue; } } try { var cmdQuery = BuildInsertQuery( queryBuilder, character, users, marketplaceItems, skills, syntyAppearance, appearances, resources, charStates, charStats, invItems, villages, villageHouses); //if (results < 7) //{ // failedCount++; // logger.LogError($"{character.Name} failed to migrate!!"); //} } catch (Exception exc) { logger?.LogError($"Failed to migrate player!!: " + exc); } ++index; } sw.Stop(); logger?.LogInformation($"Data migration inserted {characters.Count} characters, {failedCount} failed inserts and took a total of: " + sw.Elapsed); } catch (Exception exc) { logger?.LogError($"Data migration failed!! " + exc); } }
public GameData( IGameDataBackupProvider backupProvider, IRavenfallDbContextProvider db, ILogger <GameData> logger, IKernel kernel, IQueryBuilder queryBuilder) { try { this.db = db; this.logger = logger; this.kernel = kernel; this.queryBuilder = queryBuilder; this.backupProvider = backupProvider; var stopWatch = new Stopwatch(); stopWatch.Start(); using (var ctx = this.db.Get()) { IEntityRestorePoint restorePoint = backupProvider.GetRestorePoint(new[] { typeof(Appearance), typeof(SyntyAppearance), typeof(Character), typeof(CharacterState), typeof(InventoryItem), typeof(User), typeof(GameSession), typeof(Village), typeof(VillageHouse), typeof(Resources), typeof(Skills), typeof(Statistics), typeof(MarketItem) }); appearances = new EntitySet <Appearance, Guid>( restorePoint?.Get <Appearance>() ?? ctx.Appearance.ToList(), i => i.Id); syntyAppearances = new EntitySet <SyntyAppearance, Guid>( restorePoint?.Get <SyntyAppearance>() ?? ctx.SyntyAppearance.ToList(), i => i.Id); characters = new EntitySet <Character, Guid>( restorePoint?.Get <Character>() ?? ctx.Character.ToList(), i => i.Id); characters.RegisterLookupGroup(nameof(User), x => x.UserId); characters.RegisterLookupGroup(nameof(GameSession), x => x.UserIdLock.GetValueOrDefault()); characterStates = new EntitySet <CharacterState, Guid>( restorePoint?.Get <CharacterState>() ?? ctx.CharacterState.ToList(), i => i.Id); gameSessions = new EntitySet <GameSession, Guid>( restorePoint?.Get <GameSession>() ?? ctx.GameSession.ToList(), i => i.Id); gameSessions.RegisterLookupGroup(nameof(User), x => x.UserId); // we can still store the game events, but no need to load them on startup as the DB will quickly be filled. // and take a long time to load gameEvents = new EntitySet <GameEvent, Guid>(new List <GameEvent>() /*ctx.GameEvent.ToList()*/, i => i.Id); gameEvents.RegisterLookupGroup(nameof(GameSession), x => x.GameSessionId); inventoryItems = new EntitySet <InventoryItem, Guid>( restorePoint?.Get <InventoryItem>() ?? ctx.InventoryItem.ToList(), i => i.Id); inventoryItems.RegisterLookupGroup(nameof(Character), x => x.CharacterId); marketItems = new EntitySet <MarketItem, Guid>( restorePoint?.Get <MarketItem>() ?? ctx.MarketItem.ToList(), i => i.Id); marketItems.RegisterLookupGroup(nameof(Item), x => x.ItemId); items = new EntitySet <Item, Guid>(ctx.Item.ToList(), i => i.Id); npcs = new EntitySet <NPC, Guid>(ctx.NPC.ToList(), i => i.Id); npcSpawns = new EntitySet <NPCSpawn, Guid>(ctx.NPCSpawn.ToList(), i => i.Id); npcSpawns.RegisterLookupGroup(nameof(NPC), x => x.NpcId); npcItemDrops = new EntitySet <NPCItemDrop, Guid>(ctx.NPCItemDrop.ToList(), i => i.Id); npcItemDrops.RegisterLookupGroup(nameof(NPC), x => x.NpcId); itemCraftingRequirements = new EntitySet <ItemCraftingRequirement, Guid>(ctx.ItemCraftingRequirement.ToList(), i => i.Id); itemCraftingRequirements.RegisterLookupGroup(nameof(Item), x => x.ItemId); //itemCraftingRequirements.RegisterLookupGroup(nameof(ItemCraftingRequirement.ResourceItemId), x => x.ItemId); clans = new EntitySet <Clan, Guid>(ctx.Clan.ToList(), i => i.Id); villages = new EntitySet <Village, Guid>( restorePoint?.Get <Village>() ?? ctx.Village.ToList(), i => i.Id); villages.RegisterLookupGroup(nameof(User), x => x.UserId); villageHouses = new EntitySet <VillageHouse, Guid>( restorePoint?.Get <VillageHouse>() ?? ctx.VillageHouse.ToList(), i => i.Id); villageHouses.RegisterLookupGroup(nameof(Village), x => x.VillageId); resources = new EntitySet <Resources, Guid>( restorePoint?.Get <Resources>() ?? ctx.Resources.ToList(), i => i.Id); statistics = new EntitySet <Statistics, Guid>( restorePoint?.Get <Statistics>() ?? ctx.Statistics.ToList(), i => i.Id); skills = new EntitySet <Skills, Guid>( restorePoint?.Get <Skills>() ?? ctx.Skills.ToList(), i => i.Id); users = new EntitySet <User, Guid>( restorePoint?.Get <User>() ?? ctx.User.ToList(), i => i.Id); gameClients = new EntitySet <GameClient, Guid>(ctx.GameClient.ToList(), i => i.Id); Client = gameClients.Entities.First(); entitySets = new IEntitySet[] { appearances, syntyAppearances, characters, characterStates, gameSessions, /*gameEvents, */ inventoryItems, marketItems, items, resources, statistics, skills, users, gameClients, villages, villageHouses, clans, npcs, npcSpawns, npcItemDrops }; } stopWatch.Stop(); logger.LogDebug($"All database entries loaded in {stopWatch.Elapsed.TotalSeconds} seconds."); logger.LogDebug("GameData initialized... Starting kernel..."); kernel.Start(); InitializedSuccessful = true; CreateBackup(); } catch (Exception exc) { InitializedSuccessful = false; System.IO.File.WriteAllText("ravenfall-error.log", exc.ToString()); } }