Example #1
0
        private void SetUpUserDecks()
        {
            alldeckretrieval saved = new alldeckretrieval("getAllDecks", Global.getID(), Global.getToken(), false);
            string           json  = JsonConvert.SerializeObject(saved);

            Byte[] data = System.Text.Encoding.ASCII.GetBytes(json);
            Global.stream.Write(data, 0, data.Length);
            data = new Byte[256];
            string responseData = string.Empty;
            Int32  bytes        = Global.stream.Read(data, 0, data.Length);

            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            string[] temp = responseData.Split(',');
            Debug.Log("Getting deck cards");
            for (int i = 0; i < temp.Length; i++)
            {
                string deckName = temp[i];
                if (!Global.ContainsDeck(deckName) && !deckName.Equals("no decks"))
                {
                    string[] cards = Global.GetDeckByNameFromServer(temp[i], false);
                    Deck     d     = new Deck();
                    d.CardsInDeck = new List <CardContainer>();
                    d.DeckName    = deckName;
                    if (cards != null && cards.Length > 1)
                    {
                        for (int j = 1; j < cards.Length; j++)
                        {
                            //add cards
                            string[]      pair = cards[j].Split('-');
                            Card          c    = Library.GetCard(pair[0]);
                            CardContainer cc;
                            cc.c     = c;
                            cc.count = int.Parse(pair[1]);
                            d.CardsInDeck.Add(cc);
                        }
                    }
                    Global.AddDeck(d);
                }
            }
        }
Example #2
0
        public void SaveDeck()
        {
            //find input elements
            if (GameObject.Find("Deck Name Text").GetComponent <UnityEngine.UI.Text>().text.Trim().Equals(""))
            {
                UIError.transform.GetChild(0).gameObject.GetComponent <UnityEngine.UI.Text>().text = "Deck Name Cannot Be Empty";
                UIError.SetActive(true);
                return;
            }
            if (GameObject.Find("Deck Name Text").GetComponent <UnityEngine.UI.Text>().text.Trim().Equals("no decks"))
            {
                UIError.transform.GetChild(0).gameObject.GetComponent <UnityEngine.UI.Text>().text = "Invalid Deck Name";
                UIError.SetActive(true);
                return;
            }
            if (deckSize < Constants.MIN_DECK_SIZE)
            {
                UIError.transform.GetChild(0).gameObject.GetComponent <UnityEngine.UI.Text>().text = "Not Enough Cards In Deck!";
                UIError.SetActive(true);
                return;
            }
            //check if name taken
            Deck newDeck = new Deck();

            for (int i = 0; i < Global.userDecks.Count; i++)
            {
                if (GameObject.Find("Deck Name Text").GetComponent <UnityEngine.UI.Text>().text.Equals(Global.userDecks[i].DeckName))
                {
                    Deck updatedDeck = Global.userDecks[i];
                    updatedDeck.CardsInDeck = inDeck;
                    Global.userDecks[i]     = updatedDeck;
                    //sync with server
                    try
                    {
                        deckupload saved = new deckupload("saveDeck", Global.getID(), Global.getToken(), deckToString(updatedDeck), updatedDeck.DeckName, false);
                        string     json  = JsonConvert.SerializeObject(saved);
                        Byte[]     data  = System.Text.Encoding.ASCII.GetBytes(json);
                        Global.stream.Write(data, 0, data.Length);
                        data = new Byte[256];
                        string responseData = string.Empty;
                        Int32  bytes        = Global.stream.Read(data, 0, data.Length);
                        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                        Debug.Log(responseData);
                    }
                    catch (Exception)
                    {
                    }
                    SceneManager.LoadScene("Decks");
                    return;
                }
            }

            //sets new deck values
            newDeck.DeckName    = GameObject.Find("Deck Name Text").gameObject.GetComponent <UnityEngine.UI.Text>().text;
            newDeck.CardsInDeck = inDeck;
            Debug.Log(newDeck);
            //add to local deck list
            Global.AddDeck(newDeck);
            //sync with server
            try
            {
                deckupload saved = new deckupload("saveDeck", Global.getID(), Global.getToken(), deckToString(newDeck), newDeck.DeckName, false);
                string     json  = JsonConvert.SerializeObject(saved);
                Byte[]     data  = System.Text.Encoding.ASCII.GetBytes(json);
                Global.stream.Write(data, 0, data.Length);
                data = new Byte[256];
                string responseData = string.Empty;
                Int32  bytes        = Global.stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            }
            catch (Exception)
            {
            }

            SceneManager.LoadScene("Decks");
        }