Esempio n. 1
0
  @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;
  }
Esempio n. 2
0
  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();
    }
  }