From dcb106f0aa626a49e399934203dd1a73d608fec5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 3 Aug 2024 17:09:58 +0200 Subject: [PATCH] Remove duplications --- .../velocitycore/commands/EventCommand.java | 24 +++++++----- .../commands/EventRescheduleCommand.java | 38 +++---------------- 2 files changed, 20 insertions(+), 42 deletions(-) diff --git a/src/de/steamwar/velocitycore/commands/EventCommand.java b/src/de/steamwar/velocitycore/commands/EventCommand.java index dee1a8d..b64343f 100644 --- a/src/de/steamwar/velocitycore/commands/EventCommand.java +++ b/src/de/steamwar/velocitycore/commands/EventCommand.java @@ -34,6 +34,7 @@ import java.sql.Timestamp; import java.time.Instant; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; public class EventCommand extends SWCommand { @@ -117,8 +118,7 @@ public class EventCommand extends SWCommand { } } - @ClassMapper(Team.class) - public TypeMapper eventTeams() { + protected static TypeMapper eventTeam(Function> teamMapper) { return new TypeMapper<>() { @Override public Team map(Chatter sender, PreviousArguments previousArguments, String s) { @@ -134,16 +134,22 @@ public class EventCommand extends SWCommand { Event currentEvent = Event.get(); for (EventFight fight : EventFight.getEvent(currentEvent.getEventID())) { if (fight.hasFinished()) continue; - if (fight.getStartTime().after(new Timestamp(System.currentTimeMillis()))) continue; - Team red = Team.get(fight.getTeamRed()); - teams.add(red.getTeamName()); - teams.add(red.getTeamKuerzel()); - Team blue = Team.get(fight.getTeamBlue()); - teams.add(blue.getTeamName()); - teams.add(blue.getTeamKuerzel()); + teamMapper.apply(fight).forEach(integer -> { + Team team = Team.get(integer); + teams.add(team.getTeamName()); + teams.add(team.getTeamKuerzel()); + }); } return teams; } }; } + + @ClassMapper(Team.class) + public TypeMapper eventTeams() { + return eventTeam(eventFight -> { + if (eventFight.getStartTime().after(new Timestamp(System.currentTimeMillis()))) return Collections.emptyList(); + return List.of(eventFight.getTeamBlue(), eventFight.getTeamRed()); + }); + } } diff --git a/src/de/steamwar/velocitycore/commands/EventRescheduleCommand.java b/src/de/steamwar/velocitycore/commands/EventRescheduleCommand.java index ad2a3da..5e1ad2e 100644 --- a/src/de/steamwar/velocitycore/commands/EventRescheduleCommand.java +++ b/src/de/steamwar/velocitycore/commands/EventRescheduleCommand.java @@ -19,7 +19,6 @@ package de.steamwar.velocitycore.commands; -import de.steamwar.command.PreviousArguments; import de.steamwar.command.SWCommand; import de.steamwar.command.TypeMapper; import de.steamwar.messages.Chatter; @@ -29,8 +28,9 @@ import de.steamwar.sql.Team; import de.steamwar.sql.UserPerm; import java.sql.Timestamp; -import java.util.*; -import java.util.function.Function; +import java.util.Date; +import java.util.List; +import java.util.ListIterator; public class EventRescheduleCommand extends SWCommand { @@ -65,41 +65,13 @@ public class EventRescheduleCommand extends SWCommand { sender.system("EVENTRESCHEDULE_NO_FIGHT"); } - private TypeMapper eventTeam(Function teamMapper) { - return new TypeMapper<>() { - @Override - public Team map(Chatter sender, PreviousArguments previousArguments, String s) { - if ("PUB".equalsIgnoreCase(s) || "public".equalsIgnoreCase(s)) { - return Team.get(0); - } - return Team.get(s); - } - - @Override - public Collection tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) { - Event currentEvent = Event.get(); - if(currentEvent == null){ - return null; - } - Set teams = new HashSet<>(); - for (EventFight fight : EventFight.getEvent(currentEvent.getEventID())) { - if (fight.hasFinished()) continue; - Team team = Team.get(teamMapper.apply(fight)); - teams.add(team.getTeamName()); - teams.add(team.getTeamKuerzel()); - } - return teams; - } - }; - } - @Mapper("BLUE_TEAM") public TypeMapper eventBlueTeams() { - return eventTeam(EventFight::getTeamBlue); + return EventCommand.eventTeam(eventFight -> List.of(eventFight.getTeamBlue())); } @Mapper("RED_TEAM") public TypeMapper eventRedTeams() { - return eventTeam(EventFight::getTeamRed); + return EventCommand.eventTeam(eventFight -> List.of(eventFight.getTeamRed())); } }