Advertisement
LJLim

Duplicate fixer class

Mar 2nd, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.11 KB | None | 0 0
  1. using FileHelpers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using ZkData;
  7.  
  8. namespace Fixer
  9. {
  10.     public class DuplicateFinder
  11.     {
  12.         [DelimitedRecord(",")]
  13.         class ZKAccount
  14.         {
  15.             public int AccountID;
  16.             public string Name;
  17.         }
  18.  
  19.         public static void PrintDuplicates(Dictionary<string, List<int>> duplicatesByName)
  20.         {
  21.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"duplicateaccounts.htm"))
  22.             {
  23.                 file.WriteLine("DUPLICATES<br/>");
  24.                 foreach (var duplicate in duplicatesByName)
  25.                 {
  26.                     string duplies = "";
  27.                     foreach (int accountID in duplicate.Value)
  28.                     {
  29.                         duplies = duplies + String.Format("<a href=http://zero-k.info/Users/Detail/{0}>{0}</a>, ", accountID);
  30.                     }
  31.                     file.WriteLine("{0}: {1}<br/>", duplicate.Key, duplies);
  32.                 }
  33.             }
  34.         }
  35.  
  36.         public static void FixDuplicates(Dictionary<string, List<int>> duplicatesByName)
  37.         {
  38.             var db = new ZkDataContext();
  39.  
  40.             foreach (var duplicate in duplicatesByName)
  41.             {
  42.                 Account primaryAccount = null;
  43.                 int highestXP = -1;
  44.                 foreach (int accountID in duplicate.Value)
  45.                 {
  46.                     Account acc = db.Accounts.FirstOrDefault(x => x.AccountID == accountID);
  47.                     if ((acc.SpringBattles != null && acc.SpringBattles.Count > 0) || primaryAccount == null)
  48.                     {
  49.                         if (acc.Xp > highestXP)
  50.                         {
  51.                             primaryAccount = acc;
  52.                             highestXP = acc.Xp;
  53.                             Console.WriteLine("Primary account is " + acc.Name);
  54.                         }
  55.                     }
  56.                 }
  57.  
  58.                 foreach (int accountID in duplicate.Value)
  59.                 {
  60.                     Account acc = db.Accounts.FirstOrDefault(x => x.AccountID == accountID);
  61.                     if (acc == primaryAccount) continue;
  62.  
  63.                     Console.WriteLine("Terminatoring account: " + acc.Name);
  64.  
  65.                     if (acc.AccountCampaignProgress != null && acc.AccountCampaignProgress.Count > 0)
  66.                     {
  67.                         Console.WriteLine("\tAccount has campaign progress, aborting");
  68.                         continue;
  69.                        
  70.                         foreach (AccountCampaignProgress progress in acc.AccountCampaignProgress)
  71.                         {
  72.                             // FIXME
  73.                             //db.AccountCampaignProgress.DeleteOnSubmit(progress);
  74.                         }
  75.                     }
  76.                     if (acc.AccountRolesByAccountID != null)
  77.                         db.AccountRoles.DeleteAllOnSubmit(acc.AccountRolesByAccountID);
  78.                     acc.FactionID = null;
  79.                     acc.ClanID = null;
  80.                     if (acc.Commanders != null)
  81.                         db.Commanders.DeleteAllOnSubmit(acc.Commanders);
  82.                     if (acc.ForumPosts != null && acc.ForumPosts.Count > 0)
  83.                     {
  84.                         foreach (ForumPost post in acc.ForumPosts)
  85.                         {
  86.                             post.AuthorAccountID = primaryAccount.AccountID;
  87.                             Console.WriteLine("\tTransferring post " + post.ForumPostID);
  88.                         }
  89.                     }
  90.                     Console.WriteLine("\tChecking votes");
  91.                     if (acc.AccountForumVotes != null && acc.AccountForumVotes.Count > 0)
  92.                     {
  93.                         foreach (AccountForumVote vote in acc.AccountForumVotes)
  94.                         {
  95.                             if (vote.Vote > 0)
  96.                             {
  97.                                 vote.ForumPost.Upvotes = vote.ForumPost.Upvotes - 1;
  98.                                 vote.ForumPost.Account.ForumTotalUpvotes = vote.ForumPost.Account.ForumTotalUpvotes - 1;
  99.                             }
  100.                             else if (vote.Vote < 0)
  101.                             {
  102.                                 vote.ForumPost.Downvotes = vote.ForumPost.Downvotes - 1;
  103.                                 vote.ForumPost.Account.ForumTotalDownvotes = vote.ForumPost.Account.ForumTotalDownvotes - 1;
  104.                             }
  105.                             db.AccountForumVotes.DeleteOnSubmit(vote);
  106.                            
  107.                         }
  108.                     }
  109.                     Console.WriteLine("\tChecking battles");
  110.                     if (acc.SpringBattlePlayers != null && acc.SpringBattlePlayers.Count > 0)
  111.                     {
  112.                         foreach (var players in acc.SpringBattlePlayers)
  113.                         {
  114.                             players.AccountID = primaryAccount.AccountID;
  115.                             Console.WriteLine("\tTransferring battle B" + players.SpringBattleID);
  116.                         }
  117.                     }
  118.                     if (acc.PunishmentsByAccountID != null && acc.PunishmentsByAccountID.Count > 0)
  119.                     {
  120.                         foreach (Punishment pun in acc.PunishmentsByAccountID)
  121.                         {
  122.                             db.Punishments.DeleteOnSubmit(pun);
  123.                             Console.WriteLine("\tDeleting punishment");
  124.                         }
  125.                     }
  126.  
  127.                     //db.AccountUserIDs.DeleteAllOnSubmit(acc.AccountUserIDs);
  128.                     //db.AccountIPs.DeleteAllOnSubmit(acc.AccountIPs);
  129.  
  130.                     //db.ForumLastReads.DeleteAllOnSubmit(acc.ForumLastReads);
  131.                     //db.ForumThreadLastReads.DeleteAllOnSubmit(acc.ForumThreadLastReads);  
  132.                     db.Accounts.DeleteOnSubmit(acc);
  133.                 }
  134.             }
  135.             //db.SubmitChanges();
  136.         }
  137.  
  138.         public static void GetDuplicates()
  139.         {
  140.             FileHelperEngine engine = new FileHelperEngine(typeof(ZKAccount));
  141.             ZKAccount[] accounts = engine.ReadFile(@"ZKAccounts.csv") as ZKAccount[];
  142.  
  143.             Dictionary<string, List<int>> accountsByName = new Dictionary<string, List<int>>();
  144.             Dictionary<string, List<int>> duplicatesByName = new Dictionary<string, List<int>>();
  145.             Console.WriteLine("Processing " + accounts.Length + " accounts");
  146.             Console.WriteLine("---------------");
  147.             foreach (ZKAccount account in accounts)
  148.             {
  149.                 string lowerName = account.Name.ToLower();
  150.                 if (accountsByName.ContainsKey(lowerName))
  151.                 {
  152.                     var entry = accountsByName[lowerName];
  153.                     entry.Add(account.AccountID);
  154.                     duplicatesByName[lowerName] = entry;
  155.                     Console.WriteLine("Duplicate found: {0}, {1}", lowerName, account.AccountID);
  156.                 }
  157.                 else
  158.                 {
  159.                     accountsByName.Add(lowerName, new List<int>( new int[] {account.AccountID} ));
  160.                 }
  161.             }
  162.             Console.WriteLine("---------------");
  163.             Console.WriteLine("Number of duplicates: " + duplicatesByName.Count);
  164.  
  165.             //PrintDuplicates(duplicatesByName);
  166.             FixDuplicates(duplicatesByName);
  167.         }
  168.  
  169.         public static void PrintUserData(List<Account> users)
  170.         {
  171.             using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"ZKAccounts.csv"))
  172.             {
  173.                 file.WriteLine("AccountID,Name");
  174.                 foreach (Account user in users)
  175.                 {
  176.                     file.WriteLine(user.AccountID + "," + user.Name);
  177.                 }
  178.             }
  179.         }
  180.  
  181.         public static void GetUserCsv()
  182.         {
  183.             ZkDataContext db = new ZkDataContext();
  184.             var users = db.Accounts.ToList();
  185.             Console.WriteLine(users.Count);
  186.             PrintUserData(users);
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement