private ScryfallCard FetchRandomCard() { ScryfallCard card; try { System.Threading.Thread.Sleep(100); string responseContent = string.Empty; HttpResponseMessage response = client.GetAsync("cards/random").Result; response.EnsureSuccessStatusCode(); responseContent = response.Content.ReadAsStringAsync().Result; card = ScryfallCard.FromJson(responseContent); SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM CARD WHERE [id] like \'" + card.id + "\'", db.connection); SqlCeDataAdapter da = new SqlCeDataAdapter(cmd); DataTable result = new DataTable(); da.Fill(result); if (result.Rows.Count == 0) { card.SaveToDB(db); } } catch (Exception ex) { throw; } return(card); }
public static ScryfallCard LoadCardFromDatarow(DataRow row) { string propName = string.Empty; ScryfallCard retval = new ScryfallCard(); try { PropertyInfo[] pi = typeof(ScryfallCard).GetProperties(); foreach (PropertyInfo prop in pi) { propName = prop.Name; TypeCode typeCode = Type.GetTypeCode(prop.PropertyType); if (typeCode == TypeCode.Object) { prop.SetValue(retval, JsonConvert.DeserializeObject(row[propName].ToString(), prop.PropertyType), null); } else { prop.SetValue(retval, row[propName], null); } } } catch (Exception ex) { retval = null; } return(retval); }
private void LoadTestDeck() { this.currentCard = this.engine.GetNamedCardExact("Shadowborn Apostle"); for (int i = 0; i < 66; i++) { this.deck.cards.Add(currentCard); } engine.FetchPrintsByUrl(currentCard); this.currentCard = this.engine.GetNamedCardExact("Swamp"); for (int i = 0; i < 32; i++) { this.deck.cards.Add(currentCard); } engine.FetchPrintsByUrl(currentCard); this.currentCard = this.engine.GetNamedCardExact("Jeweled Lotus"); // for named card probability testing this.deck.cards.Add(currentCard); engine.FetchPrintsByUrl(currentCard); this.currentCard = this.engine.GetNamedCardExact("K\'rrik, Son of Yawgmoth"); engine.FetchPrintsByUrl(currentCard); this.deck.commander = this.currentCard; // this triggers Update Control States IN THIS CASE ONLY. // I believe it triggers the selected index changed event for the list box // specifically because the number of items in the listbox changes bs.ResetBindings(false); }
//If an item was selected don't start new search, and get named card private void combobox1_SelectedIndexChanged(object sender, EventArgs e) { _needUpdate = false; if (this.validAutocomplete) { currentCard = engine.GetNamedCardExact(combobox1.Text); UpdateControlStates(); } }
public List <Print> FetchPrintsByUrl(ScryfallCard card) { System.Threading.Thread.Sleep(100); List <string> retval = new List <string>(); List <ScryfallCard> cards = new List <ScryfallCard>(); PrintsList printsList = new PrintsList(); HttpResponseMessage response; string dbContent = string.Empty; string responseContent = string.Empty; SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM PRINTSLIST WHERE Card_Name = \'" + card.Name.Replace("\'", "\'\'") + "\'", db.connection); SqlCeDataAdapter da = new SqlCeDataAdapter(cmd); DataTable result = new DataTable(); da.Fill(result); if (result.Rows.Count != 0) { Console.WriteLine(String.Format("prints list exists in DB for {0}", card.Name)); dbContent = result.Rows[0]["Prints"].ToString(); printsList.prints = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Print> >(dbContent); } else { printsList.card_name = card.Name; printsList.prints = new List <Print>(); response = client.GetAsync(card.prints_search_uri).Result; response.EnsureSuccessStatusCode(); responseContent = response.Content.ReadAsStringAsync().Result; PrintsSearchResult apiResult = Newtonsoft.Json.JsonConvert.DeserializeObject <PrintsSearchResult>(responseContent); foreach (ScryfallCard datacard in apiResult.data) { Print p = new Print(); p.card_ID = datacard.id; p.set_name = datacard.set_name; p.set = datacard.set; printsList.prints.Add(p); } string serial = Newtonsoft.Json.JsonConvert.SerializeObject(printsList.prints); cmd = new SqlCeCommand("INSERT INTO PrintsList (Card_Name, Prints)" + "VALUES (@Card_Name, @Prints)", db.connection); cmd.Parameters.AddWithValue("@Card_Name", card.Name); cmd.Parameters.AddWithValue("@Prints", serial); cmd.ExecuteNonQuery(); Console.WriteLine(String.Format("prints added to DB for {0}", card.Name)); } return(printsList.prints); }
private void HandleSerializedValue(ref SqlCeCommand cmd, PropertyInfo info, ScryfallCard card) { try { string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(info.GetValue(card)); cmd.Parameters.AddWithValue("@" + info.Name, serialized); } catch (Exception) { throw; } }
public ScryfallCard FetchCardByID(string text) { System.Threading.Thread.Sleep(100); ScryfallCard retval = new ScryfallCard(); HttpResponseMessage response; string responseContent = string.Empty; response = client.GetAsync("cards/" + text).Result; response.EnsureSuccessStatusCode(); responseContent = response.Content.ReadAsStringAsync().Result; retval = ScryfallCard.FromJson(responseContent); return(retval); }
private void LoadCardIntoSqlCmd(ScryfallCard card, ref SqlCeCommand cmd) { try { PropertyInfo[] pi = typeof(ScryfallCard).GetProperties(); foreach (PropertyInfo info in pi) { ////DEBUG //if (info.Name == "color_identity") //{ // Console.WriteLine("here"); //} switch (info.Name) { case "card_PK": case "dateAdded": case "dateModified": case "modifiedMethod": continue; } TypeCode typeCode = Type.GetTypeCode(info.PropertyType); switch (typeCode) { case TypeCode.Object: HandleSerializedValue(ref cmd, info, card); continue; } if (info.GetValue(card) is null) { cmd.Parameters.AddWithValue("@" + info.Name, "NULL"); } else { // get the property and add it as the value for the cmd cmd.Parameters.AddWithValue("@" + info.Name, info.GetValue(card)); } } cmd.Parameters.AddWithValue("@dateAdded", DateTime.Now.ToString()); cmd.Parameters.AddWithValue("@dateModified", DateTime.Now.ToString()); cmd.Parameters.AddWithValue("@modifiedMethod", 1); } catch (Exception ex) { throw; } }
private ScryfallCard FetchNamedCardExact(string text) { ScryfallCard retval = null; try { System.Threading.Thread.Sleep(100); string responseContent = string.Empty; HttpResponseMessage response; SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM CARD WHERE name = \'" + text.Replace("\'", "\'\'") + "\'", db.connection); SqlCeDataAdapter da = new SqlCeDataAdapter(cmd); DataTable result = new DataTable(); da.Fill(result); if (result.Rows.Count > 1) { Console.WriteLine(String.Format("Card {0} has more than one record!!!", text)); } else if (result.Rows.Count != 0) { Console.WriteLine(String.Format("card {0} exists in DB", text)); //retval = ScryfallCard.LoadFromDB(result.Rows[0]); retval = ScryfallCard.LoadCardFromDatarow(result.Rows[0]); } else { response = client.GetAsync("cards/named?exact=" + text).Result; response.EnsureSuccessStatusCode(); responseContent = response.Content.ReadAsStringAsync().Result; retval = ScryfallCard.FromJson(responseContent); retval.SaveToDB(db); } } catch (Exception ex) { retval = null; throw; } return(retval); }
public static ScryfallCard LoadFromDBOld(DataRow row) { ScryfallCard retval = new ScryfallCard(); retval.id = row["id"].ToString(); retval.Name = row["name"].ToString(); retval.set_name = row["set_name"].ToString(); retval.set = row["set"].ToString(); retval.type_line = row["type_line"].ToString(); retval.card_faces = JsonConvert.DeserializeObject <Card_Face[]>(row["card_faces"].ToString()); retval.prints_search_uri = row["prints_search_uri"].ToString(); retval.image_uris = Newtonsoft.Json.JsonConvert.DeserializeObject <Image_Uris>(row["image_uris"].ToString()); //retval.json_string = row["json_string"].ToString(); //TODO: reflection return(retval); }
private void HandleTestButton() { this.btnTestStartingHands.Enabled = false; ScryfallCard cardToFind = null; // the method can handle a null value passed if (this.chkFindCard.Checked == true) { cardToFind = currentCard; } if (int.Parse(this.txtTimes.Text) >= 5) { this.txtTestOutput.Text = RunProbabilityTest(this.deck, int.Parse(this.txtTimes.Text), false, cardToFind); } else { this.txtTestOutput.Text = RunProbabilityTest(this.deck, int.Parse(this.txtTimes.Text), true, cardToFind); } this.btnTestStartingHands.Enabled = true; }
private bool FindCard(ScryfallCard cardToFind, List <ScryfallCard> list) { bool retval = false; try { foreach (ScryfallCard card in list) { if (card.Name == cardToFind.Name) { retval = true; break; } } } catch (Exception) { throw; } return(retval); }
public void ShuffleCards() { try { int n = this.cards.Count; for (int i = 0; i < 10; i++) { while (n > 1) { n--; int k = rng.Next(n + 1); ScryfallCard value = cards[k]; cards[k] = cards[n]; cards[n] = value; } } } catch (Exception) { throw; } }
private string RunProbabilityTest(Deck deck, int times, bool verboseLog, ScryfallCard cardToFind) { string retval = string.Empty; string handResult = string.Empty; string handResultTemplate = "Opening hand: "; string endResultTemplate = "Total hands: {0}. {1} lands, {2} nonlands, {3} total cards drawn."; string averageTemplate = "Average lands per hand: {0}"; string landHandsTemplate = "Hands by land distribution: {0} zero, {1} one, {2} two, {3} three, {4} four, {5} five, {6} six, {7} seven."; string foundCardResultTemplate = "Hands with {0}: {1}"; string foundCardPercentTemplate = "Chance to have {0} in opening hand: {1}%"; bool needsComma = false; int counter = 0; int lands = 0; int nonlands = 0; int totalLands = 0; int totalNonLands = 0; int totalCards = 0; // Hands by land distribution int zeroLand = 0; int oneLand = 0; int twoLand = 0; int threeLand = 0; int fourLand = 0; int fiveLand = 0; int sixLand = 0; int sevenLand = 0; decimal averageLandsPerHand = 0; decimal foundCardPercent = 0; bool findCard = (cardToFind != null); int handsWithCard = 0; List <ScryfallCard> hand = new List <ScryfallCard>(); try { for (int i = 0; i < times; i++) { counter++; hand = GetOpeningHand(deck); lands = CountStartingLands(hand); nonlands = CountStartingNonLands(hand); handResult = String.Format(handResultTemplate, counter, lands, nonlands); if (verboseLog) { retval += handResult; foreach (ScryfallCard card in hand) { if (needsComma) { retval += ", "; } needsComma = true; retval += card.Name; } retval += Environment.NewLine; needsComma = false; } totalLands += lands; totalNonLands += nonlands; totalCards += hand.Count; switch (lands) { case 0: zeroLand++; break; case 1: oneLand++; break; case 2: twoLand++; break; case 3: threeLand++; break; case 4: fourLand++; break; case 5: fiveLand++; break; case 6: sixLand++; break; case 7: sevenLand++; break; } if (findCard) { if (FindCard(cardToFind, hand)) { handsWithCard++; } } } averageLandsPerHand = (decimal)totalLands / (decimal)counter; averageLandsPerHand = Math.Round(averageLandsPerHand, 2); if (!verboseLog) { retval = string.Format(endResultTemplate, counter, totalLands, totalNonLands, totalCards); retval += Environment.NewLine; retval += string.Format(averageTemplate, averageLandsPerHand); retval += Environment.NewLine; retval += string.Format(landHandsTemplate, zeroLand, oneLand, twoLand, threeLand, fourLand, fiveLand, sixLand, sevenLand); } if (findCard) { foundCardPercent = ((decimal)handsWithCard / (decimal)times) * 100; retval += Environment.NewLine; retval += string.Format(foundCardResultTemplate, cardToFind.Name, handsWithCard); retval += Environment.NewLine; retval += string.Format(foundCardPercentTemplate, cardToFind.Name, foundCardPercent); } UpdateControlStates(); } catch (Exception) { throw; } return(retval); }
private void ViewCardOnline(ScryfallCard card) { Process.Start(card.scryfall_uri); }
private void ReplaceCardWithAlternatePrint(int index, string replacementID) { this.deck.cards[index] = this.engine.FetchCardByID(replacementID); this.currentCard = this.deck.cards[index]; bs.ResetBindings(false); }
private void RemoveCard(int index) { this.deck.cards.RemoveAt(index); this.currentCard = null; bs.ResetBindings(false); }
public Image GetCardImage(ScryfallCard card, bool frontFace) { return(FetchCardImage(card, frontFace)); }
public List <Print> GetPrintsByUrl(ScryfallCard card) { return(FetchPrintsByUrl(card)); }
private Image FetchCardImage(ScryfallCard card, bool frontFace) { Image retval = null; string cardID = card.id; string imageURL; string imagePath; SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM CardImage WHERE Card_ID = \'" + cardID + "\'", db.connection); SqlCeDataAdapter da = new SqlCeDataAdapter(cmd); DataTable result = new DataTable(); da.Fill(result); switch (result.Rows.Count) { case 0: // no rows returned // we want the front face if (frontFace) { // if the card is single faced and the normal image url isn't misssing if (card.image_uris != null && card.image_uris.normal != null) { imageURL = card.image_uris.normal; System.Net.WebRequest request = System.Net.WebRequest.Create(imageURL); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); retval = new Bitmap(responseStream); string filePath = cardID + ".jpg"; retval.Save(".\\images\\" + filePath); cmd = new SqlCeCommand("INSERT INTO CardImage (Card_ID, Image_URL, Local_Filepath)" + "VALUES (@Card_ID, @Image_URL, @Local_Filepath)", db.connection); cmd.Parameters.AddWithValue("@Card_ID", cardID); cmd.Parameters.AddWithValue("@Image_URL", imageURL); cmd.Parameters.AddWithValue("@Local_Filepath", filePath); cmd.ExecuteNonQuery(); } // if the card is multifaced else if (card.card_faces != null) { // if the front face image url is present if (card.card_faces[0].image_uris != null && card.card_faces[0].image_uris.normal != null) { imageURL = card.card_faces[0].image_uris.normal; System.Net.WebRequest request = System.Net.WebRequest.Create(imageURL); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); retval = new Bitmap(responseStream); string filePath = cardID + "_0" + ".jpg"; retval.Save(".\\images\\" + filePath); cmd = new SqlCeCommand("INSERT INTO CardImage (Card_ID, Image_URL, Local_Filepath)" + "VALUES (@Card_ID, @Image_URL, @Local_Filepath)", db.connection); cmd.Parameters.AddWithValue("@Card_ID", cardID); cmd.Parameters.AddWithValue("@Image_URL", imageURL); cmd.Parameters.AddWithValue("@Local_Filepath", filePath); cmd.ExecuteNonQuery(); } } else { throw new Exception("wtf?"); } } else // we want the back face (it must be multifaced) { // if the back face url is present if (card.card_faces[1].image_uris != null && card.card_faces[1].image_uris.normal != null) { imageURL = card.card_faces[1].image_uris.normal; System.Net.WebRequest request = System.Net.WebRequest.Create(imageURL); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); retval = new Bitmap(responseStream); string filePath = cardID + "_1" + ".jpg"; retval.Save(".\\images\\" + filePath); cmd = new SqlCeCommand("INSERT INTO CardImage (Card_ID, Image_URL, Local_Filepath)" + "VALUES (@Card_ID, @Image_URL, @Local_Filepath)", db.connection); cmd.Parameters.AddWithValue("@Card_ID", cardID); cmd.Parameters.AddWithValue("@Image_URL", imageURL); cmd.Parameters.AddWithValue("@Local_Filepath", filePath); cmd.ExecuteNonQuery(); } else { throw new NotImplementedException("Image url missing!"); } } break; case 1: // only one row returned if (frontFace) { // if the card is NOT multifaced or the card is multifaced but one-sided if (card.card_faces == null || card.card_faces[1].image_uris == null) { imagePath = result.Rows[0]["Local_Filepath"].ToString(); retval = Bitmap.FromFile(".\\images\\" + imagePath); } else { throw new Exception("wtf"); } } else // the card is multifaced and we want the back face { if (result.Rows[0]["Local_Filepath"].ToString().EndsWith("_1.jpg")) // if somehow the only row returned is the back face image { imagePath = result.Rows[0]["Local_Filepath"].ToString(); retval = Bitmap.FromFile(".\\images\\" + imagePath); } else if (card.card_faces[1].image_uris != null && card.card_faces[1].image_uris.normal != null) // if the back face image url is present { imageURL = card.card_faces[1].image_uris.normal; System.Net.WebRequest request = System.Net.WebRequest.Create(imageURL); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); retval = new Bitmap(responseStream); string filePath = cardID + "_1" + ".jpg"; retval.Save(".\\images\\" + filePath); cmd = new SqlCeCommand("INSERT INTO CardImage (Card_ID, Image_URL, Local_Filepath)" + "VALUES (@Card_ID, @Image_URL, @Local_Filepath)", db.connection); cmd.Parameters.AddWithValue("@Card_ID", cardID); cmd.Parameters.AddWithValue("@Image_URL", imageURL); cmd.Parameters.AddWithValue("@Local_Filepath", filePath); cmd.ExecuteNonQuery(); } else { throw new NotImplementedException("Second face image url missing!"); } } break; default: // more than 1 row returned if (frontFace) // we want the front face { if (result.Rows[0]["Local_Filepath"].ToString().EndsWith("_0.jpg")) // if row 1 is the front face { imagePath = result.Rows[0]["Local_Filepath"].ToString(); retval = Bitmap.FromFile(".\\images\\" + imagePath); } else if (result.Rows[1]["Local_Filepath"].ToString().EndsWith("_0.jpg")) // if row 2 is the front face { imagePath = result.Rows[1]["Local_Filepath"].ToString(); retval = Bitmap.FromFile(".\\images\\" + imagePath); } else { throw new Exception("wtf"); } } else // we want the back face { if (result.Rows[0]["Local_Filepath"].ToString().EndsWith("_1.jpg")) // if row 1 is the back face { imagePath = result.Rows[0]["Local_Filepath"].ToString(); retval = Bitmap.FromFile(".\\images\\" + imagePath); } else if (result.Rows[1]["Local_Filepath"].ToString().EndsWith("_1.jpg")) // if row 2 is the back face { imagePath = result.Rows[1]["Local_Filepath"].ToString(); retval = Bitmap.FromFile(".\\images\\" + imagePath); } else { throw new Exception("wtf"); } } break; } // OLD VERSION //if (frontFace && result.Rows.Count == 1) //{ // imageURL = result.Rows[0]["Local_Filepath"].ToString(); // retval = Bitmap.FromFile(".\\images\\" + imageURL); //} else //{ // if (frontFace && card.image_uris != null && card.image_uris.normal != null) // { // imageURL = card.image_uris.normal; // System.Net.WebRequest request = System.Net.WebRequest.Create(imageURL); // System.Net.WebResponse response = request.GetResponse(); // System.IO.Stream responseStream = // response.GetResponseStream(); // retval = new Bitmap(responseStream); // //string filePath = (".\\images\\" + cardID + ".jpg"); // string filePath = cardID + ".jpg"; // retval.Save(".\\images\\" + filePath); // cmd = new SqlCeCommand("INSERT INTO CardImage (Card_ID, Image_URL, Local_Filepath)" + // "VALUES (@Card_ID, @Image_URL, @Local_Filepath)", db.connection); // cmd.Parameters.AddWithValue("@Card_ID", cardID); // cmd.Parameters.AddWithValue("@Image_URL", imageURL); // cmd.Parameters.AddWithValue("@Local_Filepath", filePath); // cmd.ExecuteNonQuery(); // } // else if (card.card_faces != null && card.card_faces[0].image_uris != null && card.card_faces[0].image_uris.normal != null) // { // if (frontFace) // { // imageURL = card.card_faces[0].image_uris.normal; // } // else // { // if (card.card_faces[1] != null // && card.card_faces[1].image_uris != null // && card.card_faces[1].image_uris.normal != null) // { // imageURL = card.card_faces[1].image_uris.normal; // } // else // { // throw new NotImplementedException("Second face has no image!"); // } // } // System.Net.WebRequest request = System.Net.WebRequest.Create(imageURL); // System.Net.WebResponse response = request.GetResponse(); // System.IO.Stream responseStream = // response.GetResponseStream(); // retval = new Bitmap(responseStream); // //string filePath = (".\\images\\" + cardID + ".jpg"); // string filePath = cardID + ".jpg"; // retval.Save(".\\images\\" + filePath); // cmd = new SqlCeCommand("INSERT INTO CardImage (Card_ID, Image_URL, Local_Filepath)" + // "VALUES (@Card_ID, @Image_URL, @Local_Filepath)", db.connection); // cmd.Parameters.AddWithValue("@Card_ID", cardID); // cmd.Parameters.AddWithValue("@Image_URL", imageURL); // cmd.Parameters.AddWithValue("@Local_Filepath", filePath); // cmd.ExecuteNonQuery(); // } //} return(retval); }