예제 #1
0
 /// <summary>
 /// Read the data from the legacy flat-file format, add them to the new
 /// repository, and commit the new data.
 /// </summary>
 /// <param name="fisherDataPath">The path to the file containing the fisher data.</param>
 /// <param name="fisherRepository">The repository to import the fisher data to.</param>
 /// <param name="fishRepository">The repository containing the fish data.</param>
 /// <param name="userLookup">The user lookup system to convert the stored usernames into user ids.</param>
 /// <param name="token">The oauth token used to fetch user ids.</param>
 /// <param name="clientId">The client id used to identify the call to fetch the user ids.</param>
 /// <returns>Whether or not the data was imported.</returns>
 public static bool ImportFisherDataIntoSql(string fisherDataPath, IRepository <Fisher> fisherRepository, IRepository <Fish> fishRepository, UserLookup userLookup, string token, string clientId)
 {
     if (File.Exists(fisherDataPath))
     {
         var fisherList = JsonConvert.DeserializeObject <Dictionary <string, LegacyFisher> >(File.ReadAllText(FisherDataPath));
         foreach (var fisher in fisherList)
         {
             userLookup.GetId(fisher.Value.username);
         }
         userLookup.UpdateCache(token, clientId);
         foreach (var fisher in fisherList)
         {
             var records = new List <Catch>();
             foreach (var fish in fisher.Value.biggestFish)
             {
                 records.Add(new Catch()
                 {
                     Fish   = fishRepository.ReadById(fish.ID - 1),
                     UserId = userLookup.GetId(fish.caughtBy),
                     Length = fish.length,
                     Weight = fish.weight
                 });
             }
             fisherRepository.Create(new Fisher()
             {
                 UserId  = userLookup.GetId(fisher.Value.username),
                 Records = records
             });
         }
         fisherRepository.Commit();
         return(true);
     }
     return(false);
 }
예제 #2
0
 /// <summary>
 /// Read the data from the legacy flat-file format, add them to the new
 /// repository, and commit the new data.
 /// </summary>
 /// <param name="fishingLeaderboardDataPath">The path to the file containing the leaderboard data.</param>
 /// <param name="leaderboardRepository">The repository to import the leaderboard data to.</param>
 /// <param name="fishRepository">The repository containing the fish data.</param>
 /// <param name="userLookup">The user lookup system to convert the stored usernames into user ids.</param>
 /// <param name="token">The oauth token used to fetch user ids.</param>
 /// <param name="clientId">The client id used to identify the call to fetch the user ids.</param>
 /// <returns>Whether or not the data was imported.</returns>
 public static bool ImportLeaderboardDataIntoSql(string fishingLeaderboardDataPath, IRepository <Catch> leaderboardRepository, IRepository <Fish> fishRepository, UserLookup userLookup, string token, string clientId)
 {
     if (File.Exists(fishingLeaderboardDataPath))
     {
         var fishingLeaderboard = JsonConvert.DeserializeObject <List <LegacyCatch> >(File.ReadAllText(FishingLeaderboardPath));
         foreach (var record in fishingLeaderboard)
         {
             userLookup.GetId(record.caughtBy);
         }
         userLookup.UpdateCache(token, clientId);
         foreach (var record in fishingLeaderboard)
         {
             leaderboardRepository.Create(new Catch()
             {
                 Fish   = fishRepository.ReadById(record.ID - 1),
                 UserId = userLookup.GetId(record.caughtBy),
                 Length = record.length,
                 Weight = record.weight
             });
         }
         leaderboardRepository.Commit();
         return(true);
     }
     return(false);
 }
