use of com.voxelgameslib.voxelgameslib.components.team.Team in project VoxelGamesLibv2 by VoxelGamesLib.
the class TeamSelectFeature method balance.
private void balance() {
// TODO Balancing by rating
Map<String, Integer> sizes = calcSizes();
String large = findLargest(sizes);
if (large == null || large.equals("")) {
log.finer("no balancing");
teams.stream().map(t -> t.getName() + ": " + t.getPlayers().size()).forEach(log::finer);
return;
}
int largeCount = sizes.get(large);
String small = findSmallest(sizes);
int smallCount = sizes.get(small);
log.finer("LARGE: " + large + " : " + largeCount);
log.finer("SMALL: " + small + " : " + smallCount);
if (!(largeCount == smallCount || largeCount == smallCount + 1)) {
Team largeT = getTeam(large).orElseThrow(() -> new RuntimeException("Null team encountered"));
Team smallT = getTeam(small).orElseThrow(() -> new RuntimeException("Null team encountered"));
boolean switched = false;
for (int i = largeT.getPlayers().size() - 1; i > 0; i--) {
User player = largeT.getPlayers().get(i);
// if(playerIsInParty) {TODO party handling for team select
log.finer("SWITCH: " + player + " from " + large + " to " + small);
largeT.leave(player);
smallT.join(player, player.getRating(getPhase().getGame().getGameMode()));
Lang.msg(player, LangKey.TEAM_AUTO_BALANCED, largeT.getName(), smallT.getName());
switched = true;
break;
// }
}
if (!switched) {
log.finer(largeT.getName());
log.finer(largeT.getPlayers().size() + "");
User player = largeT.getPlayers().get(largeT.getPlayers().size() - 1);
largeT.leave(player);
smallT.join(player, player.getRating(getPhase().getGame().getGameMode()));
Lang.msg(player, LangKey.TEAM_AUTO_BALANCED, largeT.getName(), smallT.getName());
}
balance();
}
}
use of com.voxelgameslib.voxelgameslib.components.team.Team in project VoxelGamesLibv2 by VoxelGamesLib.
the class TeamSelectFeature method disable.
@Override
public void disable() {
Map<String, Integer> sizes = calcSizes();
// add everyone who isn't in a team yet to the smallest team
for (User user : getPhase().getGame().getPlayers()) {
if (!getTeam(user).isPresent()) {
Team t = getTeam(findSmallest(sizes)).orElseThrow(() -> new RuntimeException("Null team encountered"));
t.join(user, user.getRating(getPhase().getGame().getGameMode()));
Lang.msg(user, LangKey.TEAM_AUTO_ASSIGNED, t.getName());
sizes.put(t.getName(), sizes.get(t.getName()) + 1);
}
}
// make teams balanced
balance();
// save teams for next phase
DefaultGameData defaultGameData = getPhase().getGame().getGameData(DefaultGameData.class).orElse(new DefaultGameData());
defaultGameData.teams = teams;
getPhase().getGame().putGameData(defaultGameData);
}
use of com.voxelgameslib.voxelgameslib.components.team.Team in project VoxelGamesLibv2 by VoxelGamesLib.
the class TeamSelectFeature method enable.
@Override
public void enable() {
// create default teams
List<String> teamNames = new ArrayList<>();
teamNames.add("BLUE");
teamNames.add("RED");
teamNames.add("GREEN");
teamNames.add("YELLOW");
teamNames.add("LIGHT_PURPLE");
teamNames.add("BLACK");
List<ChatColor> teamColors = new ArrayList<>();
teamColors.add(ChatColor.BLUE);
teamColors.add(ChatColor.RED);
teamColors.add(ChatColor.GREEN);
teamColors.add(ChatColor.YELLOW);
teamColors.add(ChatColor.LIGHT_PURPLE);
teamColors.add(ChatColor.BLACK);
for (int i = 0; i < teamCount; i++) {
teams.add(new Team(teamSize, teamNames.get(i), teamColors.get(i), getPhase().getGame()));
}
}
use of com.voxelgameslib.voxelgameslib.components.team.Team in project VoxelGamesLibv2 by VoxelGamesLib.
the class AbstractPhase method checkEnd.
private void checkEnd() {
// check all victory conditions
User winner = null;
Team winnerTeam = null;
for (VictoryCondition victoryCondition : victoryConditions) {
if (!victoryCondition.completed()) {
return;
}
if (victoryCondition.getWinner() != null) {
if (!victoryCondition.getWinner().equals(winner)) {
if (winner == null) {
if (winnerTeam != null && !winnerTeam.contains(victoryCondition.getWinner())) {
throw new VoxelGameLibException(victoryCondition.getName() + " defined a winner even tho we already have a winning team!");
}
winner = victoryCondition.getWinner();
} else {
throw new VoxelGameLibException(victoryCondition.getName() + " defined a different winner than one of the conditions before it!");
}
}
}
if (victoryCondition.getWinnerTeam() != null) {
if (!victoryCondition.getWinnerTeam().equals(winnerTeam)) {
if (winnerTeam == null) {
if (winner != null && !victoryCondition.getWinnerTeam().contains(winner)) {
throw new VoxelGameLibException(victoryCondition.getName() + " defined a winning team even tho we already have a winning user!");
} else {
winnerTeam = victoryCondition.getWinnerTeam();
}
} else {
throw new VoxelGameLibException(victoryCondition.getName() + " defined a different winning team than one of the conditions before it!");
}
}
}
}
// all done, end this game
getGame().endGame(winnerTeam, winner);
}
Aggregations