Advertisement
KidaCoding

Untitled

Jun 19th, 2024 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | Source Code | 0 0
  1. case "tr":
  2. {
  3.     string playersItemsPath = Program.ServerConfig.DbLocation + "\\PlayersItems\\";
  4.     string backupPath = Program.ServerConfig.DbLocation + "BackupPlayersItems\\";
  5.     string removedItemsPath = Program.ServerConfig.DbLocation + "RemovedItems\\";
  6.  
  7.     if (!Directory.Exists(backupPath))
  8.     {
  9.         Directory.CreateDirectory(backupPath);
  10.     }
  11.  
  12.     if (!Directory.Exists(removedItemsPath))
  13.     {
  14.         Directory.CreateDirectory(removedItemsPath);
  15.     }
  16.  
  17.     HashSet<uint> removedItemIds = new HashSet<uint>();
  18.  
  19.     List<Task> tasks = new List<Task>();
  20.  
  21.     foreach (string fname in System.IO.Directory.GetFiles(playersItemsPath))
  22.     {
  23.         tasks.Add(Task.Run(() =>
  24.         {
  25.             string fileName = Path.GetFileName(fname);
  26.             string destPath = Path.Combine(backupPath, fileName);
  27.             File.Copy(fname, destPath, true);
  28.  
  29.             uint clientUID = uint.Parse(System.IO.Path.GetFileName(fname).Replace(".bin", ""));
  30.             Client.GameClient client = new Client.GameClient(null);
  31.             client.Player.UID = clientUID;
  32.             client.Inventory = new Role.Instance.Inventory(client);
  33.             client.Equipment = new Role.Instance.Equip(client);
  34.             client.Warehouse = new Role.Instance.Warehouse(client);
  35.             Database.ServerDatabase.LoadClientItems(client);
  36.  
  37.             var clientItemIds = client.Equipment.ClientItems.Values.Select(item => item.ITEM_ID)
  38.                 .Concat(client.Warehouse.ClientItems.SelectMany(warehouse => warehouse.Value.Values.Select(item => item.ITEM_ID)))
  39.                 .Concat(client.Inventory.ClientItems.Values.Select(item => item.ITEM_ID))
  40.                 .ToList();
  41.  
  42.             List<uint> allowedItemIds = File.ReadAllLines(Program.ServerConfig.DbLocation + "itemtype.txt")
  43.                 .Select(l => uint.Parse(l.Split(new string[] { "@@" }, StringSplitOptions.None)[0]))
  44.                 .ToList();
  45.  
  46.             var itemsToRemove = clientItemIds.Except(allowedItemIds).ToList();
  47.  
  48.             lock (removedItemIds)
  49.             {
  50.                 removedItemIds.UnionWith(itemsToRemove);
  51.             }
  52.  
  53.             foreach (var item in client.Equipment.ClientItems.Values.ToList())
  54.             {
  55.                 if (itemsToRemove.Contains(item.ITEM_ID))
  56.                 {
  57.                     client.Equipment.ClientItems.Remove(item.UID);
  58.                     Console.WriteLine($"Removed item Equipment with ID: {item.ITEM_ID}", ConsoleColor.DarkRed);
  59.                 }
  60.             }
  61.  
  62.             foreach (var warehouse in client.Warehouse.ClientItems)
  63.             {
  64.                 foreach (var wItem in warehouse.Value.Values.ToList())
  65.                 {
  66.                     if (itemsToRemove.Contains(wItem.ITEM_ID))
  67.                     {
  68.                         client.Warehouse.ClientItems[warehouse.Key].Remove(wItem.UID);
  69.                         Console.WriteLine($"Removed item warehouse With ID: {wItem.ITEM_ID}", ConsoleColor.DarkRed);
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             foreach (var invItem in client.Inventory.ClientItems.Values.ToList())
  75.             {
  76.                 if (itemsToRemove.Contains(invItem.ITEM_ID))
  77.                 {
  78.                     client.Inventory.ClientItems.Remove(invItem.UID);
  79.                     Console.WriteLine($"Removed item Inventory with ID: {invItem.ITEM_ID}", ConsoleColor.DarkRed);
  80.                 }
  81.             }
  82.  
  83.             Database.ServerDatabase.SaveClientItems(client);
  84.             Console.WriteLine("Done Restart Items", ConsoleColor.DarkRed);
  85.         }));
  86.     }
  87.  
  88.     Task.WaitAll(tasks.ToArray());
  89.  
  90.     string removedItemsFilePath = Path.Combine(removedItemsPath, "removed_item_ids.txt");
  91.     using (StreamWriter writer = new StreamWriter(removedItemsFilePath))
  92.     {
  93.         foreach (var id in removedItemIds)
  94.         {
  95.             writer.WriteLine(id);
  96.         }
  97.     }
  98.  
  99.     break;
  100. }
Tags: MS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement