@Override public void run() { if (run) { // Chose if an AI player should place a bid or the level should change int nextBid = auction.getCurrentBid() + AuctionEngine.BID_DELTA; EBidLevel bidLevel = auction.getBidLevel(); List<IAIPlayer> biddingPlayers = new ArrayList<>(auction.getBiddingPlayers()); biddingPlayers.remove(auction.getBiddingPlayer()); boolean aiBid = rnd.nextInt(2) % 2 == 0 || bidLevel == EBidLevel.INITIAL; if (aiBid && !biddingPlayers.isEmpty()) { Collections.shuffle(biddingPlayers); IAIPlayer bidder = biddingPlayers.get(0); clientServerEventBus.post(new AuctionBid(bidder, nextBid, auction)); } else { EBidLevel nextLevel; switch (bidLevel) { case ONE: nextLevel = EBidLevel.TWO; break; case TWO: nextLevel = EBidLevel.DONE; break; default: throw new IllegalStateException("Levels "+EBidLevel.INITIAL+" and "+EBidLevel.DONE + " are not valid"); } clientServerEventBus.post(new AuctionBidLevel(bidLevel, nextLevel, auction.getBiddingPlayer(), auction)); } } }
@Subscribe public void handleBid(AuctionBid bid) { Preconditions.checkArgument(!bid.getBidder().equals(bid.getAuction().getBiddingPlayer()), "The bidder has to change"); IAuction auction = bid.getAuction(); auction.getCurrentTask().cancel(); auction.setBidLevel(EBidLevel.INITIAL); auction.setBiddingPlayer(bid.getBidder()); auction.setCurrentBid(bid.getAmount()); clientServerEventBus.post(new AuctionBidLevel(auction.getBidLevel(), EBidLevel.INITIAL, bid.getBidder(), auction)); CancelableRunnable task = factory.createNextBidTask(auction); auction.setCurrentTask(task); timer.schedule(task, 30, TimeUnit.SECONDS); }
currentBidLevel = new SimpleObjectProperty<>(nextAuction.getBidLevel());