Advertisement
ZhongNi

TopServerPlayersSimple

Dec 19th, 2024 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.44 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 readonly List<Player> _players;
  19.         private readonly PlayerBase _playerBase;
  20.  
  21.         public Game()
  22.         {
  23.             _playerBase = new PlayerBase();
  24.             _players = _playerBase.Fill();
  25.         }
  26.  
  27.         public void ShowTopPlayers()
  28.         {
  29.             _playerBase.ShowPlayers(_players);
  30.  
  31.             bool isRuning = true;
  32.  
  33.             while (isRuning)
  34.             {
  35.                 string typeSort = _playerBase.ProcessMenu();
  36.  
  37.                 if (typeSort.ToLower() == PlayerBase.ExitCommand.ToLower())
  38.                 {
  39.                     isRuning = false;
  40.                 }
  41.                 else if (Validate(typeSort))
  42.                 {
  43.                     List<Player> sortedPlayers = _playerBase.SelectTopPlayers(_players, typeSort);
  44.                     _playerBase.ShowPlayers(sortedPlayers);
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private bool Validate(string typeSort)
  50.         {
  51.             if (typeSort == PlayerBase.LevelSortCommand || typeSort == PlayerBase.StrengthSortCommand)
  52.             {
  53.                 return true;
  54.             }
  55.  
  56.             Console.WriteLine("\nUnknown command. Press something..");
  57.             Console.ReadKey();
  58.  
  59.             return false;
  60.         }
  61.     }
  62.  
  63.     public class PlayerBase
  64.     {
  65.         public const string LevelSortCommand = "1";
  66.         public const string StrengthSortCommand = "2";
  67.         public const string ExitCommand = "Exit";
  68.  
  69.         public List<Player> Fill()
  70.         {
  71.             PlayerFactory factory = new PlayerFactory();
  72.             List<Player> players = factory.Create();
  73.  
  74.             return players;
  75.         }
  76.  
  77.         public string ProcessMenu()
  78.         {
  79.             Console.WriteLine($"\nSelect command: " +
  80.                 $"\nshow top level - {PlayerBase.LevelSortCommand}" +
  81.                 $"\nshow top strength - {PlayerBase.StrengthSortCommand}" +
  82.                 $"\n\"{ExitCommand}\" for exit");
  83.  
  84.             return Console.ReadLine();
  85.         }
  86.  
  87.         public void ShowPlayers(List<Player> players)
  88.         {
  89.             Console.WriteLine($"Name\t\tLevel\tStrength");
  90.  
  91.             foreach (Player player in players)
  92.             {
  93.                 string tabulation = FixTabulation(player.Name);
  94.  
  95.                 Console.WriteLine($"{player.Name}{tabulation}{player.Level}\t{player.Strength}");
  96.             }
  97.         }
  98.  
  99.         public List<Player> SelectTopPlayers(List<Player> players, string typeSort)
  100.         {
  101.             int countRecords = 3;
  102.  
  103.             List<Player> selectedPlayers = new List<Player>();
  104.  
  105.             if (typeSort == LevelSortCommand)
  106.             {
  107.                 selectedPlayers = players.OrderByDescending(player =>player.Level).ThenByDescending(player => player.Strength).Take(countRecords).ToList();
  108.             }
  109.             else if (typeSort == StrengthSortCommand)
  110.             {
  111.                 selectedPlayers = players.OrderByDescending(player => player.Strength).ThenByDescending(player => player.Level).Take(countRecords).ToList();
  112.             }
  113.  
  114.             return selectedPlayers;
  115.         }
  116.  
  117.         private string FixTabulation(string line)
  118.         {
  119.             string tabulation = "\t";
  120.             int maxLength = 7;
  121.  
  122.             if (line.Length <= maxLength)
  123.             {
  124.                 tabulation += tabulation;
  125.             }
  126.  
  127.             return tabulation;
  128.         }
  129.     }
  130.  
  131.     public class PlayerFactory
  132.     {
  133.         private readonly List<string> _names;
  134.         private readonly Random _random;
  135.  
  136.         public PlayerFactory()
  137.         {
  138.             _random = new Random();
  139.  
  140.             _names = new List<string>{"Aleksandr", "Nikolaj", "Ivan", "Sergej", "Vladimir", "Mikhail", "Vasilij", "Aleksej", "Andrej",
  141.                 "Dmitrij", "Viktor", "Yurij", "Petr", "Anatolij", "Pavel", "Igor", "Evgenij", "Oleg", "Valerij", "Vitalij", "Grigorij",
  142.                 "Fedor", "Boris", "Gennadij", "Leonid", "Konstantin", "Vyacheslav", "Georgij", "Denis", "Valentin", "Maksim", "Vadim",
  143.                 "Roman", "Ilya", "Ruslan", "Stepan", "Semen", "Anton", "Eduard", "Stanislav"};
  144.         }
  145.  
  146.         public List<Player> Create()
  147.         {
  148.             int playersCount = 20;
  149.             List<Player> players = new List<Player>();
  150.  
  151.             for (int i = 0; i < playersCount; i++)
  152.             {
  153.                 players.Add(new Player(GetName(), GetIntValue(), GetIntValue()));
  154.             }
  155.  
  156.             return players;
  157.         }
  158.  
  159.         public string GetName()
  160.         {
  161.             int maxValue = 99;
  162.  
  163.             return _names[_random.Next(_names.Count)] + _random.Next(maxValue + 1);
  164.         }
  165.  
  166.         public int GetIntValue()
  167.         {
  168.             int minValue = 1;
  169.             int maxValue = 10;
  170.  
  171.             return _random.Next(minValue, maxValue + 1);
  172.         }
  173.     }
  174.  
  175.     public class Player
  176.     {
  177.         public Player(string name, int level, int strength)
  178.         {
  179.             Name = name;
  180.             Level = level;
  181.             Strength = strength;
  182.         }
  183.  
  184.         public string Name { get; private set; }
  185.         public int Level { get; private set; }
  186.         public int Strength { get; private set; }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement