Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Wintellect.PowerCollections;
- namespace Scoreboard
- {
- class Program
- {
- static void Main(string[] args)
- {
- var users = new Dictionary<string, string>();
- var games = new Dictionary<string, string>();
- var sortedGames = new SortedSet<string>();
- var scoreboard = new Dictionary<string, SortedDictionary<long, OrderedBag<string>>>();
- string command = Console.ReadLine();
- while (true)
- {
- string[] commandData = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- if (commandData.Length == 0)
- {
- command = Console.ReadLine();
- continue;
- }
- string execute = commandData[0];
- bool haveToStop = false;
- switch (execute)
- {
- case "RegisterUser":
- if (users.ContainsKey(commandData[1]))
- {
- Console.WriteLine("Duplicated user");
- }
- else
- {
- users.Add(commandData[1], commandData[2]);
- Console.WriteLine("User registered");
- }
- break;
- case "RegisterGame":
- if (games.ContainsKey(commandData[1]))
- {
- Console.WriteLine("Duplicated game");
- }
- else
- {
- games.Add(commandData[1], commandData[2]);
- sortedGames.Add(commandData[1]);
- Console.WriteLine("Game registered");
- }
- break;
- case "AddScore":
- string tempUser = commandData[1];
- string tempUserPass = commandData[2];
- string tempGame = commandData[3];
- string tempGamePass = commandData[4];
- long score = long.Parse(commandData[5]);
- if (users.ContainsKey(tempUser) && users[tempUser] == tempUserPass)
- {
- if (games.ContainsKey(tempGame) && games[tempGame] == tempGamePass)
- {
- if (!scoreboard.ContainsKey(tempGame))
- {
- scoreboard.Add(tempGame, new SortedDictionary<long, OrderedBag<string>>(new ReverseComparer<long>()));
- }
- var tempDict = scoreboard[tempGame];
- if (!tempDict.ContainsKey(score))
- {
- tempDict.Add(score, new OrderedBag<string>());
- }
- var tempList = tempDict[score];
- tempList.Add(tempUser);
- tempDict[score] = tempList;
- scoreboard[tempGame] = tempDict;
- Console.WriteLine("Score added");
- }
- else
- {
- Console.WriteLine("Cannot add score");
- }
- }
- else
- {
- Console.WriteLine("Cannot add score");
- }
- break;
- case "ShowScoreboard":
- int count = 1;
- bool haveToBreak = false;
- if (!games.ContainsKey(commandData[1]))
- {
- Console.WriteLine("Game not found");
- break;
- }
- if (scoreboard.ContainsKey(commandData[1]))
- {
- foreach (var scores in scoreboard[commandData[1]])
- {
- var tempScore = scores.Key;
- var tempScoreList = scores.Value;
- foreach (var tempName in tempScoreList)
- {
- Console.WriteLine("#{0} {1} {2}", count, tempName, tempScore);
- count++;
- if (count > 10)
- {
- haveToBreak = true;
- break;
- }
- }
- if (haveToBreak)
- {
- break;
- }
- }
- }
- else
- {
- Console.WriteLine("No score");
- }
- break;
- case "ListGamesByPrefix":
- string prefix = commandData[1];
- var prefixedGames = new List<string>();
- foreach (var game in sortedGames)
- {
- if (game.Length < prefix.Length)
- {
- continue;
- }
- if (game.Substring(0, prefix.Length) == prefix)
- {
- prefixedGames.Add(game);
- if (prefixedGames.Count == 10)
- {
- break;
- }
- }
- }
- if (prefixedGames.Count != 0)
- {
- Console.WriteLine(string.Join(", ", prefixedGames));
- }
- else
- {
- Console.WriteLine("No matches");
- }
- break;
- case "DeleteGame":
- if (games.ContainsKey(commandData[1]) && games[commandData[1]] == commandData[2])
- {
- games.Remove(commandData[1]);
- sortedGames.Remove(commandData[1]);
- scoreboard.Remove(commandData[1]);
- Console.WriteLine("Game deleted");
- }
- else
- {
- Console.WriteLine("Cannot delete game");
- }
- break;
- case "End":
- haveToStop = true;
- break;
- }
- if (haveToStop)
- {
- break;
- }
- else
- {
- command = Console.ReadLine();
- }
- }
- }
- }
- public sealed class ReverseComparer<T> : IComparer<T>
- {
- private readonly IComparer<T> inner;
- public ReverseComparer() : this(null) { }
- public ReverseComparer(IComparer<T> inner)
- {
- this.inner = inner ?? Comparer<T>.Default;
- }
- int IComparer<T>.Compare(T x, T y) { return inner.Compare(y, x); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement