Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import auction.{ User, Auction, AuctionItem }
- import auction.event._
- package auction.bots {
- /**
- * A BidBot has one owner and one auction that it must listen. It can be set to
- * bid for multiple auction items for the user. The bot will make new bids
- * for an item in behalf of the user whenever another user makes a higher bit.
- * However, the bot will never bid more on an item than the maximum bid ceiling
- * set.
- */
- class BidBot(owner: User, auction: Auction with Dispatcher) extends Listener {
- auction.registerListener(this)
- private val items = scala.collection.mutable.Map[AuctionItem, (Int, Int)]()
- /**
- * Adds a new auction item that the bot bids on.
- *
- * The bot stores all this information for each item somewhere.
- *
- * @param item an item to watch and bid for
- * @param bidCeiling a maximum amount to bid
- * @param raise an amount to raise from the last bid
- */
- def bidOnItem(item: AuctionItem, bidCeiling: Int, raise: Int) = {
- items += item -> (bidCeiling, raise)
- }
- /**
- * Receives events from the auction. The bot is especially interested in
- * BidRaised events.
- *
- * If anyone else than its owner makes a new best bid for a watched item
- * and the amount is less than the bidCeiling, the bot tries to make an
- * even better bid. It will bid a sum of the new best bid amount and the
- * raise if the sum is less than bidCeiling else the bot bids the
- * bidCeiling amount.
- *
- * The bot can only make a bid if its owner has credits. Every
- * bid made by the bot spends one credit. When and only when the last
- * credit is spent, the bot first bids and then sends a MessagingEvent
- * through the auction it follows to ask for more money.
- *
- * (you can decide what the bot says in the message)
- */
- def receiveEvent(e: AuctionEvent) = {
- e match {
- case BidRaised(bid) => {
- if (bid.who != owner && items.keys.exists(_ == bid.item) && bid.price < items(bid.item)._1 && owner.hasCredits)
- {
- auction.makeBid(owner, bid.item, scala.math.min(bid.price + items(bid.item)._2, items(bid.item)._1))
- owner.useCredit()
- //if (!owner.hasCredits)
- // auction.fireEvent(MessagingEvent(owner, "pLzGimmeMoarDough"))
- }
- }
- case _ =>
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement