net.alloyggp.tournament.internal.spec.MatchSpec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ggp-tournament Show documentation
Show all versions of ggp-tournament Show documentation
A library for GGP tournament specification and scheduling.
package net.alloyggp.tournament.internal.spec;
import java.util.List;
import java.util.Map;
import javax.annotation.concurrent.Immutable;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import net.alloyggp.tournament.api.TMatchSetup;
import net.alloyggp.tournament.api.TPlayer;
import net.alloyggp.tournament.internal.Game;
import net.alloyggp.tournament.internal.YamlUtils;
@Immutable
public class MatchSpec {
private final Game game;
private final int startClock;
private final int playClock;
private final ImmutableList playerSeedOrder;
private final double weight;
private MatchSpec(Game game, int startClock, int playClock,
ImmutableList playerSeedOrder, double weight) {
Preconditions.checkArgument(weight >= 0.0);
this.game = game;
this.startClock = startClock;
this.playClock = playClock;
this.playerSeedOrder = playerSeedOrder;
this.weight = weight;
}
private static final ImmutableSet ALLOWED_KEYS = ImmutableSet.of(
"game",
"startClock",
"playClock",
"seedRoles",
"weight"
);
@SuppressWarnings("unchecked")
public static MatchSpec parseYaml(Object yamlMatch, Map games) {
Map matchMap = (Map) yamlMatch;
YamlUtils.validateKeys(matchMap, "match", ALLOWED_KEYS);
String gameName = (String) matchMap.get("game");
Game game = games.get(gameName);
if (game == null) {
throw new IllegalArgumentException("Could not find game specification with name "
+ gameName + " in the YAML file.");
}
int startClock = (int) matchMap.get("startClock");
int playClock = (int) matchMap.get("playClock");
ImmutableList playerSeedOrder;
if (matchMap.containsKey("seedRoles")) {
playerSeedOrder = ImmutableList.copyOf(
(List) matchMap.get("seedRoles"));
} else {
playerSeedOrder = getDefaultPlayerSeedOrder(game.getNumRoles());
}
double weight = 1.0;
if (matchMap.containsKey("weight")) {
weight = (double) matchMap.get("weight");
}
return new MatchSpec(game, startClock, playClock, playerSeedOrder, weight);
}
private static ImmutableList getDefaultPlayerSeedOrder(int numRoles) {
List seeds = Lists.newArrayList();
for (int i = 0; i < numRoles; i++) {
seeds.add(i);
}
return ImmutableList.copyOf(seeds);
}
public Game getGame() {
return game;
}
public int getStartClock() {
return startClock;
}
public int getPlayClock() {
return playClock;
}
//From seed order (best first), to the order of their roles
public ImmutableList putInOrder(List playersBestSeedFirst) {
ImmutableList.Builder players = ImmutableList.builder();
for (int seed : playerSeedOrder) {
players.add(playersBestSeedFirst.get(seed));
}
return players.build();
}
public TMatchSetup createMatchSetup(String matchId, List playersHighestSeedFirst) {
return TMatchSetup.create(matchId, game, putInOrder(playersHighestSeedFirst),
startClock, playClock);
}
public double getWeight() {
return weight;
}
}