private static void createAdvert(Object source, System.Timers.ElapsedEventArgs e) { //offercount = amount of adverts the market is currently offering int offercount = DatabaseInterface.ReadAllResourceOffers().FindAll(s => s.SellerName.Equals("Server")).Count; Random r = new Random(); //TEMP Console.WriteLine("MIGHT CREATE ADVERT"); //possibly creating new advert from marketplace //chance is (100 - (offercount / MAX_MARKET_ADVERTS) * 100)% //The fewer market adverts, the higher chance of creating advert) if (r.Next(MAX_MARKET_ADVERTS) >= offercount) { //TEMP Console.WriteLine("CREATING ADVERT!!!!"); //Setting random price and count int count = 1; //new Random().Next(3)+1; int price = 0; //(new Random().Next(4, 8)) * (count/2+1); //Creating array of the enum values Array resTypes = Enum.GetValues(typeof(ResourceType)); //Picking a random resource from the array, except the last, which is gold ResourceType res = (ResourceType)resTypes.GetValue(r.Next(resTypes.Length - 1)); ResourceOffer ro = new ResourceOffer(0, "Server", res, count, price); int id = DatabaseInterface.PutResourceOfferOnMarket(ro); adverts.Add(new Advert(id)); } }
/// <summary> /// Update a resource offer on the market /// </summary> /// <param name="offer">Offer to update</param> public static void UpdateResourceOffer(ResourceOffer offer) { try { string query = "UPDATE Market " + "SET SellerName = @SellerName, ResourceType = @Type, " + "Count = @Count, Price = @Price, HighestBidder = @Bidder, Bid = @Bid " + "WHERE Market.Id = " + offer.Id + " AND Market.Bid < @Bid;"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@SellerName", offer.SellerName); command.Parameters.AddWithValue("@Type", offer.Type); command.Parameters.AddWithValue("@Count", offer.Count); command.Parameters.AddWithValue("@Price", offer.Price); command.Parameters.AddWithValue("@Bidder", offer.HighestBidder ?? Convert.DBNull); command.Parameters.AddWithValue("@Bid", offer.HighestBid); // If no rows where updated if (command.ExecuteNonQuery() == 0) { throw new ResourceOfferException("Unable to bid on ressource. Either it is gone or your bid was to low", offer); } } } } catch (SqlException ex) { SqlExceptionHandling(ex); } }
/// <summary> /// Put a resource offer on the market /// </summary> /// <param name="offer">Offer to place on the market</param> public static int PutResourceOfferOnMarket(ResourceOffer offer) { try { string query = "INSERT INTO Market (SellerName, ResourceType, Count, Price, HighestBidder, Bid) " + "OUTPUT INSERTED.Id " + "VALUES (@Name, @Resource, @Count, @Price, @HighestBidder, @Bid);"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@Name", offer.SellerName); command.Parameters.AddWithValue("@Resource", offer.Type); command.Parameters.AddWithValue("@Count", offer.Count); command.Parameters.AddWithValue("@Price", offer.Price); command.Parameters.AddWithValue("@HighestBidder", "Server"); command.Parameters.AddWithValue("@Bid", offer.Price); return((int)command.ExecuteScalar()); } } } catch (SqlException ex) { SqlExceptionHandling(ex); } return(0); }
//Missing tests public static string BidOnResource(ResourceOffer offer) { ResourceOffer prevOffer = DatabaseInterface.ReadResourceOffer(offer.Id); if (player.Gold < offer.HighestBid) { return("I must apologize my good sir, but you do not seem to have enough medieval currency"); } try { //Hvad sker der nu, hvis UpdateRessourceOffer virker, men den af en eller anden grund ikke kan opdatere dit guld? //Så kaster den en exception, men updateResource bliver vel stadig kaldt? DatabaseInterface.UpdateResourceOffer(offer); //Transfering the money back to the previous highest bidder, if one exists if (prevOffer.HighestBidder != null) { DatabaseInterface.UpdatePlayerResource(prevOffer.HighestBidder, ResourceType.Gold, prevOffer.HighestBid); } //Taking money from the new highest bidder DatabaseInterface.UpdatePlayerResource(offer.HighestBidder, ResourceType.Gold, -offer.HighestBid); } catch (Exception ex) { //parse error msg to user return(ex.Message); } return(""); //MainServer.bidAccepted(offer.Id); //When the auction is ended, the user has already spent the money, and only the wares have to be transfered }
private void sellToPlayer(ResourceOffer ro) { //Take the gold - NO //Gold is already in quarantine //DatabaseInterface.UpdatePlayerResource(ro.HighestBidder, ResourceType.Gold, - ro.Price); //Give the ware DatabaseInterface.UpdatePlayerResource(ro.HighestBidder, ro.Type, ro.Count); }
protected void buttonConfirmSell_Click(Object sender, EventArgs e) { var sid = hiddenValue.Value; var soldElement = localresources.Find(se => se.Id == sid); var sellValue = Int32.Parse(inputPrice.Value); var newOffer = new ResourceOffer(MainClient.player.Name, soldElement.Type, 1, sellValue); newOffer.HighestBid = Int32.Parse(inputPrice.Value); MainClient.PlaceResourceOfferOnMarket(newOffer); RenderLocalResources(); RenderMarket(); inputPrice.Value = ""; }
public static string PlaceResourceOfferOnMarket(ResourceOffer offer) { try { int id = DatabaseInterface.PutResourceOfferOnMarket(offer); DatabaseInterface.UpdatePlayerResource(player.Name, offer.Type, -1); MainServer.adverts.Add(new Advert(id)); } catch (Exception ex) { return(ex.Message); } return(""); }
private void sellToPlayer(ResourceOffer ro) { //Take the gold - NO //Gold is already in quarantine //DatabaseInterface.UpdatePlayerResource(ro.HighestBidder, ResourceType.Gold, - ro.Price); //Give the ware if (ro.HighestBidder == "Server" && ro.SellerName != "Server") { DatabaseInterface.UpdatePlayerResource(MainClient.player.Name, ro.Type, ro.Count); } else { DatabaseInterface.UpdatePlayerResource(ro.HighestBidder, ro.Type, ro.Count); } }
protected void submitBid_Click(object sender, EventArgs e) { string ID = hidId.Value; string price = bidPrice.Value; int bidValue = Int32.Parse(price); ResourceOffer ro = marketresources.Find(x => x.Id == Int32.Parse(ID)); ro.HighestBid = bidValue; ro.HighestBidder = MainClient.player.Name; string returnedMessage = MainClient.BidOnResource(ro); goldAmount.InnerText = "You have " + MainClient.player.Gold + " pieces of gold"; if (returnedMessage != "") { //Message box error } bidwarning.Visible = false; bidPrice.Value = ""; }
public ResourceOfferException(string message, ResourceOffer offer) : base(message) { this.Offer = offer; }