/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package de.steamwar.fightsystem.winconditions;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.HashMap;
import java.util.Map;
public class WinconditionPercentSystem extends Wincondition implements Listener, PrintableWincondition, PercentWincondition {
private final Map teamMap = new HashMap<>();
public WinconditionPercentSystem() {
super("Percent", "§cTeam ", " §chat den Gegnern");
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
new StateDependentListener(Winconditions.PERCENT_SYSTEM, FightState.Running, this);
if(Config.ActiveWinconditions.contains(Winconditions.PERCENT_SYSTEM)){
printableWinconditions.add(this);
percentWincondition = this;
}
}
@EventHandler
public void handleEntityExplode(EntityExplodeEvent event) {
teamMap.values().forEach(teamPercent -> teamPercent.check(event));
}
@Override
public String getDisplay(FightTeam team) {
return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%";
}
@Override
public double getPercent(FightTeam team) {
return teamMap.get(team).percent;
}
private class TeamPercent {
private final FightTeam team;
private final int volume;
private double percent;
private int destroyedBlocks;
private TeamPercent(FightTeam team) {
this.team = team;
this.volume = team.getSchemRegion().volume();
}
private void check(EntityExplodeEvent event) {
if(!team.getExtendRegion().inRegion(event.getEntity().getLocation())){
return;
}
destroyedBlocks += event.blockList().size();
percent = (double)destroyedBlocks * 100 / volume;
if(percent >= Config.PercentWin) {
win(Fight.getOpposite(team));
}
}
}
}