Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

addons/game-day: the matchup, the top line and the countdown word for each entry

Justin Newman27e7334542e1
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Titles.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Titles.java
new file mode 100644
index 000000000..dc50f9419
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Titles.java
@@ -0,0 +1,122 @@
+package io.github.justinnewman.gameday;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * What the panel says for a calendar entry: its top line ("Minnesota Wild at Dallas Stars", always away first) and
+ * the word in front of the countdown. Calendars write games in a few ways: "Away @ Home" and "Away at Home" put the
+ * away side first, "Home vs Away" and "Home v Away" the home side. Anything else is shown as its own title.
+ */
+final class Titles {
+ private static final String STARTS = "Starts";
+ /** Sessions before the main event: they count down to "Starts", the race itself to "Lights out". */
+ private static final Pattern SESSION = Pattern.compile(
+ "\\b(practice|fp1|fp2|fp3|qualifying|quali|shootout)\\b", Pattern.CASE_INSENSITIVE);
+
+ private Titles() {
+ }
+
+ /** The title without its leading emoji, symbols and spaces; the rest is kept as written. */
+ static String clean(String title) {
+ if (title == null) return "";
+ String t = title.trim();
+ int i = 0;
+ while (i < t.length()) {
+ int cp = t.codePointAt(i);
+ if (Character.isLetterOrDigit(cp)) break;
+ i += Character.charCount(cp);
+ }
+ return t.substring(i).trim();
+ }
+
+ /** {away, home} for a cleaned title that names a game, else null. A trailing "(...)" or " - ..." is ignored. */
+ static String[] matchup(String cleaned) {
+ if (cleaned == null) return null;
+ return split(core(cleaned));
+ }
+
+ /** "Away at Home" plus any trailing decoration; the cleaned title when it is not a game; else the calendar's name. */
+ static String topLine(String title, String calendarName) {
+ String cleaned = clean(title);
+ if (cleaned.isEmpty()) return calendarName == null ? "" : calendarName;
+ String core = core(cleaned);
+ String[] sides = split(core);
+ if (sides == null) return cleaned;
+ return sides[0] + " at " + sides[1] + cleaned.substring(core.length());
+ }
+
+ /**
+ * The word in front of the countdown. A game gets the sport's word ("Puck drop"). In a team sport anything else
+ * (a draft, a welcome note) just "Starts"; in the other sports the event itself gets the word, except practice,
+ * qualifying and shootout sessions.
+ */
+ static String word(Sport sport, String title) {
+ String cleaned = clean(title);
+ if (matchup(cleaned) != null) return sport.word();
+ if (sport.team()) return STARTS;
+ if (SESSION.matcher(cleaned).find()) return STARTS;
+ return sport.word();
+ }
+
+ /** The team named in the most games, to suggest a calendar's name (Stanza names them just "NHL"); null if none. */
+ static String likelyTeam(List<String> titles) {
+ Map<String, Integer> counts = new LinkedHashMap<>();
+ for (String t : titles) {
+ String[] sides = matchup(clean(t));
+ if (sides == null) continue;
+ for (String side : sides) counts.merge(side, 1, Integer::sum);
+ }
+ String best = null;
+ int most = 0;
+ for (Map.Entry<String, Integer> e : counts.entrySet()) {
+ if (e.getValue() > most) {
+ best = e.getKey();
+ most = e.getValue();
+ }
+ }
+ return best;
+ }
+
+ /** The title without a trailing "(Preseason)" or " - NWSL", which stays at the end of the top line. */
+ private static String core(String cleaned) {
+ if (cleaned.endsWith(")")) {
+ int open = cleaned.lastIndexOf(" (");
+ if (open > 0) return cleaned.substring(0, open);
+ }
+ int dash = cleaned.lastIndexOf(" - ");
+ return dash > 0 ? cleaned.substring(0, dash) : cleaned;
+ }
+
+ private static String[] split(String core) {
+ String lower = core.toLowerCase(Locale.ROOT);
+ String[] sides = sides(core, lower.indexOf(" @ "), 3);
+ if (sides != null) return sides;
+ // " at " only before a name: "Pat at the Ballpark Night" is not a game.
+ for (int at = lower.indexOf(" at "); at >= 0; at = lower.indexOf(" at ", at + 1)) {
+ int next = at + 4;
+ if (next < core.length()) {
+ int cp = core.codePointAt(next);
+ if (Character.isUpperCase(cp) || Character.isDigit(cp)) {
+ sides = sides(core, at, 4);
+ if (sides != null) return sides;
+ }
+ }
+ }
+ for (String vs : new String[] {" vs. ", " vs ", " v. ", " v "}) {
+ sides = sides(core, lower.indexOf(vs), vs.length());
+ if (sides != null) return new String[] {sides[1], sides[0]};
+ }
+ return null;
+ }
+
+ private static String[] sides(String core, int at, int length) {
+ if (at <= 0) return null;
+ String left = core.substring(0, at).trim();
+ String right = core.substring(at + length).trim();
+ return left.isEmpty() || right.isEmpty() ? null : new String[] {left, right};
+ }
+}
diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/TitlesTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/TitlesTest.java
new file mode 100644
index 000000000..4c2333945
--- /dev/null
+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/TitlesTest.java
@@ -0,0 +1,69 @@
+package io.github.justinnewman.gameday;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.junit.Test;
+
+public class TitlesTest {
+ @Test public void cleanRemovesLeadingEmojiOnly() {
+ assertEquals("Dallas Stars @ Minnesota Wild", Titles.clean("πŸ’ Dallas Stars @ Minnesota Wild"));
+ assertEquals("The Masters", Titles.clean("🏌️ The Masters"));
+ assertEquals("2027 NHL Draft", Titles.clean(" πŸ’ 2027 NHL Draft "));
+ assertEquals("", Titles.clean("πŸ’"));
+ assertEquals("Wild πŸ’ night", Titles.clean("Wild πŸ’ night"));
+ }
+
+ @Test public void separators() {
+ assertEquals("Minnesota Wild at Dallas Stars", Titles.topLine("πŸ’ Minnesota Wild @ Dallas Stars", "NHL"));
+ assertEquals("Minnesota Wild at Dallas Stars", Titles.topLine("Minnesota Wild at Dallas Stars", "NHL"));
+ assertEquals("Portland Thorns at Utah Royals", Titles.topLine("Utah Royals vs Portland Thorns", "NWSL"));
+ assertEquals("Portland Thorns at Utah Royals", Titles.topLine("Utah Royals v. Portland Thorns", "NWSL"));
+ assertArrayEquals(new String[] {"Minnesota Wild", "Dallas Stars"}, Titles.matchup("Minnesota Wild @ Dallas Stars"));
+ assertArrayEquals(new String[] {"Portland Thorns", "Utah Royals"}, Titles.matchup("Utah Royals VS Portland Thorns"));
+ }
+
+ @Test public void decoratedTitles() {
+ assertEquals("Minnesota Wild at Dallas Stars (Preseason)", Titles.topLine("πŸ’ Minnesota Wild @ Dallas Stars (Preseason)", "NHL"));
+ assertEquals("Portland Thorns at Utah Royals - NWSL", Titles.topLine("Utah Royals vs. Portland Thorns - NWSL", "x"));
+ assertEquals("WILD at STARS", Titles.topLine("WILD AT STARS", "x"));
+ }
+
+ @Test public void notAMatchup() {
+ assertEquals("2027 NHL Draft", Titles.topLine("πŸ’ 2027 NHL Draft", "NHL"));
+ assertEquals("Mexico City Grand Prix - Qualifying", Titles.topLine("Mexico City Grand Prix - Qualifying", "F1"));
+ assertNull(Titles.matchup("Pat at the Ballpark Night"));
+ assertNull(Titles.matchup("Welcome to the Minnesota Wild calendar!"));
+ }
+
+ @Test public void emojiOnlyTitleUsesCalendarName() {
+ assertEquals("Minnesota Wild", Titles.topLine("πŸ’", "Minnesota Wild"));
+ assertEquals("Minnesota Wild", Titles.topLine("", "Minnesota Wild"));
+ }
+
+ @Test public void words() {
+ Sport hockey = Sport.byLabel("Hockey");
+ Sport f1 = Sport.byLabel("Motor racing, light start");
+ Sport golf = Sport.byLabel("Golf");
+ Sport tennis = Sport.byLabel("Tennis");
+ assertEquals("Puck drop", Titles.word(hockey, "πŸ’ Minnesota Wild @ Dallas Stars"));
+ assertEquals("Starts", Titles.word(hockey, "πŸ’ 2027 NHL Draft"));
+ assertEquals("Lights out", Titles.word(f1, "Mexico City Grand Prix"));
+ assertEquals("Starts", Titles.word(f1, "Mexico City Grand Prix - Qualifying"));
+ assertEquals("Starts", Titles.word(f1, "FP2 Mexico City"));
+ assertEquals("Starts", Titles.word(f1, "Sprint Qualifying, Austin"));
+ assertEquals("Lights out", Titles.word(f1, "Sprint, Austin"));
+ assertEquals("Tee time", Titles.word(golf, "The Masters, Round 1"));
+ assertEquals("First serve", Titles.word(tennis, "Sinner vs Alcaraz"));
+ }
+
+ @Test public void likelyTeam() {
+ assertEquals("Minnesota Wild", Titles.likelyTeam(Arrays.asList(
+ "πŸ’ Minnesota Wild @ Dallas Stars", "πŸ’ Colorado Avalanche @ Minnesota Wild", "πŸ’ 2027 NHL Draft")));
+ assertNull(Titles.likelyTeam(Collections.singletonList("πŸ’ 2027 NHL Draft")));
+ }
+}