Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.45 KB | None | 0 0
  1. import auction.{ User, Auction, AuctionItem }
  2. import auction.event._
  3.  
  4. package auction.bots {
  5.  
  6.   /**
  7.    * A BidBot has one owner and one auction that it must listen. It can be set to
  8.    * bid for multiple auction items for the user. The bot will make new bids
  9.    * for an item in behalf of the user whenever another user makes a higher bit.
  10.    * However, the bot will never bid more on an item than the maximum bid ceiling
  11.    * set.
  12.    */
  13.   class BidBot(owner: User, auction: Auction with Dispatcher) extends Listener {
  14.  
  15.     auction.registerListener(this)
  16.     private val items = scala.collection.mutable.Map[AuctionItem, (Int, Int)]()
  17.     /**
  18.      * Adds a new auction item that the bot bids on.
  19.      *
  20.      * The bot stores all this information for each item somewhere.
  21.      *
  22.      * @param item an item to watch and bid for
  23.      * @param bidCeiling a maximum amount to bid
  24.      * @param raise an amount to raise from the last bid
  25.      */
  26.     def bidOnItem(item: AuctionItem, bidCeiling: Int, raise: Int) = {
  27.       items += item -> (bidCeiling, raise)
  28.     }
  29.  
  30.     /**
  31.      * Receives events from the auction. The bot is especially interested in
  32.      * BidRaised events.
  33.      *
  34.      * If anyone else than its owner makes a new best bid for a watched item
  35.      * and the amount is less than the bidCeiling, the bot tries to make an
  36.      * even better bid. It will bid a sum of the new best bid amount and the
  37.      * raise if the sum is less than bidCeiling else the bot bids the
  38.      * bidCeiling amount.
  39.      *
  40.      * The bot can only make a bid if its owner has credits. Every
  41.      * bid made by the bot spends one credit. When and only when the last
  42.      * credit is spent, the bot first bids and then sends a MessagingEvent
  43.      * through the auction it follows to ask for more money.
  44.      *
  45.      * (you can decide what the bot says in the message)
  46.      */
  47.     def receiveEvent(e: AuctionEvent) = {
  48.       e match {
  49.           case BidRaised(bid)       => {
  50.             if (bid.who != owner && items.keys.exists(_ == bid.item) && bid.price < items(bid.item)._1 && owner.hasCredits)
  51.             {
  52.                   auction.makeBid(owner, bid.item, scala.math.min(bid.price + items(bid.item)._2, items(bid.item)._1))
  53.                   owner.useCredit()
  54.                   //if (!owner.hasCredits)
  55.                   //  auction.fireEvent(MessagingEvent(owner, "pLzGimmeMoarDough"))
  56.             }
  57.           }
  58.           case _ =>
  59.       }
  60.     }
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement