Give score based difficulty to events

This commit is contained in:
Xander 2024-09-15 21:23:00 +02:00
parent 4835fac6a7
commit 1f3dfa2d34
No known key found for this signature in database
GPG key ID: 79979C7BA303E003
8 changed files with 139 additions and 44 deletions

View file

@ -2,6 +2,7 @@ package gent.zeus.mc13dtl;
import gent.zeus.mc13dtl.group.GroupAddCommand; import gent.zeus.mc13dtl.group.GroupAddCommand;
import gent.zeus.mc13dtl.group.GroupRemoveCommand; import gent.zeus.mc13dtl.group.GroupRemoveCommand;
import gent.zeus.mc13dtl.group.SetScoreCommand;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@ -28,12 +29,13 @@ public class MC13DTL extends JavaPlugin {
commands.register("start-game", "Start a game of 13 days to live", new StartGameCommand()); commands.register("start-game", "Start a game of 13 days to live", new StartGameCommand());
commands.register("toggle-pause", "Toggle paused state", new TogglePauseCommand()); commands.register("toggle-pause", "Toggle paused state", new TogglePauseCommand());
commands.register("group-add", "Add player to team", new GroupAddCommand()); commands.register("group-add", "Add player to team", new GroupAddCommand());
commands.register("set-score", "Set the score of a team", new SetScoreCommand());
commands.register("group-remove", "Remove player from team", new GroupRemoveCommand()); commands.register("group-remove", "Remove player from team", new GroupRemoveCommand());
}); });
BukkitScheduler scheduler = this.getServer().getScheduler(); BukkitScheduler scheduler = this.getServer().getScheduler();
scheduler.runTaskTimer(this, new EventRunner(), 0, 100);
scheduler.runTask(this, () -> { scheduler.runTask(this, () -> {
board = new TeamScoreBoard(); board = new TeamScoreBoard();
}); });
scheduler.runTaskTimer(this, new EventRunner(), 0, 100);
} }
} }

View file

