Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<version>5.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.kronos</groupId>
Expand Down
146 changes: 53 additions & 93 deletions src/main/java/ch/yoinc/tasks/LeetifyTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.Locale;

public class LeetifyTask implements ScheduledTask {

Expand All @@ -24,12 +25,6 @@ public void execute(JDA jda, Properties properties) {
dataService.setBotID(jda.getSelfUser().getId());
leetifyConnection = new LeetifyConnection(properties);

DiscordService discordService = new DiscordService();

String DEFAULT_LEETIFY_URL = "https://leetify.com/app/match-details/%s/overview";
String DEFAULT_MAP_LOGO_URL = "https://raw.githubusercontent.com/MurkyYT/cs2-map-icons/main/images/%s.png";
String DEFAULT_MAP_URL = "https://raw.githubusercontent.com/MurkyYT/cs2-map-icons/main/images/thumbs/%s_1_png.png";

List<InternalUser> internalUsers = dataService.getAllSteamUsers();

//this map contains all newly played matches found during the next run of the
Expand All @@ -41,137 +36,95 @@ public void execute(JDA jda, Properties properties) {

for (String matchID : newlyPlayedMatches.keySet()) {
List<LeetifyMatchResponse> matches = newlyPlayedMatches.get(matchID);
EmbedBuilder matchEmbed = new EmbedBuilder();
if (matches.size() > 1) {
LeetifyMatchResponse match = matches.getFirst();
String title = "";
Color color = Color.BLUE;
String imageUrl = "";
String thumbnailUrl = "";
String footer = "";

List<String> playerNames = new ArrayList<>();
for (LeetifyMatchResponse playerMatch : matches) {
playerNames.add(playerMatch.stats.getFirst().name);
}
String players = String.join(", ", playerNames);
String description = "";

switch (match.data_source) {
case "faceit":
title = "New Faceit Match";
color = Color.ORANGE;
description = players + " played a new Faceit match together.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
matchEmbed = returnFilledEmbed("New Faceit Match",
Color.ORANGE, players + " played a new Faceit match together.",
match.map_name, matchID, "Finished at " + match.finished_at);
break;
case "wingman":
case "matchmaking_wingman":
title = "New Wingman Match";
color = Color.GREEN;
description = players + " played a new Wingman match together.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
break;
case "premier":
title = "New Premier Match";
color = Color.YELLOW;
description = players + " played a new Premier match together.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
matchEmbed = returnFilledEmbed("New Wingman Match",
Color.GREEN, players + " played a new Wingman match together.",
match.map_name, matchID, "Finished at " + match.finished_at);
break;
case "matchmaking":
title = "New Competitive Match";
color = Color.RED;
description = players + " played a new Competitive match together.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
matchEmbed = returnFilledEmbed("New Competitive Match",
Color.YELLOW, players + " played a new Competitive match together.",
match.map_name, matchID, "Finished at " + match.finished_at);
break;
}

EmbedBuilder updateMessage = discordService.createEmbedBuilder(title, description, imageUrl, footer);
updateMessage
.setTitle(title, DEFAULT_LEETIFY_URL.replace("%s", matchID))
.setColor(color)
.setThumbnail(thumbnailUrl);

for (LeetifyMatchResponse playerMatch : matches) {
LeetifyPlayerStatsResponse stats = playerMatch.stats.getFirst();
updateMessage.addField(
matchEmbed.addField(
stats.name,
"Kills: " + stats.total_kills +
"\nDeaths: " + stats.total_deaths +
"\nADR: " + stats.dpr +
"\nRating: " + stats.leetify_rating +
"\nCT Rating: " + stats.ct_leetify_rating +
"\nT Rating: " + stats.t_leetify_rating,
"\nRating: " + formatRating(stats.leetify_rating) +
"\nCT Rating: " + formatRating(stats.ct_leetify_rating) +
"\nT Rating: " + formatRating(stats.t_leetify_rating),
true
);
}
Objects.requireNonNull(jda.getTextChannelById(properties.getProperty("discord.channelID"))).sendMessageEmbeds(updateMessage.build()).queue();
Objects.requireNonNull(jda.getTextChannelById(properties.getProperty("discord.channelID"))).sendMessageEmbeds(matchEmbed.build()).queue();
} else {
LeetifyMatchResponse match = matches.getFirst();
String title = "";
Color color = Color.BLUE;
String description = "";
String imageUrl = "";
String thumbnailUrl = "";
String footer = "";

switch (match.data_source) {
case "faceit":
title = "New Faceit Match";
color = Color.ORANGE;
description = match.stats.getFirst().name + " played a new Faceit match.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
matchEmbed = returnFilledEmbed("New Faceit Match", Color.ORANGE,
match.stats.getFirst().name + " played a new Faceit match.",
match.map_name, matchID, "Finished at " + match.finished_at);
break;
case "wingman":
case "matchmaking_wingman":
title = "New Wingman Match";
color = Color.GREEN;
description = match.stats.getFirst().name + " played a new Wingman match.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
break;
case "premier":
title = "New Premier Match";
color = Color.YELLOW;
description = match.stats.getFirst().name + " played a new Premier match.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
matchEmbed = returnFilledEmbed("New Wingman Match", Color.GREEN,
match.stats.getFirst().name + " played a new Wingman match.",
match.map_name, matchID, "Finished at " + match.finished_at);
break;
case "matchmaking":
title = "New Competitive Match";
color = Color.RED;
description = match.stats.getFirst().name + " played a new Competitive match.";
imageUrl = DEFAULT_MAP_URL.replace("%s", match.map_name);
thumbnailUrl = DEFAULT_MAP_LOGO_URL.replace("%s", match.map_name);
footer = "Finished at " + match.finished_at;
matchEmbed = returnFilledEmbed("New Competitive Match", Color.YELLOW,
match.stats.getFirst().name + " played a new Competitive match.",
match.map_name, matchID, "Finished at " + match.finished_at);
break;
}
EmbedBuilder updateMessage = discordService.createEmbedBuilder(title, description, imageUrl, footer);
updateMessage
.setTitle(title, DEFAULT_LEETIFY_URL.replace("%s", matchID))
.setColor(color)
.setThumbnail(thumbnailUrl)
matchEmbed
.addField("Kills", Integer.toString(match.stats.getFirst().total_kills), true)
.addField("Deaths", Integer.toString(match.stats.getFirst().total_deaths), true)
.addField("ADR", Double.toString(match.stats.getFirst().dpr), true)
.addField("Rating", Double.toString(match.stats.getFirst().leetify_rating), true)
.addField("CT Rating", Double.toString(match.stats.getFirst().ct_leetify_rating), true)
.addField("T Rating", Double.toString(match.stats.getFirst().t_leetify_rating), true);
Objects.requireNonNull(jda.getTextChannelById(properties.getProperty("discord.channelID"))).sendMessageEmbeds(updateMessage.build()).queue();
.addField("Rating", formatRating(match.stats.getFirst().leetify_rating), true)
.addField("CT Rating", formatRating(match.stats.getFirst().ct_leetify_rating), true)
.addField("T Rating", formatRating(match.stats.getFirst().t_leetify_rating), true);
Objects.requireNonNull(jda.getTextChannelById(properties.getProperty("discord.channelID"))).sendMessageEmbeds(matchEmbed.build()).queue();
}
}
}

private EmbedBuilder returnFilledEmbed(String title, Color color, String description, String map_name, String matchID, String footer) {
DiscordService discordService = new DiscordService();

String DEFAULT_LEETIFY_URL = "https://leetify.com/app/match-details/%s/overview";
String DEFAULT_MAP_LOGO_URL = "https://raw.githubusercontent.com/MurkyYT/cs2-map-icons/main/images/%s.png";
String DEFAULT_MAP_URL = "https://raw.githubusercontent.com/MurkyYT/cs2-map-icons/main/images/thumbs/%s_1_png.png";

EmbedBuilder returnEmbed = discordService.createEmbedBuilder(title, description, DEFAULT_MAP_URL.replace("%s", map_name), footer);
returnEmbed
.setTitle(title, DEFAULT_LEETIFY_URL.replace("%s", matchID))
.setColor(color)
.setThumbnail(DEFAULT_MAP_LOGO_URL.replace("%s", map_name));

return returnEmbed;
}

private HashMap<String, List<LeetifyMatchResponse>> setNewlyPlayedMatches(List<InternalUser> steamInternalUsers) {
HashMap<String, List<LeetifyMatchResponse>> results = new HashMap<>();

Expand Down Expand Up @@ -204,4 +157,11 @@ private HashMap<String, List<LeetifyMatchResponse>> setNewlyPlayedMatches(List<I
public String getTaskName() {
return "LeetifyTask";
}

private static String formatRating(Double rating) {
if (rating == null) {
return "n/a";
}
return String.format(Locale.US, "%.2f", rating * 100.0);
}
}
Loading