Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 3cd8fce

Browse files
author
CodeDoctorDE
committed
add banner, add help to categories, add leaderboard
1 parent b48e2ae commit 3cd8fce

File tree

20 files changed

+164
-52
lines changed

20 files changed

+164
-52
lines changed

assets/banner.png

24.4 KB
Loading

src/main/java/com/github/codedoctorde/linwood/Linwood.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class Linwood {
3535
private final BaseCommand baseCommand;
3636
private static Linwood instance;
3737
private final DatabaseUtil database;
38-
private final SingleApplicationManager singleApplicationManager;
38+
private final SingleApplicationManager gameManager;
39+
private final SingleApplicationManager audioManager;
3940
private MainConfig config;
4041
private final KarmaListener userListener = new KarmaListener();
4142
private final File configFile = new File("./config.json");
@@ -59,7 +60,8 @@ public Linwood(String token){
5960
.addEventListeners(new ConnectionListener());
6061
activityChanger = new ActivityChanger();
6162
baseCommand = new BaseCommand();
62-
singleApplicationManager = new SingleApplicationManager();
63+
gameManager = new SingleApplicationManager();
64+
audioManager = new SingleApplicationManager();
6365

6466
// Read config file
6567
if(!configFile.exists()){
@@ -88,7 +90,7 @@ public Linwood(String token){
8890
}
8991

9092
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
91-
getSingleApplicationManager().clearGames();
93+
getGameManager().clearGames();
9294
logger.info("Shutting down...");
9395
}));
9496
configure();
@@ -132,8 +134,12 @@ public MainConfig getConfig() {
132134
return config;
133135
}
134136

