Advertisement
Guest User

Shopaspx

a guest
May 5th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Web.UI.HtmlControls;
  8. using System.Text.RegularExpressions;
  9. using System.Data;
  10. namespace Druya_1._0
  11. {
  12.     public partial class Shop : System.Web.UI.Page
  13.     {
  14.         const string ImageFormat = "png"; // The image format used in the page.
  15.         public List<shopItem> getItemsForUser(string username)
  16.         {
  17.             List<shopItem> retVal = new List<shopItem>();
  18.             foreach (int index in DbManager.getUserShopItemIndexes(username))
  19.             {
  20.                 retVal.Add(DbManager.getEntireItem(index));
  21.             }
  22.             return retVal;
  23.         }
  24.         public void UpdateSource()
  25.         {
  26.             Items.DataSource = (DataSet)Cache["Items"];
  27.             Items.DataBind();
  28.         }
  29.         public void fillShopGridForUser(string username)
  30.         {
  31.             Cache["Items"] = DbManager.getUserShopItemDS(username,getItemsForUser(username));
  32.         }
  33.         #region Past method using HtmlTables. Might be useful in the future, but gridView is more elegant.
  34.         /*static List<HtmlTableRow> rows = new List<HtmlTableRow>();
  35.         public void AddDiv(shopItem item,int count)
  36.         {
  37.             ContentPlaceHolder myCPH = (ContentPlaceHolder)this.Master.FindControl("PageContent");
  38.             HtmlTable Table = (HtmlTable)myCPH.FindControl("ItemTable");
  39.             Control possibleInstance = Table.FindControl("AmountFor" + item.ID);
  40.             if (possibleInstance != null) // making sure there are no duplicates.
  41.             {
  42.                 return;
  43.             }
  44.                 if (rows.Count == 0)
  45.                     rows.Add(new System.Web.UI.HtmlControls.HtmlTableRow());
  46.                 if (rows[rows.Count - 1].Cells.Count == 3)
  47.                     rows.Add(new System.Web.UI.HtmlControls.HtmlTableRow());
  48.                 HtmlTableCell myCell = new HtmlTableCell();
  49.                 Label Name = new Label();
  50.                 Label Herbs = new Label();
  51.                 Label Gems = new Label();
  52.                 Image Display = new Image(); // WebControls dull format image.
  53.                 Display.ImageUrl = "data:image/" + ImageFormat + ";base64," + RawSixtyFourHandler.get64FormatURLSrc(item.img);// Setting the data of the image as 64 source format.
  54.                 TextBox Amount = new TextBox();
  55.                 Amount.ID = "AmoutFor" + item.ID;
  56.                 Name.Text = item.name + "(" + item.rarity + ")";
  57.                 Herbs.Text = "Herbs: " + item.herbCost + ".    ";
  58.                 Gems.Text = "Gems: " + item.gemCost + ".";
  59.                 myCell.Controls.Add(Name);
  60.                 myCell.Controls.Add(new LiteralControl("<center>"));
  61.                 myCell.Controls.Add(Display);
  62.                 myCell.Controls.Add(new LiteralControl("<br />"));
  63.                 myCell.Controls.Add(new LiteralControl("<br />"));
  64.                 myCell.Controls.Add(new LiteralControl("<br />"));
  65.                 myCell.Controls.Add(Herbs);
  66.                 myCell.Controls.Add(Gems);
  67.                 myCell.Controls.Add(new LiteralControl("<br />"));
  68.                 myCell.Controls.Add(new LiteralControl("<br />"));
  69.                 myCell.Controls.Add(Amount);
  70.                 myCell.Controls.Add(new LiteralControl("</center>"));
  71.                 rows[rows.Count - 1].Controls.Add(myCell);
  72.                 foreach (var tr in rows)
  73.                 {
  74.                     ItemTable.Controls.Add(tr);
  75.                 }
  76.         }
  77.         /// <summary>
  78.         /// Slowly anhilates the whole table. safely. destructs all elements.
  79.         /// </summary>
  80.         private void clearTable()
  81.         {
  82.             ItemTable.Controls.Clear();
  83.             foreach (HtmlTableRow row in ItemTable.Rows)
  84.             {
  85.                 row.Cells.Clear();
  86.             }
  87.             ItemTable.Rows.Clear();
  88.         }*/
  89.         #endregion
  90.         protected void Page_Load(object sender, EventArgs e)
  91.         {
  92.             if (Session["User"] != null)
  93.             {
  94.                 if (!Page.IsPostBack)
  95.                 {
  96.                     for (int i = 1; i <= 100; i++)
  97.                     {
  98.                         ListItem t = new ListItem();
  99.                         t.Value = "" + i;
  100.                         t.Text = "" + i;
  101.                         Amount.Items.Add(t);
  102.                     }
  103.                     foreach (int id in from item in getItemsForUser(((User)Session["User"]).Username) select item.ID)
  104.                     {
  105.                         ListItem itm = new ListItem();
  106.                         itm.Text = "" + id;
  107.                         itm.Value = "" + id;
  108.                         PurchaseItemIDs.Items.Add(itm);
  109.                     }
  110.                     TotalPrice.Text = "Pick an item and an amount to see the price.";
  111.                 }
  112.                 #region past PageLoad method for the HTMLtable method.
  113.                 /*
  114.             clearTable(); // restart the table.
  115.             if (Session["User"] != null)
  116.             {
  117.                 for (int i = 0; i < getItemsForUser(((User)Session["User"]).Username).Count; i++)
  118.                 {
  119.                     AddDiv(getItemsForUser(((User)Session["User"]).Username)[i], i);
  120.                 }
  121.             }
  122.              * */
  123.                 #endregion
  124.                     Gems.Text = "" + ((Player)Session["Player"]).Gems;
  125.                     Herbs.Text = "" + ((Player)Session["Player"]).Herbs;
  126.                     if (Cache["Items"] == null)
  127.                     {
  128.                         fillShopGridForUser(((User)Session["User"]).Username);
  129.                     }
  130.                     UpdateSource();
  131.             }
  132.         }
  133.         #region past methods using HtmlTable. Useless, at the moment, unless I use them for another feature.
  134.         /*
  135.         /// <summary>
  136.         /// Returns all the cells in a table in a format which is comfortable for a foreach loop.
  137.         /// </summary>
  138.         /// <param name="table">The table to iterate on.</param>
  139.         /// <returns>The formatable IEnumerable for the foreach loop.</returns>
  140.         IEnumerable<HtmlTableCell> GetCells(HtmlTable table)
  141.         {
  142.             foreach(HtmlTableRow tr in ItemTable.Rows)
  143.             {
  144.                 foreach (HtmlTableCell tc in tr.Cells)
  145.                 {
  146.                     yield return tc;
  147.                 }
  148.             }
  149.         }
  150.        
  151.         /// <summary>
  152.         /// Returns the price in a cell - makes sure there are no injections in them. Or errors, for that metter.
  153.         /// </summary>
  154.         /// <param name="tc">The table cell to inspect</param>
  155.         /// <returns> Pair of two ints : The cost in herbs, The cost in gems.</returns>
  156.         KeyValuePair<int, int> GetSingularPriceWithFormat(HtmlTableCell tc)
  157.         {
  158.             KeyValuePair<int, int> retVal = new KeyValuePair<int, int>(0, 0);
  159.             TextBox Identification = (TextBox)tc.Controls[4]; //useful textbox - identifys the item and the user input.
  160.             string
  161.                 txt = Identification.Text,
  162.                 itemId = Identification.ID.Substring(8);
  163.             if (txt.Length != 0) // If the user inserted a value in the textbox.
  164.             {
  165.                 if (Regex.IsMatch(txt, @"^\d+$")) // making sure the string is a number.
  166.                 {
  167.                     int Amount = int.Parse(txt);
  168.                     Label Herbs = (Label)tc.Controls[2]; // Format: "Herbs: `REQ`.   ". Current IRLen: 13. IND: 7
  169.                     Label Gems = (Label)tc.Controls[3]; // Format: "Gems: `REQ`.". Current IRLen: 7. IND : 6
  170.                     int priceHerbs = int.Parse(Herbs.Text.Substring(7, Herbs.Text.Length - 13));
  171.                     int priceGems = int.Parse(Gems.Text.Substring(6, Gems.Text.Length - 6));
  172.                     retVal = new KeyValuePair<int, int>(priceHerbs, priceGems);
  173.                 }
  174.             }
  175.             return retVal;
  176.  
  177.         }
  178.         /// <summary>
  179.         /// Returns the total price, HERBS:GEMS, of all the items in the current selected  cell format. on error - skips.
  180.         /// </summary>
  181.         /// <param name="FormattedCells"> All the cells in the table</param>
  182.         /// <returns>Pair of two ints : The cost in herbs, The cost in gems.</returns>
  183.         KeyValuePair<int,int> GetCollectivePrice(IEnumerable<HtmlTableCell> FormattedCells)
  184.         {
  185.             int HerbsCost = 0;
  186.             int GemsCost = 0;
  187.             foreach (HtmlTableCell tc in FormattedCells)
  188.             {
  189.                 KeyValuePair<int, int> price = GetSingularPriceWithFormat(tc);
  190.                 HerbsCost += price.Key;
  191.                 GemsCost += price.Value;
  192.             }
  193.             return new KeyValuePair<int, int>(HerbsCost, GemsCost);
  194.         }*/
  195.         #endregion
  196.         protected void Price_Changed_Event(object sender, EventArgs e)
  197.         {
  198.             int HerbPrice = -1;
  199.             int GemPrice = -1;
  200.             int AmountPurchased = int.Parse(Amount.SelectedValue);
  201.             List<shopItem> Items = getItemsForUser(((User)Session["User"]).Username);
  202.             foreach (shopItem t in Items)
  203.             {
  204.                 if (t.ID == int.Parse(PurchaseItemIDs.SelectedValue))
  205.                 {
  206.                     HerbPrice = t.herbCost*AmountPurchased;
  207.                     GemPrice = t.gemCost * AmountPurchased;
  208.                 }
  209.             }
  210.             if (HerbPrice == -1 || GemPrice == -1)
  211.                 throw new Exception("ItemID not found.");
  212.             else
  213.                 TotalPrice.Text = "Herbs: "+ HerbPrice + ", Gems: " + GemPrice;
  214.         }
  215.         protected void Buy_Click(object sender, EventArgs e)
  216.         {
  217.             #region past method for the buying format in the old HtmlTables. Not complete, but could be useful in the future.
  218.             /*
  219.             Label myLbl = new Label();
  220.             KeyValuePair<int,int> total =GetCollectivePrice(GetCells(ItemTable));
  221.             myLbl.Text = "Total price:<br>" + total.Key + " Herbs,<br>" + total.Value + " Gems.";
  222.             myLbl.ForeColor = System.Drawing.Color.Azure;
  223.             Response.Controls.Add(myLbl);*/
  224.             #endregion
  225.             if (Session["User"] != null && Session["Player"] != null)
  226.             {
  227.                 int HerbsStart = TotalPrice.Text.IndexOf(' ') + 1;
  228.                 int HerbsEnd = TotalPrice.Text.IndexOf(',');
  229.                 int herbsPrice = int.Parse(TotalPrice.Text.Substring(HerbsStart, HerbsEnd - HerbsStart));
  230.                 int GemsStart = TotalPrice.Text.IndexOf('m') + 4;
  231.                 int GemsEnd = TotalPrice.Text.Length;
  232.                 int gemsPrice = int.Parse(TotalPrice.Text.Substring(GemsStart, GemsEnd - GemsStart));
  233.                 // Gems / Herbs are saved in the Session["Player"]. Response panel ID = " Result " .
  234.                 Player user = ((Player)Session["Player"]);
  235.                 int playerHerbs = user.Herbs;
  236.                 int playerGems = user.Gems;
  237.             }
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement