Advertisement
Guest User

Untitled

a guest
Jan 10th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace MOBAChallenger_withClass
  8. {
  9.     public static class Program
  10.     {
  11.         private static void Main()
  12.         {
  13.             var consoleReader = new Reader(Console.In);
  14.             var consoleWriter = new Writer(Console.Out);
  15.             var commandProcessor = new CommandProcessor(new PlayersPool());
  16.  
  17.             var engine = new Engine(consoleReader, consoleWriter, commandProcessor);
  18.  
  19.             engine.Run();
  20.         }
  21.     }
  22.  
  23.     public interface ILineReader
  24.     {
  25.         string ReadLine();
  26.     }
  27.  
  28.     public interface ILineWriter
  29.     {
  30.         void WriteLine(string line);
  31.     }
  32.  
  33.     public class Engine
  34.     {
  35.         private readonly ILineReader _reader;
  36.         private readonly ILineWriter _writer;
  37.         private readonly CommandProcessor _processor;
  38.  
  39.         public Engine(ILineReader reader, ILineWriter writer, CommandProcessor processor)
  40.         {
  41.             _reader = reader;
  42.             _writer = writer;
  43.             _processor = processor;
  44.         }
  45.  
  46.         public void Run()
  47.         {
  48.             while (!_processor.IsSeasonEnd)
  49.             {
  50.                 _processor.ProcessCommand(_reader.ReadLine());
  51.             }
  52.  
  53.             _writer.WriteLine(_processor.PlayersInfo());
  54.         }
  55.     }
  56.  
  57.     public class CommandProcessor
  58.     {
  59.         private readonly PlayersPool _playersPool;
  60.         public bool IsSeasonEnd { get; private set; }
  61.  
  62.         public CommandProcessor(PlayersPool playersPool)
  63.         {
  64.             _playersPool = playersPool;
  65.             IsSeasonEnd = false;
  66.         }
  67.  
  68.         public void ProcessCommand(string command)
  69.         {
  70.             if (command == "Season end")
  71.             {
  72.                 IsSeasonEnd = true;
  73.                 return;
  74.             }
  75.  
  76.             if (!command.Contains(" vs "))
  77.             {
  78.                 var commandArray = command.Split(" -> ");
  79.                 var playerName = commandArray[0];
  80.                 var position = commandArray[1];
  81.                 var skill = int.Parse(commandArray[2]);
  82.  
  83.                 _playersPool.AddPlayerData(playerName, position, skill);
  84.             }
  85.             else
  86.             {
  87.                 var commandArray = command.Split(" vs ");
  88.                 var firstPlayer = commandArray[0];
  89.                 var secondPlayer = commandArray[1];
  90.  
  91.                 _playersPool.TryDuel(firstPlayer, secondPlayer);
  92.             }
  93.         }
  94.  
  95.         public string PlayersInfo()
  96.         {
  97.             return _playersPool.ToString();
  98.         }
  99.     }
  100.  
  101.     public class Writer : ILineWriter
  102.     {
  103.         private readonly TextWriter _writer;
  104.  
  105.         public Writer(TextWriter writer)
  106.         {
  107.             _writer = writer;
  108.         }
  109.  
  110.         public void WriteLine(string text)
  111.         {
  112.             _writer.WriteLine(text);
  113.         }
  114.     }
  115.  
  116.     public class Reader : ILineReader
  117.     {
  118.         private readonly TextReader _reader;
  119.  
  120.         public Reader(TextReader reader)
  121.         {
  122.             _reader = reader;
  123.         }
  124.  
  125.         public string ReadLine()
  126.         {
  127.             return _reader.ReadLine();
  128.         }
  129.     }
  130.  
  131.     public class PlayersPool
  132.     {
  133.         private readonly Dictionary<string, Player> _players;
  134.  
  135.         public PlayersPool()
  136.         {
  137.             _players = new Dictionary<string, Player>();
  138.         }
  139.  
  140.         public void AddPlayerData(string playerName, string position, int skill)
  141.         {
  142.             if (!_players.ContainsKey(playerName))
  143.             {
  144.                 _players.Add(playerName, new Player(playerName));
  145.             }
  146.  
  147.             _players[playerName].AddOrUpdatePosition(position, skill);
  148.         }
  149.  
  150.         public override string ToString()
  151.         {
  152.             var sb = new StringBuilder();
  153.  
  154.             foreach (var player in _players.Values
  155.                 .OrderByDescending(player => player.GetTotalSkillPoints())
  156.                 .ThenBy(player => player.Name))
  157.             {
  158.                 Console.WriteLine(player);
  159.                 sb.Append(Environment.NewLine);
  160.             }
  161.  
  162.             return sb.ToString().Trim();
  163.         }
  164.  
  165.         public void TryDuel(string firstPlayer, string secondPlayer)
  166.         {
  167.             if (!_players.ContainsKey(firstPlayer) || !_players.ContainsKey(secondPlayer))
  168.             {
  169.                 return;
  170.             }
  171.  
  172.             var playerOne = _players[firstPlayer];
  173.             var playerTwo = _players[secondPlayer];
  174.  
  175.             if (playerOne.GetTotalSkillPoints() == playerTwo.GetTotalSkillPoints())
  176.             {
  177.                 return;
  178.             }
  179.  
  180.             var duelsCount = playerOne.GetPositions()
  181.                 .Intersect(playerTwo.GetPositions())
  182.                 .Count();
  183.  
  184.             if (duelsCount <= 0)
  185.             {
  186.                 return;
  187.             }
  188.  
  189.             _players.Remove(playerOne.GetTotalSkillPoints() > playerTwo.GetTotalSkillPoints()
  190.                 ? playerTwo.Name
  191.                 : playerOne.Name);
  192.         }
  193.     }
  194.  
  195.     public class Player
  196.     {
  197.         public string Name { get; }
  198.  
  199.         private readonly Dictionary<string, int> _dictPositionSkill;
  200.  
  201.         public Player(string name)
  202.         {
  203.             Name = name;
  204.             _dictPositionSkill = new Dictionary<string, int>();
  205.         }
  206.  
  207.         public void AddOrUpdatePosition(string position, int skill)
  208.         {
  209.             if (!_dictPositionSkill.ContainsKey(position))
  210.             {
  211.                 _dictPositionSkill.Add(position, skill);
  212.             }
  213.             else if (_dictPositionSkill[position] < skill)
  214.             {
  215.                 _dictPositionSkill[position] = skill;
  216.             }
  217.         }
  218.  
  219.         public override string ToString()
  220.         {
  221.             var sb = new StringBuilder($"{Name}: {GetTotalSkillPoints()} skill");
  222.  
  223.             foreach (var (position, skill) in _dictPositionSkill
  224.                 .OrderByDescending(x => x.Value)
  225.                 .ThenBy(x => x.Key))
  226.             {
  227.                 sb.Append(Environment.NewLine);
  228.                 sb.Append($"- {position} <::> {skill}");
  229.             }
  230.  
  231.             return sb.ToString();
  232.         }
  233.  
  234.         public IEnumerable<string> GetPositions()
  235.         {
  236.             return _dictPositionSkill.Keys;
  237.         }
  238.  
  239.         public int GetTotalSkillPoints()
  240.         {
  241.             return _dictPositionSkill.Values.Sum();
  242.         }
  243.     }
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement