//BiddingForSingleAuction /// <summary> /// Add bid, resolve proxy bidding situation /// </summary> /// <returns> 0 - winner, 1 - outbidder, 2 - update bid, 3 - wrong bid</returns> public byte BiddingForSingleAuction(AuctionDetail auction, BidCurrent currentBid, out BidCurrent previousBid, out BidCurrent loserBid, out BidCurrent winnerBid) { long?bid_id = -1; previousBid = loserBid = winnerBid = null; List <BidCurrent> allbids = dataContext.spBid_LotBids(auction.LinkParams.ID).ToList(); // no bids if (!allbids.Any()) { currentBid.Amount = currentBid.IsProxy ? auction.Price : currentBid.MaxBid; dataContext.spBid_PlaceBidsForSingleBidding(auction.LinkParams.ID, ref bid_id, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid, currentBid.DateMade, currentBid.IP, currentBid.IsProxy, false, currentBid.IsActive, null, null, null, null, null, null, null, null, null, null, null, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid); currentBid.ID = bid_id.GetValueOrDefault(-1); winnerBid = new BidCurrent(currentBid); return(0); } previousBid = allbids.FirstOrDefault(q => q.User_ID == currentBid.User_ID); // the duplicate if (previousBid != null) { currentBid.ID = previousBid.ID; if (previousBid.IsProxy == currentBid.IsProxy && previousBid.MaxBid >= currentBid.Amount) { return(3); } } BidCurrent topBid = allbids.First(); // current is highbidder if (topBid.User_ID == currentBid.User_ID) { currentBid.Amount = (!currentBid.IsProxy && currentBid.Amount > topBid.Amount) ? currentBid.Amount : topBid.Amount; currentBid.MaxBid = Math.Max(currentBid.MaxBid, topBid.MaxBid); currentBid.IsProxy = currentBid.Amount <= currentBid.MaxBid; bid_id = topBid.ID; dataContext.spBid_PlaceBidsForSingleBidding(auction.LinkParams.ID, ref bid_id, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid, currentBid.DateMade, currentBid.IP, currentBid.IsProxy, true, currentBid.IsActive, null, null, null, null, null, null, null, null, null, null, null, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid); currentBid.ID = bid_id.GetValueOrDefault(-1); previousBid = new BidCurrent(topBid); winnerBid = new BidCurrent(currentBid); return(2); } decimal amount; bool isautobid; // current max bid is bigger than the top bid if (topBid.MaxBid < currentBid.MaxBid) { amount = currentBid.IsProxy ? Math.Min(topBid.Amount + Consts.GetIncrement(topBid.Amount), currentBid.MaxBid) : currentBid.MaxBid; isautobid = topBid.Amount != topBid.MaxBid; topBid.Amount = topBid.MaxBid; currentBid.Amount = !isautobid ? amount : (currentBid.IsProxy ? Math.Min(topBid.Amount + Consts.GetIncrement(topBid.Amount), currentBid.MaxBid) : currentBid.MaxBid); bid_id = previousBid == null ? -1 : currentBid.ID; if (isautobid) { dataContext.spBid_PlaceBidsForSingleBidding(auction.LinkParams.ID, ref bid_id, currentBid.User_ID, amount, currentBid.MaxBid, currentBid.DateMade, currentBid.IP, currentBid.IsProxy, false, currentBid.IsActive, currentBid.Amount, currentBid.MaxBid, DateTime.Now, topBid.ID, topBid.User_ID, topBid.Amount, topBid.MaxBid, topBid.DateMade, DateTime.Now, topBid.IP, topBid.IsProxy, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid); } else { dataContext.spBid_PlaceBidsForSingleBidding(auction.LinkParams.ID, ref bid_id, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid, currentBid.DateMade, currentBid.IP, currentBid.IsProxy, false, currentBid.IsActive, null, null, null, null, null, null, null, null, null, null, null, currentBid.User_ID, currentBid.Amount, currentBid.MaxBid); } currentBid.ID = bid_id.GetValueOrDefault(-1); loserBid = new BidCurrent(topBid); winnerBid = new BidCurrent(currentBid); return(0); } amount = currentBid.IsProxy ? Math.Min(topBid.Amount + Consts.GetIncrement(topBid.Amount), currentBid.MaxBid) : currentBid.MaxBid; isautobid = amount < currentBid.MaxBid; currentBid.Amount = currentBid.MaxBid; bid_id = previousBid == null ? -1 : currentBid.ID; if (topBid.Amount == topBid.MaxBid || (topBid.Amount > currentBid.Amount && topBid.MaxBid > currentBid.MaxBid)) { dataContext.spBid_PlaceBidsForSingleBidding(auction.LinkParams.ID, ref bid_id, currentBid.User_ID, amount, currentBid.MaxBid, currentBid.DateMade, currentBid.IP, currentBid.IsProxy, false, currentBid.IsActive, isautobid ? (decimal?)currentBid.Amount : null, isautobid ? (decimal?)currentBid.MaxBid : null, isautobid ? (DateTime?)DateTime.Now : null, null, null, null, null, null, null, null, null, null, null, null); } else { topBid.Amount = Math.Min(currentBid.MaxBid + Consts.GetIncrement(currentBid.MaxBid), topBid.MaxBid); dataContext.spBid_PlaceBidsForSingleBidding(auction.LinkParams.ID, ref bid_id, currentBid.User_ID, amount, currentBid.MaxBid, currentBid.DateMade, currentBid.IP, currentBid.IsProxy, false, currentBid.IsActive, isautobid ? (decimal?)currentBid.Amount : null, isautobid ? (decimal?)currentBid.MaxBid : null, isautobid ? (DateTime?)DateTime.Now : null, topBid.ID, topBid.User_ID, topBid.Amount, topBid.MaxBid, topBid.DateMade, DateTime.Now, topBid.IP, topBid.IsProxy, topBid.User_ID, topBid.Amount, topBid.MaxBid); } currentBid.ID = bid_id.GetValueOrDefault(-1); loserBid = new BidCurrent(currentBid); winnerBid = new BidCurrent(topBid); return(1); }