Skip to content

Commit

Permalink
Display opening times on menu
Browse files Browse the repository at this point in the history
  • Loading branch information
the-codeboy committed Jun 17, 2024
1 parent 1137fa7 commit d889c29
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>com.github.the-codeboy</groupId>
<artifactId>Mensa4J</artifactId>
<version>3d8b1ea50f</version>
<version>15b65b41eb</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
42 changes: 34 additions & 8 deletions src/main/java/com/the_codeboy/mensabot/MensaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,28 +39,51 @@ public static EmbedBuilder MealsToEmbed(Mensa mensa, Date date) {
return builder;
}
builder.setTitle("Meals in " + mensa.getName());
builder.setDescription("<t:" + date.getTime() / 1000 + ":R>");
// 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("<t:" + openDate.getTime() / 1000 + ":R> - <t:" + closeDate.getTime() / 1000 + ":R>");
} 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);
}

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();
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit d889c29

Please sign in to comment.