Skip to content

Commit

Permalink
chore: various changes, preparing for Hibernate and PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Lycoon committed Jan 24, 2024
1 parent 4c82784 commit e6aa488
Show file tree
Hide file tree
Showing 29 changed files with 668 additions and 670 deletions.
40 changes: 30 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,60 @@
</properties>

<dependencies>
<!-- JDA -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.19</version>
</dependency>

<!-- ClashAPI -->
<dependency>
<groupId>io.github.lycoon</groupId>
<artifactId>clash-api</artifactId>
<version>5.1.4</version>
<version>5.1.5</version>
</dependency>

<!-- GSON -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>

<!-- Lombok -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<!-- Hibernate ORM -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.6</version>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.2.Final</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.14</version>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>6.4.2.Final</version>
<type>pom</type>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.11</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.11</version>
</dependency>
</dependencies>

Expand Down
28 changes: 15 additions & 13 deletions src/main/java/com/lycoon/clashbot/commands/CommandConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;

public class CommandConfig {
public class CommandConfig
{
private final JDA jda;
private final Guild guild;
private final String CLASHBOT_GUILD = "817384284507209768";
Expand All @@ -22,23 +24,23 @@ public void createCommands()
ClashBotMain.LOGGER.info("Creating commands...");

// Miscellaneous
CommandData infoCommand = new CommandData("info", "Shows bot information");
SlashCommandData infoCommand = Commands.slash("info", "Shows bot information");
jda.upsertCommand(infoCommand).complete();

CommandData inviteCommand = new CommandData("invite", "Shows bot's invite link");
SlashCommandData inviteCommand = Commands.slash("invite", "Shows bot's invite link");
jda.upsertCommand(inviteCommand).complete();

CommandData langCommand = new CommandData("lang", "Shows your current language");
SlashCommandData langCommand = Commands.slash("lang", "Shows your current language");
jda.upsertCommand(langCommand).complete();

CommandData helpCommand = new CommandData("help", "Shows commands and their usage");
SlashCommandData helpCommand = Commands.slash("help", "Shows commands and their usage");
jda.upsertCommand(helpCommand).complete();

// Settings
CommandData clearCommand = new CommandData("clear", "Deletes all the data the bot database has about you");
SlashCommandData clearCommand = Commands.slash("clear", "Deletes all the data the bot database has about you");
jda.upsertCommand(clearCommand).complete();

CommandData setCommand = new CommandData("set", "Shows clan profile");
SlashCommandData setCommand = Commands.slash("set", "Shows clan profile");
SubcommandData setClanSubcommand = new SubcommandData("clan", "Sets default clan tag");
setClanSubcommand.addOption(OptionType.STRING, "clan_tag", "Clan tag from the profile starting with a #", true);

Expand All @@ -52,27 +54,27 @@ public void createCommands()
jda.upsertCommand(setCommand).complete();

// Clan
CommandData clanCommand = new CommandData("clan", "Shows clan profile");
SlashCommandData clanCommand = Commands.slash("clan", "Shows clan profile");
clanCommand.addOption(OptionType.STRING, "clan_tag", "Clan tag from the profile starting with a #", false);
jda.upsertCommand(clanCommand).complete();

CommandData warCommand = new CommandData("war", "Shows current war occurring in the clan");
SlashCommandData warCommand = Commands.slash("war", "Shows current war occurring in the clan");
warCommand.addOption(OptionType.INTEGER, "page", "Page number you want to access", true);
warCommand.addOption(OptionType.STRING, "clan_tag", "Clan tag from the profile starting with a #", false);
jda.upsertCommand(warCommand).complete();

CommandData warlogCommand = new CommandData("warlog", "Shows clan warlog");
SlashCommandData warlogCommand = Commands.slash("warlog", "Shows clan warlog");
warlogCommand.addOption(OptionType.INTEGER, "page", "Page number you want to access", true);
warlogCommand.addOption(OptionType.STRING, "clan_tag", "Clan tag from the profile starting with a #", false);
jda.upsertCommand(warlogCommand).complete();

CommandData warleagueCommand = new CommandData("warleague", "Shows current warleague occurring in the clan");
SlashCommandData warleagueCommand = Commands.slash("warleague", "Shows current warleague occurring in the clan");
warleagueCommand.addOption(OptionType.INTEGER, "page", "Page number you want to access", true);
warleagueCommand.addOption(OptionType.STRING, "clan_tag", "Clan tag from the profile starting with a #", false);
jda.upsertCommand(warleagueCommand).complete();

// Player
CommandData playerCommand = new CommandData("player", "Shows player profile");
SlashCommandData playerCommand = Commands.slash("player", "Shows player profile");
playerCommand.addOption(OptionType.STRING, "player_tag", "Player tag from the profile starting with a #", false);
jda.upsertCommand(playerCommand).complete();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,58 @@
package com.lycoon.clashbot.commands;

public enum Command
{
INVITE(CommandCategory.MISC, "invite", "cmd.invite.desc"),
LANG(CommandCategory.MISC, "lang", "cmd.lang.desc"),
INFO(CommandCategory.MISC, "info", "cmd.info.desc"),
HELP(CommandCategory.MISC, "help", "cmd.help.desc"),
CLEAR(CommandCategory.SETTINGS, "clear", "cmd.clear.desc"),
SET_PLAYER(CommandCategory.SETTINGS, "set", "cmd.setplayer.desc", "player <playerTag>"),
SET_CLAN(CommandCategory.SETTINGS, "set", "cmd.setclan.desc", "clan <clanTag>"),
SET_LANG(CommandCategory.SETTINGS, "set", "cmd.setlang.desc", "lang <language>"),
CLAN(CommandCategory.CLAN, "clan", "cmd.clan.desc", "[clanTag]"),
WARLEAGUE(CommandCategory.CLAN, "warleague", "cmd.warleague.round.desc", "<page> [clanTag]"),
//WARLEAGUE_ALL (CommandCategory.CLAN, "warleague", "cmd.warleague.all.desc", "all [clanTag]"),
//WARLEAGUE_CLAN (CommandCategory.CLAN, "warleague", "cmd.warleague.clan.desc", "[clanTag]"),
WARLOG(CommandCategory.CLAN, "warlog", "cmd.warlog.desc", "<page> [clanTag]"),
WAR(CommandCategory.CLAN, "war", "cmd.war.desc", "<page> [clanTag]"),
PLAYER(CommandCategory.PLAYER, "player", "cmd.player.desc", "[playerTag]");

final CommandCategory category;
final String name, desc;
String usage;

Command(CommandCategory category, String name, String desc, String usage)
{
this.category = category;
this.name = name;
this.desc = desc;
this.usage = usage;
}

Command(CommandCategory category, String name, String desc)
{
this.category = category;
this.name = name;
this.desc = desc;
}

@Override
public String toString()
{
return name;
}

public String getDescription()
{
return desc;
}

public CommandCategory getCategory()
{
return category;
}

public String formatCommand()
{
return "/" + name + (usage == null ? "" : " " + usage);
}
}
package com.lycoon.clashbot.commands;

public enum CommandData
{
INVITE(CommandCategory.MISC, "invite", "cmd.invite.desc"),
LANG(CommandCategory.MISC, "lang", "cmd.lang.desc"),
INFO(CommandCategory.MISC, "info", "cmd.info.desc"),
HELP(CommandCategory.MISC, "help", "cmd.help.desc"),
CLEAR(CommandCategory.SETTINGS, "clear", "cmd.clear.desc"),
SET_PLAYER(CommandCategory.SETTINGS, "set", "cmd.setplayer.desc", "player <playerTag>"),
SET_CLAN(CommandCategory.SETTINGS, "set", "cmd.setclan.desc", "clan <clanTag>"),
SET_LANG(CommandCategory.SETTINGS, "set", "cmd.setlang.desc", "lang <language>"),
CLAN(CommandCategory.CLAN, "clan", "cmd.clan.desc", "[clanTag]"),
WARLEAGUE(CommandCategory.CLAN, "warleague", "cmd.warleague.round.desc", "<page> [clanTag]"),
//WARLEAGUE_ALL (CommandCategory.CLAN, "warleague", "cmd.warleague.all.desc", "all [clanTag]"),
//WARLEAGUE_CLAN (CommandCategory.CLAN, "warleague", "cmd.warleague.clan.desc", "[clanTag]"),
WARLOG(CommandCategory.CLAN, "warlog", "cmd.warlog.desc", "<page> [clanTag]"),
WAR(CommandCategory.CLAN, "war", "cmd.war.desc", "<page> [clanTag]"),
PLAYER(CommandCategory.PLAYER, "player", "cmd.player.desc", "[playerTag]");

final CommandCategory category;
final String name, desc;
final String usage;

CommandData(CommandCategory category, String name, String desc, String usage)
{
this.category = category;
this.name = name;
this.desc = desc;
this.usage = usage;
}

CommandData(CommandCategory category, String name, String desc)
{
this(category, name, desc, null);
}

@Override
public String toString()
{
return name;
}

public String getDescription()
{
return desc;
}

public CommandCategory getCategory()
{
return category;
}

public String formatCommand()
{
return "/" + name + (usage == null ? "" : " " + usage);
}
}
13 changes: 7 additions & 6 deletions src/main/java/com/lycoon/clashbot/commands/clan/ClanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import static com.lycoon.clashbot.utils.DrawUtils.*;
import static com.lycoon.clashbot.utils.FileUtils.*;
import static com.lycoon.clashbot.utils.ErrorUtils.*;
import static com.lycoon.clashbot.utils.DatabaseUtils.*;
import static com.lycoon.clashbot.utils.database.DatabaseUtils.*;
import static com.lycoon.clashbot.utils.CoreUtils.*;

import com.lycoon.clashapi.core.exceptions.ClashAPIException;
import com.lycoon.clashapi.models.clan.ClanMember;
import com.lycoon.clashapi.models.clan.Clan;
import com.lycoon.clashapi.models.common.Label;
import com.lycoon.clashapi.models.player.enums.Role;
import com.lycoon.clashbot.commands.Command;
import com.lycoon.clashbot.commands.CommandData;
import com.lycoon.clashbot.core.ClashBotMain;
import com.lycoon.clashbot.lang.LangUtils;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.List;
Expand Down Expand Up @@ -97,16 +98,16 @@ public static Clan getClan(SlashCommandInteractionEvent event, Locale lang, Stri

if (tag == null) {
sendError(event, i18n.getString("set.clan.error"),
MessageFormat.format(i18n.getString("cmd.general.tip"), Command.SET_CLAN.formatCommand()));
MessageFormat.format(i18n.getString("cmd.general.tip"), CommandData.SET_CLAN.formatCommand()));
return null;
}

try {
clan = ClashBotMain.clashAPI.getClan(tag);
} catch (ClashAPIException e) {
try { clan = ClashBotMain.clashAPI.getClan(tag); }
catch (ClashAPIException | IOException e) {
sendExceptionError(event, i18n, e, tag, "clan");
return null;
}

return clan;
}

Expand Down
28 changes: 13 additions & 15 deletions src/main/java/com/lycoon/clashbot/commands/clan/WarCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
import static com.lycoon.clashbot.utils.DrawUtils.*;
import static com.lycoon.clashbot.utils.FileUtils.*;
import static com.lycoon.clashbot.utils.ErrorUtils.*;
import static com.lycoon.clashbot.utils.DatabaseUtils.*;
import static com.lycoon.clashbot.utils.database.DatabaseUtils.*;
import static com.lycoon.clashbot.utils.CoreUtils.*;
import static com.lycoon.clashbot.utils.GameUtils.*;

import com.lycoon.clashapi.core.ClashAPI;
import com.lycoon.clashapi.core.exceptions.ClashAPIException;
import com.lycoon.clashapi.models.war.WarAttack;
import com.lycoon.clashapi.models.war.WarMember;
import com.lycoon.clashapi.models.war.War;
import com.lycoon.clashapi.core.exception.ClashAPIException;
import com.lycoon.clashapi.models.war.enums.WarState;
import com.lycoon.clashbot.commands.Command;
import com.lycoon.clashbot.commands.CommandData;
import com.lycoon.clashbot.core.CacheComponents;
import com.lycoon.clashbot.core.ClashBotMain;
import com.lycoon.clashbot.lang.LangUtils;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

import java.awt.*;
Expand Down Expand Up @@ -50,14 +46,16 @@ public class WarCommand {
private final static Color notUsedAttackColor = new Color(0xfbbf70);
private final static Color attackColor = new Color(0x4c493a);

static class SortMemberByOrder implements Comparator<WarMember> {
static class SortMemberByOrder implements Comparator<WarMember>
{
@Override
public int compare(WarMember a, WarMember b) {
return a.getMapPosition() - b.getMapPosition();
}
}

static class SortAttackByOrder implements Comparator<WarAttack> {
static class SortAttackByOrder implements Comparator<WarAttack>
{
@Override
public int compare(WarAttack a, WarAttack b) {
return a.getOrder() - b.getOrder();
Expand Down Expand Up @@ -92,8 +90,7 @@ public static List<WarAttack> getAttacksByOrder(List<WarMember> members) {
List<WarAttack> sortedAttacks = new ArrayList<>();
for (WarMember member : members) {
List<WarAttack> attacks = member.getAttacks();
if (attacks != null)
sortedAttacks.addAll(attacks);
sortedAttacks.addAll(attacks);
}
sortedAttacks.sort(new SortAttackByOrder());
return sortedAttacks;
Expand Down Expand Up @@ -127,7 +124,7 @@ public static int drawMemberResults(Graphics2D g2d, WarMember member, List<WarMe
else
drawSimpleStringLeft(g2d, MessageFormat.format(i18n.getString("attack.index"), j + 1), 1100, y + 55 + j * 47, 8f, attackColor);

if (attacks == null) {
if (attacks.isEmpty()) {
if (!rightSide)
drawShadowedString(g2d, i18n.getString("not.used"), 105, y + 76 + j * 46, 15f, 2, notUsedAttackColor);
else
Expand Down Expand Up @@ -209,16 +206,17 @@ public static War getWar(SlashCommandInteractionEvent event, Locale lang, String

if (tag == null) {
sendError(event, i18n.getString("set.clan.error"),
MessageFormat.format(i18n.getString("cmd.general.tip"), Command.SET_CLAN.formatCommand()));
MessageFormat.format(i18n.getString("cmd.general.tip"), CommandData.SET_CLAN.formatCommand()));
return null;
}

try {
war = ClashBotMain.clashAPI.getCurrentWar(tag);
} catch (ClashAPIException e) {
try { war = ClashBotMain.clashAPI.getCurrentWar(tag); }
catch (ClashAPIException | IOException e)
{
sendExceptionError(event, i18n, e, tag, "war");
return null;
}

return war;
}

Expand Down
Loading

0 comments on commit e6aa488

Please sign in to comment.