SteamWar/FightSystem
Archiviert
13
1

Merge pull request 'Refactoring percent winconditions + WhitelistWincondition' (#296) from percentRefactor into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #296
Reviewed-by: YoyoNow <jwsteam@nidido.de>
Dieser Commit ist enthalten in:
Lixfel 2021-10-29 11:59:15 +02:00
Commit 5d504741ae
10 geänderte Dateien mit 206 neuen und 239 gelöschten Zeilen

Datei anzeigen

@ -26,6 +26,7 @@ import de.steamwar.sql.EventFight;
import de.steamwar.sql.Team; import de.steamwar.sql.Team;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -97,7 +98,7 @@ public class Config {
//win condition parameters //win condition parameters
public static final int TimeoutTime; public static final int TimeoutTime;
public static final double PercentWin; public static final double PercentWin;
public static final List<String> IgnoredBlocks; public static final Set<Material> PercentBlocks;
//default kits //default kits
public static final String MemberDefault; public static final String MemberDefault;
@ -188,7 +189,7 @@ public class Config {
TimeoutTime = config.getInt("WinConditionParams.TimeoutTime"); TimeoutTime = config.getInt("WinConditionParams.TimeoutTime");
PercentWin = config.getDouble("WinConditionParams.PercentWin"); PercentWin = config.getDouble("WinConditionParams.PercentWin");
IgnoredBlocks = Collections.unmodifiableList(config.getStringList("WinConditionParams.IgnoredBlocks")); PercentBlocks = config.getStringList("WinConditionParams.IgnoredBlocks").stream().map(Material::valueOf).collect(Collectors.toSet());
EnterStages = Collections.unmodifiableList(config.getIntegerList("EnterStages")); EnterStages = Collections.unmodifiableList(config.getIntegerList("EnterStages"));

Datei anzeigen

@ -97,7 +97,8 @@ public class FightSystem extends JavaPlugin {
new WinconditionPumpkinTechKO(); new WinconditionPumpkinTechKO();
new WinconditionWaterTechKO(); new WinconditionWaterTechKO();
new WinconditionPercentSystem(); new WinconditionPercentSystem();
new WinconditionRelativePercent(); new WinconditionBlacklistPercent();
new WinconditionWhitelistPercent();
new WinconditionPoints(); new WinconditionPoints();
new WinconditionTimeout(); new WinconditionTimeout();
new WinconditionHeartRatioTimeout(); new WinconditionHeartRatioTimeout();

Datei anzeigen

@ -20,6 +20,8 @@
package de.steamwar.fightsystem.states; package de.steamwar.fightsystem.states;
import de.steamwar.fightsystem.ArenaMode; import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.winconditions.Winconditions;
import java.util.Set; import java.util.Set;
@ -33,6 +35,12 @@ public class OneShotStateDependent extends StateDependent{
register(); register();
} }
public OneShotStateDependent(Winconditions wincondition, Set<FightState> states, Runnable runnable) {
super(Config.ActiveWinconditions.contains(wincondition), states);
this.runnable = runnable;
register();
}
@Override @Override
public void enable() { public void enable() {
runnable.run(); runnable.run();

Datei anzeigen

@ -19,8 +19,116 @@
package de.steamwar.fightsystem.winconditions; 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.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.OneShotStateDependent;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.utils.Message;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
public interface PercentWincondition { import java.util.HashMap;
double getPercent(FightTeam team); import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
public class PercentWincondition extends Wincondition implements PrintableWincondition {
private static final World world = Bukkit.getWorlds().get(0);
private final Map<FightTeam, TeamPercent> teamMap = new HashMap<>();
protected BooleanSupplier explosionFilter = () -> !Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft();
protected Predicate<Material> testBlock = type -> !Config.PercentBlocks.contains(type);
protected ToIntFunction<FightTeam> totalBlockCalc = team -> {
AtomicInteger blocks = new AtomicInteger();
team.getSchemRegion().forEach((x, y, z) -> {
if (testBlock.test(world.getBlockAt(x, y, z).getType())) {
blocks.getAndIncrement();
}
});
return blocks.get();
};
protected Consumer<FightTeam> checkWin = team -> {
if (getPercent(team) >= Config.PercentWin) {
win(Fight.getOpposite(team), "WIN_PERCENT", team.getColoredName());
}
};
protected Consumer<FightTeam> postEnable = team -> {};
public PercentWincondition(String windescription, Winconditions wincondition) {
super(windescription);
if (Config.ActiveWinconditions.contains(wincondition)) {
printableWinconditions.add(this);
percentWincondition = this;
}
new TeamPercent(Fight.getBlueTeam(), wincondition);
new TeamPercent(Fight.getRedTeam(), wincondition);
}
public Message getDisplay(FightTeam team) {
return new Message("BAR_PERCENT", team.getPrefix() + (Math.round(100.0 * getPercent(team)) / 100.0));
}
public double getPercent(FightTeam team) {
return teamMap.get(team).getPercent();
}
protected int getTotalBlocks(FightTeam team) {
return teamMap.get(team).totalBlocks;
}
private class TeamPercent implements Listener {
private final FightTeam team;
private int totalBlocks = 0;
private int currentBlocks = 0;
private TeamPercent(FightTeam team, Winconditions wincondition) {
this.team = team;
new OneShotStateDependent(wincondition, FightState.Running, this::enable);
new StateDependentListener(wincondition, FightState.Running, this).register();
teamMap.put(team, this);
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
if (explosionFilter.getAsBoolean() || !team.getExtendRegion().inRegion(event.getEntity().getLocation())) {
return;
}
event.blockList().forEach(block -> {
if (testBlock.test(block.getType())) {
currentBlocks--;
}
});
checkWin.accept(team);
}
private void enable() {
totalBlocks = totalBlockCalc.applyAsInt(team);
currentBlocks = totalBlocks;
postEnable.accept(team);
}
private double getPercent() {
if (currentBlocks >= totalBlocks) {
return 0;
}
return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks;
}
}
} }

Datei anzeigen

@ -0,0 +1,27 @@
/*
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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.fightsystem.winconditions;
public class WinconditionBlacklistPercent extends PercentWincondition {
public WinconditionBlacklistPercent(){
super("RelativePercent", Winconditions.RELATIVE_PERCENT);
}
}

Datei anzeigen

@ -19,81 +19,12 @@
package de.steamwar.fightsystem.winconditions; package de.steamwar.fightsystem.winconditions;
import de.steamwar.fightsystem.Config; public class WinconditionPercentSystem extends PercentWincondition {
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 de.steamwar.fightsystem.utils.Message;
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<FightTeam, TeamPercent> teamMap = new HashMap<>();
public WinconditionPercentSystem() { public WinconditionPercentSystem() {
super("Percent"); super("Percent", Winconditions.PERCENT_SYSTEM);
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
new StateDependentListener(Winconditions.PERCENT_SYSTEM, FightState.Running, this){ totalBlockCalc = team -> team.getSchemRegion().volume();
@Override explosionFilter = () -> false;
public void enable() {
super.enable();
teamMap.forEach((team, percent) -> {
percent.destroyedBlocks = 0;
percent.percent = 0;
});
}
};
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 Message getDisplay(FightTeam team) {
return new Message("BAR_PERCENT", team.getPrefix() + (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), "WIN_PERCENT", team.getColoredName());
}
}
} }
} }

Datei anzeigen

@ -24,7 +24,6 @@ import de.steamwar.fightsystem.countdown.TimeOverCountdown;
import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent;
import de.steamwar.fightsystem.states.StateDependentCountdown; import de.steamwar.fightsystem.states.StateDependentCountdown;
import de.steamwar.fightsystem.states.StateDependentListener; import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.utils.Message; import de.steamwar.fightsystem.utils.Message;
@ -37,35 +36,27 @@ import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class WinconditionPoints extends Wincondition implements PrintableWincondition, Listener { public class WinconditionPoints extends PercentWincondition implements Listener {
private final Map<FightTeam, TeamPoints> teamMap = new HashMap<>(); private final Map<FightTeam, TeamPoints> teamMap = new HashMap<>();
public WinconditionPoints(){ public WinconditionPoints(){
super("Points"); super("Points", Winconditions.POINTS);
checkWin = team -> {};
postEnable = this::pointInit;
teamMap.put(Fight.getBlueTeam(), new TeamPoints(Fight.getBlueTeam())); teamMap.put(Fight.getBlueTeam(), new TeamPoints(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPoints(Fight.getRedTeam())); teamMap.put(Fight.getRedTeam(), new TeamPoints(Fight.getRedTeam()));
new StateDependentListener(Winconditions.POINTS, FightState.Ingame, this); new StateDependentListener(Winconditions.POINTS, FightState.Ingame, this);
new StateDependent(Winconditions.POINTS, FightState.Ingame){
@Override
public void enable() {
teamMap.values().forEach(TeamPoints::enable);
}
@Override
public void disable() {
teamMap.values().forEach(TeamPoints::disable);
}
}.register();
if(Config.ActiveWinconditions.contains(Winconditions.POINTS)){ if(Config.ActiveWinconditions.contains(Winconditions.POINTS)){
timeOverCountdown = new StateDependentCountdown(Winconditions.POINTS, FightState.Running, new TimeOverCountdown(this::timeOver)); timeOverCountdown = new StateDependentCountdown(Winconditions.POINTS, FightState.Running, new TimeOverCountdown(this::timeOver));
printableWinconditions.add(this);
} }
} }
protected void timeOver() { private void timeOver() {
int redPoints = teamMap.get(Fight.getRedTeam()).getPoints(); int redPoints = teamMap.get(Fight.getRedTeam()).getPoints();
int bluePoints = teamMap.get(Fight.getBlueTeam()).getPoints(); int bluePoints = teamMap.get(Fight.getBlueTeam()).getPoints();
@ -104,6 +95,15 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond
} }
private void pointInit(FightTeam team) {
TeamPoints opponent = teamMap.get(Fight.getOpposite(team));
if(getTotalBlocks(team) == 0)
return;
teamMap.get(team).setup(getTotalBlocks(Fight.getOpposite(team)));
opponent.setup(getTotalBlocks(team));
}
@Override @Override
public Message getDisplay(FightTeam team) { public Message getDisplay(FightTeam team) {
return new Message("BAR_POINTS", team.getPrefix() + teamMap.get(team).getPoints()); return new Message("BAR_POINTS", team.getPrefix() + teamMap.get(team).getPoints());
@ -113,26 +113,19 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond
private static final int MAX_POINTS = 2000; private static final int MAX_POINTS = 2000;
private final FightTeam team; private final FightTeam team;
private final WinconditionRelativePercent.TeamPercent percent;
private double factor; private double factor;
private int points; private int points;
TeamPoints(FightTeam team){ TeamPoints(FightTeam team){
this.team = team; this.team = team;
this.percent = new WinconditionRelativePercent.TeamPercent(team);
} }
public void enable() { private void setup(int enemyBlocks){
this.points = 0; points = 0;
percent.enable(); if(enemyBlocks < getTotalBlocks(team)) {
int ownBlocks = percent.getBlockCount();
int enemyBlocks = teamMap.get(Fight.getOpposite(team)).percent.getBlockCount();
if(enemyBlocks < ownBlocks) {
this.factor = 100; //Original mit 20 (20% = 0.2 ergeben 2000 Punkte this.factor = 100; //Original mit 20 (20% = 0.2 ergeben 2000 Punkte
} else { } else {
double f = 100.0 * ownBlocks / enemyBlocks; double f = 100.0 * getTotalBlocks(team) / enemyBlocks;
if(f > 100) if(f > 100)
f = 100; f = 100;
@ -143,12 +136,8 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond
} }
} }
public void disable(){
percent.disable();
}
public int getPoints(){ public int getPoints(){
int damagePoints = (int)(teamMap.get(Fight.getOpposite(team)).percent.getPercent() * factor); int damagePoints = (int)(getPercent(Fight.getOpposite(team)) * factor);
if(damagePoints > MAX_POINTS) if(damagePoints > MAX_POINTS)
damagePoints = MAX_POINTS; damagePoints = MAX_POINTS;
return points + damagePoints; return points + damagePoints;

Datei anzeigen

@ -1,130 +0,0 @@
/*
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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.fightsystem.winconditions;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent;
import de.steamwar.fightsystem.utils.Message;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitTask;
import java.util.*;
public class WinconditionRelativePercent extends Wincondition implements PrintableWincondition, PercentWincondition {
private static final World world = Bukkit.getWorlds().get(0);
private static final Set<Material> ignoredBlocks;
static{
Set<Material> ignored = new HashSet<>();
for(String s : Config.IgnoredBlocks)
ignored.add(Material.valueOf(s));
ignoredBlocks = Collections.unmodifiableSet(ignored);
}
private final Map<FightTeam, TeamPercent> teamMap = new HashMap<>();
public WinconditionRelativePercent(){
super("RelativePercent");
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
if(Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)){
printableWinconditions.add(this);
percentWincondition = this;
}
}
@Override
public double getPercent(FightTeam team) {
return teamMap.get(team).getPercent();
}
@Override
public Message getDisplay(FightTeam team) {
return new Message("BAR_PERCENT", team.getPrefix() + (Math.round(100.0 * getPercent(team)) / 100.0));
}
public static class TeamPercent extends StateDependent {
private final FightTeam team;
private int blockCount;
private BukkitTask task;
private int currentBlocks;
public TeamPercent(FightTeam team){
super(Winconditions.RELATIVE_PERCENT, FightState.Running);
this.team = team;
this.currentBlocks = 1;
register();
}
@Override
public void enable(){
blockCount = currentBlocks();
task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400);
}
@Override
public void disable(){
task.cancel();
}
private void check(){
currentBlocks();
if(!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT))
return;
if(getPercent() >= Config.PercentWin){
FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent", "WIN_PERCENT", team.getColoredName());
}
}
public double getPercent(){
if(currentBlocks > blockCount)
return 0;
return (blockCount - currentBlocks) * 100 / (double) blockCount;
}
public int getBlockCount(){
return blockCount;
}
private int currentBlocks(){
// Entern active
if(!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft())
return currentBlocks;
currentBlocks = 0;
team.getSchemRegion().forEach((x, y, z) -> {
if(!ignoredBlocks.contains(world.getBlockAt(x,y,z).getType()))
currentBlocks++;
});
return currentBlocks;
}
}
}

Datei anzeigen

@ -0,0 +1,31 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.fightsystem.winconditions;
import de.steamwar.fightsystem.Config;
public class WinconditionWhitelistPercent extends PercentWincondition {
public WinconditionWhitelistPercent() {
super("WhitelistPercent", Winconditions.WHITELIST_PERCENT);
testBlock = Config.PercentBlocks::contains;
}
}

Datei anzeigen

@ -28,6 +28,7 @@ public enum Winconditions {
CAPTAIN_DEAD, CAPTAIN_DEAD,
PERCENT_SYSTEM, PERCENT_SYSTEM,
WHITELIST_PERCENT,
RELATIVE_PERCENT, RELATIVE_PERCENT,
POINTS, POINTS,