Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- using System.Text.RegularExpressions;
- using System.Data;
- namespace Druya_1._0
- {
- public partial class Shop : System.Web.UI.Page
- {
- const string ImageFormat = "png"; // The image format used in the page.
- public List<shopItem> getItemsForUser(string username)
- {
- List<shopItem> retVal = new List<shopItem>();
- foreach (int index in DbManager.getUserShopItemIndexes(username))
- {
- retVal.Add(DbManager.getEntireItem(index));
- }
- return retVal;
- }
- public void UpdateSource()
- {
- Items.DataSource = (DataSet)Cache["Items"];
- Items.DataBind();
- }
- public void fillShopGridForUser(string username)
- {
- Cache["Items"] = DbManager.getUserShopItemDS(username,getItemsForUser(username));
- }
- #region Past method using HtmlTables. Might be useful in the future, but gridView is more elegant.
- /*static List<HtmlTableRow> rows = new List<HtmlTableRow>();
- public void AddDiv(shopItem item,int count)
- {
- ContentPlaceHolder myCPH = (ContentPlaceHolder)this.Master.FindControl("PageContent");
- HtmlTable Table = (HtmlTable)myCPH.FindControl("ItemTable");
- Control possibleInstance = Table.FindControl("AmountFor" + item.ID);
- if (possibleInstance != null) // making sure there are no duplicates.
- {
- return;
- }
- if (rows.Count == 0)
- rows.Add(new System.Web.UI.HtmlControls.HtmlTableRow());
- if (rows[rows.Count - 1].Cells.Count == 3)
- rows.Add(new System.Web.UI.HtmlControls.HtmlTableRow());
- HtmlTableCell myCell = new HtmlTableCell();
- Label Name = new Label();
- Label Herbs = new Label();
- Label Gems = new Label();
- Image Display = new Image(); // WebControls dull format image.
- Display.ImageUrl = "data:image/" + ImageFormat + ";base64," + RawSixtyFourHandler.get64FormatURLSrc(item.img);// Setting the data of the image as 64 source format.
- TextBox Amount = new TextBox();
- Amount.ID = "AmoutFor" + item.ID;
- Name.Text = item.name + "(" + item.rarity + ")";
- Herbs.Text = "Herbs: " + item.herbCost + ". ";
- Gems.Text = "Gems: " + item.gemCost + ".";
- myCell.Controls.Add(Name);
- myCell.Controls.Add(new LiteralControl("<center>"));
- myCell.Controls.Add(Display);
- myCell.Controls.Add(new LiteralControl("<br />"));
- myCell.Controls.Add(new LiteralControl("<br />"));
- myCell.Controls.Add(new LiteralControl("<br />"));
- myCell.Controls.Add(Herbs);
- myCell.Controls.Add(Gems);
- myCell.Controls.Add(new LiteralControl("<br />"));
- myCell.Controls.Add(new LiteralControl("<br />"));
- myCell.Controls.Add(Amount);
- myCell.Controls.Add(new LiteralControl("</center>"));
- rows[rows.Count - 1].Controls.Add(myCell);
- foreach (var tr in rows)
- {
- ItemTable.Controls.Add(tr);
- }
- }
- /// <summary>
- /// Slowly anhilates the whole table. safely. destructs all elements.
- /// </summary>
- private void clearTable()
- {
- ItemTable.Controls.Clear();
- foreach (HtmlTableRow row in ItemTable.Rows)
- {
- row.Cells.Clear();
- }
- ItemTable.Rows.Clear();
- }*/
- #endregion
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["User"] != null)
- {
- if (!Page.IsPostBack)
- {
- for (int i = 1; i <= 100; i++)
- {
- ListItem t = new ListItem();
- t.Value = "" + i;
- t.Text = "" + i;
- Amount.Items.Add(t);
- }
- foreach (int id in from item in getItemsForUser(((User)Session["User"]).Username) select item.ID)
- {
- ListItem itm = new ListItem();
- itm.Text = "" + id;
- itm.Value = "" + id;
- PurchaseItemIDs.Items.Add(itm);
- }
- TotalPrice.Text = "Pick an item and an amount to see the price.";
- }
- #region past PageLoad method for the HTMLtable method.
- /*
- clearTable(); // restart the table.
- if (Session["User"] != null)
- {
- for (int i = 0; i < getItemsForUser(((User)Session["User"]).Username).Count; i++)
- {
- AddDiv(getItemsForUser(((User)Session["User"]).Username)[i], i);
- }
- }
- * */
- #endregion
- Gems.Text = "" + ((Player)Session["Player"]).Gems;
- Herbs.Text = "" + ((Player)Session["Player"]).Herbs;
- if (Cache["Items"] == null)
- {
- fillShopGridForUser(((User)Session["User"]).Username);
- }
- UpdateSource();
- }
- }
- #region past methods using HtmlTable. Useless, at the moment, unless I use them for another feature.
- /*
- /// <summary>
- /// Returns all the cells in a table in a format which is comfortable for a foreach loop.
- /// </summary>
- /// <param name="table">The table to iterate on.</param>
- /// <returns>The formatable IEnumerable for the foreach loop.</returns>
- IEnumerable<HtmlTableCell> GetCells(HtmlTable table)
- {
- foreach(HtmlTableRow tr in ItemTable.Rows)
- {
- foreach (HtmlTableCell tc in tr.Cells)
- {
- yield return tc;
- }
- }
- }
- /// <summary>
- /// Returns the price in a cell - makes sure there are no injections in them. Or errors, for that metter.
- /// </summary>
- /// <param name="tc">The table cell to inspect</param>
- /// <returns> Pair of two ints : The cost in herbs, The cost in gems.</returns>
- KeyValuePair<int, int> GetSingularPriceWithFormat(HtmlTableCell tc)
- {
- KeyValuePair<int, int> retVal = new KeyValuePair<int, int>(0, 0);
- TextBox Identification = (TextBox)tc.Controls[4]; //useful textbox - identifys the item and the user input.
- string
- txt = Identification.Text,
- itemId = Identification.ID.Substring(8);
- if (txt.Length != 0) // If the user inserted a value in the textbox.
- {
- if (Regex.IsMatch(txt, @"^\d+$")) // making sure the string is a number.
- {
- int Amount = int.Parse(txt);
- Label Herbs = (Label)tc.Controls[2]; // Format: "Herbs: `REQ`. ". Current IRLen: 13. IND: 7
- Label Gems = (Label)tc.Controls[3]; // Format: "Gems: `REQ`.". Current IRLen: 7. IND : 6
- int priceHerbs = int.Parse(Herbs.Text.Substring(7, Herbs.Text.Length - 13));
- int priceGems = int.Parse(Gems.Text.Substring(6, Gems.Text.Length - 6));
- retVal = new KeyValuePair<int, int>(priceHerbs, priceGems);
- }
- }
- return retVal;
- }
- /// <summary>
- /// Returns the total price, HERBS:GEMS, of all the items in the current selected cell format. on error - skips.
- /// </summary>
- /// <param name="FormattedCells"> All the cells in the table</param>
- /// <returns>Pair of two ints : The cost in herbs, The cost in gems.</returns>
- KeyValuePair<int,int> GetCollectivePrice(IEnumerable<HtmlTableCell> FormattedCells)
- {
- int HerbsCost = 0;
- int GemsCost = 0;
- foreach (HtmlTableCell tc in FormattedCells)
- {
- KeyValuePair<int, int> price = GetSingularPriceWithFormat(tc);
- HerbsCost += price.Key;
- GemsCost += price.Value;
- }
- return new KeyValuePair<int, int>(HerbsCost, GemsCost);
- }*/
- #endregion
- protected void Price_Changed_Event(object sender, EventArgs e)
- {
- int HerbPrice = -1;
- int GemPrice = -1;
- int AmountPurchased = int.Parse(Amount.SelectedValue);
- List<shopItem> Items = getItemsForUser(((User)Session["User"]).Username);
- foreach (shopItem t in Items)
- {
- if (t.ID == int.Parse(PurchaseItemIDs.SelectedValue))
- {
- HerbPrice = t.herbCost*AmountPurchased;
- GemPrice = t.gemCost * AmountPurchased;
- }
- }
- if (HerbPrice == -1 || GemPrice == -1)
- throw new Exception("ItemID not found.");
- else
- TotalPrice.Text = "Herbs: "+ HerbPrice + ", Gems: " + GemPrice;
- }
- protected void Buy_Click(object sender, EventArgs e)
- {
- #region past method for the buying format in the old HtmlTables. Not complete, but could be useful in the future.
- /*
- Label myLbl = new Label();
- KeyValuePair<int,int> total =GetCollectivePrice(GetCells(ItemTable));
- myLbl.Text = "Total price:<br>" + total.Key + " Herbs,<br>" + total.Value + " Gems.";
- myLbl.ForeColor = System.Drawing.Color.Azure;
- Response.Controls.Add(myLbl);*/
- #endregion
- if (Session["User"] != null && Session["Player"] != null)
- {
- int HerbsStart = TotalPrice.Text.IndexOf(' ') + 1;
- int HerbsEnd = TotalPrice.Text.IndexOf(',');
- int herbsPrice = int.Parse(TotalPrice.Text.Substring(HerbsStart, HerbsEnd - HerbsStart));
- int GemsStart = TotalPrice.Text.IndexOf('m') + 4;
- int GemsEnd = TotalPrice.Text.Length;
- int gemsPrice = int.Parse(TotalPrice.Text.Substring(GemsStart, GemsEnd - GemsStart));
- // Gems / Herbs are saved in the Session["Player"]. Response panel ID = " Result " .
- Player user = ((Player)Session["Player"]);
- int playerHerbs = user.Herbs;
- int playerGems = user.Gems;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement