/// <summary> /// Runs every time bytes have been downloaded; shows download progress in window title /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void HandleDownloadProgress(object sender, DownloadProgressChangedEventArgs e) { userState usr = e.UserState as userState; ControlUtils.ChangeMainWindowTitle(usr.title, "Downloading data, " + e.ProgressPercentage + "% complete..."); Console.Write("\rDownloaded {0} of {1} bytes. {2} % complete... ", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage); }
/// <summary> /// Runs when download finishes; allows main thread to continue /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void HandleDownloadComplete(object sender, AsyncCompletedEventArgs e) { userState usr = e.UserState as userState; ControlUtils.ChangeMainWindowTitle(usr.title, "YARDT"); Console.WriteLine(); usr.waiter.Set(); }
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); } }