diff --git a/pom.xml b/pom.xml index 7786d4a..aab63e0 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ com.github.the-codeboy Mensa4J - 3d8b1ea50f + 15b65b41eb ch.qos.logback diff --git a/src/main/java/com/the_codeboy/mensabot/MensaUtil.java b/src/main/java/com/the_codeboy/mensabot/MensaUtil.java index 92f4ec1..303f4fd 100644 --- a/src/main/java/com/the_codeboy/mensabot/MensaUtil.java +++ b/src/main/java/com/the_codeboy/mensabot/MensaUtil.java @@ -16,6 +16,9 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.text.NumberFormat; +import java.time.DayOfWeek; +import java.time.format.TextStyle; +import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; @@ -36,21 +39,29 @@ public static EmbedBuilder MealsToEmbed(Mensa mensa, Date date) { return builder; } builder.setTitle("Meals in " + mensa.getName()); - builder.setDescription(""); -// builder.setDescription(DayOfWeek.of(date.getDay() == 0 ? 7 : date.getDay()).getDisplayName(TextStyle.FULL, Locale.GERMANY) + ", " + Util.dateToString(date)); + if (mensa.hasOpeningHours()) { + float openTime = mensa.getOpeningTime(date), + closeTime = mensa.getClosingTime(date); + Date openDate = dateAtTime(date, openTime), + closeDate = dateAtTime(date, closeTime); + + builder.setDescription(" - "); + } else { + builder.setDescription(DayOfWeek.of(date.getDay() == 0 ? 7 : date.getDay()).getDisplayName(TextStyle.FULL, Locale.GERMANY) + ", " + Util.dateToString(date)); + } boolean beilagen = false; for (Meal meal : mensa.getMeals(date)) { String title = getTitleString(meal); String description = meal.getCategory() + - (meal.getPrices().getStudents() != null ? "\n" + toPrice(meal.getPrices().getStudents()) - + (meal.getPrices().getOthers() != null ? " (" + toPrice(meal.getPrices().getOthers()) + ")" : "") : ""); + (meal.getPrices().getStudents() != null ? "\n" + toPrice(meal.getPrices().getStudents()) + + (meal.getPrices().getOthers() != null ? " (" + toPrice(meal.getPrices().getOthers()) + ")" : "") : ""); if (!beilagen - && (meal.getCategory().equalsIgnoreCase("Hauptbeilagen") || meal.getCategory().equalsIgnoreCase("Nebenbeilage"))) { + && (meal.getCategory().equalsIgnoreCase("Hauptbeilagen") || meal.getCategory().equalsIgnoreCase("Nebenbeilage"))) { beilagen = true; builder.addBlankField(false); } - boolean inline = true;//!(meal.getCategory().equalsIgnoreCase("Hauptbeilagen") || meal.getCategory().equalsIgnoreCase("Nebenbeilage")); + boolean inline = true; builder.addField(title, description, inline); } @@ -58,6 +69,21 @@ public static EmbedBuilder MealsToEmbed(Mensa mensa, Date date) { return builder; } + private static Date dateAtTime(Date date, float time) { + int hourPart = (int) time; + int minutePart = (int) ((time - hourPart) * 60); + + // Use Calendar to set the time + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); // Set the date to the calendar + calendar.set(Calendar.HOUR_OF_DAY, hourPart); + calendar.set(Calendar.MINUTE, minutePart); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + + return calendar.getTime(); + } + public static String getTitleString(Meal meal) { String title = getEmojiForMeal(meal); title += " " + meal.getName(); @@ -250,8 +276,8 @@ public static BufferedImage generateMealsImage(Mensa mensa, Date date) { drawString(g, meal.getName(), rectangle); String description = meal.getCategory() + - (meal.getPrices().getStudents() != null ? "\n " + toPrice(meal.getPrices().getStudents()) - + (meal.getPrices().getOthers() != null ? " (" + toPrice(meal.getPrices().getOthers()) + ")" : "") : ""); + (meal.getPrices().getStudents() != null ? "\n " + toPrice(meal.getPrices().getStudents()) + + (meal.getPrices().getOthers() != null ? " (" + toPrice(meal.getPrices().getOthers()) + ")" : "") : ""); rectangle.y += size;