Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace C_sharp_Light
- {
- class Program
- {
- static void Main(string[] args)
- {
- PlayerDataBase playerData = new PlayerDataBase();
- playerData.Work();
- }
- }
- class Player
- {
- public string Name { get; private set; }
- public int Lvl { get; private set; }
- public bool IsBan { get; private set; }
- public int ID { get; private set; }
- public Player(string name, int id)
- {
- Name = name;
- ID = id;
- IsBan = false;
- Lvl = 0;
- }
- public void Ban()
- {
- IsBan = true;
- }
- public void Unban()
- {
- IsBan = false;
- }
- }
- class PlayerDataBase
- {
- private List<Player> _players = new List<Player>();
- public void Work()
- {
- bool isOpen = true;
- while (isOpen)
- {
- Console.WriteLine("1 - Вывести базу данных \n2 - Добавить игрока \n3 - Удалить игрока \n4 - Забанить игрока \n5 - Разбанить игрока \n0- Выход");
- ConsoleKeyInfo key = Console.ReadKey();
- Console.Clear();
- switch (key.Key)
- {
- case ConsoleKey.D1:
- WriteAllDataBase();
- break;
- case ConsoleKey.D2:
- Console.Write("Введите имя игрока: ");
- AddPlayer(Console.ReadLine());
- break;
- case ConsoleKey.D3:
- Console.Write("Введите id игрока: ");
- DeletePlayer(Convert.ToInt32(Console.ReadLine()));
- break;
- case ConsoleKey.D4:
- Console.Write("Введите id игрока: ");
- BanPlayer(Convert.ToInt32(Console.ReadLine()));
- break;
- case ConsoleKey.D5:
- Console.Write("Введите id игрока: ");
- UnbanPlayer(Convert.ToInt32(Console.ReadLine()));
- break;
- case ConsoleKey.D0:
- isOpen = false;
- break;
- }
- if (isOpen)
- Console.ReadKey();
- Console.Clear();
- }
- }
- //
- private void WriteAllDataBase()
- {
- int counter = 0;
- int idPositionX = 5;
- int namePositionX = idPositionX + 7;
- int lvlPositionX = namePositionX + 12;
- int banPositionX = lvlPositionX + 7;
- foreach (Player player in _players)
- {
- Console.ForegroundColor = ConsoleColor.DarkBlue;
- Console.Write($"{counter}:");
- Console.ForegroundColor = ConsoleColor.White;
- Console.CursorLeft = idPositionX;
- Console.Write($"id:{player.ID}");
- Console.CursorLeft = namePositionX;
- Console.Write($"{player.Name}");
- Console.CursorLeft = lvlPositionX;
- Console.Write($"Lvl:{player.Lvl}");
- if(player.IsBan)
- Console.ForegroundColor = ConsoleColor.Red;
- else
- Console.ForegroundColor = ConsoleColor.Green;
- Console.CursorLeft = banPositionX;
- Console.Write($"ban: {player.IsBan.ToString()}. \n");
- Console.ForegroundColor = ConsoleColor.White;
- counter++;
- }
- }
- private void AddPlayer(string name)
- {
- _players.Add(new Player(name, GetFreeId()));
- SortDataBaseByID();
- }
- private void DeletePlayer(int id)
- {
- int index = FindIndexPlayerByIdPlayer(id);
- if (index >= 0)
- _players.RemoveAt(index);
- else
- DebugError("Такого ID не существует!");
- }
- private void BanPlayer(int id)
- {
- int index = FindIndexPlayerByIdPlayer(id);
- if (index >= 0)
- _players[index].Ban();
- else
- DebugError("Такого ID не существует!");
- }
- private void UnbanPlayer(int id)
- {
- int index = FindIndexPlayerByIdPlayer(id);
- if (index >= 0)
- _players[index].Unban();
- else
- DebugError("Такого ID не существует!");
- }
- //
- private void DebugError(string massage)
- {
- ConsoleColor colorConsoleBefore = Console.ForegroundColor;
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"\n Ошибка: {massage}");
- Console.ForegroundColor = colorConsoleBefore;
- }
- private int FindIndexPlayerByIdPlayer(int id)
- {
- for (int i = 0; i < _players.Count; i++)
- {
- if (_players[i].ID == id)
- return i;
- }
- return -1;
- }
- private int GetFreeId()
- {
- SortDataBaseByID();
- if (_players.Count == 0)
- return 0;
- if(_players[0].ID != 0)
- return 0;
- for (int i = 0; i < _players.Count-1; i++)
- {
- if (_players[i].ID + 1 != _players[i + 1].ID)
- return _players[i].ID + 1;
- }
- return _players[_players.Count - 1].ID + 1;
- }
- private void SortDataBaseByID()
- {
- _players.Sort(delegate (Player x, Player y)
- {
- if (x.ID > y.ID) return 1;
- else if (x.ID<y.ID) return -1;
- return 0;
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement