Skip to content

Commit

Permalink
feat(command): Implemented XP card API. Issue found with levelling up.
Browse files Browse the repository at this point in the history
Big brain needed. (#87)
  • Loading branch information
Seb committed Apr 13, 2022
1 parent e2636e4 commit 69a83a4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,12 @@ private void showAllLevelRoles(MessageChannel channel, Server server, Guild guil

private void sendLevelCard(MessageChannel channel, Member member, long serverID) {
Experience userXP = xpService.getUserExperience(member.getId(), serverID);
MessageEmbed experienceEmbed = new EmbedBuilder()
.setColor(member.getColorRaw())
.setThumbnail(member.getEffectiveAvatarUrl())
.setTitle(member.getUser().getName() + "'s Experience")
.setDescription("**Level:** `" + userXP.getLevel() +
"`\n**XP towards next level:** `" + userXP.getCurrentExperience() +
"`\n**XP Needed for next level:** `" + xpService.xpNeededForLevel(userXP.getLevel() + 1) + "`")
.build();
channel.sendMessageEmbeds(experienceEmbed).queue();

byte[] xpCard = xpService.getExperienceCard(member.getUser().getAvatarUrl(),
userXP.getCurrentExperience(), xpService.xpNeededForLevel(userXP.getLevel()),
userXP.getLevel(), 1, member.getUser().getName() + "#" + member.getUser().getDiscriminator(),
"#FF0000", "#2E2E2E");
channel.sendFile(xpCard, "xpcard.png").queue();
}

@Override
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/de/voidtech/gerald/service/ExperienceService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package main.java.de.voidtech.gerald.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.Random;

import javax.xml.bind.DatatypeConverter;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -27,8 +35,34 @@ public class ExperienceService {
@Autowired
private ServerService serverService;

@Autowired
private GeraldConfig config;

private static final int EXPERIENCE_DELAY = 0; //Delay between incrementing XP in seconds

public byte[] getExperienceCard(String avatarURL, long xpAchieved, long xpNeeded,
long level, long rank, String username, String barColour, String background) {
try {
String cardURL = config.getExperienceCardApiURL() + "?avatar_url=" + avatarURL +
"&xp=" + xpAchieved + "&xp_needed=" + xpNeeded + "&level=" + level + "&rank=" + rank
+ "&username=" + URLEncoder.encode(username, StandardCharsets.UTF_8.toString())
+ "&bar_colour=" + URLEncoder.encode(barColour, StandardCharsets.UTF_8.toString())
+ "&bg_colour=" + URLEncoder.encode(background, StandardCharsets.UTF_8.toString());
URL url = new URL(cardURL);
//Remove the data:image/png;base64 part
String response = Jsoup.connect(url.toString()).get().toString().split(",")[1];
byte[] imageBytes = DatatypeConverter.parseBase64Binary(response);
return imageBytes;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;

}

public Experience getUserExperience(String userID, long serverID) {
try(Session session = sessionFactory.openSession())
{
Expand Down Expand Up @@ -157,7 +191,7 @@ public void updateUserExperience(Member member, String guildID, String channelID
long currentExperience = userXP.getCurrentExperience() + generateExperience();
long xpToNextLevel = xpToNextLevel(userXP.getLevel(), currentExperience);

if (xpToNextLevel < 0) {
if (xpToNextLevel <= 0) {
userXP.setLevel(userXP.getLevel() + 1);
userXP.setCurrentXP(-1 * xpToNextLevel);
performLevelUpActions(userXP, server, member, channelID);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/voidtech/gerald/service/GeraldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ public String getTwitchSecret() {
public String getMemeApiURL() {
return config.getProperty("api.meme_url");
}

public String getExperienceCardApiURL() {
String url = config.getProperty("api.gavin_url");
return url != null ? url : "http://localhost:3000/api/";
}
}

0 comments on commit 69a83a4

Please sign in to comment.