Last active
May 14, 2025 21:26
-
-
Save KACTAHEDA/d3849315492be20bc45a0606a0af8351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Classes | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string AddPlayerCommand = "1"; | |
const string BanPlayerCommand = "2"; | |
const string UnbanPlayerCommand = "3"; | |
const string DeletePlayerById = "4"; | |
const string ShowPlayersInfoCommand = "5"; | |
const string ExitCommand = "6"; | |
bool isWork = true; | |
Database database = new Database(); | |
while (isWork) | |
{ | |
ShowStartMenu(AddPlayerCommand, BanPlayerCommand, UnbanPlayerCommand, DeletePlayerById, ShowPlayersInfoCommand, ExitCommand); | |
switch (Console.ReadLine()) | |
{ | |
case AddPlayerCommand: | |
database.AddPlayer(); | |
break; | |
case BanPlayerCommand: | |
database.BanPlayerById(); | |
break; | |
case UnbanPlayerCommand: | |
database.UnbanPlayerById(); | |
break; | |
case DeletePlayerById: | |
database.DeleteByID(); | |
break; | |
case ShowPlayersInfoCommand: | |
database.ShowAllPlayers(); | |
break; | |
case ExitCommand: | |
isWork = false; | |
break; | |
default: | |
Console.WriteLine("Неверная команда"); | |
Messages.Continue(); | |
break; | |
} | |
} | |
static void ShowStartMenu(string add, string ban, string unban, string delete, string show, string exit) | |
{ | |
Console.Clear(); | |
Console.WriteLine($"\nЧтобы добавить игрока введите {add}" + | |
$"\nЧтобы забанить игрока по ID введите {ban}" + | |
$"\nЧтобы разбанить игрока по ID введите {unban}" + | |
$"\nЧтобы удалить игрока по ID введите {delete}" + | |
$"\nЧтобы посмотреть всех игроков введите {show}" + | |
$"\nЧтобы выйти введите {exit}\n"); | |
} | |
} | |
} | |
class Player | |
{ | |
public Player(int uniqueID, int lvl, string name, bool isBanned) | |
{ | |
UniqueID = uniqueID; | |
Level = lvl; | |
Name = name; | |
IsBanned = isBanned; | |
} | |
public int UniqueID { get; private set; } | |
public int Level { get; private set; } | |
public string Name { get; private set; } | |
public bool IsBanned { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"\nID: {UniqueID}\nИмя: {Name}\nУровень: {Level}\nЗабанен: {IsBanned}"); | |
} | |
public void Ban() | |
{ | |
IsBanned = true; | |
} | |
public void Unban() | |
{ | |
IsBanned = false; | |
} | |
} | |
class Database | |
{ | |
private int _nextPlayerID = 101; | |
private List<Player> _players = new List<Player>(); | |
private InpurNumberUtilite _inputNumber = new InpurNumberUtilite(); | |
public void ShowAllPlayers() | |
{ | |
foreach (var player in _players) | |
{ | |
player.ShowInfo(); | |
} | |
Messages.Continue(); | |
} | |
public void AddPlayer() | |
{ | |
_players.Add(CreateNewPlayer()); | |
Messages.Continue(); | |
} | |
public void DeleteByID() | |
{ | |
Player playerToDelete; | |
Console.WriteLine("Введите ID игрока для удаления;"); | |
if (TryGetPlayerByID(out playerToDelete)) | |
{ | |
_players.Remove(playerToDelete); | |
Console.WriteLine("\nИгрок удален."); | |
} | |
Messages.Continue(); | |
} | |
public void BanPlayerById() | |
{ | |
Player playerToBanning; | |
Console.WriteLine("Введите ID игрока для бана: "); | |
if (TryGetPlayerByID(out playerToBanning)) | |
{ | |
playerToBanning.Ban(); | |
Console.WriteLine($"Игрок {playerToBanning.Name} забанен"); | |
} | |
Messages.Continue(); | |
} | |
public void UnbanPlayerById() | |
{ | |
Console.WriteLine("Введите ID игрока для разбана: "); | |
Player playerToUnbanning; | |
if (TryGetPlayerByID(out playerToUnbanning)) | |
{ | |
playerToUnbanning.Unban(); | |
Console.WriteLine($"Игрок {playerToUnbanning.Name} разбанен"); | |
} | |
Messages.Continue(); | |
} | |
private bool TryGetPlayerByID(out Player player) | |
{ | |
int checkID = _inputNumber.TryReadNumber(); | |
bool isMatch = false; | |
Player playerToReturn = null; | |
foreach (var playerInBase in _players) | |
{ | |
if (playerInBase.UniqueID == checkID) | |
{ | |
playerToReturn = playerInBase; | |
isMatch = true; | |
} | |
} | |
if (isMatch == false) | |
{ | |
Console.WriteLine($"В базе нет игрока с ID {checkID}"); | |
} | |
player = playerToReturn; | |
return isMatch; | |
} | |
private Player CreateNewPlayer() | |
{ | |
Console.WriteLine("Как зовут Вашего героя?"); | |
string name = Console.ReadLine(); | |
Console.WriteLine("Какой уровень Вашего героя?"); | |
int level = _inputNumber.TryReadNumber(); | |
Player newPlayer = new Player(_nextPlayerID, level, name, false); | |
_nextPlayerID++; | |
return newPlayer; | |
} | |
} | |
class InpurNumberUtilite | |
{ | |
public int TryReadNumber() | |
{ | |
int number = 0; | |
bool isFine = false; | |
while (isFine == false) | |
{ | |
isFine = Int32.TryParse(Console.ReadLine(), out int value); | |
if (isFine == false) | |
{ | |
Console.WriteLine("Ожидалось, что Вы введете число."); | |
Console.WriteLine("Попробуйте еще раз: "); | |
} | |
number = value; | |
} | |
return number; | |
} | |
} | |
static class Messages | |
{ | |
public static void Continue() | |
{ | |
Console.WriteLine("\nНажмите любую клавишу для продолжения..."); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment