Ejemplo n.º 1
0
 /// <summary>
 /// reset all variables and clear window
 /// </summary>
 private void ResetVars()
 {
     gameIsRunning         = false;
     inMatch               = false;
     setLoaded             = false;
     gotDeck               = false;
     sortedManaCost        = false;
     inMulligan            = true;
     deck                  = new JObject();
     toDelete              = new List <string>();
     manaCostOrder         = new List <string>();
     set                   = new JArray();
     playerCardsInPlay     = new JArray();
     enemyCardsInPlay      = new JArray();
     cardsInPlayCopy       = new JArray();
     playerCards           = new Dictionary <string, JObject>();
     purgatory             = new Dictionary <string, JObject>();
     aTimer.IsEnabled      = false;
     labelsDrawn           = false;
     printMenu             = true;
     cardsLeftInDeck       = 0;
     ControlUtils.isGreyed = new Dictionary <string, bool>();
     ControlUtils.ClearControls(sp, cardDrawPercentage1, cardDrawPercentage2, cardDrawPercentage3, cardsInHandText);
 }
Ejemplo n.º 2
0
        public void Main()
        {
            ControlUtils.ClearControls(sp, cardDrawPercentage1, cardDrawPercentage2, cardDrawPercentage3, cardsInHandText);
            if (Properties.Settings.Default.AutoMinimize)
            {
                ControlUtils.MinimizeWindow(this, true);
                isMinimized = true;
            }

            while (true)
            {
                while (!inMatch || !gameIsRunning)
                {
                    try
                    {
                        JObject responseString = JsonConvert.DeserializeObject <JObject>(Utils.HttpReq($"http://localhost:{Properties.Settings.Default.Port}/positional-rectangles"));


                        gameIsRunning = true;
                        if (responseString["GameState"].ToString() == "InProgress")
                        {
                            inMatch = true;
                            Console.WriteLine("Starting timer");
                            ControlUtils.ChangeMainWindowTitle(WindowTitle, "YARDT");
                            aTimer.IsEnabled = true;


                            if (Properties.Settings.Default.AutoMinimize)
                            {
                                ControlUtils.MinimizeWindow(this, false);
                                isMinimized = false;
                            }

                            if (!gotDeck)
                            {
                                gotDeck = true;

                                string resString = Utils.HttpReq($"http://localhost:{Properties.Settings.Default.Port}/static-decklist");
                                if (resString == "failure")
                                {
                                    JObject expeditionState = JsonConvert.DeserializeObject <JObject>(Utils.HttpReq($"http://localhost:{Properties.Settings.Default.Port}/expeditions-state"));
                                    deck = DeckFromExpedition(expeditionState);
                                }
                                else
                                {
                                    deck = JsonConvert.DeserializeObject <JObject>(resString);
                                }
                                manaCostOrder.Clear();
                                foreach (JToken card in deck["CardsInDeck"])
                                {
                                    JProperty cardProperty = card.ToObject <JProperty>();
                                    manaCostOrder.Add(cardProperty.Name);
                                    cardsLeftInDeck += (int)cardProperty.Value;
                                }
                                sortedManaCost = false;
                                Console.WriteLine("Got deck");
                            }
                        }
                        else
                        {
                            if (printMenu)
                            {
                                Console.WriteLine("In menu, waiting for game to start");
                                ControlUtils.ChangeMainWindowTitle(WindowTitle, "Waiting for match to start");
                                printMenu = false;
                            }

                            if (inMatch || aTimer.IsEnabled)
                            {
                                Console.WriteLine("Not currently in game, stopping timer");
                                aTimer.IsEnabled = false;
                                inMatch          = false;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Could not connect to game!");
                        Console.WriteLine("Trying again in 2 sec");
                        ControlUtils.ChangeMainWindowTitle(WindowTitle, "Waiting for game to start");
                        //Console.WriteLine("Message :{0} ", err.Message);
                        gameIsRunning = false;
                        Thread.Sleep(2000);
                    }
                }

                //Load set from json
                if (!setLoaded)
                {
                    set       = FileUtils.LoadJson(mainDirName);
                    setLoaded = true;
                    Console.WriteLine("Loaded set");
                }

                if (!sortedManaCost && setLoaded)
                {
                    Console.WriteLine("Sorting deck");
                    manaCostOrder.Sort((x, y) =>
                    {
                        int xManaCost = -1, yManaCost = -1;
                        foreach (JToken item in set)
                        {
                            if (item.Value <string>("cardCode") == x)
                            {
                                xManaCost = item.Value <int>("cost");
                            }
                            else if (item.Value <string>("cardCode") == y)
                            {
                                yManaCost = item.Value <int>("cost");
                            }
                            if (xManaCost >= 0 && yManaCost >= 0)
                            {
                                break;
                            }
                        }
                        return(xManaCost.CompareTo(yManaCost));
                    });

                    sortedManaCost = true;
                    Console.WriteLine("Sorted deck");
                }

                if (playerCardsInPlay is JArray && !JToken.DeepEquals(playerCardsInPlay, cardsInPlayCopy))
                {
                    Console.WriteLine("Cards are different");
                    cardsInPlayCopy = playerCardsInPlay;
                    foreach (JToken card in cardsInPlayCopy)
                    {
                        if (!playerCards.ContainsKey(card.Value <string>("CardID")))
                        {
                            Console.WriteLine("Adding card: " + card.Value <string>("CardID") + " to playerCards");
                            playerCards.Add(card.Value <string>("CardID"), card.ToObject <JObject>());
                        }
                    }

                    numOfCardsInHand = Utils.GetCardsInHand(playerCardsInPlay, gameWindowHeight);
                    ControlUtils.UpdateCardsInHand(cardsInHandText, numOfCardsInHand);

                    if (inMulligan && playerCards.Count > 4)
                    {
                        playerCards.Clear();
                        inMulligan = false;
                        Console.WriteLine("No longer in mulligan phase");
                        Utils.PrintDeckList(deck, set, manaCostOrder, sp, ref labelsDrawn, mainDirName);
                    }

                    if (!inMulligan && deck.Count > 0)
                    {
                        foreach (string card in playerCards.Keys)
                        {
                            if (!purgatory.ContainsKey(card))
                            {
                                purgatory.Add(card, playerCards[card]);
                                foreach (JToken item in deck["CardsInDeck"])
                                {
                                    JProperty itemProperty = item.ToObject <JProperty>();

                                    if (itemProperty.Name == (string)playerCards[card]["CardCode"] && (int)itemProperty.Value > 0)
                                    {
                                        toDelete.Add(itemProperty.Name);
                                        break;
                                    }
                                }
                            }

                            //To-do: add card to graveyard
                        }
                        if (toDelete.Count > 0)
                        {
                            Console.WriteLine("Deleting cards from deck");
                            foreach (string name in toDelete)
                            {
                                deck["CardsInDeck"][name] = deck["CardsInDeck"].Value <int>(name) - 1;
                                cardsLeftInDeck--;

                                ControlUtils.UpdateCardsLeftInDeck(cardDrawPercentage1, cardDrawPercentage2, cardDrawPercentage3, cardsLeftInDeckText, cardsLeftInDeck);
                                Console.Write("Decremented item: ");
                                Console.WriteLine(name);
                            }
                            toDelete.Clear();
                            Utils.PrintDeckList(deck, set, manaCostOrder, sp, ref labelsDrawn, mainDirName);
                        }
                    }
                }
                Thread.Sleep(500);
            }
        }