package arena;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import megamek.common.Player;
import megamek.common.compute.Compute;
import megamek.common.game.Game;
import megamek.common.game.IGame;
import megamek.common.loaders.MekSummaryCache;
import megamek.common.scenario.Scenario;
import megamek.common.scenario.ScenarioLoader;
/**
* Build the result's player array after removing a player from the game, and
* print it.
*
*
* RosterCheck data/scenarios/TrainingScenarios/1-FirstRun.mms
*
*
* This is the shape of a real match ending. A bot calls die() the moment it
* sees the victory phase, and MegaMek removes rather than ghosts a player that
* disconnects during that phase, so the host reads a player list the loser has
* already left. Removing one by hand is the same state without playing a match
* for it.
*
*
{@code tests/roster.sh} is what asserts against the output.
*/
public final class RosterCheck {
private RosterCheck() {
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("usage: RosterCheck ");
System.exit(2);
}
Compute.d6();
MekSummaryCache cache = MekSummaryCache.getInstance();
while (!cache.isInitialized()) {
Thread.sleep(250L);
}
ScenarioLoader loader = new ScenarioLoader(new File(args[0]));
Scenario scenario = loader.load();
IGame live = scenario.createGame();
// A scenario's game has not left the lounge, so the initial counts
// the game manager stamps at that point are still zero. Stamp them
// here, because "the numbers survive the player" is the half of this
// that only means something if there were numbers.
for (Player p : ((Game) live).getPlayersList()) {
p.setInitialEntityCount(live.getEntitiesOwnedBy(p));
p.setInitialBV(p.getBV());
}
// The roster, as the host's loop keeps it while the match runs.
Map roster = new LinkedHashMap<>();
for (Player p : ((Game) live).getPlayersList()) {
roster.put(p.getId(), MatchHost.Seat.of(p));
}
if (roster.size() < 2) {
System.err.println("scenario has fewer than two players");
System.exit(1);
}
// The one that leaves. Named on stderr so the assertions know which.
int leaver = roster.keySet().iterator().next();
System.err.println("removed=" + roster.get(leaver).name());
((Game) live).removePlayer(leaver);
System.out.println("{\"players\": [\n"
+ MatchHost.playersJson(live, roster, Map.of())
+ "]}");
}
}