Refactor schematic checks to handle unseen notifications

This commit is contained in:
2025-03-24 16:50:39 +01:00
parent 0da3ebfbcc
commit 71522973a7
4 changed files with 62 additions and 21 deletions
@@ -85,12 +85,15 @@ public interface Chatter {
else
return withPlayer.apply(player);
}
default void withPlayerOrOffline(Consumer<Player> withPlayer, Runnable withOffline) {
default boolean withPlayerOrOffline(Consumer<Player> withPlayer, Runnable withOffline) {
Player player = getPlayer();
if(player == null)
if(player == null) {
withOffline.run();
else
return false;
} else {
withPlayer.accept(player);
return true;
}
}
default void withPlayer(Consumer<Player> function) {
withPlayerOrOffline(function, () -> {});
@@ -41,6 +41,7 @@ import java.time.Instant;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
public class CheckCommand extends SWCommand {
@@ -237,25 +238,29 @@ public class CheckCommand extends SWCommand {
}
private void accept(){
if(concludeCheckSession("freigegeben", fightTypes.get(schematic.getSchemtype()))) {
concludeCheckSession("freigegeben", fightTypes.get(schematic.getSchemtype()), () -> {
Chatter owner = Chatter.of(SteamwarUser.get(schematic.getOwner()).getUUID());
owner.withPlayerOrOffline(
boolean isOnline = owner.withPlayerOrOffline(
player -> owner.system("CHECK_ACCEPTED", schematic.getSchemtype().name(), schematic.getName()),
() -> DiscordAlert.send(owner, Color.GREEN, new Message("DC_TITLE_SCHEMINFO"), new Message("DC_SCHEM_ACCEPT", schematic.getName()), true)
);
notifyTeam(new Message("CHECK_ACCEPTED_TEAM", schematic.getName(), owner.user().getUserName()));
}
return isOnline;
});
}
private void decline(String reason){
if(concludeCheckSession(reason, SchematicType.Normal)) {
concludeCheckSession(reason, SchematicType.Normal, () -> {
Chatter owner = Chatter.of(SteamwarUser.get(schematic.getOwner()).getUUID());
owner.withPlayerOrOffline(
boolean isOnline = owner.withPlayerOrOffline(
player -> owner.system("CHECK_DECLINED", schematic.getSchemtype().name(), schematic.getName(), reason),
() -> DiscordAlert.send(owner, Color.RED, new Message("DC_TITLE_SCHEMINFO"), new Message("DC_SCHEM_DECLINE", schematic.getName(), reason), false)
);
notifyTeam(new Message("CHECK_DECLINED_TEAM", schematic.getName(), owner.user().getUserName(), reason));
}
return isOnline;
});
}
private void notifyTeam(Message message) {
@@ -264,14 +269,12 @@ public class CheckCommand extends SWCommand {
}
private void abort(){
concludeCheckSession("Prüfvorgang abgebrochen", null);
concludeCheckSession("Prüfvorgang abgebrochen", null, () -> true);
}
private boolean concludeCheckSession(String reason, SchematicType type) {
boolean exists = SchematicNode.getSchematicNode(schematic.getId()) != null;
if(exists) {
CheckedSchematic.create(schematic, checker.user().getId(), startTime, Timestamp.from(Instant.now()), reason);
private void concludeCheckSession(String reason, SchematicType type, Supplier<Boolean> sendMessageIsOnline) {
if(SchematicNode.getSchematicNode(schematic.getId()) != null) {
CheckedSchematic.create(schematic, checker.user().getId(), startTime, Timestamp.from(Instant.now()), reason, sendMessageIsOnline.get());
if(type != null)
schematic.setSchemtype(type);
}
@@ -282,7 +285,6 @@ public class CheckCommand extends SWCommand {
if(subserver != null)
subserver.stop();
}).schedule();
return exists;
}
private void remove() {
@@ -28,6 +28,8 @@ import com.velocitypowered.api.proxy.Player;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import de.steamwar.persistent.Subserver;
import de.steamwar.sql.CheckedSchematic;
import de.steamwar.sql.SchematicType;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserPerm;
import de.steamwar.velocitycore.commands.*;
@@ -82,6 +84,16 @@ public class ConnectionListener extends BasicListener {
}
}
for (CheckedSchematic checkedSchematic : CheckedSchematic.getUnseen(user)) {
SchematicType type = SchematicType.fromDB(checkedSchematic.getNodeType());
if(type == null) continue;
if (checkedSchematic.getDeclineReason().equals("freigegeben")) {
chatter.system("CHECK_ACCEPTED", type.name(), checkedSchematic.getSchemName());
} else {
chatter.system("CHECK_DECLINED", type.name(), checkedSchematic.getSchemName(), checkedSchematic.getDeclineReason());
}
}
if(newPlayers.contains(player.getUniqueId())){
Chatter.broadcast().system("JOIN_FIRST", player);
newPlayers.remove(player.getUniqueId());