123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System;
- using System.Runtime.InteropServices;
- using uqac_ia_aspirobot.Agent;
- using uqac_ia_aspirobot.Agent.Interfaces.Effectors;
- using uqac_ia_aspirobot.Agent.Interfaces.Sensors;
- using uqac_ia_aspirobot.Extensions;
- using uqac_ia_aspirobot.Interfaces;
-
- namespace uqac_ia_aspirobot.UI
- {
- public class UiConsole : IUi
- {
- private readonly IEnvironment _environment;
- private readonly IAgEngineEffector _agEngineEffector;
- private readonly IAgPickedSensor _agPickedSensor;
- private readonly IAgBatterySensor _agBatterySensor;
- private readonly IAgVaccumSensor _agVaccumSensor;
- private readonly AgState _agState;
- private readonly IAgPerformanceSensor _agPerformanceSensor;
-
- public UiConsole(IEnvironment environment, IAgEngineEffector agEngineEffector, IAgPickedSensor agPickedSensor,
- IAgBatterySensor agBatterySensor, IAgVaccumSensor agVaccumSensor, AgState agState, IAgPerformanceSensor agPerformanceSensor)
- {
- _environment = environment;
- _agEngineEffector = agEngineEffector;
- _agPickedSensor = agPickedSensor;
- _agBatterySensor = agBatterySensor;
- _agVaccumSensor = agVaccumSensor;
- _agState = agState;
- _agPerformanceSensor = agPerformanceSensor;
- }
-
- public void SetBackgroundColor(ConsoleColor? color)
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- Console.BackgroundColor = color.GetValueOrDefault(ConsoleColor.Black);
- }
- else
- {
- var str = "";
- if (color == null)
- {
- str = $"{(char)27}[1;49m";
- }
- if (color == ConsoleColor.Black)
- {
- str = $"{(char)27}[1;40m";
- }
- if (color == ConsoleColor.Red)
- {
- str = $"{(char)27}[1;41m";
- }
- if (color == ConsoleColor.Green)
- {
- str = $"{(char)27}[1;42m";
- }
- if (color == ConsoleColor.Yellow)
- {
- str = $"{(char)27}[1;43m";
- }
- if (color == ConsoleColor.Blue)
- {
- str = $"{(char)27}[1;44m";
- }
- if (color == ConsoleColor.Magenta)
- {
- str = $"{(char)27}[1;45m";
- }
- if (color == ConsoleColor.Cyan)
- {
- str = $"{(char)27}[1;46m";
- }
- if (color == ConsoleColor.Gray)
- {
- str = $"{(char)27}[1;47m";
- }
- Console.Write(str);
- }
- }
-
- public void SetForegroundColor(ConsoleColor? color)
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- Console.ForegroundColor = color.GetValueOrDefault(ConsoleColor.White);
- }
- else
- {
- var str = "";
- if (color == null)
- {
- str = $"{(char)27}[1;39m";
- }
- if (color == ConsoleColor.Black)
- {
- str = $"{(char)27}[1;30m";
- }
- if (color == ConsoleColor.Red)
- {
- str = $"{(char)27}[1;31m";
- }
- if (color == ConsoleColor.Green)
- {
- str = $"{(char)27}[1;32m";
- }
- if (color == ConsoleColor.Yellow)
- {
- str = $"{(char)27}[1;33m";
- }
- if (color == ConsoleColor.Blue)
- {
- str = $"{(char)27}[1;34m";
- }
- if (color == ConsoleColor.Magenta)
- {
- str = $"{(char)27}[1;35m";
- }
- if (color == ConsoleColor.Cyan)
- {
- str = $"{(char)27}[1;36m";
- }
- if (color == ConsoleColor.Gray)
- {
- str = $"{(char)27}[1;37m";
- }
- Console.Write(str);
- }
- }
-
- public void Clear()
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- Console.Clear();
- }
- else
- {
- Console.WriteLine();
- }
- }
-
- public void Update()
- {
- Clear();
- var bgColor = Console.BackgroundColor;
- for (var x = 0; x < _environment.GetWidth(); ++x)
- {
- Console.Write("|");
- for (var y = 0; y < _environment.GetHeight(); ++y)
- {
- var state = _environment.GetRoomState(x, y);
- ConsoleColor? color = null;
- if (state == RoomState.Dust)
- {
- color = ConsoleColor.Gray;
- }
- else if (state == RoomState.Jewel)
- {
- color = ConsoleColor.Cyan;
- }
- else if (state == RoomState.Unknown)
- {
- color = ConsoleColor.Yellow;
- }
- else if (state == (RoomState.Jewel | RoomState.Dust))
- {
- color = ConsoleColor.Red;
- }
- SetBackgroundColor(color);
- var isDest = _agState.Destination?.IsInPosition(x, y) ?? false;
- if (_agEngineEffector.IsInPosition(x, y))
- {
- Console.Write("X");
- }
- else if (isDest)
- {
- SetForegroundColor(ConsoleColor.Red);
- Console.Write("+");
- }
- else
- {
- Console.Write(" ");
- }
- SetForegroundColor(null);
- SetBackgroundColor(null);
- }
- Console.WriteLine("|");
- }
- SetBackgroundColor(bgColor);
- var nextThink = Math.Max(0, (_agState.LastThinkTime.AddMilliseconds(_agState.ThinkTimeInterval) - DateTime.Now).TotalMilliseconds);
- Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
- Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
- Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
- Console.WriteLine($"Think Interval: {_agState.ThinkTimeInterval}ms");
- Console.WriteLine($"Next Think: {nextThink}ms");
- Console.WriteLine($"Performance: {_agPerformanceSensor.Performance}");
- }
- }
- }
|