Skip to content

Commit

Permalink
Reservations are now shown in event details discord message
Browse files Browse the repository at this point in the history
  • Loading branch information
Alf-Melmac committed Feb 7, 2022
1 parent 463785a commit 72cc227
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
13 changes: 3 additions & 10 deletions src/main/java/de/webalf/slotbot/model/Slot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonBackReference;
import de.webalf.slotbot.exception.BusinessRuntimeException;
import de.webalf.slotbot.util.SlotUtils;
import lombok.*;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -96,18 +97,10 @@ public Guild getEffectiveReservedFor() {
}

/**
* Same as {@link #getEffectiveReservedFor()} but doesn't return {@link #reservedFor} if the whole squad is reserved
* for the {@link Guild} the squad is reserved for
*
* @see #getEffectiveReservedFor()
* @see SlotUtils#getEffectiveReservedForDisplay(Guild, Squad)
*/
public Guild getEffectiveReservedForDisplay() {
final Guild effectiveReservedFor = getEffectiveReservedFor();
if (getSquad().getReservedFor() != null && getSquad().getReservedFor().equals(effectiveReservedFor) &&
getSquad().getSlotList().stream().allMatch(slot -> effectiveReservedFor.equals(slot.getEffectiveReservedFor()))) {
return null;
}
return effectiveReservedFor;
return SlotUtils.getEffectiveReservedForDisplay(getReservedFor(), getSquad());
}

// Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import de.webalf.slotbot.model.dtos.GuildDto;
import de.webalf.slotbot.model.dtos.UserDto;
import de.webalf.slotbot.util.LongUtils;
import de.webalf.slotbot.util.SlotUtils;
import de.webalf.slotbot.util.bot.MentionUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;

import javax.validation.constraints.Size;
import java.util.List;

import static de.webalf.slotbot.util.MaxLength.TEXT;

Expand Down Expand Up @@ -39,14 +41,15 @@ public class SlotReferencelessDto extends AbstractIdEntityDto {
*
* @param guildId in which the slotlist will be printed
* @param squadReservedFor fallback if slot is not reserved
* @param slotList of the squad containing this slot
* @return slot in discord message format
*/
StringBuilder toSlotList(long guildId, GuildDto squadReservedFor) {
StringBuilder toSlotList(long guildId, GuildDto squadReservedFor, List<? extends SlotReferencelessDto> slotList) {
StringBuilder slotText = new StringBuilder();

boolean notReservedForOthers;
if (reservedFor != null) { //Slot is reserved
notReservedForOthers = Long.toString(guildId).equals(reservedFor.getId());
notReservedForOthers = Long.toString(guildId).equals(reservedFor.getId());
} else { //Use reservedFor of Squad
notReservedForOthers = squadReservedFor == null || Long.toString(guildId).equals(squadReservedFor.getId());
}
Expand All @@ -61,6 +64,11 @@ StringBuilder toSlotList(long guildId, GuildDto squadReservedFor) {
slotText.append("**");
}

final GuildDto reservedForDisplay = SlotUtils.getEffectiveReservedForDisplay(reservedFor, squadReservedFor, slotList);
if (reservedForDisplay != null) {
slotText.append(" [").append(reservedForDisplay.getGroupIdentifier()).append("]");
}

slotText.append(":");

final boolean isBlocked = !isEmpty && LongUtils.parseLong(getUser().getId()) == User.DEFAULT_USER_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public class SquadReferencelessDto extends AbstractIdEntityDto {
* @return squad in discord message format
*/
public StringBuilder toSlotList(long guildId) {
StringBuilder squadText = new StringBuilder("**").append(getName()).append("**");
StringBuilder squadText = new StringBuilder("**").append(getName());
if (reservedFor != null) {
squadText.append(" [").append(reservedFor.getGroupIdentifier()).append("]");
}
squadText.append("**");

for (SlotReferencelessDto slot : getSlotList()) {
squadText.append("\n").append(slot.toSlotList(guildId, reservedFor));
squadText.append("\n").append(slot.toSlotList(guildId, reservedFor, slotList));
}
return squadText;
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/de/webalf/slotbot/util/SlotUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.webalf.slotbot.util;

import de.webalf.slotbot.model.Guild;
import de.webalf.slotbot.model.Squad;
import de.webalf.slotbot.model.dtos.GuildDto;
import de.webalf.slotbot.model.dtos.referenceless.SlotReferencelessDto;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
* @author Alf
* @since 07.02.2022
*/
@UtilityClass
public final class SlotUtils {
/**
* Evaluates the reservation for a slot. Slot reservation or if empty the squad reservation.
* Doesn't return {@code reservedFor} if the whole squad is reserved for the {@link Guild} the squad is reserved for
*
* @param reservedFor reservation of slot
* @param squad the slot is in
* @return effective reservation for display
* @see #getEffectiveReservedForDisplay(GuildDto, GuildDto, List)
*/
public Guild getEffectiveReservedForDisplay(Guild reservedFor, @NonNull Squad squad) {
final Guild effectiveReservedFor = reservedFor != null ? reservedFor : squad.getReservedFor();
if (squad.getReservedFor() != null && squad.getReservedFor().equals(effectiveReservedFor) &&
squad.getSlotList().stream().allMatch(slot -> effectiveReservedFor.equals(slot.getEffectiveReservedFor()))) {
return null;
}
return effectiveReservedFor;
}

/**
* Evaluates the reservation for a slot. Slot reservation or if empty the squad reservation.
* Doesn't return {@code squadReservedFor} if the whole squad is reserved for the {@link GuildDto} the squad is reserved for
*
* @param reservedFor reservation of slot
* @param squadReservedFor reservation of slots squad
* @param slotList of slots squad
* @return effective reservation for display
* @see #getEffectiveReservedForDisplay(Guild, Squad)
*/
public GuildDto getEffectiveReservedForDisplay(GuildDto reservedFor, GuildDto squadReservedFor, List<? extends SlotReferencelessDto> slotList) {
final GuildDto effectiveReservedFor = reservedFor != null ? reservedFor : squadReservedFor;
if (squadReservedFor != null && squadReservedFor.equals(effectiveReservedFor) &&
slotList.stream().allMatch(slot -> effectiveReservedFor.equals(slot.getReservedFor() != null ? slot.getReservedFor() : squadReservedFor))) {
return null;
}
return effectiveReservedFor;
}
}

0 comments on commit 72cc227

Please sign in to comment.