Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Wintellect.PowerCollections;
  7.  
  8. namespace Scoreboard
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var users = new Dictionary<string, string>();
  15.             var games = new Dictionary<string, string>();
  16.             var sortedGames = new SortedSet<string>();
  17.             var scoreboard = new Dictionary<string, SortedDictionary<long, OrderedBag<string>>>();
  18.  
  19.             string command = Console.ReadLine();
  20.             while (true)
  21.             {
  22.                 string[] commandData = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  23.                 if (commandData.Length == 0)
  24.                 {
  25.                     command = Console.ReadLine();
  26.                     continue;
  27.                 }
  28.                 string execute = commandData[0];
  29.                 bool haveToStop = false;
  30.  
  31.                 switch (execute)
  32.                 {
  33.                     case "RegisterUser":
  34.                         if (users.ContainsKey(commandData[1]))
  35.                         {
  36.                             Console.WriteLine("Duplicated user");
  37.                         }
  38.                         else
  39.                         {
  40.                             users.Add(commandData[1], commandData[2]);
  41.                             Console.WriteLine("User registered");
  42.                         }
  43.                         break;
  44.  
  45.                     case "RegisterGame":
  46.                         if (games.ContainsKey(commandData[1]))
  47.                         {
  48.                             Console.WriteLine("Duplicated game");
  49.                         }
  50.                         else
  51.                         {
  52.                             games.Add(commandData[1], commandData[2]);
  53.                             sortedGames.Add(commandData[1]);
  54.                             Console.WriteLine("Game registered");
  55.                         }
  56.                         break;
  57.  
  58.                     case "AddScore":
  59.                         string tempUser = commandData[1];
  60.                         string tempUserPass = commandData[2];
  61.                         string tempGame = commandData[3];
  62.                         string tempGamePass = commandData[4];
  63.                         long score = long.Parse(commandData[5]);
  64.  
  65.                         if (users.ContainsKey(tempUser) && users[tempUser] == tempUserPass)
  66.                         {
  67.                             if (games.ContainsKey(tempGame) && games[tempGame] == tempGamePass)
  68.                             {
  69.                                 if (!scoreboard.ContainsKey(tempGame))
  70.                                 {
  71.                                     scoreboard.Add(tempGame, new SortedDictionary<long, OrderedBag<string>>(new ReverseComparer<long>()));
  72.                                 }
  73.                                 var tempDict = scoreboard[tempGame];
  74.  
  75.                                 if (!tempDict.ContainsKey(score))
  76.                                 {
  77.                                     tempDict.Add(score, new OrderedBag<string>());
  78.                                 }
  79.  
  80.                                 var tempList = tempDict[score];
  81.                                 tempList.Add(tempUser);
  82.                                 tempDict[score] = tempList;
  83.                                 scoreboard[tempGame] = tempDict;
  84.                                 Console.WriteLine("Score added");
  85.                             }
  86.                             else
  87.                             {
  88.                                 Console.WriteLine("Cannot add score");
  89.                             }
  90.                         }
  91.                         else
  92.                         {
  93.                             Console.WriteLine("Cannot add score");
  94.                         }
  95.                         break;
  96.  
  97.                     case "ShowScoreboard":
  98.                         int count = 1;
  99.                         bool haveToBreak = false;
  100.                         if (!games.ContainsKey(commandData[1]))
  101.                         {
  102.                             Console.WriteLine("Game not found");
  103.                             break;
  104.                         }
  105.                         if (scoreboard.ContainsKey(commandData[1]))
  106.                         {
  107.                             foreach (var scores in scoreboard[commandData[1]])
  108.                             {
  109.                                 var tempScore = scores.Key;
  110.                                 var tempScoreList = scores.Value;
  111.                                 foreach (var tempName in tempScoreList)
  112.                                 {
  113.                                     Console.WriteLine("#{0} {1} {2}", count, tempName, tempScore);
  114.                                     count++;
  115.                                     if (count > 10)
  116.                                     {
  117.                                         haveToBreak = true;
  118.                                         break;
  119.                                     }
  120.                                 }
  121.                                 if (haveToBreak)
  122.                                 {
  123.                                     break;
  124.                                 }
  125.                             }
  126.                         }
  127.                         else
  128.                         {
  129.                             Console.WriteLine("No score");
  130.                         }
  131.                         break;
  132.  
  133.                     case "ListGamesByPrefix":
  134.                         string prefix = commandData[1];
  135.                         var prefixedGames = new List<string>();
  136.                         foreach (var game in sortedGames)
  137.                         {
  138.                             if (game.Length < prefix.Length)
  139.                             {
  140.                                 continue;
  141.                             }
  142.                             if (game.Substring(0, prefix.Length) == prefix)
  143.                             {
  144.                                 prefixedGames.Add(game);
  145.                                 if (prefixedGames.Count == 10)
  146.                                 {
  147.                                     break;
  148.                                 }
  149.                             }
  150.                         }
  151.                         if (prefixedGames.Count != 0)
  152.                         {
  153.                             Console.WriteLine(string.Join(", ", prefixedGames));
  154.                         }
  155.                         else
  156.                         {
  157.                             Console.WriteLine("No matches");
  158.                         }
  159.                         break;
  160.  
  161.                     case "DeleteGame":
  162.  
  163.                         if (games.ContainsKey(commandData[1]) && games[commandData[1]] == commandData[2])
  164.                         {
  165.                             games.Remove(commandData[1]);
  166.                             sortedGames.Remove(commandData[1]);
  167.                             scoreboard.Remove(commandData[1]);
  168.                             Console.WriteLine("Game deleted");
  169.                         }
  170.                         else
  171.                         {
  172.                             Console.WriteLine("Cannot delete game");
  173.                         }
  174.                         break;
  175.  
  176.                     case "End":
  177.                         haveToStop = true;
  178.                         break;
  179.                 }
  180.  
  181.                 if (haveToStop)
  182.                 {
  183.                     break;
  184.                 }
  185.                 else
  186.                 {
  187.                     command = Console.ReadLine();
  188.                 }
  189.             }
  190.         }
  191.     }
  192.  
  193.     public sealed class ReverseComparer<T> : IComparer<T>
  194.     {
  195.         private readonly IComparer<T> inner;
  196.         public ReverseComparer() : this(null) { }
  197.         public ReverseComparer(IComparer<T> inner)
  198.         {
  199.             this.inner = inner ?? Comparer<T>.Default;
  200.         }
  201.         int IComparer<T>.Compare(T x, T y) { return inner.Compare(y, x); }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement