Namelists.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Server.Account
  11. {
  12. public partial class Namelists : Form
  13. {
  14. public Namelists()
  15. {
  16. InitializeComponent();
  17. UpdateNamelists();
  18. }
  19. #region Update Name List
  20. private void UpdateNamelists()
  21. {
  22. // Define the directory path for the Namelists folder
  23. string namelistsPath = Path.Combine("Envir", "Namelists");
  24. // Ensure the directory exists
  25. if (!Directory.Exists(namelistsPath))
  26. {
  27. NamelistView.Items.Clear();
  28. NamelistView.Items.Add("Namelists directory not found.");
  29. NamelistCount.Text = "Namelist Count: 0";
  30. TotalPlayerLabel.Text = "Total Players: 0 (In all Namelists)";
  31. TotalUniquePlayerLabel.Text = "Total Unique Players: 0 (In all Namelists)";
  32. return;
  33. }
  34. // Clear the NamelistView before updating
  35. NamelistView.Items.Clear();
  36. // Track the number of namelists, total players, and unique players
  37. int namelistCount = 0;
  38. int totalPlayerCount = 0;
  39. HashSet<string> uniquePlayers = new HashSet<string>();
  40. // Iterate over each text file in the directory and subdirectories
  41. foreach (string filePath in Directory.GetFiles(namelistsPath, "*.txt", SearchOption.AllDirectories))
  42. {
  43. // Get the relative path from the Namelists directory
  44. string relativePath = Path.GetRelativePath(namelistsPath, filePath);
  45. // Remove the .txt extension
  46. relativePath = Path.ChangeExtension(relativePath, null);
  47. // Add the relative path to the NamelistView
  48. NamelistView.Items.Add(relativePath);
  49. // Increment the namelist count
  50. namelistCount++;
  51. // Count players in the current file and add to totalPlayerCount
  52. string[] players = File.ReadAllLines(filePath);
  53. totalPlayerCount += players.Length;
  54. // Add each player to the HashSet to track unique players
  55. foreach (string player in players)
  56. {
  57. uniquePlayers.Add(player);
  58. }
  59. }
  60. // Update the labels with the total and unique counts
  61. NamelistCount.Text = $"Namelist Count: {namelistCount}";
  62. TotalPlayerLabel.Text = $"Total Players: {totalPlayerCount} (In all Namelists)";
  63. TotalUniquePlayerLabel.Text = $"Total Unique Players: {uniquePlayers.Count} (In all Namelists)";
  64. }
  65. private void NamelistView_SelectedIndexChanged(object sender, EventArgs e)
  66. {
  67. // Clear the contents of the NamelistViewBox before updating
  68. NamelistViewBox.Items.Clear();
  69. // Check if any item is selected
  70. if (NamelistView.SelectedItems.Count == 0) return;
  71. // Get the selected item's text, which is the relative path without the .txt extension
  72. string relativePath = NamelistView.SelectedItems[0].Text;
  73. // Construct the full path to the .txt file
  74. string fullPath = Path.Combine("Envir", "Namelists", relativePath + ".txt");
  75. // Check if the file exists
  76. if (File.Exists(fullPath))
  77. {
  78. // Read all lines from the file
  79. string[] lines = File.ReadAllLines(fullPath);
  80. // Check if the file is empty and display "Empty" if so, otherwise display each line as a new item
  81. if (lines.Length == 0)
  82. {
  83. NamelistViewBox.Items.Add("Empty");
  84. }
  85. else
  86. {
  87. foreach (string line in lines)
  88. {
  89. NamelistViewBox.Items.Add(line);
  90. }
  91. }
  92. }
  93. else
  94. {
  95. // Display a message if the file is not found
  96. NamelistViewBox.Items.Add("File not found.");
  97. }
  98. }
  99. #endregion
  100. #region Refresh Button
  101. private void RefreshButton_Click(object sender, EventArgs e)
  102. {
  103. // Define the directory path for the Namelists folder
  104. string namelistsPath = Path.Combine("Envir", "Namelists");
  105. // Ensure the directory exists
  106. if (!Directory.Exists(namelistsPath))
  107. {
  108. NamelistView.Items.Clear();
  109. NamelistView.Items.Add("Namelists directory not found.");
  110. NamelistCountLabel.Text = "Found in: 0 Namelists";
  111. return;
  112. }
  113. // Get the player's name from FindPlayerBox
  114. string playerName = FindPlayerBox.Text.Trim();
  115. // Clear the NamelistView before updating
  116. NamelistView.Items.Clear();
  117. // If the player name is empty, reload all namelists and reset the count label
  118. if (string.IsNullOrEmpty(playerName))
  119. {
  120. UpdateNamelists();
  121. NamelistCountLabel.Text = "Found in: 0 Namelists";
  122. return;
  123. }
  124. // Track the count of namelists containing the player's name
  125. int count = 0;
  126. // Iterate over each text file in the directory and subdirectories
  127. foreach (string filePath in Directory.GetFiles(namelistsPath, "*.txt", SearchOption.AllDirectories))
  128. {
  129. // Read all lines from the current file
  130. string[] lines = File.ReadAllLines(filePath);
  131. // Check if any line contains the player's name
  132. if (lines.Any(line => line.Contains(playerName)))
  133. {
  134. // Get the relative path from the Namelists directory
  135. string relativePath = Path.GetRelativePath(namelistsPath, filePath);
  136. // Remove the .txt extension
  137. relativePath = Path.ChangeExtension(relativePath, null);
  138. // Add the relative path to the NamelistView
  139. NamelistView.Items.Add(relativePath);
  140. // Increment the count of namelists containing the player's name
  141. count++;
  142. }
  143. }
  144. // Update the NamelistCountLabel with the count of namelists containing the player
  145. NamelistCountLabel.Text = $"Found in: {count} Namelists";
  146. // If no files contain the player's name, add a message to the NamelistView
  147. if (count == 0)
  148. {
  149. NamelistView.Items.Add("Player not found on any Namelists.");
  150. }
  151. }
  152. #endregion
  153. #region Delete Player Button
  154. private void DeletePlayerButton_Click(object sender, EventArgs e)
  155. {
  156. // Ensure an item is selected in NamelistViewBox
  157. if (NamelistViewBox.SelectedItems.Count == 0) return;
  158. // Get the selected player's name
  159. string playerToDelete = NamelistViewBox.SelectedItems[0].Text;
  160. // Ensure an item is selected in NamelistView to get the relevant file
  161. if (NamelistView.SelectedItems.Count == 0) return;
  162. // Get the file associated with the selected namelist
  163. string relativePath = NamelistView.SelectedItems[0].Text;
  164. string fullPath = Path.Combine("Envir", "Namelists", relativePath + ".txt");
  165. // Read all lines from the file and remove the selected player's name
  166. var lines = File.ReadAllLines(fullPath).ToList();
  167. bool removed = lines.Remove(playerToDelete);
  168. if (removed)
  169. {
  170. // Write updated lines back to the file
  171. File.WriteAllLines(fullPath, lines);
  172. // Remove the player from the ListView display
  173. NamelistViewBox.Items.Remove(NamelistViewBox.SelectedItems[0]);
  174. // If the file becomes empty, display "Empty"
  175. if (lines.Count == 0)
  176. {
  177. NamelistViewBox.Items.Add("Empty");
  178. }
  179. }
  180. }
  181. #endregion
  182. #region Add Player Button
  183. private void AddPlayerButton_Click(object sender, EventArgs e)
  184. {
  185. // Ensure a namelist is selected in NamelistView
  186. if (NamelistView.SelectedItems.Count == 0)
  187. {
  188. MessageBox.Show("Please select a namelist to add the player to.");
  189. return;
  190. }
  191. // Prompt for the player's name
  192. string playerName = Microsoft.VisualBasic.Interaction.InputBox("Enter the player's name:", "Add Player", "");
  193. // Check if the input was empty
  194. if (string.IsNullOrWhiteSpace(playerName))
  195. {
  196. MessageBox.Show("Player name cannot be empty.");
  197. return;
  198. }
  199. // Get the file associated with the selected namelist
  200. string relativePath = NamelistView.SelectedItems[0].Text;
  201. string fullPath = Path.Combine("Envir", "Namelists", relativePath + ".txt");
  202. // Read all lines from the file to check if the player is already listed
  203. var lines = File.ReadAllLines(fullPath).ToList();
  204. if (lines.Contains(playerName))
  205. {
  206. MessageBox.Show("Player is already in the selected namelist.");
  207. return;
  208. }
  209. // Add the player to the file and update the ListView display
  210. lines.Add(playerName);
  211. File.WriteAllLines(fullPath, lines);
  212. // Refresh NamelistViewBox to display the new player if the current namelist is selected
  213. if (NamelistViewBox.Items.Contains(new ListViewItem("Empty")))
  214. {
  215. NamelistViewBox.Items.Clear();
  216. }
  217. NamelistViewBox.Items.Add(playerName);
  218. }
  219. #endregion
  220. #region Create Namelist Button
  221. private void CreateNamelistButton_Click(object sender, EventArgs e)
  222. {
  223. // Prompt for the namelist name
  224. string namelistName = Microsoft.VisualBasic.Interaction.InputBox("Enter the name for the new namelist:", "Create Namelist", "");
  225. // Check if the input was empty
  226. if (string.IsNullOrWhiteSpace(namelistName))
  227. {
  228. MessageBox.Show("Namelist name cannot be empty.");
  229. return;
  230. }
  231. // Define the full path for the new namelist file
  232. string namelistsPath = Path.Combine("Envir", "Namelists");
  233. string fullPath = Path.Combine(namelistsPath, namelistName + ".txt");
  234. // Check if the file already exists
  235. if (File.Exists(fullPath))
  236. {
  237. MessageBox.Show("A namelist with this name already exists.");
  238. return;
  239. }
  240. // Create the new namelist file
  241. File.Create(fullPath).Dispose();
  242. // Update the NamelistView to include the new namelist
  243. UpdateNamelists();
  244. }
  245. #endregion
  246. #region Delete Namelist Button
  247. private void DeleteNamelistButton_Click(object sender, EventArgs e)
  248. {
  249. // Ensure a namelist is selected in NamelistView
  250. if (NamelistView.SelectedItems.Count == 0)
  251. {
  252. MessageBox.Show("Please select a namelist to delete.");
  253. return;
  254. }
  255. // Get the selected namelist's relative path
  256. string relativePath = NamelistView.SelectedItems[0].Text;
  257. string fullPath = Path.Combine("Envir", "Namelists", relativePath + ".txt");
  258. // Confirm deletion
  259. var confirmResult = MessageBox.Show($"Are you sure you want to delete the namelist '{relativePath}'?",
  260. "Confirm Delete",
  261. MessageBoxButtons.YesNo,
  262. MessageBoxIcon.Warning);
  263. if (confirmResult == DialogResult.Yes)
  264. {
  265. // Check if the file exists before attempting to delete
  266. if (File.Exists(fullPath))
  267. {
  268. File.Delete(fullPath);
  269. UpdateNamelists(); // Refresh the NamelistView to reflect deletion
  270. }
  271. else
  272. {
  273. MessageBox.Show("Namelist file not found.");
  274. }
  275. }
  276. }
  277. #endregion
  278. }
  279. }