Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class StartUp
  6. {
  7.     static List<IWeapon> weapons = new List<IWeapon>();
  8.  
  9.     static void Main()
  10.     {
  11.         string input;
  12.  
  13.         while ((input = Console.ReadLine()) != "END")
  14.         {
  15.             string[] inputTokens = input.Split(';', StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             try
  18.             {
  19.                 switch (inputTokens[0])
  20.                 {
  21.                     case "Create":
  22.                         IWeapon weapon = CreateWeapon(inputTokens);
  23.                         weapons.Add(weapon);
  24.                         break;
  25.                     case "Add":
  26.                         AddSocket(inputTokens);
  27.                         break;
  28.                     case "Remove":
  29.                         RemoveSocket(inputTokens);
  30.                         break;
  31.                     case "Print":
  32.                         PrintWeapon(inputTokens);
  33.                         break;
  34.                     default:
  35.                         throw new ArgumentException($"Invalid Command: {inputTokens[0]}!");
  36.                 }
  37.             }
  38.             catch (Exception)
  39.             {
  40.  
  41.             }
  42.         }
  43.     }
  44.  
  45.     private static void PrintWeapon(string[] inputTokens)
  46.     {
  47.         string weaponName = inputTokens[1];
  48.         IWeapon weapon = weapons.First(w => w.Name == weaponName);
  49.  
  50.         // If we do any modification to the weapon at this point using "weapon.Modify"
  51.         // we will accumulate extra stats to it.
  52.         // One option to avoid this will be: create a new weapon of the very same type and acumulate it stats using Modify.
  53.         // Another option will be to store the initial min and max damage (we already know that vitality, agility and strength are zeroes)
  54.         // for that concrete weapon and add a method to the weapon "Recalculate"
  55.         // which will loop through its gems and add to the inital max and min damage, based on the type of the gems it owns.
  56.  
  57.         //foreach (IGem gem in weapon.Gems.Where(g => g != null))
  58.         //{
  59.         // weapon.Modify(gem);
  60.         //}
  61.  
  62.         Console.WriteLine(weapon);
  63.     }
  64.  
  65.     private static void RemoveSocket(string[] inputTokens)
  66.     {
  67.         string weaponName = inputTokens[1];
  68.         int socketIndex = int.Parse(inputTokens[2]);
  69.  
  70.         IWeapon weapon = weapons.First(w => w.Name == weaponName);
  71.  
  72.         if (socketIndex >= 0 && socketIndex < weapon.Gems.Length)
  73.         {
  74.             IGem gem = weapon.Gems[socketIndex];
  75.  
  76.             if (gem != null)
  77.             {
  78.                 weapon.DegradeWeapon(gem);
  79.                 weapon.Gems[socketIndex] = null;
  80.             }
  81.         }
  82.     }
  83.  
  84.     private static void AddSocket(string[] inputTokens)
  85.     {
  86.         string weaponName = inputTokens[1];
  87.         IWeapon weapon = weapons.First(w => w.Name == weaponName);
  88.         int socketIndex = int.Parse(inputTokens[2]);
  89.         string[] gemTokens = inputTokens[3].Split(' ', StringSplitOptions.RemoveEmptyEntries);
  90.  
  91.         GemQuality quality = Enum.Parse<GemQuality>(gemTokens[0]);
  92.  
  93.         Type gemType = Type.GetType(gemTokens[1]);
  94.  
  95.         if (gemType == null)
  96.         {
  97.             throw new ArgumentException("Invalid Gem Type!");
  98.         }
  99.  
  100.         if (!typeof(IGem).IsAssignableFrom(gemType))
  101.         {
  102.             throw new ArgumentException("Invalid Gem!");
  103.         }
  104.  
  105.         IGem gem = (IGem)Activator.CreateInstance(gemType, new object[] { quality });
  106.  
  107.         if (socketIndex >= 0 && socketIndex < weapon.Gems.Length)
  108.         {
  109.  
  110.             // Since we add or substract to the max and min damage each time when we add or remove а gem,
  111.             // the proper way in case of an existing gem will be to degrade the weapon first and then to Modify it.
  112.             var newGem = weapon.Gems[socketIndex];
  113.             if (newGem != null)
  114.             {
  115.                 weapon.DegradeWeapon(newGem);
  116.             }
  117.             weapon.Modify(gem);
  118.             weapon.Gems[socketIndex] = gem;
  119.            
  120.         }
  121.     }
  122.  
  123.     private static IWeapon CreateWeapon(string[] inputTokens)
  124.     {
  125.         string[] weaponTokens = inputTokens[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);
  126.  
  127.         WeaponRarity rarity = Enum.Parse<WeaponRarity>(weaponTokens[0]);
  128.  
  129.         Type weaponType = Type.GetType(weaponTokens[1]);
  130.  
  131.         if (weaponType == null)
  132.         {
  133.             throw new ArgumentException("Invalid Weapon Type!");
  134.         }
  135.  
  136.         if (!typeof(IWeapon).IsAssignableFrom(weaponType))
  137.         {
  138.             throw new ArgumentException("Invalid Weapon!");
  139.         }
  140.  
  141.         IWeapon weapon = (IWeapon)Activator.CreateInstance(weaponType, new object[] { rarity, inputTokens[2] });
  142.  
  143.         return weapon;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement