Advertisement
Guest User

Card Manager

a guest
Mar 28th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. using System.Text;
  6. using System.IO;
  7. using System.Linq;
  8.  
  9. public class CardManager
  10. {
  11.     private List<int> _cardListKeys = new List<int>();
  12.     private Dictionary<int, Card> _cardList = new Dictionary<int, Card>();
  13.     private List<Card> _allOwnedCardsList = new List<Card>();
  14.     private static CardManager _instance = null;
  15.     private List<int> _ownedIDList = new List<int>();
  16.     private List<Card> _tier1List = new List<Card>();
  17.  
  18.     public List<Card> AllOwnedCardsList
  19.     {
  20.         get { return _allOwnedCardsList; }
  21.     }
  22.  
  23.     public List<int> OwnedIDList
  24.     {
  25.         get { return _ownedIDList; }
  26.     }
  27.  
  28.     public Dictionary<int, Card> CardList
  29.     {
  30.         get { return _cardList; }
  31.         private set { _cardList = value; }
  32.     }
  33.  
  34.     public int CardListCount
  35.     {
  36.         get { return _cardList.Count; }
  37.     }
  38.  
  39.     public int GetKeyAt(int index)
  40.     {
  41.         return _cardListKeys[index];
  42.     }
  43.  
  44.     public Card GetCard(int key)
  45.     {
  46.         return CardList[key];
  47.     }
  48.  
  49.     public Card[] GetUpgrades(Card c)
  50.     {
  51.         List<Card> tempList = new List<Card>();
  52.  
  53.         foreach(int key in _cardListKeys)
  54.         {
  55.             if(CardList[key].PrerequisiteID == c.ID)
  56.             {
  57.                 tempList.Add(CardList[key]);
  58.             }
  59.         }
  60.  
  61.         return tempList.ToArray();
  62.     }
  63.  
  64.     public Card GetBaseCard(Card c)
  65.     {
  66.         //ownedIDList andAllOwnedCardsList don't match sometimes
  67.         Debug.Log("CardManager::GetBaseCard->" + _ownedIDList.Count + " == " + _allOwnedCardsList.Count);
  68.         if (c.Tier == 2)
  69.         {
  70.             for (int i = 0; i < _allOwnedCardsList.Count; ++i)
  71.             {
  72.                 if (_allOwnedCardsList[i].ID == c.PrerequisiteID)
  73.                 {
  74.                     return _allOwnedCardsList[i];
  75.                 }
  76.             }
  77.         }
  78.         else if(c.Tier == 1)
  79.         {
  80.             for(int i = 0; i < _allOwnedCardsList.Count; ++i)
  81.             {
  82.                 if (_allOwnedCardsList[i].ID == c.ID)
  83.                 {
  84.                     return _allOwnedCardsList[i];
  85.                 }
  86.             }
  87.         }
  88.  
  89.         return null;
  90.     }
  91.  
  92.     private static void CreateInstance()
  93.     {
  94.         if (_instance == null)
  95.             _instance = new CardManager();
  96.     }
  97.  
  98.     public static CardManager GetInstance()
  99.     {
  100.         if (_instance == null)
  101.             CreateInstance();
  102.         return _instance;
  103.     }
  104.  
  105.     private CardManager()
  106.     {
  107.         Debug.Log("Initialising CardManager");
  108.         TextAsset cards = (TextAsset)Resources.Load("Data/Cards");
  109.         if (CardList.Count <= 0)
  110.             LoadCardsFromXml(cards);
  111.  
  112.         if (Application.isWebPlayer)
  113.         {
  114.             TextAsset inventory = (TextAsset)Resources.Load("Data/Inventory");
  115.             LoadPlayerInventoryFromXml(inventory);
  116.         }
  117.         else
  118.         {
  119.             if (!File.Exists(Application.dataPath + "/Resources/Data/Inventory.xml"))
  120.             {
  121.                 Debug.Log("Inventory not found -> loading from Assets");
  122.                 TextAsset inventory = (TextAsset)Resources.Load("Data/Inventory");
  123.                 LoadPlayerInventoryFromXml(inventory);
  124.             }
  125.             else
  126.             {
  127.                 Debug.Log("Inventory loaded");
  128.                 LoadPlayerInventory();
  129.             }
  130.         }
  131.  
  132.         foreach (int key in OwnedIDList)
  133.         {
  134.             AllOwnedCardsList.Add(new Card(GetCard(key)));
  135.         }
  136.  
  137.     }
  138.  
  139.     public Card GetRandomReward()
  140.     {
  141.         //get list of tier 1 cards
  142.         Card reward = new Card(_tier1List[Random.Range(0, _tier1List.Count - 1)]);
  143.         return reward;
  144.     }
  145.  
  146.     public void SaveInventory()
  147.     {
  148.         //XmlWriterSettings settings = new XmlWriterSettings();
  149.         //settings.Indent = true;
  150.         //settings.OmitXmlDeclaration = true;
  151.  
  152.         //TextAsset textAsset = (TextAsset)Resources.Load("Data/Decks");
  153.  
  154.         StringBuilder sb = new StringBuilder();
  155.         XmlWriterSettings xws = new XmlWriterSettings();
  156.         xws.OmitXmlDeclaration = true;
  157.         xws.Indent = true;
  158.  
  159.         using (XmlWriter writer = XmlWriter.Create(sb, xws))
  160.         {
  161.             //writer.WriteStartDocument();
  162.             writer.WriteStartElement("deck");
  163.             foreach (int key in OwnedIDList)
  164.             {
  165.                 writer.WriteElementString("card", key.ToString());
  166.             }
  167.  
  168.             writer.WriteEndElement();
  169.             //save to file
  170.             XmlDocument doc = new XmlDocument();
  171.             doc.LoadXml(sb.ToString());
  172.             //txtDebug.text = Application.dataPath + "/Resources/Data/Decks.xml";
  173.             doc.Save(Application.dataPath + "/Resources/Data/Inventory.xml"); // does not work in webbuild
  174.  
  175.             writer.Close();
  176.         }
  177.     }
  178.  
  179.     void LoadPlayerInventory()
  180.     {
  181.         XmlDocument xDoc = new XmlDocument();
  182.         xDoc.Load(Application.dataPath + "/Resources/Data/Inventory.xml");
  183.         XmlNode root = xDoc.DocumentElement;
  184.         XmlNodeList nodeList = root.SelectNodes("card");
  185.         foreach (XmlNode node in nodeList)
  186.         {
  187.             OwnedIDList.Add(int.Parse(node.InnerText));
  188.         }
  189.     }
  190.  
  191.     void LoadPlayerInventoryFromXml(TextAsset asset)
  192.     {
  193.         XmlDocument xDoc = new XmlDocument();
  194.         xDoc.LoadXml(asset.text);
  195.         XmlNode root = xDoc.DocumentElement;
  196.         XmlNodeList nodeList = root.SelectNodes("card");
  197.         foreach (XmlNode node in nodeList)
  198.         {
  199.             OwnedIDList.Add(int.Parse(node.InnerText));
  200.         }
  201.     }
  202.  
  203.    public void ReplaceCardInInventory(Card c1, Card c2)
  204.     {
  205.         if(AllOwnedCardsList.Find(card => card == c1) == null)
  206.         {
  207.             Debug.Log("CardManager::ReplaceCardInInventory->card not found");
  208.         }
  209.         AllOwnedCardsList.Find(card => card == c1).ReplaceCard(c2);
  210.     }
  211.  
  212.     public void LoadCardsFromXml(TextAsset asset)
  213.     {
  214.         XmlDocument xDoc = new XmlDocument();
  215.         xDoc.LoadXml(asset.text);
  216.         XmlNode root = xDoc.DocumentElement;
  217.         XmlNodeList nodeList = root.SelectNodes("card");
  218.         foreach (XmlNode node in nodeList)
  219.         {
  220.             int id = int.Parse(node.SelectSingleNode("ID").InnerText);
  221.             string element = node.SelectSingleNode("Element").InnerText;
  222.             int eId = 0;
  223.             switch (element.ToLower())
  224.             {
  225.                 case "air":
  226.                     eId = (int)Element.Air;
  227.                     break;
  228.                 case "earth":
  229.                     eId = (int)Element.Earth;
  230.                     break;
  231.                 case "spirit":
  232.                     eId = (int)Element.Spirit;
  233.                     break;
  234.                 case "water":
  235.                     eId = (int)Element.Water;
  236.                     break;
  237.                 case "fire":
  238.                     eId = (int)Element.Fire;
  239.                     break;
  240.                 case "void":
  241.                     eId = (int)Element.Void;
  242.                     break;
  243.             }
  244.             int eff1Id = 0, eff2Id = 0;
  245.             if (node.SelectSingleNode("Effect1") != null)
  246.             {
  247.  
  248.                 string effect1 = node.SelectSingleNode("Effect1").InnerText;
  249.  
  250.  
  251.                 switch (effect1.ToLower())
  252.                 {
  253.                     case "damage":
  254.                         eff1Id = (int)AbilityType.Damage;
  255.                         break;
  256.                     case "block":
  257.                         eff1Id = (int)AbilityType.Block;
  258.                         break;
  259.                     case "doubledamage":
  260.                         eff1Id = (int)AbilityType.DoubleDamage;
  261.                         break;
  262.                     case "doubleblock":
  263.                         eff1Id = (int)AbilityType.DoubleBlock;
  264.                         break;
  265.                     case "doubletimesdamage":
  266.                         eff1Id = (int)AbilityType.DoubleTimesDamage;
  267.                         break;
  268.                     case "doubletimesblock":
  269.                         eff1Id = (int)AbilityType.DoubleTimesBlock;
  270.                         break;
  271.                     case "spiritsword":
  272.                         eff1Id = (int)AbilityType.Shockwave;
  273.                         break;
  274.                     case "drain":
  275.                         eff1Id = (int)AbilityType.Drain;
  276.                         break;
  277.                     case "souloverflow":
  278.                         eff1Id = (int)AbilityType.Mindblow;
  279.                         break;
  280.                     case "soulfire":
  281.                         eff1Id = (int)AbilityType.SoulBlast;
  282.                         break;
  283.                     case "doublesouls":
  284.                         eff1Id = (int)AbilityType.DoubleSouls;
  285.                         break;
  286.                     case "soulshield":
  287.                         eff1Id = (int)AbilityType.SoulShield;
  288.                         break;
  289.                     case "soulBluster":
  290.                         eff1Id = (int)AbilityType.SoulCharge;
  291.                         break;
  292.                     case "soularmor":
  293.                         eff1Id = (int)AbilityType.SoulArmor;
  294.                         break;
  295.                     case "voidstaff":
  296.                         eff1Id = (int)AbilityType.Whirlwind;
  297.                         break;
  298.                 }
  299.             }
  300.             if (node.SelectSingleNode("Effect2") != null)
  301.             {
  302.                 string effect2 = node.SelectSingleNode("Effect2").InnerText;
  303.                 switch (effect2.ToLower())
  304.                 {
  305.                     case "damage":
  306.                         eff2Id = (int)AbilityType.Damage;
  307.                         break;
  308.                     case "block":
  309.                         eff2Id = (int)AbilityType.Block;
  310.                         break;
  311.                     case "doubledamage":
  312.                         eff2Id = (int)AbilityType.DoubleDamage;
  313.                         break;
  314.                     case "doubleblock":
  315.                         eff2Id = (int)AbilityType.DoubleBlock;
  316.                         break;
  317.                     case "doubletimesdamage":
  318.                         eff2Id = (int)AbilityType.DoubleTimesDamage;
  319.                         break;
  320.                     case "doubletimesblock":
  321.                         eff2Id = (int)AbilityType.DoubleTimesBlock;
  322.                         break;
  323.                     case "spiritsword":
  324.                         eff2Id = (int)AbilityType.Shockwave;
  325.                         break;
  326.                     case "drain":
  327.                         eff2Id = (int)AbilityType.Drain;
  328.                         break;
  329.                     case "souloverflow":
  330.                         eff2Id = (int)AbilityType.Mindblow;
  331.                         break;
  332.                     case "soulfire":
  333.                         eff2Id = (int)AbilityType.SoulBlast;
  334.                         break;
  335.                     case "doublesouls":
  336.                         eff2Id = (int)AbilityType.DoubleSouls;
  337.                         break;
  338.                     case "soulshield":
  339.                         eff2Id = (int)AbilityType.SoulShield;
  340.                         break;
  341.                     case "soulBluster":
  342.                         eff2Id = (int)AbilityType.SoulCharge;
  343.                         break;
  344.                     case "soularmor":
  345.                         eff2Id = (int)AbilityType.SoulArmor;
  346.                         break;
  347.                     case "voidstaff":
  348.                         eff2Id = (int)AbilityType.Whirlwind;
  349.                         break;
  350.                 }
  351.             }
  352.  
  353.             //rarity
  354.             string rarity = node.SelectSingleNode("Rarity").InnerText;
  355.             int rar = 0;
  356.             switch (rarity.ToLower())
  357.             {
  358.                 case "common":
  359.                     rar = (int)Rarity.Common;
  360.                     break;
  361.                 case "rare":
  362.                     rar = (int)Rarity.Rare;
  363.                     break;
  364.                 case "legendary":
  365.                     rar = (int)Rarity.Legendary;
  366.                     break;
  367.             }
  368.  
  369.  
  370.             //Debug.Log(id + " name: " + node.Element("Name").Value + " element: " + element + "," + eId + " tier: " + int.Parse(node.Element("Tier").Value));
  371.  
  372.             Card c = new Card(id, node.SelectSingleNode("Name").InnerText,
  373.                 eId,
  374.                 rar,
  375.                 int.Parse(node.SelectSingleNode("Tier").InnerText),
  376.                 int.Parse(node.SelectSingleNode("Souls").InnerText),
  377.                 eff1Id,
  378.                 int.Parse(node.SelectSingleNode("Instance1").InnerText),
  379.                 int.Parse(node.SelectSingleNode("Value1").InnerText),
  380.                 eff2Id,
  381.                 int.Parse(node.SelectSingleNode("Instance2").InnerText),
  382.                 int.Parse(node.SelectSingleNode("Value2").InnerText),
  383.                 int.Parse(node.SelectSingleNode("PrerequisiteID").InnerText),
  384.                 node.SelectSingleNode("DescriptionLong").InnerText,
  385.                 node.SelectSingleNode("DescriptionShort").InnerText
  386.                 );
  387.  
  388.  
  389.             CardList.Add(id, c);
  390.             _cardListKeys.Add(id);
  391.             if (int.Parse(node.SelectSingleNode("Tier").InnerText) == 1)
  392.                 _tier1List.Add(c);
  393.         }
  394.  
  395.     }
  396.  
  397.     public Card GetRandomCard()
  398.     {
  399.         Card rndCard = CardList[ArenaHelper.RandomNumber(1, CardListCount)];
  400.         return rndCard;
  401.     }
  402.  
  403.     // Use this for initialization
  404.     //IEnumerator LoadAllCards()
  405.     //{
  406.     //    WWW loadCards = new WWW("http://web.howest.be/dae.2015.team10/php/allCards.php");
  407.     //    while (!loadCards.isDone)
  408.     //    {
  409.     //        new WaitForSeconds(0.1f);
  410.     //    }
  411.     //    if (string.IsNullOrEmpty(loadCards.error))
  412.     //    {
  413.     //        if (loadCards.isDone && !string.IsNullOrEmpty(loadCards.text))
  414.     //        {
  415.  
  416.     //            //parse through the data,
  417.     //            //make card objects with data
  418.     //            string cardData = loadCards.text;
  419.     //            string[] stringArray = cardData.Split('|');
  420.  
  421.     //            for (int i = 0; i < stringArray.Length / CARDPIECES; ++i)
  422.     //            {
  423.     //                int id = -1;
  424.     //                string name = "";
  425.     //                int element = -1;
  426.     //                int tier = 1;
  427.     //                string imagePath = "";
  428.     //                int ability1 = -1;
  429.     //                int ability2 = -1;
  430.     //                int ability3 = -1;
  431.     //                int baseId = -1;
  432.     //                int upgrade1Id = -1;
  433.     //                int upgrade2Id = -1;
  434.  
  435.     //                id = int.Parse(stringArray[0 + (CARDPIECES * i)]);
  436.  
  437.     //                name = stringArray[1 + (CARDPIECES * i)];
  438.  
  439.     //                element = int.Parse(stringArray[2 + (CARDPIECES * i)]);
  440.  
  441.     //                tier = int.Parse(stringArray[3 + (CARDPIECES * i)]);
  442.  
  443.     //                if (!string.IsNullOrEmpty(stringArray[4 + (CARDPIECES * i)]))
  444.     //                    imagePath = stringArray[4 + (CARDPIECES * i)];
  445.  
  446.     //                //use ability ID to load in the ability, 5 6 7
  447.     //                if (!string.IsNullOrEmpty(stringArray[5 + (CARDPIECES * i)]))
  448.     //                    ability1 = int.Parse(stringArray[5 + (CARDPIECES * i)]);
  449.  
  450.     //                if (!string.IsNullOrEmpty(stringArray[6 + (CARDPIECES * i)]))
  451.     //                    ability2 = int.Parse(stringArray[6 + (CARDPIECES * i)]);
  452.  
  453.     //                if (!string.IsNullOrEmpty(stringArray[7 + (CARDPIECES * i)]))
  454.     //                    ability3 = int.Parse(stringArray[7 + (CARDPIECES * i)]);
  455.  
  456.     //                if (!string.IsNullOrEmpty(stringArray[8 + (CARDPIECES * i)]))
  457.     //                    baseId = int.Parse(stringArray[8 + (CARDPIECES * i)]);
  458.  
  459.     //                if(!string.IsNullOrEmpty(stringArray[9 + (CARDPIECES * i)]))
  460.     //                    upgrade1Id = int.Parse(stringArray[9 + (CARDPIECES * i)]);
  461.  
  462.     //                if (!string.IsNullOrEmpty(stringArray[10 + (CARDPIECES * i)]))
  463.     //                    upgrade2Id = int.Parse(stringArray[10 + (CARDPIECES * i)]);
  464.  
  465.     //                Card test = new Card(id, name, element, tier, imagePath, ability1, ability2, ability3, baseId, upgrade1Id, upgrade2Id);
  466.     //                CardList.Add(id, test);
  467.     //            }
  468.  
  469.  
  470.     //        }
  471.     //        else
  472.     //        {
  473.     //            //something went wrong
  474.     //        }
  475.     //    }
  476.     //    else
  477.     //    {
  478.     //        Debug.Log(loadCards.error);
  479.     //    }
  480.     //    yield return loadCards;
  481.     //}
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement