Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace MISTBowl_Brackets
  7. {
  8. class MainClass
  9. {
  10. static string originalTitle = Console.Title;
  11. static string fileName = string.Empty;
  12. static List<Team> teamsList = new List<Team>();
  13. static List<Team> origTeamsList = new List<Team>();
  14. static string roundName = string.Empty;
  15. static string LastFixtures;
  16. static List<List<Team>> combinations = new List<List<Team>>();
  17. public static int Main()
  18. {
  19. try
  20. {
  21. Console.BackgroundColor = ConsoleColor.DarkBlue;
  22. Console.ForegroundColor = ConsoleColor.White;
  23. Console.Title = "MIST Bowl Bracket Software";
  24. Clear();
  25. while (true)
  26. {
  27. Console.WriteLine("Press [c] to create a new tournament file, [e] to use an existing one, or [q] to quit.");
  28. char option = Console.ReadKey(true).KeyChar;
  29. Console.WriteLine();
  30. if (option == 'c')
  31. {
  32. Console.WriteLine("OK, creating new file. . .");
  33. do
  34. fileName = GetNewFileName();
  35. while (File.Exists(fileName) || Directory.Exists(fileName));
  36. break;
  37. }
  38. if (option == 'e')
  39. {
  40. Console.WriteLine("OK, using existing file. . .");
  41. fileName = GetExistingFileName();
  42. break;
  43. }
  44. if (option == 'q')
  45. return 0;
  46. Console.WriteLine("Invalid option '" + option + "'.");
  47. }
  48. Clear();
  49. if (!File.Exists(fileName))
  50. {
  51. Console.WriteLine("For each team participating in the MIST Bowl tournament, please type the team ID, followed by a comma, followed by the team's region name, like as follows:");
  52. Console.ForegroundColor = ConsoleColor.Green;
  53. Console.WriteLine("1000,Detroit");
  54. Console.WriteLine("1100/1600,Toronto");
  55. Console.ForegroundColor = ConsoleColor.Magenta;
  56. Console.WriteLine("MAKE SURE ALL REGION NAMES ARE SPELLED CORRECTLY!");
  57. Console.ForegroundColor = ConsoleColor.White;
  58. Console.WriteLine("Press [Enter] twice when finished.");
  59. Console.WriteLine();
  60. while (true)
  61. {
  62. var s = Console.ReadLine().ToUpper();
  63. if (s == string.Empty)
  64. break;
  65. if (!Regex.IsMatch(s, "^[0-9]+(/[0-9]+)* *, *[^ ,][^,]*$") || s.Split('/').Any(a => teamsList.Any(b => b.teamId.Split('/').Contains(a))))
  66. throw new InvalidDataException();
  67. teamsList.Add(new Team(Regex.Replace(s, " *, *", ",").Split(',')));
  68. }
  69. if (teamsList.ToArray().Length == 0)
  70. throw new InvalidDataException();
  71. origTeamsList = teamsList.ToList();
  72. Clear();
  73. WriteToFile();
  74. }
  75. Console.Write("Please enter the number of preliminary rounds in this tournament: ");
  76. var prelimRounds = int.Parse(Console.ReadLine());
  77. for (int i = 0; i < prelimRounds; i++)
  78. {
  79. roundName = "PRELIM" + (i + 1) + "/" + prelimRounds;
  80. Clear();
  81. Console.WriteLine("Please list below the teams in each match during this preliminary round, like as follows:");
  82. Console.ForegroundColor = ConsoleColor.Green;
  83. Console.WriteLine("1000,1100/1600,2000");
  84. Console.WriteLine("3000,3300,3400");
  85. Console.ForegroundColor = ConsoleColor.White;
  86. Console.WriteLine();
  87. var thisRoundTeams = new List<Team>();
  88. while (true)
  89. {
  90. string s;
  91. if ((s = Console.ReadLine()) == string.Empty)
  92. break;
  93. if (!Regex.IsMatch(s, "^[0-9]+(/[0-9]+)* *(, *[0-9]+(/[0-9]+)* *)+$"))
  94. throw new InvalidDataException();
  95. var matchTeams = s.Replace(" ", string.Empty).Split(',').Select(a => teamsList.First(b => b.teamId == a)).ToList();
  96. if (matchTeams.Any(a => thisRoundTeams.Any(b => b.teamId == a.teamId)))
  97. throw new InvalidDataException();
  98. matchTeams.ForEach(a => thisRoundTeams.Add(a));
  99. Matches.AddMatch(matchTeams.ToArray());
  100. }
  101. if (teamsList.Count != thisRoundTeams.Count)
  102. throw new InvalidDataException();
  103. Clear();
  104. Console.WriteLine("Please enter the amount of points earned by each team during this preliminary round.");
  105. foreach (Team team in teamsList)
  106. {
  107. Console.Write(team.teamId + "," + team.region + ": ");
  108. team.points += int.Parse(Console.ReadLine());
  109. }
  110. }
  111. while (roundName != "F")
  112. {
  113. roundName = string.Empty;
  114. Clear();
  115. Console.Write("Please enter the number of teams progressing to the next round: ");
  116. teamsList = teamsList.OrderByDescending(a => a.points).ThenBy(a => TickBasedRandom.GetInteger(int.MinValue, int.MaxValue - 1)).ToList().GetRange(0, int.Parse(Console.ReadLine()) - 1);
  117. teamsList.ForEach(a => a.points = 0);
  118. var matchCount = 0;
  119. while (true)
  120. {
  121. Console.Write("Please enter the number of matches taking place in the next round: ");
  122. try {
  123. matchCount = int.Parse(Console.ReadLine());
  124. if (teamsList.Count % matchCount == 0)
  125. break;
  126. }
  127. catch { }
  128. Console.ForegroundColor = ConsoleColor.Red;
  129. Console.WriteLine("That is not a valid number of matches given the number of teams. Please try again.");
  130. Console.ForegroundColor = ConsoleColor.White;
  131. Console.WriteLine();
  132. }
  133. roundName = matchCount == 1 ? "F" : "ROUNDOF" + teamsList.Count;
  134. Clear();
  135. Console.WriteLine("The following matches will take place in this round:");
  136. var matches = Combinations.GenerateBestMatches(teamsList, matchCount / teamsList.Count, matchCount, origTeamsList);
  137. matches.ForEach(a => Matches.AddMatch(a.ToArray()));
  138. Console.WriteLine(string.Join(Environment.NewLine, matches.ConvertAll(a => string.Join(" v. ", a.ConvertAll(b => b.teamId)))));
  139. Console.ForegroundColor = ConsoleColor.Magenta;
  140. Console.WriteLine("MAKE SURE TO WRITE DOWN THIS MATCH LIST BEFORE CONTINUING.");
  141. Console.ForegroundColor = ConsoleColor.White;
  142. Console.WriteLine();
  143. Console.WriteLine("Press any key to continue. . .");
  144. System.Threading.Thread.Sleep(3000);
  145. Console.ReadKey();
  146. Clear();
  147. Console.WriteLine("Please enter the amount of points earned by each team.");
  148. foreach (Team team in teamsList)
  149. {
  150. Console.Write(team.teamId + "," + team.region + ": ");
  151. team.points += int.Parse(Console.ReadLine());
  152. }
  153. }
  154. teamsList = teamsList.OrderByDescending(a => a.points).ThenBy(a => TickBasedRandom.GetInteger(int.MinValue, int.MaxValue - 1)).ToList();
  155. Console.WriteLine("1ST PLACE CHAMPIONS: " + teamsList[0].teamId + "," + teamsList[0].region);
  156. Console.WriteLine("2ND PLACE: " + teamsList[1].teamId + "," + teamsList[1].region);
  157. Console.WriteLine("3RD PLACE: " + teamsList[2].teamId + "," + teamsList[2].region);
  158. Console.WriteLine();
  159. Console.Error.Write("Press any key to quit. . .");
  160. Console.ReadKey(true);
  161. Console.Error.WriteLine();
  162. Console.ResetColor();
  163. Console.Title = originalTitle;
  164. return 0;
  165. }
  166. catch (Exception e)
  167. {
  168. Console.Error.WriteLine();
  169. Console.Error.WriteLine("Traceback (most recent call last):");
  170. Console.Error.Write(Traceback.Read());
  171. Console.Error.WriteLine("\tAt int Main(string[]) (class MainClass):");
  172. Console.ForegroundColor = ConsoleColor.Red;
  173. Console.Error.WriteLine("Error: " + e.GetType() + " occurred.");
  174. Console.Error.WriteLine("Description: " + e.Message);
  175. Console.ForegroundColor = ConsoleColor.White;
  176. Console.Error.Write("Press any key to quit. . .");
  177. Console.ReadKey(true);
  178. Console.Error.WriteLine();
  179. Console.ResetColor();
  180. Console.Title = originalTitle;
  181. return -1;
  182. }
  183. }
  184. public static string GetNewFileName()
  185. {
  186. try
  187. {
  188. return GetTempPath() + "MISTBowl_Brackets_" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".tmp";
  189. }
  190. catch (Exception e)
  191. {
  192. Traceback.Write("At string GetNewFileName() (class MainClass):");
  193. throw e;
  194. }
  195. }
  196. public static string GetExistingFileName()
  197. {
  198. try
  199. {
  200. var searchResults = Directory.GetFiles(GetTempPath()).ToList();
  201. if (searchResults.FindIndex(a => Regex.IsMatch(Path.GetFileName(a), "^MISTBowl_Brackets_.*[.]tmp$")) != -1)
  202. return searchResults.FindAll(a => Regex.IsMatch(Path.GetFileName(a), "^MISTBowl_Brackets_.*[.]tmp$")).OrderByDescending(a => File.GetLastWriteTimeUtc(a).Ticks).ToArray()[0];
  203. throw new FileNotFoundException();
  204. }
  205. catch (Exception e)
  206. {
  207. Traceback.Write("At string GetExistingFileName() (class MainClass):");
  208. throw e;
  209. }
  210. }
  211. public static string GetTempPath()
  212. {
  213. try
  214. {
  215. var tempPath = Path.GetTempPath();
  216. if (tempPath == "/tmp/" && Directory.Exists("/var/tmp/"))
  217. return "/var/tmp/";
  218. return tempPath;
  219. }
  220. catch (Exception e)
  221. {
  222. Traceback.Write("At string GetTempPath() (class MainClass);");
  223. throw e;
  224. }
  225. }
  226. public static void Clear()
  227. {
  228. try
  229. {
  230. Console.Clear();
  231. Console.Error.WriteLine("MIST Bowl Bracket Software");
  232. Console.Error.WriteLine("Copyright (c) 2017 Muhammad Mu'að Imtiaz");
  233. var maxLength = 40;
  234. if (teamsList.ToArray().Length > 0)
  235. {
  236. maxLength = Math.Max(roundName != string.Empty ? HumanReadableRoundNames.GetHumanReadableName(roundName).Length : 0, Math.Max(40, teamsList.ConvertAll(a => string.Join(",", a).Length).Aggregate((a, b) => Math.Max(a, b))));
  237. for (int i = 0; i < maxLength; i++)
  238. Console.Error.Write("-");
  239. Console.Error.WriteLine();
  240. Console.Error.WriteLine(string.Join(Environment.NewLine, teamsList.ConvertAll(a => a.teamId + "," + a.region + (teamsList.ConvertAll(b => b.points).Max() != 0 ? "," + a.points.ToString() : string.Empty))));
  241. }
  242. if (roundName != string.Empty)
  243. Console.Error.WriteLine(HumanReadableRoundNames.GetHumanReadableName(roundName));
  244. for (int i = 0; i < maxLength; i++)
  245. Console.Error.Write("-");
  246. Console.Error.WriteLine();
  247. }
  248. catch (Exception e)
  249. {
  250. Traceback.Write("At void Clear() (class MainClass):");
  251. throw e;
  252. }
  253. }
  254. public static void WriteToFile()
  255. {
  256. try
  257. {
  258. using (var fileStream = File.OpenWrite(fileName))
  259. using (var streamWriter = new StreamWriter(fileStream))
  260. {
  261. foreach (Team team in teamsList)
  262. streamWriter.WriteLine("TEAM:" + team.teamId + "," + team.region);
  263. if (roundName != string.Empty)
  264. streamWriter.Write(roundName + ":");
  265. streamWriter.WriteLine(string.Join(";", teamsList.ConvertAll(a => string.Join(",", a))));
  266. if (LastFixtures != string.Empty)
  267. {
  268. streamWriter.WriteLine("MTCHS:" + LastFixtures);
  269. LastFixtures = string.Empty;
  270. }
  271. }
  272. }
  273. catch (Exception e)
  274. {
  275. Traceback.Write("At void WriteToFile() (class MainClass):");
  276. throw e;
  277. }
  278. }
  279. public static void ReadFromFile()
  280. {
  281. try
  282. {
  283. /* string text = string.Empty;
  284. using (var fileStream = File.OpenRead(fileName))
  285. using (var streamReader = new StreamReader(fileStream))
  286. text = streamReader.ReadToEnd();
  287. foreach (string line in Regex.Split(text, Environment.NewLine))
  288. {
  289. if (line.StartsWith("TEAM:", StringComparison.Ordinal))
  290. {
  291. for(line.Split(':').ToList().GetRange(1, line.Count(a => a == ':'))
  292. }
  293. else if (line.Contains(":") && !line.StartsWith("MTCHS:", StringComparison.Ordinal))
  294. roundName = line.Split(':')[0];
  295. else if (line.StartsWith("MTCHS:", StringComparison.Ordinal))
  296. foreach (string match in line.Substring(6).Split(' '))
  297. Matches.AddMatch(match.Split(' ').ToList().ConvertAll(a => teamsList.First(b => b.teamId == a)).ToArray());
  298. }*/
  299. throw new NotImplementedException();
  300. }
  301. catch (Exception e)
  302. {
  303. Traceback.Write("At void ReadFromFile() (class MainClass):");
  304. throw e;
  305. }
  306. }
  307. }
  308. }