135-
public SingleApplicationManager getSingleApplicationManager() {
136-
return singleApplicationManager;
137+
public SingleApplicationManager getGameManager() {
138+
return gameManager;
139+
}
140+
141+
public SingleApplicationManager getAudioManager() {
142+
return audioManager;
137143
}
138144

139145
public WebInterface getWebInterface() {

src/main/java/com/github/codedoctorde/linwood/apps/single/game/mode/whatisit/WhatIsIt.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.text.MessageFormat;
1313
import java.util.*;
14+
import java.util.stream.Collectors;
1415

1516
/**
1617
* @author CodeDoctorDE
@@ -141,7 +142,7 @@ public void finishGame(){
141142
@Override
142143
public void run() {
143144
stopTimer();
144-
Linwood.getInstance().getSingleApplicationManager().stopGame(game);
145+
Linwood.getInstance().getGameManager().stopGame(game);
145146
}
146147
}, 30 * 1000);
147148
}
@@ -174,28 +175,23 @@ public void sendLeaderboard(Session session){
174175

175176
public void sendLeaderboard(Session session, TextChannel textChannel){
176177
var bundle = getBundle(session);
177-
sendLeaderboard(0, "", " ", bundle, textChannel);
178+
sendLeaderboard(bundle, textChannel);
178179
}
179180

180-
private void sendLeaderboard(int index, String description, String message, ResourceBundle bundle){
181-
sendLeaderboard(index, description, message, bundle, getTextChannel());
182-
}
183-
184-
private void sendLeaderboard(int index, String description, String message, ResourceBundle bundle, TextChannel textChannel){
181+
private void sendLeaderboard(ResourceBundle bundle, TextChannel textChannel){
185182
var leaderboard = getLeaderboard();
186183
if(textChannel == null)
187184
return;
188-
if(index >= leaderboard.size())
189-
textChannel.sendMessage(message).embed(new EmbedBuilder().setTitle(bundle.getString("LeaderboardHeader")).setDescription(description).setFooter(bundle.getString("LeaderboardFooter")).build()).queue();
190-
else{
191-
Linwood.getInstance().getJda().retrieveUserById(leaderboard.get(index).getKey()).queue(user -> {
192-
var entry = leaderboard.get(index);
193-
String newDescription = description;
194-
if(user != null) newDescription += (MessageFormat.format(bundle.getString("Leaderboard"), index + 1,
195-
user.getAsMention(), entry.getValue()));
196-
sendLeaderboard(index +1, newDescription, message, bundle);
185+
textChannel.getGuild().retrieveMembersByIds(leaderboard.stream().map(Map.Entry::getKey).collect(Collectors.toList())).onSuccess(members -> {
186+
StringBuilder stringBuilder = new StringBuilder();
187+
for (int i = 0; i < members.size(); i++) {
188+
var member = members.get(i);
189+
if (member != null)
190+
stringBuilder.append(MessageFormat.format(bundle.getString("Leaderboard"), i + 1,
191+
member.getAsMention(), leaderboard.get(i).getValue()));
192+
}
193+
textChannel.sendMessage(new EmbedBuilder().setTitle(bundle.getString("LeaderboardHeader")).setDescription(stringBuilder.toString()).setFooter(bundle.getString("LeaderboardFooter")).build()).queue();
197194
});
198-
}
199195
}
200196

201197
private ArrayList<Map.Entry<Long, Integer>> getLeaderboard() {

src/main/java/com/github/codedoctorde/linwood/commands/BaseCommand.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
public class BaseCommand extends CommandManager {
2020
private final InfoCommand infoCommand = new InfoCommand();
21+
private final HelpCommand helpCommand = new HelpCommand();
2122
@Override
2223
public Command[] commands() {
2324
return new Command[]{
@@ -26,7 +27,7 @@ public Command[] commands() {
2627
new FunCommand(),
2728
new GameCommand(),
2829
new SettingsCommand(),
29-
new InfoCommand()
30+
infoCommand
3031
};
3132
}
3233

@@ -43,4 +44,8 @@ public Command[] commands() {
4344
public void runInfo(Session session, GuildEntity entity, Message message) {
4445
infoCommand.onCommand(session, message, entity, "", new String[0]);
4546
}
47+
48+
public HelpCommand getHelpCommand() {
49+
return helpCommand;
50+
}
4651
}

src/main/java/com/github/codedoctorde/linwood/commands/CommandManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public boolean onCommand(Session session, Message message, GuildEntity entity, S
3333
message.getChannel().sendMessage(baseBundle.getString("NoPermission")).queue();
3434
return true;
3535
}
36-
if(args.length <= 0)message.getChannel().sendMessage(bundle.containsKey("Description")?bundle.getString("Description"): Objects.requireNonNull(getBundle(entity)).getString("Syntax")).queue();
36+
if(args.length <= 0)Linwood.getInstance().getBaseCommand().getHelpCommand().sendHelp(entity, this, message.getTextChannel());
3737
else
3838
return false;
3939
return true;
@@ -49,6 +49,9 @@ public Command getCommand(GuildEntity entity, String... args){
4949
if (current.aliases(entity).contains(arg.toLowerCase())) command = current;
5050
return command;
5151
}
52+
public Command getCommand(Class<? extends Command> commandClass){
53+
return Arrays.stream(commands()).filter(command -> command.getClass().equals(commandClass)).findFirst().orElse(null);
54+
}
5255
private ResourceBundle getBaseBundle(GuildEntity entity){
5356
return ResourceBundle.getBundle("locale.Command", entity.getLocalization());
5457
}

src/main/java/com/github/codedoctorde/linwood/commands/HelpCommand.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import net.dv8tion.jda.api.EmbedBuilder;
66
import net.dv8tion.jda.api.MessageBuilder;
77
import net.dv8tion.jda.api.entities.Message;
8+
import net.dv8tion.jda.api.entities.TextChannel;
89
import org.hibernate.Session;
910
import org.jetbrains.annotations.NotNull;
1011

1112
import java.awt.*;
13+
import java.nio.channels.Channel;
1214
import java.time.LocalDateTime;
1315
import java.util.Arrays;
1416
import java.util.HashSet;
@@ -25,7 +27,24 @@ public boolean onCommand(Session session, Message message, GuildEntity entity, S
2527
Command command = Linwood.getInstance().getBaseCommand().getCommand(entity, args);
2628
if(command == null)
2729
return false;
30+
sendHelp(entity, command, message.getTextChannel());
31+
return true;
32+
}
33+
34+
@Override
35+
public @NotNull Set<String> aliases(GuildEntity entity) {
36+
return new HashSet<>(Arrays.asList(
37+
"help",
38+
"h"
39+
));
40+
}
41+
42+
@Override
43+
public @NotNull ResourceBundle getBundle(GuildEntity entity) {
44+
return ResourceBundle.getBundle("locale.commands.Help", entity.getLocalization());
45+
}
2846

47+
public void sendHelp(@NotNull GuildEntity entity, @NotNull Command command, @NotNull TextChannel channel) {
2948
var commandBundle = command.getBundle(entity);
3049
var output = new MessageBuilder()
3150
.append(" ")
@@ -40,20 +59,6 @@ public boolean onCommand(Session session, Message message, GuildEntity entity, S
4059
.addField("Syntax", commandBundle.containsKey("Syntax")?commandBundle.getString("Syntax"):"", false)
4160
.build())
4261
.build();
43-
message.getChannel().sendMessage(output).queue();
44-
return true;
45-
}
46-
47-
@Override
48-
public @NotNull Set<String> aliases(GuildEntity entity) {
49-
return new HashSet<>(Arrays.asList(
50-
"help",
51-
"h"
52-
));
53-
}
54-
55-
@Override
56-
public @NotNull ResourceBundle getBundle(GuildEntity entity) {
57-
return ResourceBundle.getBundle("locale.commands.Help", entity.getLocalization());
62+
channel.sendMessage(output).queue();
5863
}
5964
}

src/main/java/com/github/codedoctorde/linwood/commands/game/StopGameCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public boolean onCommand(Session session, Message message, GuildEntity entity, S
2727
message.getChannel().sendMessage(bundle.getString("NoPermission")).queue();
2828
return true;
2929
}
30-
if(Linwood.getInstance().getSingleApplicationManager().getGame(entity.getGuildId()) == null)
30+
if(Linwood.getInstance().getGameManager().getGame(entity.getGuildId()) == null)
3131
message.getTextChannel().sendMessage(bundle.getString("NoGameRunning")).queue();
3232
else {
33-
Linwood.getInstance().getSingleApplicationManager().stopGame(entity.getGuildId());
33+
Linwood.getInstance().getGameManager().stopGame(entity.getGuildId());
3434
message.getTextChannel().sendMessage(bundle.getString("Success")).queue();
3535
}
3636
return true;

src/main/java/com/github/codedoctorde/linwood/commands/game/WhatIsItCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public boolean onCommand(Session session, Message message, GuildEntity entity, S
3939
message.getTextChannel().sendMessage(bundle.getString("Invalid")).queue();
4040
return true;
4141
}
42-
Linwood.getInstance().getSingleApplicationManager().startGame(entity.getGuildId(), new WhatIsIt(rounds, message.getChannel().getIdLong()));
42+
Linwood.getInstance().getGameManager().startGame(entity.getGuildId(), new WhatIsIt(rounds, message.getChannel().getIdLong()));
4343
message.getTextChannel().sendMessage(bundle.getString("Success")).queue();
4444
return true;
4545
}

src/main/java/com/github/codedoctorde/linwood/commands/karma/KarmaCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class KarmaCommand extends CommandManager {
1111
@Override
1212
public @NotNull Command[] commands() {
1313
return new Command[]{
14-
new KarmaInfoCommand()
14+
new KarmaInfoCommand(),
15+
new KarmaLeaderboardCommand()
1516
};
1617
}
1718

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.codedoctorde.linwood.commands.karma;
2+
3+
import com.github.codedoctorde.linwood.Linwood;
4+
import com.github.codedoctorde.linwood.commands.Command;
5+
import com.github.codedoctorde.linwood.entity.GuildEntity;
6+
import com.github.codedoctorde.linwood.entity.MemberEntity;
7+
import com.github.codedoctorde.linwood.utils.DatabaseUtil;
8+
import net.dv8tion.jda.api.EmbedBuilder;
9+
import net.dv8tion.jda.api.entities.Message;
10+
import org.hibernate.Session;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.awt.*;
14+
import java.text.MessageFormat;
15+
import java.time.LocalDateTime;
16+
import java.util.*;
17+
import java.util.stream.Collectors;
18+
19+
/**
20+
* @author CodeDoctorDE
21+
*/
22+
public class KarmaLeaderboardCommand implements Command {
23+
@Override
24+
public boolean onCommand(Session session, Message message, GuildEntity entity, String label, String[] args) {
25+
if(args.length != 0)
26+
return false;
27+
var bundle = getBundle(entity);
28+
var leaderboard = Linwood.getInstance().getDatabase().getKarmaLeaderboard(session, message.getGuild().getIdLong());
29+
message.getGuild().retrieveMembersByIds(Arrays.stream(leaderboard).map(MemberEntity::getMemberId).collect(Collectors.toList())).onSuccess(members -> {
30+
var description = new StringBuilder();
31+
description.append(bundle.getString("LeaderboardBodyStart"));
32+
for (int i = 0; i < members.size(); i++) {
33+
var member = members.get(i);
34+
description.append(MessageFormat.format(bundle.getString("LeaderboardBody"), i, member.getUser().getAsMention(), leaderboard[i].getLikes() - leaderboard[i].getDislikes(),
35+
leaderboard[i].getLikes(), leaderboard[i].getDislikes()));
36+
}
37+
description.append(bundle.getString("LeaderboardBodyEnd"));
38+
message.getChannel().sendMessage(new EmbedBuilder()
39+
.setTitle(bundle.getString("LeaderboardHeader"))
40+
.setFooter(bundle.getString("LeaderboardFooter"))
41+
.setDescription(description)
42+
.setColor(new Color(0x3B863B))
43+
.setTimestamp(LocalDateTime.now())
44+
.build()).queue();
45+
});
46+
return true;
47+
}
48+
49+
@Override
50+
public @NotNull Set<String> aliases(GuildEntity entity) {
51+
return new HashSet<>(Arrays.asList(
52+
"leaderboard", "lb", "rank", "ranks", "top", "toplist", "top-list", "list"
53+
));
54+
}
55+
56+
@Override
57+
public @NotNull ResourceBundle getBundle(GuildEntity entity) {
58+
return ResourceBundle.getBundle("locale.commands.karma.Leaderbaord");
59+
}
60+
}

0 commit comments

Comments
 (0)