Advertisement
ZhongNi

TopServerPlayers

Dec 18th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TopServerPlayers
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             Game game = new Game();
  12.             game.ShowTopPlayers();
  13.         }
  14.     }
  15.  
  16.     public class Game
  17.     {
  18.         private const string ExitCommand = "Exit";
  19.  
  20.         private readonly List<Player> _players;
  21.         private readonly PlayerBase _playerBase;
  22.  
  23.         public Game()
  24.         {
  25.             _playerBase = new PlayerBase();
  26.             _players = _playerBase.Fill();
  27.         }
  28.  
  29.         public void ShowTopPlayers()
  30.         {
  31.             _playerBase.ShowPlayers(_players);
  32.  
  33.             bool isRuning = true;
  34.  
  35.             while (isRuning)
  36.             {
  37.                 string typeSort = ProcessMenu();
  38.  
  39.                 if (typeSort.ToLower() == ExitCommand.ToLower())
  40.                 {
  41.                     isRuning = false;
  42.                 }
  43.                 else if (Validate(typeSort))
  44.                 {
  45.                     List<Top> sortedPlayers = _playerBase.SelectTopPlayers(_players, typeSort);
  46.                     _playerBase.ShowTopPlayers(sortedPlayers);
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private string ProcessMenu()
  52.         {
  53.             Console.WriteLine($"\nSelect command: " +
  54.                 $"\nshow top level - {PlayerBase.LevelSortCommand}" +
  55.                 $"\nshow top strength - {PlayerBase.StrengthSortCommand}" +
  56.                 $"\n\"{ExitCommand}\" for exit");
  57.  
  58.             return Console.ReadLine();
  59.         }
  60.  
  61.         private bool Validate(string typeSort)
  62.         {
  63.             if (typeSort == PlayerBase.LevelSortCommand)
  64.             {
  65.                 return true;
  66.             }
  67.             else if (typeSort == PlayerBase.StrengthSortCommand)
  68.             {
  69.                 return true;
  70.             }
  71.  
  72.             Console.WriteLine("\nUnknown command. Press something..");
  73.             Console.ReadKey();
  74.  
  75.             return false;
  76.         }
  77.     }
  78.  
  79.     public class PlayerBase
  80.     {
  81.         public const string LevelSortCommand = "1";
  82.         public const string StrengthSortCommand = "2";
  83.  
  84.         public List<Player> Fill()
  85.         {
  86.             PlayerFactory factory = new PlayerFactory();
  87.             List<Player> players = factory.Create();
  88.  
  89.             return players;
  90.         }
  91.  
  92.         public void ShowPlayers(List<Player> players)
  93.         {
  94.             Console.WriteLine($"Name\t\tLevel\tStrength");
  95.  
  96.             foreach (Player player in players)
  97.             {
  98.                 string tabulation = FixTabulation(player.Name);
  99.  
  100.                 Console.WriteLine($"{player.Name}{tabulation}{player.Level}\t{player.Strength}");
  101.             }
  102.         }
  103.  
  104.         public void ShowTopPlayers(List<Top> players)
  105.         {
  106.             int currentPlace = 0;
  107.  
  108.             foreach (Top player in players)
  109.             {
  110.                 if (currentPlace != player.Place)
  111.                 {
  112.                     Console.WriteLine($"\nPlace {player.Place} ");
  113.                     currentPlace = player.Place;
  114.                 }
  115.  
  116.                 string tabulation = FixTabulation(player.Player.Name);
  117.  
  118.                 Console.WriteLine($"{player.Player.Name}{tabulation}level: {player.Player.Level}\t strength: {player.Player.Strength}");
  119.             }
  120.         }
  121.  
  122.         public List<Top> SelectTopPlayers(List<Player> players, string typeSort)
  123.         {
  124.             int countRecords = 3;
  125.  
  126.             List<Top> selectedPlayers = new List<Top>();
  127.             List<int> topCharacteristic = null;
  128.  
  129.             if (typeSort == LevelSortCommand)
  130.             {
  131.                 topCharacteristic = players.Select(player => player.Level).Distinct().OrderByDescending(level => level).Take(countRecords).ToList();
  132.             }
  133.             else if (typeSort == StrengthSortCommand)
  134.             {
  135.                 topCharacteristic = players.Select(player => player.Strength).Distinct().OrderByDescending(strength => strength).Take(countRecords).ToList();
  136.             }
  137.  
  138.             foreach (Player player in players)
  139.             {
  140.                 for (int i = 0; i < countRecords; i++)
  141.                 {
  142.                     if (typeSort == LevelSortCommand)
  143.                     {
  144.                         if (player.Level == topCharacteristic[i])
  145.                         {
  146.                             selectedPlayers.Add(new Top(i + 1, player));
  147.                         }
  148.                     }
  149.                     else if (typeSort == StrengthSortCommand)
  150.                     {
  151.                         if (player.Strength == topCharacteristic[i])
  152.                         {
  153.                             selectedPlayers.Add(new Top(i + 1, player));
  154.                         }
  155.                     }
  156.                 }
  157.             }
  158.  
  159.             selectedPlayers = selectedPlayers.OrderBy(player => player.Place).ToList();
  160.  
  161.             return selectedPlayers;
  162.         }
  163.  
  164.         private string FixTabulation(string line)
  165.         {
  166.             string tabulation = "\t";
  167.             int maxLength = 7;
  168.  
  169.             if (line.Length <= maxLength)
  170.             {
  171.                 tabulation += tabulation;
  172.             }
  173.  
  174.             return tabulation;
  175.         }
  176.     }
  177.  
  178.     public class Top
  179.     {
  180.         public Top(int place, Player player)
  181.         {
  182.             Place = place;
  183.             Player = player;
  184.         }
  185.  
  186.         public int Place { get; private set; }
  187.         public Player Player { get; private set; }
  188.     }
  189.  
  190.     public class PlayerFactory
  191.     {
  192.         private readonly List<string> _names;
  193.         private readonly Random _random;
  194.  
  195.         public PlayerFactory()
  196.         {
  197.             _random = new Random();
  198.  
  199.             _names = new List<string>{"Aleksandr", "Nikolaj", "Ivan", "Sergej", "Vladimir", "Mikhail", "Vasilij", "Aleksej", "Andrej",
  200.                 "Dmitrij", "Viktor", "Yurij", "Petr", "Anatolij", "Pavel", "Igor", "Evgenij", "Oleg", "Valerij", "Vitalij", "Grigorij",
  201.                 "Fedor", "Boris", "Gennadij", "Leonid", "Konstantin", "Vyacheslav", "Georgij", "Denis", "Valentin", "Maksim", "Vadim",
  202.                 "Roman", "Ilya", "Ruslan", "Stepan", "Semen", "Anton", "Eduard", "Stanislav"};
  203.         }
  204.  
  205.         public List<Player> Create()
  206.         {
  207.             int playersCount = 20;
  208.             List<Player> players = new List<Player>();
  209.  
  210.             for (int i = 0; i < playersCount; i++)
  211.             {
  212.                 players.Add(new Player(GetName(), GetIntValue(), GetIntValue()));
  213.             }
  214.  
  215.             return players;
  216.         }
  217.  
  218.         public string GetName()
  219.         {
  220.             int maxValue = 99;
  221.  
  222.             return _names[_random.Next(_names.Count)] + _random.Next(maxValue+1);
  223.         }
  224.  
  225.         public int GetIntValue()
  226.         {
  227.             int minValue = 1;
  228.             int maxValue = 10;
  229.  
  230.             return _random.Next(minValue, maxValue + 1);
  231.         }
  232.     }
  233.  
  234.     public class Player
  235.     {
  236.         public Player(string name, int level, int strength)
  237.         {
  238.             Name = name;
  239.             Level = level;
  240.             Strength = strength;
  241.         }
  242.        
  243.         public string Name { get; private set; }
  244.         public int Level { get; private set; }
  245.         public int Strength { get; private set; }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement