Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml;
- using System.Text;
- using System.IO;
- using System.Linq;
- public class CardManager
- {
- private List<int> _cardListKeys = new List<int>();
- private Dictionary<int, Card> _cardList = new Dictionary<int, Card>();
- private List<Card> _allOwnedCardsList = new List<Card>();
- private static CardManager _instance = null;
- private List<int> _ownedIDList = new List<int>();
- private List<Card> _tier1List = new List<Card>();
- public List<Card> AllOwnedCardsList
- {
- get { return _allOwnedCardsList; }
- }
- public List<int> OwnedIDList
- {
- get { return _ownedIDList; }
- }
- public Dictionary<int, Card> CardList
- {
- get { return _cardList; }
- private set { _cardList = value; }
- }
- public int CardListCount
- {
- get { return _cardList.Count; }
- }
- public int GetKeyAt(int index)
- {
- return _cardListKeys[index];
- }
- public Card GetCard(int key)
- {
- return CardList[key];
- }
- public Card[] GetUpgrades(Card c)
- {
- List<Card> tempList = new List<Card>();
- foreach(int key in _cardListKeys)
- {
- if(CardList[key].PrerequisiteID == c.ID)
- {
- tempList.Add(CardList[key]);
- }
- }
- return tempList.ToArray();
- }
- public Card GetBaseCard(Card c)
- {
- //ownedIDList andAllOwnedCardsList don't match sometimes
- Debug.Log("CardManager::GetBaseCard->" + _ownedIDList.Count + " == " + _allOwnedCardsList.Count);
- if (c.Tier == 2)
- {
- for (int i = 0; i < _allOwnedCardsList.Count; ++i)
- {
- if (_allOwnedCardsList[i].ID == c.PrerequisiteID)
- {
- return _allOwnedCardsList[i];
- }
- }
- }
- else if(c.Tier == 1)
- {
- for(int i = 0; i < _allOwnedCardsList.Count; ++i)
- {
- if (_allOwnedCardsList[i].ID == c.ID)
- {
- return _allOwnedCardsList[i];
- }
- }
- }
- return null;
- }
- private static void CreateInstance()
- {
- if (_instance == null)
- _instance = new CardManager();
- }
- public static CardManager GetInstance()
- {
- if (_instance == null)
- CreateInstance();
- return _instance;
- }
- private CardManager()
- {
- Debug.Log("Initialising CardManager");
- TextAsset cards = (TextAsset)Resources.Load("Data/Cards");
- if (CardList.Count <= 0)
- LoadCardsFromXml(cards);
- if (Application.isWebPlayer)
- {
- TextAsset inventory = (TextAsset)Resources.Load("Data/Inventory");
- LoadPlayerInventoryFromXml(inventory);
- }
- else
- {
- if (!File.Exists(Application.dataPath + "/Resources/Data/Inventory.xml"))
- {
- Debug.Log("Inventory not found -> loading from Assets");
- TextAsset inventory = (TextAsset)Resources.Load("Data/Inventory");
- LoadPlayerInventoryFromXml(inventory);
- }
- else
- {
- Debug.Log("Inventory loaded");
- LoadPlayerInventory();
- }
- }
- foreach (int key in OwnedIDList)
- {
- AllOwnedCardsList.Add(new Card(GetCard(key)));
- }
- }
- public Card GetRandomReward()
- {
- //get list of tier 1 cards
- Card reward = new Card(_tier1List[Random.Range(0, _tier1List.Count - 1)]);
- return reward;
- }
- public void SaveInventory()
- {
- //XmlWriterSettings settings = new XmlWriterSettings();
- //settings.Indent = true;
- //settings.OmitXmlDeclaration = true;
- //TextAsset textAsset = (TextAsset)Resources.Load("Data/Decks");
- StringBuilder sb = new StringBuilder();
- XmlWriterSettings xws = new XmlWriterSettings();
- xws.OmitXmlDeclaration = true;
- xws.Indent = true;
- using (XmlWriter writer = XmlWriter.Create(sb, xws))
- {
- //writer.WriteStartDocument();
- writer.WriteStartElement("deck");
- foreach (int key in OwnedIDList)
- {
- writer.WriteElementString("card", key.ToString());
- }
- writer.WriteEndElement();
- //save to file
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(sb.ToString());
- //txtDebug.text = Application.dataPath + "/Resources/Data/Decks.xml";
- doc.Save(Application.dataPath + "/Resources/Data/Inventory.xml"); // does not work in webbuild
- writer.Close();
- }
- }
- void LoadPlayerInventory()
- {
- XmlDocument xDoc = new XmlDocument();
- xDoc.Load(Application.dataPath + "/Resources/Data/Inventory.xml");
- XmlNode root = xDoc.DocumentElement;
- XmlNodeList nodeList = root.SelectNodes("card");
- foreach (XmlNode node in nodeList)
- {
- OwnedIDList.Add(int.Parse(node.InnerText));
- }
- }
- void LoadPlayerInventoryFromXml(TextAsset asset)
- {
- XmlDocument xDoc = new XmlDocument();
- xDoc.LoadXml(asset.text);
- XmlNode root = xDoc.DocumentElement;
- XmlNodeList nodeList = root.SelectNodes("card");
- foreach (XmlNode node in nodeList)
- {
- OwnedIDList.Add(int.Parse(node.InnerText));
- }
- }
- public void ReplaceCardInInventory(Card c1, Card c2)
- {
- if(AllOwnedCardsList.Find(card => card == c1) == null)
- {
- Debug.Log("CardManager::ReplaceCardInInventory->card not found");
- }
- AllOwnedCardsList.Find(card => card == c1).ReplaceCard(c2);
- }
- public void LoadCardsFromXml(TextAsset asset)
- {
- XmlDocument xDoc = new XmlDocument();
- xDoc.LoadXml(asset.text);
- XmlNode root = xDoc.DocumentElement;
- XmlNodeList nodeList = root.SelectNodes("card");
- foreach (XmlNode node in nodeList)
- {
- int id = int.Parse(node.SelectSingleNode("ID").InnerText);
- string element = node.SelectSingleNode("Element").InnerText;
- int eId = 0;
- switch (element.ToLower())
- {
- case "air":
- eId = (int)Element.Air;
- break;
- case "earth":
- eId = (int)Element.Earth;
- break;
- case "spirit":
- eId = (int)Element.Spirit;
- break;
- case "water":
- eId = (int)Element.Water;
- break;
- case "fire":
- eId = (int)Element.Fire;
- break;
- case "void":
- eId = (int)Element.Void;
- break;
- }
- int eff1Id = 0, eff2Id = 0;
- if (node.SelectSingleNode("Effect1") != null)
- {
- string effect1 = node.SelectSingleNode("Effect1").InnerText;
- switch (effect1.ToLower())
- {
- case "damage":
- eff1Id = (int)AbilityType.Damage;
- break;
- case "block":
- eff1Id = (int)AbilityType.Block;
- break;
- case "doubledamage":
- eff1Id = (int)AbilityType.DoubleDamage;
- break;
- case "doubleblock":
- eff1Id = (int)AbilityType.DoubleBlock;
- break;
- case "doubletimesdamage":
- eff1Id = (int)AbilityType.DoubleTimesDamage;
- break;
- case "doubletimesblock":
- eff1Id = (int)AbilityType.DoubleTimesBlock;
- break;
- case "spiritsword":
- eff1Id = (int)AbilityType.Shockwave;
- break;
- case "drain":
- eff1Id = (int)AbilityType.Drain;
- break;
- case "souloverflow":
- eff1Id = (int)AbilityType.Mindblow;
- break;
- case "soulfire":
- eff1Id = (int)AbilityType.SoulBlast;
- break;
- case "doublesouls":
- eff1Id = (int)AbilityType.DoubleSouls;
- break;
- case "soulshield":
- eff1Id = (int)AbilityType.SoulShield;
- break;
- case "soulBluster":
- eff1Id = (int)AbilityType.SoulCharge;
- break;
- case "soularmor":
- eff1Id = (int)AbilityType.SoulArmor;
- break;
- case "voidstaff":
- eff1Id = (int)AbilityType.Whirlwind;
- break;
- }
- }
- if (node.SelectSingleNode("Effect2") != null)
- {
- string effect2 = node.SelectSingleNode("Effect2").InnerText;
- switch (effect2.ToLower())
- {
- case "damage":
- eff2Id = (int)AbilityType.Damage;
- break;
- case "block":
- eff2Id = (int)AbilityType.Block;
- break;
- case "doubledamage":
- eff2Id = (int)AbilityType.DoubleDamage;
- break;
- case "doubleblock":
- eff2Id = (int)AbilityType.DoubleBlock;
- break;
- case "doubletimesdamage":
- eff2Id = (int)AbilityType.DoubleTimesDamage;
- break;
- case "doubletimesblock":
- eff2Id = (int)AbilityType.DoubleTimesBlock;
- break;
- case "spiritsword":
- eff2Id = (int)AbilityType.Shockwave;
- break;
- case "drain":
- eff2Id = (int)AbilityType.Drain;
- break;
- case "souloverflow":
- eff2Id = (int)AbilityType.Mindblow;
- break;
- case "soulfire":
- eff2Id = (int)AbilityType.SoulBlast;
- break;
- case "doublesouls":
- eff2Id = (int)AbilityType.DoubleSouls;
- break;
- case "soulshield":
- eff2Id = (int)AbilityType.SoulShield;
- break;
- case "soulBluster":
- eff2Id = (int)AbilityType.SoulCharge;
- break;
- case "soularmor":
- eff2Id = (int)AbilityType.SoulArmor;
- break;
- case "voidstaff":
- eff2Id = (int)AbilityType.Whirlwind;
- break;
- }
- }
- //rarity
- string rarity = node.SelectSingleNode("Rarity").InnerText;
- int rar = 0;
- switch (rarity.ToLower())
- {
- case "common":
- rar = (int)Rarity.Common;
- break;
- case "rare":
- rar = (int)Rarity.Rare;
- break;
- case "legendary":
- rar = (int)Rarity.Legendary;
- break;
- }
- //Debug.Log(id + " name: " + node.Element("Name").Value + " element: " + element + "," + eId + " tier: " + int.Parse(node.Element("Tier").Value));
- Card c = new Card(id, node.SelectSingleNode("Name").InnerText,
- eId,
- rar,
- int.Parse(node.SelectSingleNode("Tier").InnerText),
- int.Parse(node.SelectSingleNode("Souls").InnerText),
- eff1Id,
- int.Parse(node.SelectSingleNode("Instance1").InnerText),
- int.Parse(node.SelectSingleNode("Value1").InnerText),
- eff2Id,
- int.Parse(node.SelectSingleNode("Instance2").InnerText),
- int.Parse(node.SelectSingleNode("Value2").InnerText),
- int.Parse(node.SelectSingleNode("PrerequisiteID").InnerText),
- node.SelectSingleNode("DescriptionLong").InnerText,
- node.SelectSingleNode("DescriptionShort").InnerText
- );
- CardList.Add(id, c);
- _cardListKeys.Add(id);
- if (int.Parse(node.SelectSingleNode("Tier").InnerText) == 1)
- _tier1List.Add(c);
- }
- }
- public Card GetRandomCard()
- {
- Card rndCard = CardList[ArenaHelper.RandomNumber(1, CardListCount)];
- return rndCard;
- }
- // Use this for initialization
- //IEnumerator LoadAllCards()
- //{
- // WWW loadCards = new WWW("http://web.howest.be/dae.2015.team10/php/allCards.php");
- // while (!loadCards.isDone)
- // {
- // new WaitForSeconds(0.1f);
- // }
- // if (string.IsNullOrEmpty(loadCards.error))
- // {
- // if (loadCards.isDone && !string.IsNullOrEmpty(loadCards.text))
- // {
- // //parse through the data,
- // //make card objects with data
- // string cardData = loadCards.text;
- // string[] stringArray = cardData.Split('|');
- // for (int i = 0; i < stringArray.Length / CARDPIECES; ++i)
- // {
- // int id = -1;
- // string name = "";
- // int element = -1;
- // int tier = 1;
- // string imagePath = "";
- // int ability1 = -1;
- // int ability2 = -1;
- // int ability3 = -1;
- // int baseId = -1;
- // int upgrade1Id = -1;
- // int upgrade2Id = -1;
- // id = int.Parse(stringArray[0 + (CARDPIECES * i)]);
- // name = stringArray[1 + (CARDPIECES * i)];
- // element = int.Parse(stringArray[2 + (CARDPIECES * i)]);
- // tier = int.Parse(stringArray[3 + (CARDPIECES * i)]);
- // if (!string.IsNullOrEmpty(stringArray[4 + (CARDPIECES * i)]))
- // imagePath = stringArray[4 + (CARDPIECES * i)];
- // //use ability ID to load in the ability, 5 6 7
- // if (!string.IsNullOrEmpty(stringArray[5 + (CARDPIECES * i)]))
- // ability1 = int.Parse(stringArray[5 + (CARDPIECES * i)]);
- // if (!string.IsNullOrEmpty(stringArray[6 + (CARDPIECES * i)]))
- // ability2 = int.Parse(stringArray[6 + (CARDPIECES * i)]);
- // if (!string.IsNullOrEmpty(stringArray[7 + (CARDPIECES * i)]))
- // ability3 = int.Parse(stringArray[7 + (CARDPIECES * i)]);
- // if (!string.IsNullOrEmpty(stringArray[8 + (CARDPIECES * i)]))
- // baseId = int.Parse(stringArray[8 + (CARDPIECES * i)]);
- // if(!string.IsNullOrEmpty(stringArray[9 + (CARDPIECES * i)]))
- // upgrade1Id = int.Parse(stringArray[9 + (CARDPIECES * i)]);
- // if (!string.IsNullOrEmpty(stringArray[10 + (CARDPIECES * i)]))
- // upgrade2Id = int.Parse(stringArray[10 + (CARDPIECES * i)]);
- // Card test = new Card(id, name, element, tier, imagePath, ability1, ability2, ability3, baseId, upgrade1Id, upgrade2Id);
- // CardList.Add(id, test);
- // }
- // }
- // else
- // {
- // //something went wrong
- // }
- // }
- // else
- // {
- // Debug.Log(loadCards.error);
- // }
- // yield return loadCards;
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement