Skip to content

Commit

Permalink
Added stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Kub94ekCZ committed Dec 7, 2024
1 parent 97c0ec7 commit 58d1caa
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/kub94ek/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Random;

public class Generator {
public final class Generator {
private static final String HEX = "0123456789ABCDEF";


Expand Down
23 changes: 12 additions & 11 deletions src/main/java/me/kub94ek/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.kub94ek.card.Card;
import me.kub94ek.card.CardType;
import me.kub94ek.data.database.Database;
import me.kub94ek.data.stats.Stats;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
Expand Down Expand Up @@ -38,7 +39,7 @@
public class Main extends ListenerAdapter {
private static JDA jda;
private static Database database;
private static List<String> availableChannels = new ArrayList<>();
private static final List<String> availableChannels = new ArrayList<>();

private static final List<String> messageIds = new ArrayList<>();

Expand Down Expand Up @@ -171,6 +172,8 @@ public void onButtonInteraction(ButtonInteractionEvent e) {

@Override
public void onModalInteraction(ModalInteractionEvent event) {
String memberId = event.getMember().getId();

if (event.getModalId().equals("catch")) {
String answer = event.getValue("answer").getAsString();
if (!messageIds.contains(event.getMessage().getId())) {
Expand All @@ -188,7 +191,7 @@ public void onModalInteraction(ModalInteractionEvent event) {
Random random = new Random();
CardType type = cardTypes.get(event.getMessage().getId());

Card card = new Card(Generator.createUniqueCardId(), event.getMember().getId(), type,
Card card = new Card(Generator.createUniqueCardId(), memberId, type,
random.nextInt(-20, 21),
random.nextInt(-20, 21));

Expand All @@ -208,20 +211,18 @@ public void onModalInteraction(ModalInteractionEvent event) {
).queue();
buttons.remove(event.getMessage().getId());


try {
database.addCard(card);
/*if (!database.hasStats(event.getMember().getId())) {
database.registerStats(event.getMember().getId());
if (!database.hasStats(memberId)) {
database.initStats(memberId);
database.setStat(
event.getMember().getId(),
"cards_caught",
database.getUserCards(event.getMember().getId()).size()
memberId,
Stats.CARDS_CAUGHT,
database.getUserCards(memberId).size()
);
} else {
database.setStat(event.getMember().getId(), "cards_caught",
database.getStat(event.getMember().getId(), "cards_caught") + 1);
}*/
database.increaseStat(memberId, Stats.CARDS_CAUGHT, 1);
}

} catch (SQLException e) {
e.printStackTrace();
Expand Down
122 changes: 122 additions & 0 deletions src/main/java/me/kub94ek/data/database/Database.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package me.kub94ek.data.database;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import me.kub94ek.Main;
import me.kub94ek.card.Card;
import me.kub94ek.card.CardType;
import me.kub94ek.data.stats.Stats;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -231,4 +239,118 @@ public void giveCoins(String fromUser, String toUser, int coins) {
}
}

public JsonObject initStats(String userId) {
if (hasStats(userId)) {
throw new IllegalStateException("This user already has stats initialized (id: " + userId + ")");
}

JsonObject jsonObject = new JsonObject();

for (Stats stat : Stats.values()) {
jsonObject.add(stat.id, new JsonPrimitive(0));
}

try (PreparedStatement statement = databaseConnection.prepareStatement(
"INSERT INTO cards VALUES (?, ?)"
)) {
statement.setString(1, userId);
statement.setString(2, jsonObject.toString());

statement.execute();
} catch (SQLException e) {
e.printStackTrace();
return jsonObject;
}

return jsonObject;
}
public boolean hasStats(String userId) {
boolean exists;
try (Statement statement = databaseConnection.createStatement()) {
ResultSet resultSet = statement.executeQuery("SELECT * FROM stats WHERE user_id='" + userId + "'");
exists = resultSet.next();
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return exists;
}
public JsonObject getStats(String userId) {
if (!hasStats(userId)) {
return initStats(userId);
}

String statsJson = "{}";
try (Statement statement = databaseConnection.createStatement()) {
ResultSet resultSet = statement.executeQuery("SELECT * FROM stats WHERE id='" + userId + "'");
if (resultSet.next()) {
statsJson = resultSet.getString("stats");
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}

Gson gson = new Gson();

return gson.fromJson(statsJson, JsonObject.class);
}
public int getStat(String userId, Stats stat) {
return getStat(userId, stat.id);
}
public int getStat(String userId, String stat) {
JsonObject json = getStats(userId);

if (!json.has(stat)) {
return -1;
}

return json.get(stat).getAsInt();
}
public void setStats(String userId, JsonObject json) {
setStats(userId, json.toString());
}
public void setStats(String userId, String json) {
if (!hasStats(userId)) {
initStats(userId);
}

try (Statement statement = databaseConnection.createStatement()) {
statement.execute("UPDATE stats SET stats='" + json + "' WHERE user_id='" + userId + "'");
} catch (SQLException e) {
e.printStackTrace();
}

}
public void setStat(String userId, Stats stat, int value) {
setStat(userId, stat.id, value);
}
public void setStat(String userId, String stat, int value) {
JsonObject stats = getStats(userId);
stats.add(stat, new JsonPrimitive(value));
setStats(userId, stats);
}
public void increaseStat(String userId, Stats stat, int increment) {
setStat(userId, stat.id, getStat(userId, stat.id) + increment);
}
public void increaseStat(String userId, String stat, int increment) {
setStat(userId, stat, getStat(userId, stat) + increment);
}
public MessageEmbed createStatsEmbed(String userId) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.ORANGE);
embedBuilder.setAuthor("Statistics:",
"https://discord.com/invite/D4wjvR8Dw9",
Main.getJda().getUserById(userId).getEffectiveAvatarUrl());

JsonObject json = getStats(userId);

for (Stats stat : Stats.values()) {
embedBuilder.addField(stat.name, "" + json.get(stat.id).getAsInt(), false);
}

return embedBuilder.build();
}

}
21 changes: 21 additions & 0 deletions src/main/java/me/kub94ek/data/stats/Stats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.kub94ek.data.stats;

public enum Stats {
WINS("Wins", "win"),
LOSES("Loses", "lose"),
BATTLES_PLAYED("Battles Played", "battles"),
CARDS_CAUGHT("Cards Caught", "c_catch"),
CARDS_BOUGHT("Cards Bought", "c_buy"),
CARDS_TRADED("Cards Traded", "c_trade"),
KILLS("Kills", "kill"),
CARDS_LOST("Cards Lost", "death"),
COINS("Coins Total", "coins");

public final String name;
public final String id;

Stats(String name, String id) {
this.name = name;
this.id = id;
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/kub94ek/image/CardCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.io.IOException;
import java.io.InputStream;

public class CardCreator {
public final class CardCreator {

private static InputStream getFileInputStream(String fileName) {
ClassLoader classLoader = CardCreator.class.getClassLoader();
Expand Down

0 comments on commit 58d1caa

Please sign in to comment.