Advertisement
TeT91

ДЗ База данных игроков

May 26th, 2024 (edited)
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const string CommandAddNewPlayer = "0";
  11.             const string CommandDeletePlayer = "1";
  12.             const string CommandBanPlayer = "2";
  13.             const string CommandUnbanPlayer = "3";
  14.             const string CommandShowPlayerInfo = "4";
  15.  
  16.             PlayersDataBase Database = new PlayersDataBase();
  17.  
  18.             bool isRunning = true;
  19.  
  20.             while (isRunning)
  21.             {
  22.                 Console.WriteLine("Введите команду \n" +
  23.                     $"{CommandAddNewPlayer} - Добавить игрока\n" +
  24.                     $"{CommandDeletePlayer} - Удалить игрока\n" +
  25.                     $"{CommandBanPlayer} - Забанить игрока\n" +
  26.                     $"{CommandUnbanPlayer} - Разбанить игрока\n" +
  27.                     $"{CommandShowPlayerInfo} - Показать инфо игроков\n");
  28.  
  29.                 string userInput = Console.ReadLine();
  30.  
  31.                 switch (userInput)
  32.                 {
  33.                     case CommandAddNewPlayer:
  34.                         Database.AddPlayer();
  35.                         break;
  36.  
  37.                     case CommandDeletePlayer:
  38.                         Database.DeletePlayer();
  39.                         break;
  40.  
  41.                     case CommandBanPlayer:
  42.                         Database.BanPlayer();
  43.                         break;
  44.  
  45.                     case CommandUnbanPlayer:
  46.                         Database.UnbanPlayer();
  47.                         break;
  48.  
  49.                     case CommandShowPlayerInfo:
  50.                         Database.ShowPlayersInfo();
  51.                         break;
  52.  
  53.                     default:
  54.                         isRunning = false;
  55.                         break;
  56.                 }
  57.  
  58.                 Console.ReadKey();
  59.                 Console.Clear();
  60.             }
  61.         }
  62.     }
  63.  
  64.     class PlayersDataBase
  65.     {
  66.         private Dictionary<int, Player> _players = new Dictionary<int, Player>();
  67.  
  68.         public PlayersDataBase()
  69.         {
  70.             Random random = new Random();
  71.             int minValue = 1;
  72.             int maxValue = 10;
  73.  
  74.             for (int i = 0; i < random.Next(minValue, maxValue); i++)
  75.             {
  76.                 AddPlayer();
  77.             }
  78.         }
  79.  
  80.  
  81.         public void ShowPlayersInfo()
  82.         {
  83.             foreach (Player player in _players.Values)
  84.             {
  85.                 Console.WriteLine($"ID - {player.Id} ИМЯ - {player.Name} УРОВЕНЬ - {player.Level} ЗАБАНЕН? - {player.IsBanned}");
  86.             }
  87.         }
  88.  
  89.         public void AddPlayer()
  90.         {
  91.             int id = GenerateUniqueId();
  92.             _players.Add(id, new Player(id));
  93.             Console.WriteLine($"Игрок с id {id} добавлен");
  94.         }
  95.  
  96.         public void DeletePlayer()
  97.         {
  98.             if (TryGetPlayer(out Player player))
  99.             {
  100.                 _players.Remove(player.Id);
  101.             }
  102.         }
  103.  
  104.         public void BanPlayer()
  105.         {
  106.             if (TryGetPlayer(out Player player))
  107.             {
  108.                 player.Ban(); ;
  109.             }
  110.         }
  111.  
  112.         public void UnbanPlayer()
  113.         {
  114.             if (TryGetPlayer(out Player player))
  115.             {
  116.                 player.Unban();
  117.             }
  118.         }
  119.  
  120.         private bool TryGetPlayer(out Player player)
  121.         {
  122.             player = null;
  123.  
  124.             Console.WriteLine("Введите ID");
  125.  
  126.             if (int.TryParse(Console.ReadLine(), out int id))
  127.             {
  128.                 if (_players.ContainsKey(id))
  129.                 {
  130.                     player = _players[id];
  131.                     return true;
  132.                 }
  133.                 else
  134.                 {
  135.                     Console.WriteLine("Такого ID не найдено");
  136.                 }
  137.             }
  138.             else
  139.             {
  140.                 Console.WriteLine("Неверный ввод");
  141.             }
  142.             return false;
  143.         }
  144.  
  145.         private int GenerateUniqueId()
  146.         {
  147.             int id = 0;
  148.             bool isIdGenerated = false;
  149.  
  150.             while (isIdGenerated == false)
  151.             {
  152.                 if (_players.ContainsKey(id) == false)
  153.                 {
  154.                     isIdGenerated = true;
  155.                 }
  156.                 else
  157.                 {
  158.                     id++;
  159.                 }
  160.             }
  161.  
  162.             return id;
  163.         }
  164.     }
  165.  
  166.     class Player
  167.     {
  168.         public Player(int id)
  169.         {
  170.             Id = id;
  171.             Name = "Player " + Id;
  172.         }
  173.  
  174.         public int Id { get; private set; }
  175.  
  176.         public bool IsBanned { get; private set; } = false;
  177.  
  178.         public string Name { get; private set; }
  179.  
  180.         public int Level { get; private set; } = 1;
  181.  
  182.         public void Ban()
  183.         {
  184.             IsBanned = true;
  185.         }
  186.  
  187.         public void Unban()
  188.         {
  189.             IsBanned = false;
  190.         }
  191.     }
  192. }
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement