Skip to content

Commit

Permalink
better lineup response
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed Jun 9, 2024
1 parent 67fc64b commit dc6a3f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
27 changes: 21 additions & 6 deletions src/main/java/pw/chew/mlb/listeners/InteractionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
var lineup = MLBAPIUtil.getLineup(gamePk, awayHome);

List<String> friendly = new ArrayList<>();
for (var player : lineup) {
friendly.add(player.friendlyString());

friendly.add("# " + event.getButton().getLabel());
friendly.add("The following is the lineup for this team. It is subject to change at any time.");
friendly.add("## Batting Order");

var battingOrder = lineup.get("Batting Order");

if (battingOrder.isEmpty()) {
friendly.add("The batting order is currently not available. Please try again closer to the scheduled game time.");
} else {
for (var player : battingOrder) {
friendly.add(player.friendlyString());
}
}

friendly.add(0, event.getButton().getLabel());
friendly.add(1, "");
// add a string before the next-to-last element. e.g. "a", "b", "c" <-- between b and c
friendly.add("## Probable Pitcher");

var probablePitcher = lineup.get("Probable Pitcher");

if (friendly.size() == 2) {
friendly.add("No lineup has been submitted yet. Try closer to the game!");
if (probablePitcher.isEmpty()) {
friendly.add("The probable pitcher is currently not available. Please try again closer to the scheduled game time.");
} else {
friendly.add(probablePitcher.get(0).friendlyString());
}

event.reply(String.join("\n", friendly)).setEphemeral(true).queue();
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/pw/chew/mlb/util/MLBAPIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -48,12 +49,15 @@ public static Teams getTeams(String sportId) {
return teams;
}

public static List<Player> getLineup(String gamePk, String homeAway) {
JSONObject data = new JSONObject(RestClient.get("https://statsapi.mlb.com/api/v1.1/game/%s/feed/live?language=en".formatted(gamePk)));
public static Map<String, List<Player>> getLineup(String gamePk, String homeAway) {
JSONObject data = new JSONObject(RestClient.get("https://statsapi.mlb.com/api/v1.1/game/%s/feed/live?language=en&fields=liveData,boxscore,teams,away,home,players,id,fullName,jerseyNumber,position,name,abbreviation,seasonStats,pitching,era,wins,losses,strikeOuts,batting,avg,ops,homeRuns,gameData,probablePitchers,away,home,id"
.formatted(gamePk)));
JSONObject boxScore = data.getJSONObject("liveData")
.getJSONObject("boxscore")
.getJSONObject("teams");

Map<String, List<Player>> response = new HashMap<>();

Map<Integer, Player> players = new HashMap<>();
for (String team :new String[]{"home", "away"}) {
JSONObject teamPlayers = boxScore.getJSONObject(team).getJSONObject("players");
Expand All @@ -63,6 +67,7 @@ public static List<Player> getLineup(String gamePk, String homeAway) {
}
}

// get the batting order
JSONArray lineup = boxScore.getJSONObject(homeAway).getJSONArray("battingOrder");
List<Player> lineupPlayers = new ArrayList<>();
for (Object item : lineup) {
Expand All @@ -72,7 +77,14 @@ public static List<Player> getLineup(String gamePk, String homeAway) {
lineupPlayers.add(player);
}

return lineupPlayers;
response.put("Batting Order", lineupPlayers);

// add pitcher to end
int probablePitcher = data.getJSONObject("gameData").getJSONObject("probablePitchers").getJSONObject(homeAway).getInt("id");
Player pitcher = players.get(probablePitcher);
response.put("Probable Pitcher", Collections.singletonList(pitcher));

return response;
}

/**
Expand Down Expand Up @@ -175,6 +187,18 @@ public String era() {
return raw.getJSONObject("seasonStats").getJSONObject("pitching").getString("era");
}

public int wins() {
return raw.getJSONObject("seasonStats").getJSONObject("pitching").getInt("wins");
}

public int losses() {
return raw.getJSONObject("seasonStats").getJSONObject("pitching").getInt("losses");
}

public int strikeouts() {
return raw.getJSONObject("seasonStats").getJSONObject("pitching").getInt("strikeOuts");
}

public String avg() {
return raw.getJSONObject("seasonStats").getJSONObject("batting").getString("avg");
}
Expand All @@ -189,14 +213,14 @@ public int homers() {

public String friendlyString() {
if (isPitcher()) {
return "%s (%s ERA)".formatted(name(), era());
return "%s (%s - %s | %s ERA | %s K)".formatted(name(), wins(), losses(), era(), strikeouts());
} else {
return "%s (%s) - %s AVG, %s OPS, %s HR".formatted(name(), position().abbreviation(), avg(), ops(), homers());
}
}
}

private record Position(JSONObject raw) {
public record Position(JSONObject raw) {
public String name() {
return raw.getString("name");
}
Expand Down

0 comments on commit dc6a3f3

Please sign in to comment.