예제 #3
0
        public DatabaseMigrationResult Update(SqliteContext context, SqliteRepositoryManager repositoryManager)
        {
            var result = new DatabaseMigrationResult {
                Success = true
            };
            var commands = new string[]
            {
                "CREATE TABLE \"AppSettings\" ([Id] INTEGER PRIMARY KEY, [DatabaseVersion] nvarchar, [GeneralCacheUpdateTime] int NOT NULL, [FishingCastMinimum] int NOT NULL, [FishingCastMaximum] int NOT NULL, [FishingHookLength] int NOT NULL, [FishingUseNormalRarity] bit NOT NULL, [FishingUseNormalSizes] bit NOT NULL, [FishingGloatCost] int NOT NULL, [FishingTournamentDuration] int NOT NULL, [FishingTournamentInterval] int NOT NULL, [FishingTournamentCastMinimum] int NOT NULL, [FishingTournamentCastMaximum] int NOT NULL)",
                "CREATE TABLE \"Catches\" ([Id] INTEGER PRIMARY KEY, [UserId] nvarchar, [Length] real NOT NULL, [Weight] real NOT NULL, [Points] int NOT NULL, [Fish_Id] int, [Fisher_Id] int, FOREIGN KEY (Fish_Id) REFERENCES \"Fish\"(Id), FOREIGN KEY (Fisher_Id) REFERENCES \"Fishers\"(Id))",
                "CREATE TABLE \"Fish\" ([Id] INTEGER PRIMARY KEY, [Name] nvarchar, [MinimumLength] real NOT NULL, [MaximumLength] real NOT NULL, [MinimumWeight] real NOT NULL, [MaximumWeight] real NOT NULL, [FlavorText] nvarchar, [Rarity_Id] int, [SizeCategory_Id] int, FOREIGN KEY (Rarity_Id) REFERENCES \"FishRarities\"(Id), FOREIGN KEY (SizeCategory_Id) REFERENCES \"FishSizes\"(Id))",
                "CREATE TABLE \"FishRarities\" ([Id] INTEGER PRIMARY KEY, [Name] nvarchar, [Weight] real NOT NULL)",
                "CREATE TABLE \"FishSizes\" ([Id] INTEGER PRIMARY KEY, [Name] nvarchar, [Message] nvarchar)",
                "CREATE TABLE \"Fishers\" ([Id] INTEGER PRIMARY KEY, [UserId] nvarchar)",
                "CREATE TABLE \"UserMaps\" ([Id] INTEGER PRIMARY KEY, [Username] nvarchar, [TwitchId] nvarchar)",
                "CREATE INDEX \"IX_Catch_Fish_Id\" ON \"Catches\" (\"Fish_Id\")",
                "CREATE INDEX \"IX_Catch_Fisher_Id\" ON \"Catches\" (\"Fisher_Id\")",
                "CREATE INDEX \"IX_Fish_Rarity_Id\" ON \"Fish\" (\"Rarity_Id\")",
                "CREATE INDEX \"IX_Fish_SizeCategory_Id\" ON \"Fish\" (\"SizeCategory_Id\")",
                "ALTER TABLE \"TournamentEntries\" RENAME COLUMN [Name] TO [UserId]"
            };

            result.DebugOutput.Add("Executing SQL statements to add/update tables...");
            foreach (var command in commands)
            {
                result.DebugOutput.Add(command);
                try
                {
                    context.Database.ExecuteSqlCommand(command);
                }
                catch (Exception e)
                {
                    result.DebugOutput.Add($"Exception: {e}");
                }
            }

            foreach (var tournament in repositoryManager.TournamentResults.Read())
            {
                foreach (var entry in tournament.Entries)
                {
                    UserLookup.GetId(entry.UserId);
                }
            }
            foreach (var userRole in repositoryManager.UserRoles.Read())
            {
                var userList = userRole.UserIds;
                foreach (var user in userList)
                {
                    UserLookup.GetId(user);
                }
            }
            UserLookup.UpdateCache(Token, ClientId);

            foreach (var tournament in repositoryManager.TournamentResults.Read())
            {
                foreach (var entry in tournament.Entries)
                {
                    entry.UserId = UserLookup.GetId(entry.UserId);
                }
                repositoryManager.TournamentResults.Update(tournament);
            }
            repositoryManager.TournamentResults.Commit();
            foreach (var userRole in repositoryManager.UserRoles.Read())
            {
                var userList = userRole.UserIds;
                var idList   = new List <string>();
                foreach (var user in userList)
                {
                    idList.Add(UserLookup.GetId(user));
                }
                userRole.UserIds = idList;
                repositoryManager.UserRoles.Update(userRole);
            }
            repositoryManager.UserRoles.Commit();

            return(result);
        }