Advertisement
KAMEN1973

Royal Arena

Nov 7th, 2020
2,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. namespace _01.RoyaleArena
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class RoyaleArena : IArena
  9.     {
  10.         private Dictionary<int, BattleCard> cards;
  11.         private Dictionary<CardType, Table<BattleCard>> cardTypeSortedByDamage;
  12.         private Dictionary<string, Table<BattleCard>> cardNameSortedBySawg;
  13.         private Table<BattleCard> cardsSortedBySwag;
  14.  
  15.         public RoyaleArena()
  16.         {
  17.             this.cards = new Dictionary<int, BattleCard>();
  18.             this.cardTypeSortedByDamage = new Dictionary<CardType, Table<BattleCard>>();
  19.             this.cardNameSortedBySawg = new Dictionary<string, Table<BattleCard>>();
  20.             this.cardsSortedBySwag = new Table<BattleCard>(new SwagIndex());
  21.         }
  22.  
  23.         public void Add(BattleCard card)
  24.         {
  25.             if (!this.Contains(card))
  26.             {
  27.                 this.cards[card.Id] = card;
  28.             }
  29.  
  30.             this.AddToSearchableCollection<DamageIndex>(this.cardTypeSortedByDamage, card, c => c.Type);
  31.             this.AddToSearchableCollection<SwagIndex>(this.cardNameSortedBySawg, card, c => c.Name);
  32.             this.cardsSortedBySwag.Add(card);
  33.         }
  34.  
  35.         private void AddToSearchableCollection<T>(IDictionary dictionary, BattleCard card, Func<BattleCard, object> getKey)
  36.             where T : Index<double>, new()
  37.         {
  38.             var key = getKey(card);
  39.  
  40.             if (dictionary[key] == null)
  41.             {
  42.                 dictionary[key] = new Table<BattleCard>(new T());
  43.             }
  44.  
  45.             (dictionary[key] as Table<BattleCard>).Add(card);
  46.         }
  47.  
  48.         public bool Contains(BattleCard card)
  49.         {
  50.             return this.cards.ContainsKey(card.Id);
  51.         }
  52.  
  53.         public int Count => this.cards.Count;
  54.  
  55.         public void ChangeCardType(int id, CardType type)
  56.         {
  57.             this.CardNotExistException(id);
  58.  
  59.             BattleCard card = this.cards[id];
  60.             this.RemoveFromSearchableCollection(this.cardTypeSortedByDamage, card, c => c.Type);
  61.             card.Type = type;
  62.             this.AddToSearchableCollection<DamageIndex>(this.cardTypeSortedByDamage, card, c => c.Type);
  63.         }
  64.  
  65.         public BattleCard GetById(int id)
  66.         {
  67.             this.CardNotExistException(id);
  68.  
  69.             return this.cards[id];
  70.         }
  71.  
  72.         private void CardNotExistException(int id)
  73.         {
  74.             if (!this.cards.ContainsKey(id))
  75.             {
  76.                 throw new InvalidOperationException();
  77.             }
  78.         }
  79.  
  80.         public void RemoveById(int id)
  81.         {
  82.             this.CardNotExistException(id);
  83.             BattleCard card = this.cards[id];
  84.  
  85.             this.RemoveFromSearchableCollection(this.cardTypeSortedByDamage, card, c => c.Type);
  86.             this.RemoveFromSearchableCollection(this.cardNameSortedBySawg, card, c => c.Name);
  87.             this.cardsSortedBySwag.Remove(card);
  88.             this.cards.Remove(id);
  89.         }
  90.  
  91.         private void RemoveFromSearchableCollection(IDictionary dictionary, BattleCard card, Func<BattleCard, object> getKey)
  92.         {
  93.             var key = getKey(card);
  94.  
  95.             (dictionary[key] as Table<BattleCard>).Remove(card);
  96.             if (!(dictionary[key] as Table<BattleCard>).Any())
  97.             {
  98.                 dictionary.Remove(key);
  99.             }
  100.         }
  101.  
  102.         public IEnumerable<BattleCard> GetByCardType(CardType type)
  103.         {
  104.             if (!this.cardTypeSortedByDamage.ContainsKey(type))
  105.             {
  106.                 throw new InvalidOperationException();
  107.             }
  108.  
  109.             IEnumerable<BattleCard> cardsByType = this.cardTypeSortedByDamage[type];
  110.  
  111.             this.CollectionEmtyException(cardsByType);
  112.  
  113.             return cardsByType;
  114.         }
  115.  
  116.         public IEnumerable<BattleCard> GetByTypeAndDamageRangeOrderedByDamageThenById(CardType type, int lo, int hi)
  117.         {
  118.             if (!this.cardTypeSortedByDamage.ContainsKey(type))
  119.             {
  120.                 throw new InvalidOperationException();
  121.             }
  122.  
  123.             IEnumerable<BattleCard> cardsByTypeInDamageRange =
  124.                 this.cardTypeSortedByDamage[type]
  125.                 .GetViewBetween(lo, hi)
  126.                 .OrderBy(c => c);
  127.  
  128.             this.CollectionEmtyException(cardsByTypeInDamageRange);
  129.  
  130.             return cardsByTypeInDamageRange;
  131.         }
  132.  
  133.         private void CollectionEmtyException(IEnumerable<BattleCard> battleCards)
  134.         {
  135.             if (!battleCards.Any())
  136.             {
  137.                 throw new InvalidOperationException();
  138.             }
  139.         }
  140.  
  141.         public IEnumerable<BattleCard> GetByCardTypeAndMaximumDamage(CardType type, double damage)
  142.         {
  143.             if (!this.cardTypeSortedByDamage.ContainsKey(type))
  144.             {
  145.                 throw new InvalidOperationException();
  146.             }
  147.  
  148.             IEnumerable<BattleCard> cardsByTypeAndMaxDamage = this.cardTypeSortedByDamage[type]
  149.                 .GetViewBetween(this.cardTypeSortedByDamage[type].MinKey, damage)
  150.                 .OrderBy(c => c);
  151.  
  152.             this.CollectionEmtyException(cardsByTypeAndMaxDamage);
  153.  
  154.             return cardsByTypeAndMaxDamage;
  155.         }
  156.  
  157.         public IEnumerable<BattleCard> GetByNameOrderedBySwagDescending(string name)
  158.         {
  159.             if (!this.cardNameSortedBySawg.ContainsKey(name))
  160.             {
  161.                 throw new InvalidOperationException();
  162.             }
  163.  
  164.             IEnumerable<BattleCard> cardsByName = this.cardNameSortedBySawg[name];
  165.  
  166.             this.CollectionEmtyException(cardsByName);
  167.  
  168.             return cardsByName;
  169.         }
  170.  
  171.         public IEnumerable<BattleCard> GetByNameAndSwagRange(string name, double lo, double hi)
  172.         {
  173.             if (!this.cardNameSortedBySawg.ContainsKey(name))
  174.             {
  175.                 throw new InvalidOperationException();
  176.             }
  177.  
  178.             IEnumerable<BattleCard> cardsByNameInSwagRange = this.cardNameSortedBySawg[name]?.GetViewBetween(lo, hi);
  179.  
  180.             this.CollectionEmtyException(cardsByNameInSwagRange);
  181.  
  182.             return cardsByNameInSwagRange;
  183.         }
  184.  
  185.         public IEnumerable<BattleCard> FindFirstLeastSwag(int n)
  186.         {
  187.             if (n > this.Count)
  188.             {
  189.                 throw new InvalidOperationException();
  190.             }
  191.  
  192.             IEnumerable<BattleCard> cardsByLeastSwag = this.cardsSortedBySwag.GetFirstN(n, c => c.Id);
  193.  
  194.             return cardsByLeastSwag;
  195.         }
  196.  
  197.         public IEnumerable<BattleCard> GetAllInSwagRange(double lo, double hi)
  198.         {
  199.             IEnumerable<BattleCard> cardsInSwagRange = this.cardsSortedBySwag.GetViewBetween(lo, hi);
  200.  
  201.  
  202.  
  203.             return cardsInSwagRange;
  204.         }
  205.  
  206.  
  207.         public IEnumerator<BattleCard> GetEnumerator()
  208.         {
  209.             return this.cards.Values.GetEnumerator();
  210.         }
  211.  
  212.         IEnumerator IEnumerable.GetEnumerator()
  213.         {
  214.             return GetEnumerator();
  215.         }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement