Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace MOBAChallenger_withClass
- {
- public static class Program
- {
- private static void Main()
- {
- var consoleReader = new Reader(Console.In);
- var consoleWriter = new Writer(Console.Out);
- var commandProcessor = new CommandProcessor(new PlayersPool());
- var engine = new Engine(consoleReader, consoleWriter, commandProcessor);
- engine.Run();
- }
- }
- public interface ILineReader
- {
- string ReadLine();
- }
- public interface ILineWriter
- {
- void WriteLine(string line);
- }
- public class Engine
- {
- private readonly ILineReader _reader;
- private readonly ILineWriter _writer;
- private readonly CommandProcessor _processor;
- public Engine(ILineReader reader, ILineWriter writer, CommandProcessor processor)
- {
- _reader = reader;
- _writer = writer;
- _processor = processor;
- }
- public void Run()
- {
- while (!_processor.IsSeasonEnd)
- {
- _processor.ProcessCommand(_reader.ReadLine());
- }
- _writer.WriteLine(_processor.PlayersInfo());
- }
- }
- public class CommandProcessor
- {
- private readonly PlayersPool _playersPool;
- public bool IsSeasonEnd { get; private set; }
- public CommandProcessor(PlayersPool playersPool)
- {
- _playersPool = playersPool;
- IsSeasonEnd = false;
- }
- public void ProcessCommand(string command)
- {
- if (command == "Season end")
- {
- IsSeasonEnd = true;
- return;
- }
- if (!command.Contains(" vs "))
- {
- var commandArray = command.Split(" -> ");
- var playerName = commandArray[0];
- var position = commandArray[1];
- var skill = int.Parse(commandArray[2]);
- _playersPool.AddPlayerData(playerName, position, skill);
- }
- else
- {
- var commandArray = command.Split(" vs ");
- var firstPlayer = commandArray[0];
- var secondPlayer = commandArray[1];
- _playersPool.TryDuel(firstPlayer, secondPlayer);
- }
- }
- public string PlayersInfo()
- {
- return _playersPool.ToString();
- }
- }
- public class Writer : ILineWriter
- {
- private readonly TextWriter _writer;
- public Writer(TextWriter writer)
- {
- _writer = writer;
- }
- public void WriteLine(string text)
- {
- _writer.WriteLine(text);
- }
- }
- public class Reader : ILineReader
- {
- private readonly TextReader _reader;
- public Reader(TextReader reader)
- {
- _reader = reader;
- }
- public string ReadLine()
- {
- return _reader.ReadLine();
- }
- }
- public class PlayersPool
- {
- private readonly Dictionary<string, Player> _players;
- public PlayersPool()
- {
- _players = new Dictionary<string, Player>();
- }
- public void AddPlayerData(string playerName, string position, int skill)
- {
- if (!_players.ContainsKey(playerName))
- {
- _players.Add(playerName, new Player(playerName));
- }
- _players[playerName].AddOrUpdatePosition(position, skill);
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- foreach (var player in _players.Values
- .OrderByDescending(player => player.GetTotalSkillPoints())
- .ThenBy(player => player.Name))
- {
- Console.WriteLine(player);
- sb.Append(Environment.NewLine);
- }
- return sb.ToString().Trim();
- }
- public void TryDuel(string firstPlayer, string secondPlayer)
- {
- if (!_players.ContainsKey(firstPlayer) || !_players.ContainsKey(secondPlayer))
- {
- return;
- }
- var playerOne = _players[firstPlayer];
- var playerTwo = _players[secondPlayer];
- if (playerOne.GetTotalSkillPoints() == playerTwo.GetTotalSkillPoints())
- {
- return;
- }
- var duelsCount = playerOne.GetPositions()
- .Intersect(playerTwo.GetPositions())
- .Count();
- if (duelsCount <= 0)
- {
- return;
- }
- _players.Remove(playerOne.GetTotalSkillPoints() > playerTwo.GetTotalSkillPoints()
- ? playerTwo.Name
- : playerOne.Name);
- }
- }
- public class Player
- {
- public string Name { get; }
- private readonly Dictionary<string, int> _dictPositionSkill;
- public Player(string name)
- {
- Name = name;
- _dictPositionSkill = new Dictionary<string, int>();
- }
- public void AddOrUpdatePosition(string position, int skill)
- {
- if (!_dictPositionSkill.ContainsKey(position))
- {
- _dictPositionSkill.Add(position, skill);
- }
- else if (_dictPositionSkill[position] < skill)
- {
- _dictPositionSkill[position] = skill;
- }
- }
- public override string ToString()
- {
- var sb = new StringBuilder($"{Name}: {GetTotalSkillPoints()} skill");
- foreach (var (position, skill) in _dictPositionSkill
- .OrderByDescending(x => x.Value)
- .ThenBy(x => x.Key))
- {
- sb.Append(Environment.NewLine);
- sb.Append($"- {position} <::> {skill}");
- }
- return sb.ToString();
- }
- public IEnumerable<string> GetPositions()
- {
- return _dictPositionSkill.Keys;
- }
- public int GetTotalSkillPoints()
- {
- return _dictPositionSkill.Values.Sum();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement