use of com.ebicep.warlords.game.option.marker.PointPredicterMarker in project Warlords by ebicep.
the class WinAfterTimeoutOption method register.
@Override
public void register(Game game) {
new TimerSkipAbleMarker() {
@Override
public int getDelay() {
return timeRemaining * 20;
}
@Override
public void skipTimer(int delay) {
timeRemaining -= delay / 20;
}
}.register(game);
game.registerGameMarker(ScoreboardHandler.class, scoreboard = new SimpleScoreboardHandler(SCOREBOARD_PRIORITY, "timeout") {
@Override
public List<String> computeLines(@Nullable WarlordsPlayer player) {
final EnumSet<Team> teams = TeamMarker.getTeams(game);
Team winner = null;
if (teams.size() > 1) {
List<PointPredicterMarker> predictionMarkers = game.getMarkers(PointPredicterMarker.class);
int scoreNeededToEndGame = game.getOptions().stream().filter(e -> e instanceof WinByPointsOption).mapToInt(e -> ((WinByPointsOption) e).getPointLimit()).sorted().findFirst().orElse(Integer.MAX_VALUE);
int highestScore = Integer.MIN_VALUE;
int highestWinInSeconds = Integer.MAX_VALUE;
for (Team team : teams) {
int points = game.getPoints(team);
int winInSeconds;
if (predictionMarkers.isEmpty()) {
winInSeconds = Integer.MAX_VALUE;
} else {
double pointsPerMinute = predictionMarkers.stream().mapToDouble(e -> e.predictPointsNextMinute(team)).sum();
int pointsRemaining = scoreNeededToEndGame - points;
int winInSecondsCalculated = pointsPerMinute <= 0 ? Integer.MAX_VALUE : (int) (pointsRemaining / pointsPerMinute * 60);
int pointsAfterTimeIsOver = (int) (points + timeRemaining * pointsPerMinute / 60);
if (winInSecondsCalculated >= 0 && winInSecondsCalculated < timeRemaining) {
// This teamis going to win before the timer is over
winInSeconds = winInSecondsCalculated;
points = scoreNeededToEndGame;
} else {
winInSeconds = timeRemaining;
points = pointsAfterTimeIsOver;
}
}
if (points > highestScore) {
highestScore = points;
highestWinInSeconds = winInSeconds;
winner = team;
} else if (points == highestScore) {
if (winInSeconds < highestWinInSeconds) {
highestWinInSeconds = winInSeconds;
winner = team;
} else if (winInSeconds == highestWinInSeconds) {
winner = null;
}
}
}
}
StringBuilder message = new StringBuilder(64);
if (winner != null) {
message.append(winner.coloredPrefix()).append(ChatColor.GOLD).append(" Wins in: ");
} else {
message.append(ChatColor.WHITE).append("Time Left: ");
}
message.append(ChatColor.GREEN);
Utils.formatTimeLeft(message, timeRemaining);
return Collections.singletonList(message.toString());
}
});
}
Aggregations