public AdminDATDB() { mw = Application.Current.Windows.OfType <MainWindow>().FirstOrDefault(); systems = DAT_System.GetSystems(); }
/// <summary> /// Creates / updates the top-level games table (DAT_Game) /// matching rom names and updating the gid in DAT_Rom /// </summary> public async void ProcessTopLevelGames() { var mySettings = new MetroDialogSettings() { NegativeButtonText = "Cancel Procedure", AnimateShow = false, AnimateHide = false }; int gamesCreated = 0; int gamesUpdated = 0; int romsUpdated = 0; string output = "Processing DAT_Game Table \n"; var controller = await mw.ShowProgressAsync("Top-Level Game Builder", output, true, settings : mySettings); controller.SetCancelable(true); controller.SetIndeterminate(); await Task.Delay(1000); await Task.Run(() => { List <DAT_Game> AllGames = DAT_Game.GetGames(); // iterate through each platform (system) and do processing foreach (DAT_System sys in DAT_System.GetSystems()) { int gamesCreatedProc = 0; int gamesUpdatedProc = 0; int romsMatchedProc = 0; // instantiate working lists List <DAT_Game> gWorking = new List <DAT_Game>(); List <DAT_Rom> rWorking = new List <DAT_Rom>(); // get all roms wihtout a gameid set controller.SetMessage(output + "Loading ROMs for " + sys.platformName); List <DAT_Rom> roms = DAT_Rom.GetRomsWithNoGameId(sys.pid); // get all games for this system controller.SetMessage(output + "Loading Games for " + sys.platformName); List <DAT_Game> games = AllGames.Where(a => a.pid == sys.pid).ToList(); // iterate through each ROM foreach (var rom in roms) { controller.SetMessage(output + "Processing ROMs for " + sys.platformName + "\n\n***In-Progress Stats***" + "\nGames Created (processing): " + gamesCreatedProc + "\nGames Updated (processing): " + gamesUpdatedProc + "\nROMs Matched (processing): " + romsMatchedProc + "\n\n***Database Stats***" + "\nGames Created: " + gamesCreated + "\nGames Updated: " + gamesUpdated + "\nROMs Updated: " + romsUpdated ); // check whether game already exists var gCheck = (from a in games where FormatName(a.gameName) == FormatName(rom.name) select a).FirstOrDefault(); if (gCheck == null) { // no game found - create one DAT_Game dg = new DAT_Game(); dg.gameName = rom.name; dg.pid = sys.pid; if (rom.year != null && rom.year.Trim() != "") { dg.year = rom.year; } if (rom.publisher != null && rom.publisher.Trim() != "") { dg.publisher = rom.publisher; } if (rom.developer != null && rom.developer.Trim() != "") { dg.developer = rom.developer; } // get the latest availble gid and set this var newg = AllGames.ToList().OrderByDescending(a => a.gid).FirstOrDefault(); int gid = 1; if (newg != null) { gid = (newg.gid + 1); } // set gid on the game object dg.gid = gid; // add to working game set gWorking.Add(dg); // since we have created a new game - add it to the games list games.Add(dg); AllGames.Add(dg); gamesCreatedProc++; // update the gid in the rom record and add to working set DAT_Rom dr = rom; rom.gid = gid; rWorking.Add(dr); } else { // game has been found - update the game (if neccessary) and update the ROM with the gameId DAT_Game dg = gCheck; bool updateNeeded = false; if (rom.year != null && rom.year.Trim() != "") { dg.year = rom.year; updateNeeded = true; } if (rom.publisher != null && rom.publisher.Trim() != "") { updateNeeded = true; dg.publisher = rom.publisher; } if (rom.developer != null && rom.developer.Trim() != "") { updateNeeded = true; dg.developer = rom.developer; } // update game directly if (updateNeeded == true) { gWorking.Add(dg); gamesUpdatedProc++; } // update the gid in the rom record and add to working set DAT_Rom dr = rom; rom.gid = dg.gid; rWorking.Add(dr); } romsMatchedProc++; } // process working list of games controller.SetMessage(output + "Updating Games in Database for " + sys.platformName + "\n\n***In-Progress Stats***" + "\nGames Created (processing): " + gamesCreatedProc + "\nGames Updated (processing): " + gamesUpdatedProc + "\nROMs Matched (processing): " + romsMatchedProc + "\n\n***Database Stats***" + "\nGames Created: " + gamesCreated + "\nGames Updated: " + gamesUpdated + "\nROMs Updated: " + romsUpdated ); int[] resG = DAT_Game.SaveToDatabase(gWorking); gamesCreated += resG[0]; gamesUpdated += resG[1]; // now process the working list of ROMs controller.SetMessage(output + "Updating ROMs in Database for " + sys.platformName + "\n\n***In-Progress Stats***" + "\nGames Created (processing): " + gamesCreatedProc + "\nGames Updated (processing): " + gamesUpdatedProc + "\nROMs Matched (processing): " + romsMatchedProc + "\n\n***Database Stats***" + "\nGames Created: " + gamesCreated + "\nGames Updated: " + gamesUpdated + "\nROMs Updated: " + romsUpdated ); DAT_Rom.UpdateRoms(rWorking); romsUpdated += romsMatchedProc; } }); await controller.CloseAsync(); if (controller.IsCanceled) { await mw.ShowMessageAsync("DAT_Game Builder", "Procedure Cancelled"); } else { await mw.ShowMessageAsync("DAT_Game Builder", "Procedure Completed\n\nGames Created/Updated: " + (gamesCreated + gamesUpdated) + "\nRoms Updated: " + romsUpdated); } }