Skip to content

Commit

Permalink
maintain all not synchronized ranks when the ranks get updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Chickenpowerrr committed Apr 21, 2019
1 parent 1fc90cc commit 5a7bf1f
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@ public interface RankFactory<R> {
* Returns the Bot that uses this factory
*/
Bot<?, R> getBot();

/**
* Adds a RankHelper to validate all ranks
*
* @param rankHelper the helper that validates if the given ranks exist
*/
void addRankHelper(RankHelper rankHelper);

/**
* Returns if the given rank is a valid platform rank according to the RankHelpers
*
* @param rank the rank that could be synchronized
* @return if the given rank is a valid platform rank according to the RankHelpers
*/
boolean isValidRank(Rank rank);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.gmail.chickenpowerrr.ranksync.api.rank;

import com.gmail.chickenpowerrr.ranksync.api.bot.Bot;

/**
* This interface contains the methods needed to validate all given ranks
*
* @author Chickenpowerrr
* @since 1.2.1
*/
public interface RankHelper {

/**
* Returns if the given rank has been synchronized over the platforms
*
* @param bot the bot that could synchronize the rank
* @param rank the rank that could be synchronized
* @return if the given rank has been synchronized over the platforms
*/
boolean isSynchronized(Bot bot, Rank rank);

/**
* Returns a Rank based on the Bot that is able to give to users and the name of the service's
* Rank
*
* @param bot the running Bot
* @param serviceGroupName the name of the Minecraft Rank
* @return a Rank based on the Bot that is able to give to users and the name of the Rank
*/
Rank getRank(Bot bot, String serviceGroupName);

/**
* Validates all cached ranks and if they don't exist in the given Bot or service, a message will
* be send to the console
*/
void validateRanks();
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public void setRanks(Collection<Rank> ranks) {
Collection<Role> roles = this.member.getRoles();

Collection<Role> toRemove = new HashSet<>(roles);
toRemove.removeIf(role -> ranks.contains(this.rankFactory.getRankFromRole(role)));
toRemove.removeIf(role -> {
Rank rank = this.rankFactory.getRankFromRole(role);
return ranks.contains(rank) || !this.rankFactory.isValidRank(rank);
});

Collection<Role> toAdd = this.rankFactory.getRolesFromRanks(ranks);
toAdd.removeIf(roles::contains);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import com.gmail.chickenpowerrr.ranksync.api.bot.Bot;
import com.gmail.chickenpowerrr.ranksync.api.rank.Rank;
import lombok.Getter;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Role;

import com.gmail.chickenpowerrr.ranksync.api.rank.RankHelper;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Getter;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Role;

/**
* This class contains the functionalities to get a representation of a Discord role
Expand All @@ -25,7 +26,9 @@ public class RankFactory implements com.gmail.chickenpowerrr.ranksync.api.rank.R
private final Map<Role, Rank> ranks = new HashMap<>();
private final Map<String, Role> roles = new HashMap<>();
private final Guild guild;
@Getter private final Bot<?, Role> bot;
@Getter
private final Bot<?, Role> bot;
private final Collection<RankHelper> rankHelpers = new HashSet<>();

/**
* @param bot the Bot that is running
Expand Down Expand Up @@ -150,4 +153,26 @@ public Role getRoleFromRank(Rank rank) {
public Collection<Role> getRolesFromRanks(Collection<Rank> ranks) {
return ranks.stream().map(this::getRoleFromRank).collect(Collectors.toSet());
}

/**
* Adds a RankHelper to validate all ranks
*
* @param rankHelper the helper that validates if the given ranks exist
*/
@Override
public void addRankHelper(RankHelper rankHelper) {
this.rankHelpers.add(rankHelper);
}

/**
* Returns if the given rank is a valid platform rank according to the RankHelpers
*
* @param rank the rank that could be synchronized
* @return if the given rank is a valid platform rank according to the RankHelpers
*/
@Override
public boolean isValidRank(Rank rank) {
return this.rankHelpers.stream()
.anyMatch(rankHelper -> rankHelper.isSynchronized(this.bot, rank));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public Class<BotEnabledEvent> getTarget() {
}

/**
* Validates the ranks used by this Bot
* Validates the ranks used by this Bot and adds the validator to the RankFactory
*
* @param event the event that triggered the method
*/
@Override
public void onEvent(BotEnabledEvent event) {
this.rankHelper.validateRanks();
event.getBot().getRankFactory().addRankHelper(this.rankHelper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @since 1.0.0
*/
@Slf4j
public class RankHelper {
public class RankHelper implements com.gmail.chickenpowerrr.ranksync.api.rank.RankHelper {

private static final Logger LOGGER = Logger.getLogger(RankHelper.class.getSimpleName());

Expand All @@ -29,6 +29,19 @@ public RankHelper(Map<String, Map<Bot<?, ?>, String>> ranks) {
this.ranks = ranks;
}

/**
* Returns if the given rank has been synchronized over the platforms
*
* @param bot the bot that could synchronize the rank
* @param rank the rank that could be synchronized
* @return if the given rank has been synchronized over the platforms
*/
@Override
public boolean isSynchronized(Bot bot, Rank rank) {
return this.ranks.values().stream()
.anyMatch((map) -> map.containsKey(bot) && map.get(bot).equalsIgnoreCase(rank.getName()));
}

/**
* Returns a Rank based on the Bot that is able to give to users and the name of the Minecraft
* Rank
Expand All @@ -38,7 +51,7 @@ public RankHelper(Map<String, Map<Bot<?, ?>, String>> ranks) {
* @return a Rank based on the Bot that is able to give to users and the name of the Rank
*/
@SuppressWarnings("unchecked")
Rank getRank(Bot bot, String minecraftGroupName) {
public Rank getRank(Bot bot, String minecraftGroupName) {
if (this.ranks.containsKey(minecraftGroupName) && this.ranks.get(minecraftGroupName)
.containsKey(bot)) {
return bot.getRankFactory().getRankFromRole(
Expand All @@ -49,9 +62,10 @@ Rank getRank(Bot bot, String minecraftGroupName) {
}

/**
* Validates all cached and if they don't exist in the given Bot or Minecraft server, a message
* will be send to the console
* Validates all cached ranks and if they don't exist in the given Bot or Minecraft server, a
* message will be send to the console
*/
@Override
public void validateRanks() {
this.ranks.forEach((minecraftRank, syncedRanks) -> {
AtomicBoolean minecraftChecked = new AtomicBoolean(false);
Expand Down

0 comments on commit 5a7bf1f

Please sign in to comment.