Advertisement
MaoChessy

Task 30

Nov 7th, 2020 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. namespace C_sharp_Light
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             PlayerDataBase playerData = new PlayerDataBase();
  10.             playerData.Work();
  11.         }
  12.     }
  13.  
  14. class Player
  15.     {
  16.         public string Name { get; private set;  }
  17.         public int Lvl { get; private set; }
  18.         public bool IsBan { get; private set;  }
  19.         public int ID { get; private set; }
  20.  
  21.         public Player(string name, int id)
  22.         {
  23.             Name = name;
  24.             ID = id;
  25.             IsBan = false;
  26.             Lvl = 0;
  27.         }
  28.         public void Ban()
  29.         {
  30.             IsBan = true;
  31.         }
  32.         public void Unban()
  33.         {
  34.             IsBan = false;
  35.         }
  36.     }
  37.     class PlayerDataBase
  38.     {
  39.         private List<Player> _players = new List<Player>();
  40.  
  41.         public void Work()
  42.         {
  43.             bool isOpen = true;
  44.             while (isOpen)
  45.             {
  46.                 Console.WriteLine("1 - Вывести базу данных \n2 - Добавить игрока \n3 - Удалить игрока \n4 - Забанить игрока \n5 - Разбанить игрока \n0- Выход");
  47.                 ConsoleKeyInfo key = Console.ReadKey();
  48.                 Console.Clear();
  49.                 switch (key.Key)
  50.                 {
  51.                     case ConsoleKey.D1:
  52.                         WriteAllDataBase();
  53.                         break;
  54.                     case ConsoleKey.D2:
  55.                         Console.Write("Введите имя игрока: ");
  56.                         AddPlayer(Console.ReadLine());
  57.                         break;
  58.                     case ConsoleKey.D3:
  59.                         Console.Write("Введите id игрока: ");
  60.                         DeletePlayer(Convert.ToInt32(Console.ReadLine()));
  61.                         break;
  62.                     case ConsoleKey.D4:
  63.                         Console.Write("Введите id игрока: ");
  64.                         BanPlayer(Convert.ToInt32(Console.ReadLine()));
  65.                         break;
  66.                     case ConsoleKey.D5:
  67.                         Console.Write("Введите id игрока: ");
  68.                         UnbanPlayer(Convert.ToInt32(Console.ReadLine()));
  69.                         break;
  70.                     case ConsoleKey.D0:
  71.                         isOpen = false;
  72.                         break;
  73.                 }
  74.                 if (isOpen)
  75.                     Console.ReadKey();
  76.                 Console.Clear();
  77.             }
  78.         }
  79.         //
  80.         private void WriteAllDataBase()
  81.         {
  82.             int counter = 0;
  83.             int idPositionX = 5;
  84.             int namePositionX = idPositionX + 7;
  85.             int lvlPositionX = namePositionX + 12;
  86.             int banPositionX = lvlPositionX + 7;
  87.             foreach (Player player in _players)
  88.             {
  89.                 Console.ForegroundColor = ConsoleColor.DarkBlue;
  90.                 Console.Write($"{counter}:");
  91.  
  92.                 Console.ForegroundColor = ConsoleColor.White;
  93.                 Console.CursorLeft = idPositionX;
  94.                 Console.Write($"id:{player.ID}");
  95.                 Console.CursorLeft = namePositionX;
  96.                 Console.Write($"{player.Name}");
  97.                 Console.CursorLeft = lvlPositionX;
  98.                 Console.Write($"Lvl:{player.Lvl}");
  99.                
  100.                 if(player.IsBan)
  101.                     Console.ForegroundColor = ConsoleColor.Red;
  102.                 else
  103.                     Console.ForegroundColor = ConsoleColor.Green;
  104.                 Console.CursorLeft = banPositionX;
  105.         Console.Write($"ban: {player.IsBan.ToString()}. \n");
  106.  
  107.                 Console.ForegroundColor = ConsoleColor.White;
  108.                 counter++;
  109.             }
  110.         }
  111.         private void AddPlayer(string name)
  112.         {
  113.             _players.Add(new Player(name, GetFreeId()));
  114.             SortDataBaseByID();
  115.         }
  116.         private void DeletePlayer(int id)
  117.         {
  118.             int index = FindIndexPlayerByIdPlayer(id);
  119.             if (index >= 0)
  120.                 _players.RemoveAt(index);
  121.             else
  122.                 DebugError("Такого ID не существует!");
  123.         }
  124.         private void BanPlayer(int id)
  125.         {
  126.             int index = FindIndexPlayerByIdPlayer(id);
  127.             if (index >= 0)
  128.                 _players[index].Ban();
  129.             else
  130.                 DebugError("Такого ID не существует!");
  131.         }
  132.         private void UnbanPlayer(int id)
  133.         {
  134.             int index = FindIndexPlayerByIdPlayer(id);
  135.             if (index >= 0)
  136.                 _players[index].Unban();
  137.             else
  138.                 DebugError("Такого ID не существует!");
  139.         }
  140.         //
  141.         private void DebugError(string massage)
  142.         {
  143.             ConsoleColor colorConsoleBefore = Console.ForegroundColor;
  144.             Console.ForegroundColor = ConsoleColor.Red;
  145.             Console.WriteLine($"\n Ошибка: {massage}");
  146.             Console.ForegroundColor = colorConsoleBefore;
  147.         }
  148.         private int FindIndexPlayerByIdPlayer(int id)
  149.         {
  150.             for (int i = 0; i < _players.Count; i++)
  151.             {
  152.                 if (_players[i].ID == id)
  153.                     return i;
  154.             }
  155.             return -1;
  156.         }
  157.         private int GetFreeId()
  158.         {
  159.             SortDataBaseByID();
  160.             if (_players.Count == 0)
  161.                 return 0;
  162.             if(_players[0].ID != 0)
  163.                 return 0;
  164.             for (int i = 0; i < _players.Count-1; i++)
  165.             {
  166.                 if (_players[i].ID + 1 != _players[i + 1].ID)
  167.                     return _players[i].ID + 1;
  168.             }
  169.             return _players[_players.Count - 1].ID + 1;
  170.         }
  171.         private void SortDataBaseByID()
  172.         {
  173.             _players.Sort(delegate (Player x, Player y)
  174.             {
  175.                     if (x.ID > y.ID) return 1;
  176.                     else if (x.ID<y.ID) return -1;
  177.                     return 0;
  178.             });
  179.         }
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement