/
expens1ve
/
project_war
Обзор
Документация
Войти
/
expens1ve
/
project_war
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
solid
UI/ConsoleUserInterface.cs
176 строк
7 KB
Vadim
add command, undo, redo
20 май 2026, 01:42
20 май 2026, 01:42
27e173e
Код
Авторство
О чём код?
using System; using System.IO; using project_war.Engine; using project_war.Engine.Formations; using project_war.Engine.Interfaces; using project_war.Models.Interfaces; using project_war.UI.Core; namespace project_war.UI { public sealed class ConsoleUserInterface : IUserInterface { private readonly IGameService _gameService; private readonly ArmyConfigurator _armyConfigurator; public ConsoleUserInterface(IGameService gameService) { _gameService = gameService ?? throw new ArgumentNullException(nameof(gameService)); _armyConfigurator = new ArmyConfigurator(gameService); } public void Run() { bool exitMainMenu = false; while (!exitMainMenu) { Console.Clear(); Console.WriteLine("=== Project War — главное меню ==="); Console.WriteLine("1. Начать новую игру (выбор random/manual для каждой армии)"); Console.WriteLine("2. Загрузить игру из сохранения"); Console.WriteLine("3. Полностью выйти из игры"); Console.WriteLine("4. Настройки (Вкл/Выкл логи и звук)"); Console.Write("Ваш выбор (1-4, по умолчанию 1): "); IArmy? armyA = null; IArmy? armyB = null; int initialRound = 0; string? formationType = null; string? choice = Console.ReadLine(); if (string.IsNullOrWhiteSpace(choice)) choice = "1"; if (choice == "3") { exitMainMenu = true; continue; } if (choice == "4") { ShowSettingsMenu(); continue; } if (choice == "2") { try { var savePath = SaveLoadDialogs.PromptForExistingSaveFilePath(); if (string.IsNullOrWhiteSpace(savePath)) { Console.WriteLine("Сохранения не выбраны или не найдены."); Pause(); continue; } Console.WriteLine("Загрузка игры..."); var state = _gameService.LoadGame(savePath); armyA = state.army1; armyB = state.army2; initialRound = state.roundNumber; formationType = state.formationType; } catch (Exception ex) { Console.WriteLine($"Ошибка загрузки: {ex.Message}"); Pause(); continue; } } else if (armyA == null || armyB == null) { armyA = _armyConfigurator.CreateOneArmy("Армия A"); armyB = _armyConfigurator.CreateOneArmy("Армия B"); formationType = PromptFormationType(); Console.WriteLine(); Console.WriteLine($"Созданы армии: {armyA.Name} ({armyA.Units.Count} юнитов) VS {armyB.Name} ({armyB.Units.Count} юнитов)"); Console.WriteLine("Нажмите Enter, чтобы перейти к интерфейсу боя..."); Console.ReadLine(); } IBattlefield battlefield = _gameService.StartBattle(armyA, armyB, formationType); using var battleScreen = new BattleScreen( _gameService, battlefield, armiesProvider: () => new[] { armyA, armyB }, initialRound: initialRound, saveCallback: round => { var savePath = SaveLoadDialogs.PromptForSaveFilePath(); _gameService.SaveGame( armyA, armyB, round, battlefield.CurrentFormation.GetType().Name, savePath); Console.WriteLine($"Сохранение записано: {Path.GetFileName(savePath)}"); }); battleScreen.Run(); } } private static string PromptFormationType() { Console.WriteLine(); Console.WriteLine("=== Выбор построения до начала боя ==="); Console.WriteLine("1. Узкий мост"); Console.WriteLine("2. Широкий мост (3 бойца)"); Console.WriteLine("3. Стенка на стенку"); Console.Write("Ваш выбор (1-3, по умолчанию 1): "); string? choice = Console.ReadLine(); return choice?.Trim() switch { "2" => nameof(WideBridgeFormation), "3" => nameof(WallFormation), _ => nameof(BridgeFormation) }; } private static void ShowSettingsMenu() { var config = ConfigurationManager.Instance; bool exitSettings = false; while (!exitSettings) { Console.Clear(); Console.WriteLine("=== Настройки ==="); Console.WriteLine($"1. Логирование урона: {(config.IsDamageLoggingEnabled ? "ВКЛ" : "ВЫКЛ")}"); Console.WriteLine($"2. Звук при смерти: {(config.IsDeathBeepEnabled ? "ВКЛ" : "ВЫКЛ")}"); Console.WriteLine("3. Вернуться в главное меню"); Console.Write("Выберите пункт: "); string? choice = Console.ReadLine(); switch (choice) { case "1": config.IsDamageLoggingEnabled = !config.IsDamageLoggingEnabled; break; case "2": config.IsDeathBeepEnabled = !config.IsDeathBeepEnabled; break; case "3": exitSettings = true; break; } } } private static void Pause() { Console.WriteLine("Нажмите любую клавишу, чтобы продолжить..."); Console.ReadKey(intercept: true); } public void Dispose() { } } }