Skip to content

Commit

Permalink
Merge pull request #2 from spacechunks/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
NicoND1 authored Jul 22, 2024
2 parents d9eac80 + c8f7372 commit 4696ae8
Show file tree
Hide file tree
Showing 80 changed files with 1,868 additions and 373 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ tasks.named("shadowJar", ShadowJar::class) {
mergeServiceFiles()
archiveFileName.set("${project.name}.jar")
manifest {
attributes["Main-Class"] = "space.chunks.gamecup.template.minestom.GameCupExampleMinestomServer"
attributes["Main-Class"] = "space.chunks.gamecup.dgr.launcher.GameLauncher"
}
}
5 changes: 3 additions & 2 deletions src/main/java/space/chunks/gamecup/dgr/GameFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import space.chunks.gamecup.dgr.map.Map;
import space.chunks.gamecup.dgr.map.object.registry.MapObjectRegistry;
import space.chunks.gamecup.dgr.map.object.impl.flight.Flight;
import space.chunks.gamecup.dgr.map.object.impl.procedure.incident.TroubleMaker;
import space.chunks.gamecup.dgr.map.object.registry.MapObjectRegistry;
import space.chunks.gamecup.dgr.passenger.Passenger;
import space.chunks.gamecup.dgr.passenger.PassengerConfig;
import space.chunks.gamecup.dgr.passenger.queue.PassengerQueue;
Expand All @@ -30,7 +31,7 @@ public interface GameFactory {
TroubleMaker createTroubleMaker(@NotNull Map parent);

@NotNull
Passenger createPassenger(@NotNull PassengerConfig config);
Passenger createPassenger(@NotNull Flight flight, @NotNull Map map, @NotNull PassengerConfig config);

@NotNull
PassengerQueue createPassengerQueue(@NotNull PassengerQueueConfig config);
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/space/chunks/gamecup/dgr/GameImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
@Accessors(fluent=true)
@Log4j2
public final class GameImpl implements Game {
private final GameTickTask tickTask;
private final GameGoal goal;
private final PhaseHandler phases;
private final List<Map> maps;
private final List<Team> teams;

@Inject
public GameImpl(@NotNull PhaseHandler phases, @NotNull GameConfig config, @NotNull GameFactory factory, @NotNull Provider<Team> teamProvider) {
public GameImpl(@NotNull GameTickTask tickTask, @NotNull PhaseHandler phases, @NotNull GameConfig config, @NotNull GameFactory factory, @NotNull Provider<Team> teamProvider) {
this.tickTask = tickTask;
this.goal = new FixedHappyPassengersGameGoal(this, 500, new int[]{100, 200, 300, 400, 450});
this.phases = phases;

Expand Down Expand Up @@ -72,6 +74,7 @@ public void launch() {
log.info("Launching game, entering waiting phase");

this.phases.enterPhase(WaitingPhase.class);
MinecraftServer.getSchedulerManager().scheduleTask(this.tickTask, TaskSchedule.tick(1), TaskSchedule.tick(1));
}

@Override
Expand All @@ -82,12 +85,6 @@ public void end(@Nullable Team winnerTeam, @NotNull EndReason reason) {
}
}

@Inject
public void registerTickTask(@NotNull GameTickTask task) {
MinecraftServer.getSchedulerManager().scheduleTask(task, TaskSchedule.seconds(5), TaskSchedule.tick(1));
log.info("Scheduled tick task");
}

@Inject
public void registerCommands(@NotNull Set<Command> commands) {
for (Command command : commands) {
Expand All @@ -98,6 +95,13 @@ public void registerCommands(@NotNull Set<Command> commands) {

@Override
public void tick(int currentTick) {
this.phases.tick(currentTick);
try {
this.phases.tick(currentTick);
for (Team team : this.teams) {
team.tick(currentTick);
}
} catch (Throwable throwable) {
log.error("Error during game tick @ {}", currentTick, throwable);
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/space/chunks/gamecup/dgr/GameModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import space.chunks.gamecup.dgr.map.object.impl.procedure.incident.TroubleMakerImpl;
import space.chunks.gamecup.dgr.map.object.registry.MapObjectRegistry;
import space.chunks.gamecup.dgr.map.object.registry.MapObjectRegistryImpl;
import space.chunks.gamecup.dgr.minestom.actionbar.ActionBarHelper;
import space.chunks.gamecup.dgr.minestom.npc.NPCEntityDebugCommand;
import space.chunks.gamecup.dgr.passenger.Passenger;
import space.chunks.gamecup.dgr.passenger.PassengerImpl;
import space.chunks.gamecup.dgr.passenger.SpawnPassengersCommand;
import space.chunks.gamecup.dgr.passenger.queue.PassengerQueue;
import space.chunks.gamecup.dgr.passenger.queue.PassengerQueueImpl;
import space.chunks.gamecup.dgr.phase.ActiveGamePhase;
import space.chunks.gamecup.dgr.phase.EndingPhase;
import space.chunks.gamecup.dgr.phase.Phase;
import space.chunks.gamecup.dgr.phase.ShoppingPhase;
import space.chunks.gamecup.dgr.phase.WaitingPhase;
Expand Down Expand Up @@ -53,17 +54,19 @@ protected void configure() {
phaseBinder.addBinding().to(WaitingPhase.class);
phaseBinder.addBinding().to(ActiveGamePhase.class);
phaseBinder.addBinding().to(ShoppingPhase.class);
phaseBinder.addBinding().to(EndingPhase.class);

bind(PhaseHandler.class).to(PhaseHandlerImpl.class).asEagerSingleton();
bind(GameConfig.class).toInstance(GameConfig.defaultConfig());

Multibinder<Command> commandsBinder = Multibinder.newSetBinder(binder(), Command.class);
commandsBinder.addBinding().to(EnterPhaseCommand.class);
commandsBinder.addBinding().to(SpawnPassengersCommand.class);
commandsBinder.addBinding().to(NPCEntityDebugCommand.class);
commandsBinder.addBinding().to(ForceFlightCommand.class);
commandsBinder.addBinding().to(ProcedureDebugCommand.class);

bind(ActionBarHelper.class).asEagerSingleton();

bind(Game.class).to(GameImpl.class).asEagerSingleton();
bind(GameTickTask.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import space.chunks.gamecup.dgr.team.Team;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -41,9 +42,9 @@ public FixedHappyPassengersGameGoal(@NotNull Game game, int happyPassengers, int

@Override
public @NotNull Component bossBar(@NotNull Team team) {
return Component.text(team.passengersMoved()).color(NamedTextColor.YELLOW)
return Component.text(team.passengersMoved()).color(NamedTextColor.GREEN)
.append(Component.text("/").color(NamedTextColor.GRAY))
.append(Component.text(this.happyPassengers).color(NamedTextColor.GOLD))
.append(Component.text(this.happyPassengers).color(NamedTextColor.DARK_GREEN))
.append(Component.text(" Passengers").color(NamedTextColor.GRAY));
}

Expand All @@ -65,7 +66,7 @@ public boolean testReached(@NotNull Team team) {

@Override
public double progress(@NotNull Team team) {
return (double) team.passengersMoved() / this.happyPassengers;
return Math.min((double) team.passengersMoved() / this.happyPassengers, 1.0D);
}

@Override
Expand All @@ -77,23 +78,23 @@ public void tick(int currentTick) {
}

Boolean[] goalSentStates = this.alertTicksSentPerMap.computeIfAbsent(team, t -> new Boolean[this.alertTicks.length]);

int goalDiff = this.happyPassengers-team.passengersMoved();

for (int i = 0; i < this.alertTicks.length; i++) {
int alertTick = this.alertTicks[i];
if (alertTick == goalDiff) {
if (alertTick == team.passengersMoved()) {
Boolean goalSentState = goalSentStates[i];

if (goalSentState == null || !goalSentState) {
team.map().executeForMembers(member -> Component.text("A Team").color(TextColor.color(team.color()))
.append(Component.text(" has reached ").color(NamedTextColor.GRAY)
.append(Component.text(team.passengersMoved()).color(NamedTextColor.YELLOW))
.append(Component.text("/").color(NamedTextColor.GRAY))
.append(Component.text(this.happyPassengers).color(NamedTextColor.GOLD))
.append(Component.text(" happy passengers!").color(NamedTextColor.GRAY))
));
goalSentStates[alertTick] = true;
System.out.println("A Team has reached "+team.passengersMoved()+"/"+this.happyPassengers+" happy passengers!\nStates: " +Arrays.toString(goalSentStates) + " with " + i + "=" + alertTick);
for (Team t : this.game.teams()) {
t.audience().sendMessage(Component.text("A Team").color(TextColor.color(team.color()))
.append(Component.text(" has reached ").color(NamedTextColor.GRAY)
.append(Component.text(team.passengersMoved()).color(NamedTextColor.YELLOW))
.append(Component.text("/").color(NamedTextColor.GRAY))
.append(Component.text(this.happyPassengers).color(NamedTextColor.GOLD))
.append(Component.text(" happy passengers!").color(NamedTextColor.GRAY))
));
}
goalSentStates[i] = true;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/space/chunks/gamecup/dgr/goal/GameGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import space.chunks.gamecup.dgr.Ticking;
import space.chunks.gamecup.dgr.team.Team;


/**
* @author Nico_ND1
*/
public interface GameGoal {
public interface GameGoal extends Ticking {
@NotNull
Component name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minestom.server.extras.MojangAuth;
import space.chunks.gamecup.dgr.Game;
import space.chunks.gamecup.dgr.GameModule;
import space.chunks.gamecup.dgr.launcher.profiler.ApplicationProfiler;
import space.chunks.gamecup.dgr.launcher.profiler.ProfilerModule;


/**
Expand All @@ -16,9 +18,13 @@ public static void main(String[] args) {
launchServer();

Injector injector = Guice.createInjector(
new GameModule()
new GameModule(),
new ProfilerModule()
);

ApplicationProfiler applicationProfiler = injector.getInstance(ApplicationProfiler.class);
applicationProfiler.register();

Game game = injector.getInstance(Game.class);
game.launch();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package space.chunks.gamecup.dgr.launcher.profiler;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import lombok.extern.log4j.Log4j2;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.EventListener;
import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.server.ServerTickMonitorEvent;
import net.minestom.server.monitoring.BenchmarkManager;
import net.minestom.server.utils.time.TimeUnit;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;


/**
* @author Nico_ND1
*/
@Log4j2
public final class ApplicationProfiler {
private final ProfilerRepository profilerRepository;
private final Profiler<ServerTickMonitorEvent> minecraftServerProfiler;
private final ApplicationProfilerCommand command;

@Inject
public ApplicationProfiler(
@NotNull ProfilerRepository profilerRepository,
@NotNull @Named("minecraftServer") Profiler minecraftServerProfiler,
@NotNull ApplicationProfilerCommand command
) {
this.profilerRepository = profilerRepository;
this.minecraftServerProfiler = minecraftServerProfiler;
this.command = command;
}

public void register() {
/*
// TODO: use to persist dirty data
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> {
});*/

log.info("Registering application profiler");

BenchmarkManager benchmarkManager = MinecraftServer.getBenchmarkManager();
benchmarkManager.enable(Duration.of(10, TimeUnit.SECOND));
log.info("Enabled benchmark manager with a duration of 10 seconds");

GlobalEventHandler eventHandler = MinecraftServer.getGlobalEventHandler();
eventHandler.addListener(EventListener.of(ServerTickMonitorEvent.class, this.minecraftServerProfiler::feed));

eventHandler.addListener(EventListener.of(InstanceTickEvent.class, event -> {
for (SessionProfiler profiler : this.profilerRepository.sessionProfilers()) {
profiler.feed(event);
}
}));

log.info("Registered event listeners for application profiler");

MinecraftServer.getCommandManager().register(this.command);
log.info("Registered command for application profiler");
}

}
Loading

0 comments on commit 4696ae8

Please sign in to comment.