Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
3.0.3
Browse files Browse the repository at this point in the history
- Bumped version to 3.0.3
- Added simple test for TopicalSort
- Added cache for Rank all permissions calculation
- Added rank cache invalidation
- Restructered main help command. Instead of showing "Name: syntax" it now shows "syntax description"
  • Loading branch information
Timmi6790 committed Jul 19, 2020
1 parent 6027b65 commit 510dd92
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.timmi6790.statsbotdiscord</groupId>
<artifactId>StatsBotDiscord</artifactId>
<version>3.0.2</version>
<version>3.0.3</version>

<packaging>jar
</packaging>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/timmi6790/statsbotdiscord/StatsBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.io.File;

public class StatsBot {
public static final String BOT_VERSION = "3.0.2";
public static final String BOT_VERSION = "3.0.3";
@Getter
private static ModuleManager moduleManager;
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Pre(final AbstractCommand command, final CommandParameters parameters) {
}
}

@EqualsAndHashCode(callSuper = true)
public static class Post extends CommandExecutionEvent {
@Getter
private final CommandResult commandResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public AbstractModule(final String name) {
this.name = name;
}

public abstract void onEnable();

public abstract void onDisable();

protected final void addDependencies(final Class<? extends AbstractModule>... dependencies) {
this.dependencies.addAll(Arrays.asList(dependencies));
}
Expand All @@ -34,8 +38,4 @@ protected final void addLoadAfter(final Class<? extends AbstractModule>... loadA
protected final void addLoadBefore(final Class<? extends AbstractModule>... loadBefore) {
this.loadBefore.addAll(Arrays.asList(loadBefore));
}

public abstract void onEnable();

public abstract void onDisable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public CommandParameters(final CommandParameters commandParameters) {
System.arraycopy(commandParameters.args, 0, this.args, 0, this.args.length);
}

public boolean isFromGuild() {
return this.event.isFromGuild();
}

public GuildDb getServer() {
return this.channelDb.getGuildDb();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import de.timmi6790.statsbotdiscord.modules.core.stats.SuccessfulCommandStat;
import de.timmi6790.statsbotdiscord.modules.setting.SettingModule;
import de.timmi6790.statsbotdiscord.modules.stat.StatModule;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@EqualsAndHashCode(callSuper = true)
public class CoreModule extends AbstractModule {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ protected CommandResult onCommand(final CommandParameters commandParameters) {
value.stream()
.sorted(Comparator.comparing(AbstractCommand::getName))
.map(command -> {
final String capitalizedName = command.getName().substring(0, 1).toUpperCase() + command.getName().substring(1);
final String syntax = command.getSyntax().length() == 0 ? "" : " " + command.getSyntax();

return capitalizedName + ": " + MarkdownUtil.monospace(mainCommand + command.getName() + syntax);
return MarkdownUtil.monospace(mainCommand + command.getName() + syntax) + " " + command.getDescription();
})
.collect(Collectors.joining("\n")),
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import de.timmi6790.statsbotdiscord.modules.rank.RankManager;
import net.dv8tion.jda.api.utils.MarkdownUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RankCommand extends AbstractCommand {
private static final List<String> VALID_1_ARGS = new ArrayList<>(Arrays.asList("perms", "extend", "rename", "create", "delete", "info"));
private static final List<String> PERMS_2_ARGS = new ArrayList<>(Arrays.asList("add", "remove"));
private static final List<String> EXTEND_2_ARGS = new ArrayList<>(Arrays.asList("add", "remove"));
private static final List<String> VALID_1_ARGS = Arrays.asList("perms", "extend", "rename", "create", "delete", "info");
private static final List<String> PERMS_2_ARGS = Arrays.asList("add", "remove");
private static final List<String> EXTEND_2_ARGS = Arrays.asList("add", "remove");

public RankCommand() {
// TODO: Add a better command system, to support more complex commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import de.timmi6790.statsbotdiscord.modules.mineplexstats.MineplexStatsModule;
import net.dv8tion.jda.api.utils.MarkdownUtil;

import java.util.Arrays;
import java.util.List;

public class ReloadDataCommand extends AbstractCommand {
private static final List<String> VALID_0_ARGS = Arrays.asList("javaGame", "javaGroup", "bedrockGame");

public ReloadDataCommand() {
super("sReload", "Debug", "", "[data]", "sr");

Expand All @@ -18,38 +23,29 @@ public ReloadDataCommand() {
@Override
protected CommandResult onCommand(final CommandParameters commandParameters) {
final MineplexStatsModule module = StatsBot.getModuleManager().getModule(MineplexStatsModule.class).orElseThrow(RuntimeException::new);
final String arg0 = this.getFromListIgnoreCase(commandParameters, 0, VALID_0_ARGS);

final String firstArg = commandParameters.getArgs()[0];
switch (firstArg.toLowerCase()) {
case "javagame":
switch (arg0) {
case "javaGame":
module.loadJavaGames();
break;

case "javagroup":
case "javaGroup":
module.loadJavaGroups();
break;

case "bedrockgame":
case "bedrockGame":
module.loadBedrockGames();
break;

default:
this.sendTimedMessage(
commandParameters,
this.getEmbedBuilder(commandParameters)
.setTitle("Incorrect data type")
.setDescription(MarkdownUtil.monospace(firstArg) + " is not a valid type.\n" +
"[javaGame, javaGroup, bedrockGame]"),
90
);
return CommandResult.INVALID_ARGS;
return CommandResult.ERROR;
}

this.sendTimedMessage(
commandParameters,
this.getEmbedBuilder(commandParameters)
.setTitle("Reloaded data")
.setDescription("Reloaded " + MarkdownUtil.monospace(firstArg)),
.setDescription("Reloaded " + MarkdownUtil.monospace(arg0)),
90
);
return CommandResult.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class JavaPlayerStatsRatioCommand extends AbstractJavaStatsCommand {
public JavaPlayerStatsRatioCommand() {
super("playerstats", "Player stats", "<player> <stat> [board]", "pls", "plsats", "plstat");
super("playerstats", "Player stats as graph", "<player> <stat> [board]", "pls", "plsats", "plstat");

this.setCategory("PROTOTYPE - MineplexStats - Java");
this.setDefaultPerms(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PicturePie extends AbstractPicture {
private final double total;

public PicturePie(final Slice[] slices) {
this.slices = slices;
this.slices = slices.clone();
this.total = Arrays.stream(this.slices).mapToDouble(Slice::getValue).sum();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public PictureTable(final String[] header, final String date, final String[][] l
}

public PictureTable(final String[] header, final String date, final String[][] leaderboard, final BufferedImage skin) {
this.header = header;
this.leaderboard = leaderboard;
this.header = header.clone();
this.leaderboard = leaderboard.clone();
this.date = date;

this.widthHeader = new int[this.header.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class JavaGame {

public JavaGame(final String name, final String[] aliasNames, final String category, final String wikiUrl, final String description, final Map<String, JavaStat> stats) {
this.name = name;
this.aliasNames = aliasNames;
this.aliasNames = aliasNames.clone();
this.category = category;
this.wikiUrl = wikiUrl;
this.description = description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JavaStat {

public JavaStat(final String name, final String[] aliasNames, final boolean achievement, final String description, final Map<String, JavaBoard> boards) {
this.name = name;
this.aliasNames = aliasNames;
this.aliasNames = aliasNames.clone();
this.achievement = achievement;
this.description = description;
this.boards = boards;
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/de/timmi6790/statsbotdiscord/modules/rank/Rank.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public class Rank {
private String name;
private final Set<Integer> extendedRanks;
private final Set<Integer> permissions;
private Set<Integer> cachedAllPermissions;

public Rank(final int databaseId, final String name, final Set<Integer> extendedRanks, final Set<Integer> permissions) {
this.databaseId = databaseId;
this.name = name;
this.extendedRanks = extendedRanks;
this.permissions = permissions;
}

public void invalidateCachedPermissions() {
this.cachedAllPermissions = null;
}

// Permissions
public boolean hasPermission(final int permissionId) {
Expand All @@ -53,6 +65,8 @@ public boolean addPermission(final int permissionId) {
);
this.permissions.add(permissionId);

StatsBot.getRankManager().invalidateAllPermCaches();

return true;
}

Expand All @@ -69,13 +83,17 @@ public boolean removePermission(final int permissionId) {
);
this.permissions.remove(permissionId);

StatsBot.getRankManager().invalidateAllPermCaches();

return true;
}

public Set<Integer> getAllPermissions() {
// TODO: Cache the result
final Set<Integer> foundPermissions = new HashSet<>(this.permissions);
if (this.cachedAllPermissions != null) {
return this.cachedAllPermissions;
}

final Set<Integer> foundPermissions = new HashSet<>(this.permissions);
final RankManager rankManager = StatsBot.getRankManager();
final MemoryDeque<Integer> ranksQueue = new MemoryDeque<>();
ranksQueue.addAll(this.extendedRanks);
Expand All @@ -87,6 +105,7 @@ public Set<Integer> getAllPermissions() {
});
}

this.cachedAllPermissions = foundPermissions;
return foundPermissions;
}

Expand All @@ -111,6 +130,9 @@ public boolean addExtendedRank(final int rankId) {
.execute()
);
this.extendedRanks.add(rankId);

StatsBot.getRankManager().invalidateAllPermCaches();

return true;
}

Expand All @@ -130,6 +152,9 @@ public boolean removeExtendedRank(final int rankId) {
.execute()
);
this.extendedRanks.remove(rankId);

StatsBot.getRankManager().invalidateAllPermCaches();

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ public void loadRanksFromDatabase() {
rankList.forEach(this::addRank);
}

public void invalidateAllPermCaches() {
this.rankMap.values().forEach(Rank::invalidateCachedPermissions);
}

public void addRank(final Rank rank) {
this.rankMappingMap.put(rank.getDatabaseId(), rank.getName());
this.rankMap.put(rank.getDatabaseId(), rank);
this.invalidateAllPermCaches();
}

public boolean hasRank(final int id) {
Expand Down Expand Up @@ -114,6 +119,8 @@ public boolean deleteRank(final int rankId) {
this.rankMap.remove(rankId);
this.rankMappingMap.remove(rankId);

this.invalidateAllPermCaches();

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* Allow owners to disable and/or limit this in their server
* Even if limited, add bypas via discord admin perms
*/
public class TimedCommandsManager {
public class TimedCommandsModule {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.timmi6790.statsbotdiscord.utilities.sorting;

import de.timmi6790.statsbotdiscord.datatypes.ListBuilder;
import de.timmi6790.statsbotdiscord.exceptions.TopicalSortCycleException;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TopicalSortTest {
@Test
void simplePathSort() throws TopicalSortCycleException {
final List<Integer> values = Arrays.asList(10, 2, 5, 1, 4, 3, 8, 6, 7, 9);
final List<TopicalSort.Dependency> dependencies = new ListBuilder<TopicalSort.Dependency>(ArrayList::new)
.add(new TopicalSort.Dependency(0, 9))
.add(new TopicalSort.Dependency(9, 6))
.add(new TopicalSort.Dependency(6, 8))
.add(new TopicalSort.Dependency(8, 7))
.add(new TopicalSort.Dependency(7, 2))
.add(new TopicalSort.Dependency(2, 4))
.add(new TopicalSort.Dependency(4, 5))
.add(new TopicalSort.Dependency(5, 1))
.add(new TopicalSort.Dependency(1, 3))
.build();

final TopicalSort<Integer> topicalSort = new TopicalSort<>(values, dependencies);
final List<Integer> sortedList = topicalSort.sort();

final List<Integer> controlList = IntStream.range(1, 11)
.boxed()
.collect(Collectors.toList());
assertThat(sortedList).isEqualTo(controlList);
}

@Test
void simpleLoopDetectionCheck() {
final List<Integer> values = Arrays.asList(1, 2);
final List<TopicalSort.Dependency> dependencies = new ListBuilder<TopicalSort.Dependency>(ArrayList::new)
.add(new TopicalSort.Dependency(0, 1))
.add(new TopicalSort.Dependency(1, 0))
.build();

final TopicalSort<Integer> topicalSort = new TopicalSort<>(values, dependencies);
assertThrows(TopicalSortCycleException.class, topicalSort::sort);
}
}

0 comments on commit 510dd92

Please sign in to comment.