@Override protected void actIntelligently() { log("beginRound", Level.INFO); PlayerState player = getPlayerState(); int nrUnits = player.units.size(); for (int i = 0; i < nrUnits; i++) { UnitState unit = player.units.get(i); log("unit state: " + unit.toString(), Level.INFO); player = getCmd().move(unit, smartChoiceDst(getValidPoints(unit))); if (player != null) { if (player.validLastTransition()) { log(" ==== Last transition valid ==== ", Level.INFO); } else { log(" ==== Last transition NOT valid ==== ", Level.INFO); log(player.getLastTransitionError(), Level.INFO); } } else { log(" ==== Last transition out of sync ==== ", Level.INFO); break; } } }
@Override public synchronized PlayerState process(Transition action) { int clientID = allowedSecret(action.secret); if (clientID == -1) { return null; } Integer playerID = state.getPlayerIds().get(clientID); PlayerState player = state.playerStates.get(playerID); if (action.operator == ActionType.PlayerReady) { TransitionResult res = new TransitionResult(action.id); res.errorType = TransitionResult.TransitionError.NoError; player.response = res; printToGuiLog(player, action); return player; } if (!allowedPlayer(clientID)) { return null; } if (player.round.currentRound < state.round.currentRound) { player.round.currentRound = state.round.currentRound; player.round.startTime = GamePolicy.connectWaitTime + state.round.currentRound * GamePolicy.roundTime + clientID * GamePolicy.playerActionTime; } player.response = actionEngine.process(player, action); if (player.response.valid()) { logger.info("[srv]Executed action [" + action.operator.name() + "] for client: " + clientID); } else { logger.info("[srv]Failed action [" + action.operator.name() + "] for client: " + clientID); } // update the view of the player's own units after the execution of an action actionEngine.updatePlayerSight(state, playerID); // update the view of the player's own towers after the execution of an action actionEngine.updateTowerSight(state, playerID); return player; }
private void printToGuiLog(PlayerState player, Transition action) { if (player.validLastTransition()) { if (action.operator == ActionType.PlayerReady) { Point2i initPos = GamePolicy.initialPlayerPositions.get(player.id); gui_logger.info( state.round.currentRound + " " + action.operator.name() + " " + player.id + " " + +initPos.x + " " + initPos.y + " " + player.getScore()); for (UnitState u : player.units) { gui_logger.info( state.round.currentRound + " " + "UnitReady" + " " + player.id + " " + +u.id + " " + u.pos.x + " " + u.pos.y + " " + player.getScore() + " " + u.energy); } } } }
public String toString() { return player.id.toString() + "," + score + "," + player.getKills() + "," + player.getRetaliationKills() + "," + player.getDeadUnits() + "," + player.getPlacedTowers() + "," + player.getSuccessfulTraps() + "," + player.getPlacedTraps() + "," + player.getKillingSprees() + "," + player.getFirstBlood(); }
private void declareWinner() { // System.out.println("PRINTING RESULTS"); class PlayerScore { public PlayerState player; public float score; public PlayerScore(PlayerState player, float score) { this.player = player; this.score = score; } public String toString() { return player.id.toString() + "," + score + "," + player.getKills() + "," + player.getRetaliationKills() + "," + player.getDeadUnits() + "," + player.getPlacedTowers() + "," + player.getSuccessfulTraps() + "," + player.getPlacedTraps() + "," + player.getKillingSprees() + "," + player.getFirstBlood(); } } List<Integer> playerIDs = state.getPlayerIds(); ArrayList<PlayerScore> scores = new ArrayList<PlayerScore>(); for (Integer playerID : playerIDs) { PlayerState ps = state.playerStates.get(playerID); float score = ps.getScore(); scores.add(new PlayerScore(ps, score)); } Collections.sort( scores, new Comparator<PlayerScore>() { public int compare(PlayerScore a, PlayerScore b) { return Float.compare(b.score, a.score); } }); try { BufferedWriter bw = new BufferedWriter(new FileWriter("winner.txt")); Iterator<PlayerScore> it = scores.iterator(); logger.info("Scores:"); while (it.hasNext()) { PlayerScore ps = it.next(); bw.write(ps.toString()); bw.newLine(); logger.info(ps.toString()); } bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } }