From 5ed592017502750efd9c09afae33db1c3de789ae Mon Sep 17 00:00:00 2001 From: stelar7 Date: Fri, 2 Oct 2020 02:47:09 +0200 Subject: [PATCH] Add LOR match api --- .../r4j/basic/cache/CacheLifetimeHint.java | 4 +- .../r4j/basic/constants/api/URLEndpoint.java | 4 +- .../java/no/stelar7/api/r4j/impl/R4J.java | 10 ++ .../stelar7/api/r4j/impl/lor/LORMatchAPI.java | 96 ++++++++++++ .../api/r4j/pojo/lor/match/LORMatch.java | 97 ++++++++++++ .../api/r4j/pojo/lor/match/LORMetadata.java | 58 +++++++ .../r4j/pojo/lor/match/LORParticipant.java | 94 ++++++++++++ .../api/r4j/pojo/shared/GAMHSMatch.java | 141 ++++++++++-------- .../api/r4j/tests/lor/TestLORMatch.java | 54 +++++++ 9 files changed, 491 insertions(+), 67 deletions(-) create mode 100644 src/main/java/no/stelar7/api/r4j/impl/lor/LORMatchAPI.java create mode 100644 src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMatch.java create mode 100644 src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMetadata.java create mode 100644 src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORParticipant.java create mode 100644 src/test/java/no/stelar7/api/r4j/tests/lor/TestLORMatch.java diff --git a/src/main/java/no/stelar7/api/r4j/basic/cache/CacheLifetimeHint.java b/src/main/java/no/stelar7/api/r4j/basic/cache/CacheLifetimeHint.java index 0a8ec459..5a19f955 100644 --- a/src/main/java/no/stelar7/api/r4j/basic/cache/CacheLifetimeHint.java +++ b/src/main/java/no/stelar7/api/r4j/basic/cache/CacheLifetimeHint.java @@ -73,7 +73,9 @@ public Long get(URLEndpoint endpoint) DEFAULTS.add(URLEndpoint.V4_LEAGUE_RANK, MINUTES_30); DEFAULTS.add(URLEndpoint.V1_VAL_MATCHLIST_BY_PUUID, MINUTES_30); - DEFAULTS.add(URLEndpoint.V1_LOR_RANKED_LEADERBOARD, MINUTES_30); DEFAULTS.add(URLEndpoint.V1_VAL_RECENT_MATCH_BY_QUEUE, MINUTES_10); + + DEFAULTS.add(URLEndpoint.V1_LOR_RANKED_LEADERBOARD, MINUTES_30); + DEFAULTS.add(URLEndpoint.V1_LOR_MATCHES_BY_PUUID, MINUTES_30); } } diff --git a/src/main/java/no/stelar7/api/r4j/basic/constants/api/URLEndpoint.java b/src/main/java/no/stelar7/api/r4j/basic/constants/api/URLEndpoint.java index eaf0ba72..89cbabdc 100644 --- a/src/main/java/no/stelar7/api/r4j/basic/constants/api/URLEndpoint.java +++ b/src/main/java/no/stelar7/api/r4j/basic/constants/api/URLEndpoint.java @@ -219,11 +219,13 @@ public enum URLEndpoint V1_SHARED_ACCOUNT_BY_TAG("riot", "account", "v1", "accounts/by-riot-id/" + Constants.GAME_NAME_PLACEHOLDER + "/" + Constants.TAG_LINE_PLACEHOLDER, RiotAccount.class), V1_SHARED_SHARD_BY_PUUID("riot", "account", "v1", "active-shards/by-game/" + Constants.GAME_PLACEHOLDER + "/by-puuid/" + Constants.PUUID_ID_PLACEHOLDER, RiotAccountShard.class), + V1_LOR_MATCHES_BY_PUUID("lor", "match", "v1", "matches/by-puuid/" + Constants.PUUID_ID_PLACEHOLDER + "/ids", StringList.class), + V1_LOR_MATCH("lor", "match", "v1", "matches/" + Constants.MATCH_ID_PLACEHOLDER, GAMHSMatch.class), ; - private final String game; + private final String game; private final String service; private final String version; private final String resource; diff --git a/src/main/java/no/stelar7/api/r4j/impl/R4J.java b/src/main/java/no/stelar7/api/r4j/impl/R4J.java index 0774410a..3508d4e0 100644 --- a/src/main/java/no/stelar7/api/r4j/impl/R4J.java +++ b/src/main/java/no/stelar7/api/r4j/impl/R4J.java @@ -153,6 +153,16 @@ public LoRClientAPI getClientAPI() { return LoRClientAPI.getInstance(); } + + /** + * Gets match api. + * + * @return the match api + */ + public LORMatchAPI getMatchAPI() + { + return LORMatchAPI.getInstance(); + } } diff --git a/src/main/java/no/stelar7/api/r4j/impl/lor/LORMatchAPI.java b/src/main/java/no/stelar7/api/r4j/impl/lor/LORMatchAPI.java new file mode 100644 index 00000000..9418232f --- /dev/null +++ b/src/main/java/no/stelar7/api/r4j/impl/lor/LORMatchAPI.java @@ -0,0 +1,96 @@ +package no.stelar7.api.r4j.impl.lor; + +import no.stelar7.api.r4j.basic.calling.*; +import no.stelar7.api.r4j.basic.constants.api.*; +import no.stelar7.api.r4j.basic.constants.api.regions.RuneterraShard; +import no.stelar7.api.r4j.pojo.lor.match.*; +import no.stelar7.api.r4j.pojo.shared.GAMHSMatch; + +import java.util.*; + +public class LORMatchAPI +{ + private static final LORMatchAPI INSTANCE = new LORMatchAPI(); + + private LORMatchAPI() + { + } + + public static LORMatchAPI getInstance() + { + return LORMatchAPI.INSTANCE; + } + + public List getMatchList(RuneterraShard server, String puuid) + { + DataCallBuilder builder = new DataCallBuilder() + .withHeader(Constants.X_RIOT_TOKEN_HEADER_KEY, DataCall.getCredentials().getLORAPIKey()) + .withEndpoint(URLEndpoint.V1_LOR_MATCHES_BY_PUUID) + .withURLParameter(Constants.PUUID_ID_PLACEHOLDER, puuid) + .withPlatform(server); + + Map data = new TreeMap<>(); + data.put("platform", server); + data.put("puuid", puuid); + + Optional chl = DataCall.getCacheProvider().get(URLEndpoint.V1_LOR_MATCHES_BY_PUUID, data); + if (chl.isPresent()) + { + return (List) chl.get(); + } + + try + { + List list = (List) builder.build(); + + data.put("value", list); + DataCall.getCacheProvider().store(URLEndpoint.V1_LOR_MATCHES_BY_PUUID, data); + + return list; + } catch (ClassCastException e) + { + return null; + } + } + + public LORMatch getMatch(RuneterraShard platform, String gameId) + { + return getMatchRAW(platform, gameId).toLORMatch(); + } + + public LORMetadata getMetadata(RuneterraShard platform, String gameId) + { + return getMatchRAW(platform, gameId).toLORMetadata(); + } + + public GAMHSMatch getMatchRAW(RuneterraShard platform, String gameId) + { + DataCallBuilder builder = new DataCallBuilder().withURLParameter(Constants.MATCH_ID_PLACEHOLDER, gameId) + .withHeader(Constants.X_RIOT_TOKEN_HEADER_KEY, DataCall.getCredentials().getTFTAPIKey()) + .withEndpoint(URLEndpoint.V1_LOR_MATCH) + .withPlatform(platform); + + Map data = new TreeMap<>(); + data.put("platform", platform); + data.put("gameid", gameId); + + Optional chl = DataCall.getCacheProvider().get(URLEndpoint.V1_LOR_MATCH, data); + if (chl.isPresent()) + { + return (GAMHSMatch) chl.get(); + } + + try + { + GAMHSMatch match = (GAMHSMatch) builder.build(); + + data.put("value", match); + DataCall.getCacheProvider().store(URLEndpoint.V1_LOR_MATCH, data); + + return match; + } catch (ClassCastException e) + { + return null; + } + } +} diff --git a/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMatch.java b/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMatch.java new file mode 100644 index 00000000..6d7b2191 --- /dev/null +++ b/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMatch.java @@ -0,0 +1,97 @@ +package no.stelar7.api.r4j.pojo.lor.match; + +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; +import java.util.*; + +public class LORMatch +{ + private String game_mode; + private String game_type; + private String game_start_time_utc; + private String game_version; + private List players; + private Integer total_turn_count; + + public String getGameMode() + { + return game_mode; + } + + public String getGameType() + { + return game_type; + } + + public String getGameStartTimeUTC() + { + return game_start_time_utc; + } + + public ZonedDateTime getGameStartAsDate() + { + DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; + TemporalAccessor dateTime = formatter.parse(game_start_time_utc); + return ZonedDateTime.from(dateTime); + } + + public String getGameVersion() + { + return game_version; + } + + public List getPlayers() + { + return players; + } + + public LORParticipant getWinner() + { + return players.stream().filter(LORParticipant::didWin).findFirst().orElse(null); + } + + public Integer getTotalTurnCount() + { + return total_turn_count; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + LORMatch lorMatch = (LORMatch) o; + return Objects.equals(game_mode, lorMatch.game_mode) && + Objects.equals(game_type, lorMatch.game_type) && + Objects.equals(game_start_time_utc, lorMatch.game_start_time_utc) && + Objects.equals(game_version, lorMatch.game_version) && + Objects.equals(players, lorMatch.players) && + Objects.equals(total_turn_count, lorMatch.total_turn_count); + } + + @Override + public int hashCode() + { + return Objects.hash(game_mode, game_type, game_start_time_utc, game_version, players, total_turn_count); + } + + @Override + public String toString() + { + return "LORMatch{" + + "game_mode='" + game_mode + '\'' + + ", game_type='" + game_type + '\'' + + ", game_start_time_utc='" + game_start_time_utc + '\'' + + ", game_version='" + game_version + '\'' + + ", players=" + players + + ", total_turn_count=" + total_turn_count + + '}'; + } +} diff --git a/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMetadata.java b/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMetadata.java new file mode 100644 index 00000000..42bef59f --- /dev/null +++ b/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORMetadata.java @@ -0,0 +1,58 @@ +package no.stelar7.api.r4j.pojo.lor.match; + +import java.util.*; + +public class LORMetadata +{ + private String data_version; + private String match_id; + private List participants; + + public String getDataVersion() + { + return data_version; + } + + public String getMatchID() + { + return match_id; + } + + public List getParticipants() + { + return participants; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + LORMetadata that = (LORMetadata) o; + return Objects.equals(data_version, that.data_version) && + Objects.equals(match_id, that.match_id) && + Objects.equals(participants, that.participants); + } + + @Override + public int hashCode() + { + return Objects.hash(data_version, match_id, participants); + } + + @Override + public String toString() + { + return "LORMetadata{" + + "data_version='" + data_version + '\'' + + ", match_id='" + match_id + '\'' + + ", participants=" + participants + + '}'; + } +} diff --git a/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORParticipant.java b/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORParticipant.java new file mode 100644 index 00000000..a9d660e7 --- /dev/null +++ b/src/main/java/no/stelar7/api/r4j/pojo/lor/match/LORParticipant.java @@ -0,0 +1,94 @@ +package no.stelar7.api.r4j.pojo.lor.match; + +import no.stelar7.api.r4j.impl.lor.LoRDeckCode; +import no.stelar7.api.r4j.pojo.lor.offline.card.LoRDeck; + +import java.util.*; + +public class LORParticipant +{ + private String puuid; + private String deck_id; + private String deck_code; + private List factions; + private String game_outcome; + private Integer order_of_play; + + public String getPuuid() + { + return puuid; + } + + public String getDeckID() + { + return deck_id; + } + + public String getDeckCode() + { + return deck_code; + } + + public LoRDeck getDeck() + { + return LoRDeckCode.decode(deck_code); + } + + public List getFactions() + { + return factions; + } + + public String getGameOutcome() + { + return game_outcome; + } + + public boolean didWin() { + return game_outcome.equalsIgnoreCase("win"); + } + + public Integer getOrderOfPlay() + { + return order_of_play; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + LORParticipant that = (LORParticipant) o; + return Objects.equals(puuid, that.puuid) && + Objects.equals(deck_id, that.deck_id) && + Objects.equals(deck_code, that.deck_code) && + Objects.equals(factions, that.factions) && + Objects.equals(game_outcome, that.game_outcome) && + Objects.equals(order_of_play, that.order_of_play); + } + + @Override + public int hashCode() + { + return Objects.hash(puuid, deck_id, deck_code, factions, game_outcome, order_of_play); + } + + @Override + public String toString() + { + return "LORParticipant{" + + "puuid='" + puuid + '\'' + + ", deck_id='" + deck_id + '\'' + + ", deck_code='" + deck_code + '\'' + + ", factions=" + factions + + ", game_outcome='" + game_outcome + '\'' + + ", order_of_play=" + order_of_play + + '}'; + } +} diff --git a/src/main/java/no/stelar7/api/r4j/pojo/shared/GAMHSMatch.java b/src/main/java/no/stelar7/api/r4j/pojo/shared/GAMHSMatch.java index d4ebf29b..8f834cc8 100644 --- a/src/main/java/no/stelar7/api/r4j/pojo/shared/GAMHSMatch.java +++ b/src/main/java/no/stelar7/api/r4j/pojo/shared/GAMHSMatch.java @@ -1,65 +1,76 @@ -package no.stelar7.api.r4j.pojo.shared; - -import com.google.gson.JsonObject; -import no.stelar7.api.r4j.basic.utils.Utils; -import no.stelar7.api.r4j.pojo.tft.*; - -import java.io.Serializable; -import java.util.Objects; - -public class GAMHSMatch implements Serializable -{ - private Object metadata; - private Object info; - - public TFTMatch toTFTMatch() - { - return Utils.getGson().fromJson(Utils.getGson().toJsonTree(info), TFTMatch.class); - } - - public TFTMetadata toTFTMetadata() - { - return Utils.getGson().fromJson(Utils.getGson().toJsonTree(metadata), TFTMetadata.class); - } - - public JsonObject getMetadata() - { - return Utils.getGson().toJsonTree(metadata).getAsJsonObject(); - } - - public JsonObject getInfo() - { - return Utils.getGson().toJsonTree(info).getAsJsonObject(); - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - GAMHSMatch that = (GAMHSMatch) o; - return Objects.equals(metadata, that.metadata) && - Objects.equals(info, that.info); - } - - @Override - public int hashCode() - { - return Objects.hash(metadata, info); - } - - @Override - public String toString() - { - return "GAMHSMatch{" + - "metadata='" + metadata + '\'' + - ", json='" + info + '\'' + - '}'; - } -} +package no.stelar7.api.r4j.pojo.shared; + +import com.google.gson.JsonObject; +import no.stelar7.api.r4j.basic.utils.Utils; +import no.stelar7.api.r4j.pojo.lor.match.*; +import no.stelar7.api.r4j.pojo.tft.*; + +import java.io.Serializable; +import java.util.Objects; + +public class GAMHSMatch implements Serializable +{ + private Object metadata; + private Object info; + + public TFTMatch toTFTMatch() + { + return Utils.getGson().fromJson(Utils.getGson().toJsonTree(info), TFTMatch.class); + } + + public TFTMetadata toTFTMetadata() + { + return Utils.getGson().fromJson(Utils.getGson().toJsonTree(metadata), TFTMetadata.class); + } + + public LORMatch toLORMatch() + { + return Utils.getGson().fromJson(Utils.getGson().toJsonTree(info), LORMatch.class); + } + + public LORMetadata toLORMetadata() + { + return Utils.getGson().fromJson(Utils.getGson().toJsonTree(metadata), LORMetadata.class); + } + + public JsonObject getMetadata() + { + return Utils.getGson().toJsonTree(metadata).getAsJsonObject(); + } + + public JsonObject getInfo() + { + return Utils.getGson().toJsonTree(info).getAsJsonObject(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + GAMHSMatch that = (GAMHSMatch) o; + return Objects.equals(metadata, that.metadata) && + Objects.equals(info, that.info); + } + + @Override + public int hashCode() + { + return Objects.hash(metadata, info); + } + + @Override + public String toString() + { + return "GAMHSMatch{" + + "metadata='" + metadata + '\'' + + ", json='" + info + '\'' + + '}'; + } +} diff --git a/src/test/java/no/stelar7/api/r4j/tests/lor/TestLORMatch.java b/src/test/java/no/stelar7/api/r4j/tests/lor/TestLORMatch.java new file mode 100644 index 00000000..528d0135 --- /dev/null +++ b/src/test/java/no/stelar7/api/r4j/tests/lor/TestLORMatch.java @@ -0,0 +1,54 @@ +package no.stelar7.api.r4j.tests.lor; + +import no.stelar7.api.r4j.basic.constants.api.regions.*; +import no.stelar7.api.r4j.impl.R4J; +import no.stelar7.api.r4j.impl.lor.LORMatchAPI; +import no.stelar7.api.r4j.pojo.lol.summoner.Summoner; +import no.stelar7.api.r4j.pojo.lor.match.*; +import no.stelar7.api.r4j.pojo.shared.GAMHSMatch; +import no.stelar7.api.r4j.tests.SecretFile; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class TestLORMatch +{ + final R4J l4j8 = new R4J(SecretFile.CREDS); + final LORMatchAPI api = l4j8.getLORAPI().getMatchAPI(); + + + @Test + public void testFetchList() + { + List stelar7 = api.getMatchList(RuneterraShard.EUROPE, Summoner.byName(LeagueShard.EUW1, "stelar7").getPUUID()); + System.out.println(); + } + + @Test + public void testFetchMatch() + { + LORMatch match = api.getMatch(RuneterraShard.EUROPE, "6f70a089-0bb6-4ad8-ab16-9979562aaa7d"); + LORMetadata metadata = api.getMetadata(RuneterraShard.EUROPE, "6f70a089-0bb6-4ad8-ab16-9979562aaa7d"); + System.out.println(); + } + + @Test + public void testFetchMatchRAW() + { + GAMHSMatch stelar7 = api.getMatchRAW(RuneterraShard.EUROPE, "6f70a089-0bb6-4ad8-ab16-9979562aaa7d"); + System.out.println(); + } + + + @Test + public void testFetchMany() + { + List list = api.getMatchList(RuneterraShard.EUROPE, Summoner.byName(LeagueShard.EUW1, "stelar7").getPUUID()); + list.forEach(id -> { + LORMatch match = api.getMatch(RuneterraShard.EUROPE, id); + System.out.println(match.getGameStartTimeUTC()); + System.out.println(match.getGameStartAsDate()); + System.out.println(); + }); + } +}