📄 tacagent.java
字号:
requestQuote(quotes[i], conn, false); } } } } else if (key == "quotes") { if (value == connection) { // Request the entertainment quotes only td.addTask(time + INFO_UPDATE_PERIOD, key, value, this); requestQuotes((TACConnection) value, false, false); } } else if (key == "bids") { if (value == connection) { td.addTask(time + INFO_UPDATE_PERIOD, key, value, this); requestBidInfos((TACConnection) value); } } else if (key == "printOwn") { if (value == connection && (printOwnDelay > 0)) { td.addTask(time + printOwnDelay, key, value, this); printOwn(); } } else if (key == "gameStarts") { nextGameStarts((TACConnection) value); } else if (key == "gameEnds") { gameEnds(); } else if (key instanceof Quote) { requestQuote((Quote) key, (TACConnection) value, true); } } // ------------------------------------------------------------------- // API's for the connection handlers // ------------------------------------------------------------------- public String getHost() { return host; } public int getPort() { return port; } public String getUser() { return userName; } public String getPassword() { return password; } // ------------------------------------------------------------------- // API's for the agent // ------------------------------------------------------------------- public int getGameID() { return playingGame; } public long getServerTime() { return System.currentTimeMillis() - timeDiff; } public long getGameTime() { return getServerTime() - startTime; } public long getGameTimeLeft() { long time = startTime + gameLength - getServerTime(); return time > 0L ? time : 0L; } public String getGameTimeAsString() { if (playingGame < 0) { return "--:--"; } return getTimeAsString(getGameTime()); } public String getGameTimeLeftAsString() { if (playingGame < 0) { return "--:--"; } return getTimeAsString(getGameTimeLeft()); } // Returns the time in minutes and seconds private String getTimeAsString(long time) { long timeInSeconds = time / 1000; int sek = (int) (timeInSeconds % 60); StringBuffer sb = new StringBuffer(); sb.append(timeInSeconds / 60).append(':'); if (sek < 10) { sb.append('0'); } return sb.append(sek).toString(); } public int getGameLength() { return gameLength; } public String commandStatusToString(int status) { for (int i = 0, n = statusCodes.length; i < n; i++) { if (statusCodes[i] == status) { return statusName[i]; } } return Integer.toString(status); } public int getServerAuctionID(int auction) { return auctionIDs[auction]; } /** * @deprecated Use getServerAuctionID() instead! **/ public int getAuctionID(int auction) { return auctionIDs[auction]; } public static String getAuctionTypeAsString(int auction) { return auctionType[auction]; } public static int getAuctionNo() { return NO_AUCTIONS; } public static int getAuctionCategory(int auction) { if (auction < 8) { return CAT_FLIGHT; } else if (auction < 16) { return CAT_HOTEL; } return CAT_ENTERTAINMENT; } // Returns the day of the auction in the range 1 - 5 public static int getAuctionDay(int auction) { int day = (auction % 4) + 1; if ((auction / 4) == 1) { // Outflights are specified as day 2 to day 5 day++; } return day; } // Returns the type of the auction. Please note that // auctions in different categories might have the // same value as type. public static int getAuctionType(int auction) { int type = auction / 4; switch (type) { case 0: return TYPE_INFLIGHT; case 1: return TYPE_OUTFLIGHT; case 2: return TYPE_CHEAP_HOTEL; case 3: return TYPE_GOOD_HOTEL; case 4: return TYPE_ALLIGATOR_WRESTLING; case 5: return TYPE_AMUSEMENT; default: return TYPE_MUSEUM; } } public static int getAuctionFor(int category, int type, int day) { if (category == 0) { if (type == 1) { return day - 1; } else { return day + 2; } } else if (category == 2) { type--; } return category * 8 + type * 4 + day - 1; } private int getAuctionCategory(String cat) { if ("flight".equals(cat)) return CAT_FLIGHT; if ("hotel".equals(cat)) return CAT_HOTEL; if ("entertainment".equals(cat)) return CAT_ENTERTAINMENT; log.warning("illegal category '" + cat + '\''); return -1; } public static String auctionCategoryToString(int category) { return (category >= CAT_FLIGHT) && (category < categoryName.length) ? categoryName[category] : Integer.toString(category); } public int getClientPreference(int client, int type) { return clientPrefs[client][type]; } public int getOwn(int auctionID) { // The id that the agent gets for auctions is always 0 - 27 return owns[auctionID]; } // What might be owned in addition to "getOwn" public int getProbablyOwn(int auctionID) { Bid bid = getBid(auctionID); if (bid == null) return 0; Quote quote = getQuote(auctionID); if (quote.hasHQW(bid)) { return quote.getHQW(); } return bid.getQuantity(); } public synchronized Bid getBid(int auctionID) { return bids[auctionID]; } public Quote getQuote(int auctionID) { return quotes[auctionID]; } public int getAllocation(int auction) { return allocate[auction]; } public void setAllocation(int auction, int alloc) { allocate[auction] = alloc; if (tableModel != null) { tableModel.fireTableCellUpdated(auction, 8); } } public void clearAllocation() { for (int i = 0; i < NO_AUCTIONS; i++) { allocate[i] = 0; } } private void clearAll() { isGameStarted = false; lastHotelAuction = -1; clearID = 0; for (int i = 0, n = clientPrefs.length; i < n; i++) { int[] tmp = clientPrefs[i]; for (int j = 0, m = tmp.length; j < m; j++) { tmp[j] = 0; } } for (int i = 0; i < NO_AUCTIONS; i++) { auctionIDs[i] = 0; owns[i] = 0; bids[i] = null; costs[i] = 0f; allocate[i] = 0; quotes[i].clearAll(); pendingQuotes[i] = 0L; } if (tableModel != null) { tableModel.fireTableDataChanged(); } } public void submitBid(Bid bid) { if (getGameID() < 0) { throw new IllegalStateException("No game playing"); } int auction = bid.getAuction(); bid.submitted(); TACMessage msg = new TACMessage("submitBid"); prepareBidMsg(msg, bid); updateBid(bid); sendMessage(msg, this); } public void replaceBid(Bid oldBid, Bid bid) { if (getGameID() < 0) { throw new IllegalStateException("No game playing"); } int auction = bid.getAuction(); int oldAuction = oldBid.getAuction(); if (oldBid.isPreliminary()) { throw new IllegalStateException("Old bid is still preliminary"); } if (auction != oldAuction) { throw new IllegalArgumentException("Bids do not have same AuctionID"); } bid.submitted(); if (oldBid != bids[auction]) { bid.setRejectReason(Bid.ACTIVE_BID_CHANGED); bid.setProcessingState(Bid.REJECTED); try { agent.bidRejected(bid); } catch (Exception e) { log.log(Level.SEVERE, "agent could not handle bidRejected", e); } } else { TACMessage msg = new TACMessage("replaceBid"); msg.setParameter("bidID", oldBid.getID()); msg.setParameter("bidHash", oldBid.getBidHash()); prepareBidMsg(msg, bid); updateBid(bid); sendMessage(msg, this); } } // inflight((AllocDay1-Own|ProbablyOwn-BidQ[R][C])...) public void printOwn() { StringBuffer sb = new StringBuffer(); sb.append(EOL); printOwn("inflights", MIN_FLIGHT, MIN_FLIGHT + 4, sb); sb.append(','); printOwn("outflights", MAX_FLIGHT - 3, MAX_FLIGHT + 1, sb); sb.append(EOL); printOwn("good hotel", MIN_HOTEL, MIN_HOTEL + 4, sb); sb.append(','); printOwn("cheap hotel", MAX_HOTEL - 3, MAX_HOTEL + 1, sb); sb.append(EOL); printOwn("wrestling", MIN_ENTERTAINMENT, MIN_ENTERTAINMENT + 4, sb); sb.append(','); printOwn("amusement", MIN_ENTERTAINMENT + 4, MIN_ENTERTAINMENT + 8, sb); sb.append(','); printOwn("museum", MIN_ENTERTAINMENT + 8, MIN_ENTERTAINMENT + 12, sb); sb.append(EOL); log.fine(sb.toString()); } private void printOwn(String type, int startAuction, int endAuction, StringBuffer sb) { sb.append(type).append('('); for (int i = startAuction; i < endAuction; i++) { Bid bid = getBid(i); Quote q = getQuote(i); sb.append("(").append(getAllocation(i)).append('-') .append(getOwn(i)).append('|') .append(getProbablyOwn(i)).append('-') .append(bid != null ? bid.getQuantity() : 0); if (q.isAuctionClosed()) { sb.append('C'); } sb.append(')'); } sb.append(')'); } // ------------------------------------------------------------------- // API's to the connection handlers // ------------------------------------------------------------------- void reset(long timeout, TACConnection conn) { if (connection == conn) { log.fine("performing connection reset"); cancelTimers(); // Clear transaction q transActionsNum = 0; waitActionsNum = 0; disconnect(500); playingGame = -1; nextGameID = -1; exitGameLog(); TACMessage.resetResponseTime(); if (timeout > 0) { try { Thread.sleep(timeout); } catch (Exception e) { } } connect(); } } private void disconnect(int timeout) { if (connection != null && connection.isConnected()) { TACMessage m = new TACMessage("quit"); try { m.setMessageReceiver(this); connection.sendMessage(m); Thread.sleep(timeout); } catch (Exception e) { } connection.disconnect(); } } public void sendMessage(TACMessage msg, TACMessageReceiver recv) { TACConnection connection = this.connection; if (connection != null) { try { msg.setMessageReceiver(recv); connection.sendMessage(msg); } catch (IOException e) { log.log(Level.WARNING, "could not send message " + msg.getType(), e); reset(0, connection); } } else { log.log(Level.WARNING, "could not send message: no connection"); } } public void messageReceived(TACMessage msg) { if (msg.isTACError()) { // A TAC Error was received as reply for the message String type = msg.getType(); if ("getGameConsts".equals(type)) { // This is only for backward compability. Older versions of the // server might return tacerror for unknown message types. log.warning("ignoring error for getGameConst: " + msg.getValue()); } else { agent.tacerrorReceived(msg); } } else if (msg.nextTag() && (!msg.isDeclaration() || msg.nextTag())) { if (msg.isTag("auth")) { handleLogin(msg); } else if (msg.isTag("serverTime")) { handleServerTime(msg); } else if (msg.isTag("nextGame")) { handleNextGame(msg); } else if (msg.isTag("getGameParams")) { handleGetGame(msg); // Get the transactions before the games starts requestTransactions(OP_GAME_STARTS); if (display != null) { String status = playingGameType != null ? userName + ": Showing game " + nextGameID + " of type " + playingGameType : userName + ": Showing game " + nextGameID; display.setGameStatus(status); } } else if (msg.isTag("getGameAuctionIDs")) { handleGetAuctions(msg); } else if (msg.isTag("getGameConsts")) { handleGetConstants(msg); } else if (msg.isTag("submitBid")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -