Market.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Server.MirDatabase;
  2. using Server.MirEnvir;
  3. using System.Data;
  4. namespace Server.Database
  5. {
  6. public partial class Market : Form
  7. {
  8. public Market()
  9. {
  10. InitializeComponent();
  11. LoadMarket();
  12. }
  13. #region Load Market
  14. private void LoadMarket()
  15. {
  16. // Clear existing items in the MarketListing
  17. MarketListing.Items.Clear();
  18. // Retrieve all auctions from the user database and filter out expired or sold items
  19. List<AuctionInfo> allAuctions = Envir.Main.Auctions.ToList();
  20. List<AuctionInfo> activeAuctions = allAuctions.Where(a => !a.Expired && !a.Sold).ToList();
  21. // Update the TotalItemsLabel with the count of active items
  22. TotalItemsLabel.Text = $"Total Items: {activeAuctions.Count}";
  23. // Retrieve search keyword from SearchBox and convert to lowercase for case-insensitive search
  24. string searchKeyword = SearchBox.Text.Trim().ToLower();
  25. bool filterByPlayer = FilterByPlayer.Checked;
  26. // Variables to track filtered player name and count
  27. string filteredPlayerName = string.Empty;
  28. int filteredPlayerItemCount = 0;
  29. // Apply filters based on checkboxes and search keyword
  30. var filteredAuctions = activeAuctions.Where(a =>
  31. {
  32. bool matchesSearch = string.IsNullOrEmpty(searchKeyword) ||
  33. (FilterByItem.Checked && a.Item?.Info.FriendlyName.ToLower().Contains(searchKeyword) == true) ||
  34. (filterByPlayer && a.SellerInfo?.Name.ToLower().Contains(searchKeyword) == true);
  35. // Count items by the filtered player if the player filter is applied
  36. if (filterByPlayer && a.SellerInfo?.Name.ToLower() == searchKeyword)
  37. {
  38. filteredPlayerName = a.SellerInfo.Name; // Capture exact player name for label
  39. filteredPlayerItemCount++;
  40. }
  41. return matchesSearch;
  42. }).ToList();
  43. // Update TotalItemsOwnedLabel based on the player filter results
  44. if (filterByPlayer && !string.IsNullOrEmpty(filteredPlayerName))
  45. {
  46. TotalItemsOwnedLabel.Text = $"Total Items owned by: {filteredPlayerName} ({filteredPlayerItemCount})";
  47. }
  48. else
  49. {
  50. TotalItemsOwnedLabel.Text = "Total Items owned by: ";
  51. }
  52. // Iterate over each filtered auction listing and add it to the MarketListing
  53. foreach (var listing in filteredAuctions)
  54. {
  55. // Create a new ListViewItem with the item's name or "Unknown Item" as a fallback
  56. ListViewItem item = new ListViewItem(listing.Item?.Info.FriendlyName ?? "Unknown Item");
  57. // Add sub-items for UID, Price, Seller, and Expiry
  58. item.SubItems.Add(listing.AuctionID.ToString()); // Auction ID
  59. item.SubItems.Add(listing.Price.ToString("N0")); // Gold Price
  60. item.SubItems.Add(listing.SellerInfo?.Name ?? "Unknown Seller"); // Seller Name
  61. item.SubItems.Add(listing.ConsignmentDate.AddDays(7).ToString("g")); // Expiry date assuming 7 days from consignment
  62. // Add the item to the MarketListing
  63. MarketListing.Items.Add(item);
  64. }
  65. }
  66. #endregion
  67. #region Refresh Listings Button
  68. private void RefreshListings_Click(object sender, EventArgs e)
  69. {
  70. LoadMarket();
  71. }
  72. #endregion
  73. #region Expire Listings Button
  74. private void ExpireListingButton_Click(object sender, EventArgs e)
  75. {
  76. // Ensure an item is selected in the MarketListing
  77. if (MarketListing.SelectedItems.Count == 0)
  78. {
  79. MessageBox.Show("Please select a listing to expire.");
  80. return;
  81. }
  82. // Get the selected auction ID from the ListView
  83. var selectedItem = MarketListing.SelectedItems[0];
  84. if (!ulong.TryParse(selectedItem.SubItems[1].Text, out ulong auctionId))
  85. {
  86. MessageBox.Show("Invalid Auction ID selected.");
  87. return;
  88. }
  89. // Find the auction in Envir.Main.Auctions by AuctionID
  90. var auction = Envir.Main.Auctions.FirstOrDefault(a => a.AuctionID == auctionId);
  91. if (auction == null)
  92. {
  93. MessageBox.Show("Auction listing not found.");
  94. return;
  95. }
  96. // Mark the listing as expired
  97. auction.Expired = true;
  98. // Refresh the MarketListing to reflect the update
  99. LoadMarket();
  100. MessageBox.Show("Listing marked as expired successfully.");
  101. }
  102. #endregion
  103. #region Delete Listings Button
  104. private void DeleteListingButton_Click(object sender, EventArgs e)
  105. {
  106. if (MarketListing.SelectedItems.Count == 0)
  107. {
  108. MessageBox.Show("Please select a listing to delete.");
  109. return;
  110. }
  111. var selectedItem = MarketListing.SelectedItems[0];
  112. if (!ulong.TryParse(selectedItem.SubItems[1].Text, out ulong auctionId))
  113. {
  114. MessageBox.Show("Invalid Auction ID selected.");
  115. return;
  116. }
  117. var auction = Envir.Main.Auctions.FirstOrDefault(a => a.AuctionID == auctionId);
  118. if (auction == null)
  119. {
  120. MessageBox.Show("Auction listing not found.");
  121. return;
  122. }
  123. var confirmResult = MessageBox.Show(
  124. "Are you sure you want to delete this listing?\n\n" +
  125. "Warning: This action is irreversible, and neither the item nor the asking price will be returned to the player.",
  126. "Confirm Deletion",
  127. MessageBoxButtons.YesNo,
  128. MessageBoxIcon.Warning
  129. );
  130. if (confirmResult != DialogResult.Yes) return;
  131. string reason = ReasonTextBox.Text.Trim();
  132. if (auction.SellerInfo?.Player != null && !string.IsNullOrEmpty(reason))
  133. {
  134. auction.SellerInfo.Player.ReceiveChat(reason, ChatType.Announcement);
  135. }
  136. // Remove the auction from the main auction list and the seller's account
  137. Envir.Main.Auctions.Remove(auction);
  138. auction.SellerInfo.AccountInfo.Auctions.Remove(auction);
  139. LoadMarket();
  140. MessageBox.Show("Listing deleted successfully, and the owner has been notified.");
  141. }
  142. #endregion
  143. }
  144. }