diff --git a/app/src/main/java/fi/sundae/bot/api/ActiveMatchesHandler.java b/app/src/main/java/fi/sundae/bot/api/ActiveMatchesHandler.java index abc7219..2c400ad 100644 --- a/app/src/main/java/fi/sundae/bot/api/ActiveMatchesHandler.java +++ b/app/src/main/java/fi/sundae/bot/api/ActiveMatchesHandler.java @@ -1,5 +1,8 @@ package fi.sundae.bot.api; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import fi.sundae.bot.tournament.Match; import fi.sundae.bot.tournament.Matchmaker; import io.javalin.http.Context; import io.javalin.http.Handler; @@ -15,6 +18,7 @@ public ActiveMatchesHandler(Matchmaker matchmaker) { @Override public void handle(@NotNull Context ctx) throws Exception { - ctx.json(MATCHMAKER.getActiveMatches()); + Gson gson = new GsonBuilder().registerTypeAdapter(Match.class, new Match.MatchSerializer()).create(); + ctx.json(gson.toJson(MATCHMAKER.getActiveMatches())); } } diff --git a/app/src/main/java/fi/sundae/bot/tournament/Match.java b/app/src/main/java/fi/sundae/bot/tournament/Match.java index 2c8adca..69bb777 100644 --- a/app/src/main/java/fi/sundae/bot/tournament/Match.java +++ b/app/src/main/java/fi/sundae/bot/tournament/Match.java @@ -1,14 +1,16 @@ package fi.sundae.bot.tournament; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import com.google.gson.*; + import java.awt.*; import java.io.IOException; +import java.lang.reflect.Type; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; + import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; @@ -229,4 +231,19 @@ public int hashCode() { return hash; } + + public static class MatchSerializer implements JsonSerializer { + @Override + public JsonElement serialize(Match match, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("player_one", match.getPlayerOne()); + jsonObject.addProperty("player_two", match.getPlayerTwo()); + jsonObject.addProperty("region", match.getRegion().getPrettyName()); + jsonObject.addProperty("code", match.getCode()); + jsonObject.addProperty("thread", match.getThread().getId()); + jsonObject.addProperty("gameTxHash", match.getGameTxHash()); + return jsonObject; + } + } + }