Advertisement
ZhongNi

TopServerPlayers (2)

Dec 19th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.22 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<PlayerBase> 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 || typeSort == PlayerBase.StrengthSortCommand)
  64.             {
  65.                 return true;
  66.             }
  67.  
  68.             Console.WriteLine("\nUnknown command. Press something..");
  69.             Console.ReadKey();
  70.  
  71.             return false;
  72.         }
  73.     }
  74.  
  75.     public class PlayerBase
  76.     {
  77.         public const string LevelSortCommand = "1";
  78.         public const string StrengthSortCommand = "2";
  79.  
  80.         public PlayerBase() { }
  81.  
  82.         public PlayerBase(int place, Player player)
  83.         {
  84.             Place = place;
  85.             Player = player;
  86.         }
  87.  
  88.         public int Place { get; private set; }
  89.         public Player Player { get; private set; }
  90.  
  91.         public List<Player> Fill()
  92.         {
  93.             PlayerFactory factory = new PlayerFactory();
  94.             List<Player> players = factory.Create();
  95.  
  96.             return players;
  97.         }
  98.  
  99.         public void ShowPlayers(List<Player> players)
  100.         {
  101.             Console.WriteLine($"Name\t\tLevel\tStrength");
  102.  
  103.             foreach (Player player in players)
  104.             {
  105.                 string tabulation = FixTabulation(player.Name);
  106.  
  107.                 Console.WriteLine($"{player.Name}{tabulation}{player.Level}\t{player.Strength}");
  108.             }
  109.         }
  110.  
  111.         public void ShowTopPlayers(List<PlayerBase> players)
  112.         {
  113.             int currentPlace = 0;
  114.  
  115.             foreach (PlayerBase player in players)
  116.             {
  117.                 if (currentPlace != player.Place)
  118.                 {
  119.                     Console.WriteLine($"\nPlace {player.Place} ");
  120.                     currentPlace = player.Place;
  121.                 }
  122.  
  123.                 string tabulation = FixTabulation(player.Player.Name);
  124.  
  125.                 Console.WriteLine($"{player.Player.Name}{tabulation}level: {player.Player.Level}\t strength: {player.Player.Strength}");
  126.             }
  127.         }
  128.  
  129.         public List<PlayerBase> SelectTopPlayers(List<Player> players, string typeSort)
  130.         {
  131.             int countRecords = 3;
  132.  
  133.             List<PlayerBase> selectedPlayers = new List<PlayerBase>();
  134.             List<int> topCharacteristic = null;
  135.  
  136.             if (typeSort == LevelSortCommand)
  137.             {
  138.                 topCharacteristic = players.Select(player => player.Level).Distinct().OrderByDescending(level => level).Take(countRecords).ToList();
  139.             }
  140.             else if (typeSort == StrengthSortCommand)
  141.             {
  142.                 topCharacteristic = players.Select(player => player.Strength).Distinct().OrderByDescending(strength => strength).Take(countRecords).ToList();
  143.             }
  144.  
  145.             foreach (Player player in players)
  146.             {
  147.                 for (int i = 0; i < countRecords; i++)
  148.                 {
  149.                     if (typeSort == LevelSortCommand)
  150.                     {
  151.                         if (player.Level == topCharacteristic[i])
  152.                         {
  153.                             selectedPlayers.Add(new PlayerBase(i + 1, player));
  154.                         }
  155.                     }
  156.                     else if (typeSort == StrengthSortCommand)
  157.                     {
  158.                         if (player.Strength == topCharacteristic[i])
  159.                         {
  160.                             selectedPlayers.Add(new PlayerBase(i + 1, player));
  161.                         }
  162.                     }
  163.                 }
  164.             }
  165.  
  166.             selectedPlayers = selectedPlayers.OrderBy(player => player.Place).ToList();
  167.  
  168.             return selectedPlayers;
  169.         }
  170.  
  171.         private string FixTabulation(string line)
  172.         {
  173.             string tabulation = "\t";
  174.             int maxLength = 7;
  175.  
  176.             if (line.Length <= maxLength)
  177.             {
  178.                 tabulation += tabulation;
  179.             }
  180.  
  181.             return tabulation;
  182.         }
  183.     }
  184.  
  185.     public class PlayerFactory
  186.     {
  187.         private readonly List<string> _names;
  188.         private readonly Random _random;
  189.  
  190.         public PlayerFactory()
  191.         {
  192.             _random = new Random();
  193.  
  194.             _names = new List<string>{"Aleksandr", "Nikolaj", "Ivan", "Sergej", "Vladimir", "Mikhail", "Vasilij", "Aleksej", "Andrej",
  195.                 "Dmitrij", "Viktor", "Yurij", "Petr", "Anatolij", "Pavel", "Igor", "Evgenij", "Oleg", "Valerij", "Vitalij", "Grigorij",
  196.                 "Fedor", "Boris", "Gennadij", "Leonid", "Konstantin", "Vyacheslav", "Georgij", "Denis", "Valentin", "Maksim", "Vadim",
  197.                 "Roman", "Ilya", "Ruslan", "Stepan", "Semen", "Anton", "Eduard", "Stanislav"};
  198.         }
  199.  
  200.         public List<Player> Create()
  201.         {
  202.             int playersCount = 20;
  203.             List<Player> players = new List<Player>();
  204.  
  205.             for (int i = 0; i < playersCount; i++)
  206.             {
  207.                 players.Add(new Player(GetName(), GetIntValue(), GetIntValue()));
  208.             }
  209.  
  210.             return players;
  211.         }
  212.  
  213.         public string GetName()
  214.         {
  215.             int maxValue = 99;
  216.  
  217.             return _names[_random.Next(_names.Count)] + _random.Next(maxValue + 1);
  218.         }
  219.  
  220.         public int GetIntValue()
  221.         {
  222.             int minValue = 1;
  223.             int maxValue = 10;
  224.  
  225.             return _random.Next(minValue, maxValue + 1);
  226.         }
  227.     }
  228.  
  229.     public class Player
  230.     {
  231.         public Player(string name, int level, int strength)
  232.         {
  233.             Name = name;
  234.             Level = level;
  235.             Strength = strength;
  236.         }
  237.  
  238.         public string Name { get; private set; }
  239.         public int Level { get; private set; }
  240.         public int Strength { get; private set; }
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement