Search in sources :

Example 1 with SpaceSettlersSimulator

use of spacesettlers.simulator.SpaceSettlersSimulator in project spacesettlers by amymcgovern.

the class Ladder method run.

/**
 * Runs the ladder for the specified number of games
 * @throws SimulatorException
 */
@SuppressWarnings("unchecked")
public void run() throws SimulatorException {
    ArrayList<HighLevelTeamConfig[]> clientsPerMatch = getAllClientsForAllMatches();
    int numGames = clientsPerMatch.size() * ladderConfig.getNumRepeatMatches();
    System.out.println("Ladder will run " + numGames + " games");
    System.out.println("Variable teams are: ");
    for (HighLevelTeamConfig team : ladderConfig.getVariableTeams()) {
        System.out.println(team);
    }
    int gameIndex = 0;
    for (int repeat = 0; repeat < ladderConfig.getNumRepeatMatches(); repeat++) {
        for (HighLevelTeamConfig[] teamsForMatch : clientsPerMatch) {
            gameIndex++;
            // setup the simulator for this match
            simConfig.setTeams(teamsForMatch);
            // set the bases to match the teams for this game.  Read in the ones
            // from the config file first (and rename them)
            // only make new ones if we don't have enough
            BaseConfig[] defaultBases = simConfig.getBases();
            BaseConfig[] baseConfig = new BaseConfig[teamsForMatch.length];
            for (int i = 0; i < teamsForMatch.length; i++) {
                if (i < defaultBases.length) {
                    baseConfig[i] = defaultBases[i];
                    baseConfig[i].setTeamName(teamsForMatch[i].getTeamName());
                } else {
                    baseConfig[i] = new BaseConfig(teamsForMatch[i].getTeamName());
                }
            }
            simConfig.setBases(baseConfig);
            // if there are flags, then set the flags to also match the teams for this game
            FlagConfig[] flagConfigs = simConfig.getFlags();
            if (flagConfigs != null && flagConfigs.length > 0) {
                if (flagConfigs.length != teamsForMatch.length) {
                    throw new SimulatorException("Error: The number of flags in the config file doesn't match the number of teams for the match");
                }
                for (int i = 0; i < teamsForMatch.length; i++) {
                    flagConfigs[i].setTeamName(teamsForMatch[i].getTeamName());
                }
            }
            // tell the user the match is about to begin
            String str = "***Game " + gameIndex + " / " + numGames + " with teams ";
            for (HighLevelTeamConfig team : teamsForMatch) {
                str += (team.getTeamName() + " ");
            }
            str += "***";
            System.out.println(str);
            ladderOutputString.add(str);
            try {
                // try to make a simulator and run it
                simulator = new SpaceSettlersSimulator(simConfig, parserConfig);
                str = "***Game " + gameIndex + " / " + numGames + " with teams ";
                Set<Team> teams = simulator.getTeams();
                for (Team team : teams) {
                    str += (team.getTeamName() + " = " + team.getLadderName() + " ");
                }
                str += "***";
                System.out.println(str);
                ladderOutputString.add(str);
                // run the game
                simulator.run();
                // get the teams and print out their scores
                for (Team team : teams) {
                    str = "Team: " + team.getLadderName() + " scored " + team.getScore();
                    ladderOutputString.add(str);
                    System.out.println(str);
                    TeamRecord thisRecord;
                    if (ladderResultsMap.containsKey(team.getLadderName())) {
                        thisRecord = ladderResultsMap.get(team.getLadderName());
                    } else {
                        thisRecord = new TeamRecord(team);
                    }
                    thisRecord.update(team);
                    ladderResultsMap.put(team.getLadderName(), thisRecord);
                }
            } catch (Exception e) {
                System.err.println("Error in match : skipping and moving to next one");
                ladderOutputString.add("Error in match : skipping and moving to next one");
                ladderOutputString.add(e.toString());
                e.printStackTrace();
            }
        }
    }
    // the games are over so sort the records
    sortedLadderResults = new ArrayList<TeamRecord>();
    for (TeamRecord record : ladderResultsMap.values()) {
        sortedLadderResults.add(record);
    }
    Collections.sort(sortedLadderResults, new TeamRecordComparator());
    System.out.println("Overall team order: ");
    for (TeamRecord record : sortedLadderResults) {
        System.out.println(record.getTeamName() + " average score " + record.getAverageScore());
    }
}
Also used : IOException(java.io.IOException) SimulatorException(spacesettlers.simulator.SimulatorException) SpaceSettlersSimulator(spacesettlers.simulator.SpaceSettlersSimulator) Team(spacesettlers.clients.Team) SimulatorException(spacesettlers.simulator.SimulatorException)

Aggregations

IOException (java.io.IOException)1 Team (spacesettlers.clients.Team)1 SimulatorException (spacesettlers.simulator.SimulatorException)1 SpaceSettlersSimulator (spacesettlers.simulator.SpaceSettlersSimulator)1