@ -12,8 +12,8 @@ import java.util.Set;
public class TeamScoreBoard { public class TeamScoreBoard {
private ScoreboardManager sm; private final ScoreboardManager sm;
private Scoreboard scoreboard; private final Scoreboard scoreboard;
private Objective objective; private Objective objective;

View file

@ -0,0 +1,10 @@
package gent.zeus.mc13dtl.events;
public enum Difficulty {
BABY,
EASY,
MEDIUM,
HARD,
SWEAT,
IMPOSSIBLE
}

View file

@ -11,11 +11,15 @@ import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
public abstract class Event implements Listener { public abstract class Event implements Listener {
protected Team team; protected Team team;
protected final Difficulty difficulty;
public Event(Team team) { public Event(Team team, Difficulty difficulty) {
this.team = team; this.team = team;
this.difficulty = difficulty;
MC13DTL.board.sendMessageToTeam(team, Component.text("Difficulty is " + this.difficulty.toString()));
MC13DTL.board.sendMessageToTeam(team, Component.text(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC)); MC13DTL.board.sendMessageToTeam(team, Component.text(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC));
MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance); MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance);
EventRunner.currentEvents.add(this); EventRunner.currentEvents.add(this);

View file

@ -5,8 +5,12 @@ import org.bukkit.NamespacedKey;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.OptionalDouble;
import java.util.Set;
import java.util.stream.Collectors;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
import gent.zeus.mc13dtl.events.catalog.TestEvent; import gent.zeus.mc13dtl.events.catalog.TestEvent;
@ -15,24 +19,64 @@ import gent.zeus.mc13dtl.events.catalog.TestEvent2;
public class EventRunner implements Runnable { public class EventRunner implements Runnable {
static List<Event> currentEvents = new ArrayList<>(); static List<Event> currentEvents = new ArrayList<>();
private long day;
@Override @Override
public void run() { public void run() {
World world = Bukkit.getWorld("world"); World world = Bukkit.getWorld("world");
NamespacedKey eventKey = new NamespacedKey(MC13DTL.instance, "last-event-day"); NamespacedKey eventKey = new NamespacedKey(MC13DTL.instance, "last-event-day");
long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG); long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG);
long day = Bukkit.getWorld("world").getFullTime() / 24000; day = Bukkit.getWorld("world").getFullTime() / 24000;
if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) { if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) {
while (!currentEvents.isEmpty()) { while (!currentEvents.isEmpty()) {
Event event = currentEvents.get(0); Event event = currentEvents.getFirst();
event.eventFailed(); event.eventFailed();
} }
for (Team i : MC13DTL.board.getTeams()) {
List<Team> teams = MC13DTL.board.getTeams().stream().toList();
List<Difficulty> difficulties = getDifficulties(teams);
for (int i = 0; i < teams.size() && i < difficulties.size(); i++) {
Team team = teams.get(i);
Difficulty difficulty = difficulties.get(i);
switch ((int) day) { switch ((int) day) {
case 0 -> new TestEvent(i); case 0 -> new TestEvent(team, difficulty);
case 1 -> new TestEvent2(i); case 1 -> new TestEvent2(team, difficulty);
} }
} }
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day); world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day);
} }
} }
private long getDayScore() {
return this.day * 10 + 1;
}
private List<Difficulty> getDifficulties(List<Team> teams) {
List<Integer> scores = teams.stream().map(team -> MC13DTL.board.getScore(team)).toList();
OptionalDouble averageOpt = scores.stream().mapToDouble(a -> a).average();
Long average = Math.round(averageOpt.isPresent() ? averageOpt.getAsDouble() : 0);
List<Difficulty> difficulties = new ArrayList<>();
// average is mapped to Difficulty.Medium
for (Integer score : scores) {
long distance = (score - average) / getDayScore();
if (distance <= -3) {
difficulties.add(Difficulty.BABY);
} else if (distance <= -1) {
difficulties.add(Difficulty.EASY);
} else if (distance == 0) {
difficulties.add(Difficulty.MEDIUM);
} else if (distance <= 4) {
difficulties.add(Difficulty.SWEAT);
} else {
difficulties.add(Difficulty.IMPOSSIBLE);
}
}
return difficulties;
}
} }

View file

@ -1,5 +1,6 @@
package gent.zeus.mc13dtl.events.catalog; package gent.zeus.mc13dtl.events.catalog;
import gent.zeus.mc13dtl.events.Difficulty;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
@ -7,8 +8,8 @@ import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.events.Event; import gent.zeus.mc13dtl.events.Event;
public class TestEvent extends Event { public class TestEvent extends Event {
public TestEvent(Team team) { public TestEvent(Team team, Difficulty difficulty) {
super(team); super(team, difficulty);
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package gent.zeus.mc13dtl.events.catalog; package gent.zeus.mc13dtl.events.catalog;
import gent.zeus.mc13dtl.events.Difficulty;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
@ -7,8 +8,8 @@ import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.events.Event; import gent.zeus.mc13dtl.events.Event;
public class TestEvent2 extends Event { public class TestEvent2 extends Event {
public TestEvent2(Team team) { public TestEvent2(Team team, Difficulty difficulty) {
super(team); super(team, difficulty);
} }
@Override @Override

View file

@ -0,0 +1,33 @@
package gent.zeus.mc13dtl.group;
import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
import org.jetbrains.annotations.NotNull;
public class SetScoreCommand implements BasicCommand {
@Override
public void execute(@NotNull CommandSourceStack commandSourceStack, @NotNull String[] args) {
try {
if (args.length == 2) {
int index = Integer.parseInt(args[0]);
int score = Integer.parseInt(args[1]);
Team team = MC13DTL.board.getTeam(index);
MC13DTL.board.setScore(team, score);
} else {
commandSourceStack.getSender().sendMessage("usage: /group-add <player> <index>");
}
} catch (NumberFormatException ex) {
commandSourceStack.getSender().sendMessage("usage: /group-add <player> <NUMBER>");
}
}
@Override
public String permission() {
return "13dtl.manage-groups";
}
}