Skip to content

Commit

Permalink
fix: activeMatches API
Browse files Browse the repository at this point in the history
  • Loading branch information
yHSJ committed Dec 11, 2024
1 parent 0a1aa45 commit 987f186
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
}
}
21 changes: 19 additions & 2 deletions app/src/main/java/fi/sundae/bot/tournament/Match.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -229,4 +231,19 @@ public int hashCode() {

return hash;
}

public static class MatchSerializer implements JsonSerializer<Match> {
@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;
}
}

}

0 comments on commit 987f186

Please sign in to comment.