Search in sources :

Example 1 with IMatchAlliancesContainer

use of com.thebluealliance.api.model.IMatchAlliancesContainer in project the-blue-alliance-android by the-blue-alliance.

the class MatchRenderer method renderFromModel.

/**
 * Renders a MatchListElement for displaying this match. ASSUMES 3v3 match structure with
 * red/blue alliances Use different render methods for other structures
 */
@WorkerThread
@Override
@Nullable
public MatchListElement renderFromModel(Match match, Integer renderMode) {
    RenderArgs args = argsFromMode(renderMode);
    @Nullable IMatchAlliancesContainer alliances = match.getAlliances();
    @Nullable List<IMatchVideo> videos = match.getVideos();
    String key = match.getKey();
    if (key.isEmpty()) {
        return null;
    }
    String redScore = (alliances == null) ? "-1" : Integer.toString(alliances.getRed().getScore());
    String blueScore = (alliances == null) ? "-1" : Integer.toString(alliances.getBlue().getScore());
    if (Integer.parseInt(redScore) < 0)
        redScore = "?";
    if (Integer.parseInt(blueScore) < 0)
        blueScore = "?";
    String youTubeVideoKey = null;
    if (videos != null) {
        for (IMatchVideo video : videos) {
            if ("youtube".equals(video.getType())) {
                youTubeVideoKey = video.getKey();
                break;
            }
        }
    }
    String[] redAlliance, blueAlliance;
    // Add teams based on alliance size (or none if there isn't for some reason)
    List<String> redTeams = (alliances != null) ? alliances.getRed().getTeamKeys() : null;
    if (redTeams != null && redTeams.size() == 3) {
        redAlliance = new String[] { redTeams.get(0).substring(3), redTeams.get(1).substring(3), redTeams.get(2).substring(3) };
    } else if (redTeams != null && redTeams.size() == 2) {
        redAlliance = new String[] { redTeams.get(0).substring(3), redTeams.get(1).substring(3) };
    } else {
        redAlliance = new String[] { "", "", "" };
    }
    List<String> blueTeams = (alliances != null) ? alliances.getBlue().getTeamKeys() : null;
    if (blueTeams != null && blueTeams.size() == 3) {
        blueAlliance = new String[] { blueTeams.get(0).substring(3), blueTeams.get(1).substring(3), blueTeams.get(2).substring(3) };
    } else if (blueTeams != null && blueTeams.size() == 2) {
        blueAlliance = new String[] { blueTeams.get(0).substring(3), blueTeams.get(1).substring(3) };
    } else {
        blueAlliance = new String[] { "", "", "" };
    }
    long matchTime = match.getTime() != null ? match.getTime() : -1;
    int redExtraRp = 0;
    int blueExtraRp = 0;
    if (match.getYear() >= 2016) {
        JsonObject scoreBreakdown = JSONHelper.getasJsonObject(match.getScoreBreakdown());
        JsonObject redScoreBreakdown = null;
        if (scoreBreakdown.has("red")) {
            redScoreBreakdown = scoreBreakdown.get("red").getAsJsonObject();
        }
        JsonObject blueScoreBreakdown = null;
        if (scoreBreakdown.has("blue")) {
            blueScoreBreakdown = scoreBreakdown.get("blue").getAsJsonObject();
        }
        String rpName1 = null;
        String rpName2 = null;
        switch(match.getYear()) {
            case 2016:
                rpName1 = "teleopDefensesBreached";
                rpName2 = "teleopTowerCaptured";
                break;
            case 2017:
                rpName1 = "kPaRankingPointAchieved";
                rpName2 = "rotorRankingPointAchieved";
                break;
            case 2018:
                rpName1 = "autoQuestRankingPoint";
                rpName2 = "faceTheBossRankingPoint";
                break;
            case 2019:
                rpName1 = "habDockingRankingPoint";
                rpName2 = "completeRocketRankingPoint";
                break;
            case 2020:
                rpName1 = "shieldOperationalRankingPoint";
                rpName2 = "shieldEnergizedRankingPoint";
                break;
        }
        if (rpName1 != null) {
            if (redScoreBreakdown != null && redScoreBreakdown.get(rpName1).getAsBoolean()) {
                redExtraRp++;
            }
            if (blueScoreBreakdown != null && blueScoreBreakdown.get(rpName1).getAsBoolean()) {
                blueExtraRp++;
            }
        }
        if (rpName2 != null) {
            if (redScoreBreakdown != null && redScoreBreakdown.get(rpName2).getAsBoolean()) {
                redExtraRp++;
            }
            if (blueScoreBreakdown != null && blueScoreBreakdown.get(rpName2).getAsBoolean()) {
                blueExtraRp++;
            }
        }
    }
    return new MatchListElement(youTubeVideoKey, match.getTitle(mResources, true), redAlliance, blueAlliance, redScore, blueScore, match.getWinningAlliance(), key, matchTime, match.getSelectedTeam(), args.showVideo, args.showHeaders, args.showMatchTitle, args.clickable, redExtraRp, blueExtraRp);
}
Also used : MatchListElement(com.thebluealliance.androidclient.listitems.MatchListElement) IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) JsonObject(com.google.gson.JsonObject) IMatchVideo(com.thebluealliance.api.model.IMatchVideo) Nullable(javax.annotation.Nullable) WorkerThread(androidx.annotation.WorkerThread) Nullable(javax.annotation.Nullable)

Example 2 with IMatchAlliancesContainer

use of com.thebluealliance.api.model.IMatchAlliancesContainer in project the-blue-alliance-android by the-blue-alliance.

the class ScoreNotification method buildNotification.

@Override
public Notification buildNotification(Context context, FollowsChecker followsChecker) {
    Resources r = context.getResources();
    matchKey = match.getKey();
    String matchTitle = MatchHelper.getMatchTitleFromMatchKey(context, matchKey);
    String matchAbbrevTitle = MatchHelper.getAbbrevMatchTitleFromMatchKey(context, matchKey);
    IMatchAlliancesContainer alliances = match.getAlliances();
    int redScore = Match.getRedScore(alliances);
    int blueScore = Match.getBlueScore(alliances);
    // Boldify the team numbers that the user is following, but only if the system supports
    // java 8 language features
    CharSequence firstTeams;
    CharSequence secondTeams;
    ArrayList<String> redTeams = Match.teamNumbers(Match.getRedTeams(alliances));
    ArrayList<String> blueTeams = Match.teamNumbers(Match.getBlueTeams(alliances));
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Predicate<String> isFollowing = teamNumber -> followsChecker.followsTeam(context, teamNumber, matchKey, NotificationTypes.MATCH_SCORE);
        firstTeams = Utilities.boldNameList(redTeams, isFollowing);
        secondTeams = Utilities.boldNameList(blueTeams, isFollowing);
    } else {
        firstTeams = Utilities.stringifyListOfStrings(context, redTeams);
        secondTeams = Utilities.stringifyListOfStrings(context, blueTeams);
    }
    // Make sure the score string is formatted properly with the winning score first
    String scoreString;
    if (blueScore > redScore) {
        scoreString = blueScore + "-" + redScore;
        CharSequence temp = firstTeams;
        firstTeams = secondTeams;
        secondTeams = temp;
    } else {
        scoreString = redScore + "-" + blueScore;
    }
    MatchType matchType = MatchType.fromShortType(match.getCompLevel());
    boolean useSpecial2015Format = match.getYear() == 2015 && matchType != MatchType.FINAL;
    String eventShortName = EventHelper.shortName(eventName);
    String template;
    if (useSpecial2015Format) {
        // firstTeams played secondTeams (for 2015 non-finals matches)
        template = context.getString(R.string.notification_score_teams_played_teams);
    } else if (blueScore == redScore) {
        // firstTeams tied secondTeams
        template = context.getString(R.string.notification_score_teams_tied_teams);
    } else {
        // firstTeams beat secondTeams
        template = context.getString(R.string.notification_score_teams_beat_teams);
    }
    CharSequence notificationBody = TextUtils.expandTemplate(template, eventShortName, matchTitle, firstTeams, secondTeams, scoreString);
    // We can finally build the notification!
    Intent instance = getIntent(context);
    stored = new StoredNotification();
    stored.setType(getNotificationType());
    String eventCode = EventHelper.getEventCode(matchKey);
    String notificationTitle = r.getString(R.string.notification_score_title, eventCode, matchAbbrevTitle);
    stored.setTitle(notificationTitle);
    stored.setBody(notificationBody.toString());
    stored.setIntent(MyTBAHelper.serializeIntent(instance));
    stored.setTime(Calendar.getInstance().getTime());
    stored.setMessageData(messageData);
    stored.setSystemId(getNotificationId());
    NotificationCompat.Builder builder = getBaseBuilder(context, instance).setContentTitle(notificationTitle).setContentText(notificationBody);
    NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(notificationBody);
    builder.setStyle(style);
    return builder.build();
}
Also used : JsonParseException(com.google.gson.JsonParseException) Context(android.content.Context) JsonObject(com.google.gson.JsonObject) MatchWriter(com.thebluealliance.androidclient.database.writers.MatchWriter) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) NotificationCompat(androidx.core.app.NotificationCompat) Date(java.util.Date) Intent(android.content.Intent) MatchHelper(com.thebluealliance.androidclient.helpers.MatchHelper) ArrayList(java.util.ArrayList) Calendar(java.util.Calendar) Gson(com.google.gson.Gson) MatchType(com.thebluealliance.androidclient.types.MatchType) View(android.view.View) Build(android.os.Build) R(com.thebluealliance.androidclient.R) EventHelper(com.thebluealliance.androidclient.helpers.EventHelper) JSONHelper(com.thebluealliance.androidclient.helpers.JSONHelper) ViewMatchActivity(com.thebluealliance.androidclient.activities.ViewMatchActivity) LayoutInflater(android.view.LayoutInflater) Match(com.thebluealliance.androidclient.models.Match) TextUtils(android.text.TextUtils) MatchRenderer(com.thebluealliance.androidclient.renderers.MatchRenderer) ScoreNotificationViewModel(com.thebluealliance.androidclient.viewmodels.ScoreNotificationViewModel) IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) MyTBAHelper(com.thebluealliance.androidclient.helpers.MyTBAHelper) MatchListElement(com.thebluealliance.androidclient.listitems.MatchListElement) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) Predicate(com.google.common.base.Predicate) Utilities(com.thebluealliance.androidclient.Utilities) FollowsChecker(com.thebluealliance.androidclient.gcm.FollowsChecker) GamedayTickerClickListener(com.thebluealliance.androidclient.listeners.GamedayTickerClickListener) Notification(android.app.Notification) Resources(android.content.res.Resources) MatchView(com.thebluealliance.androidclient.views.MatchView) StoredNotification(com.thebluealliance.androidclient.models.StoredNotification) Intent(android.content.Intent) MatchType(com.thebluealliance.androidclient.types.MatchType) IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) NotificationCompat(androidx.core.app.NotificationCompat) Resources(android.content.res.Resources)

Example 3 with IMatchAlliancesContainer

use of com.thebluealliance.api.model.IMatchAlliancesContainer in project the-blue-alliance-android by the-blue-alliance.

the class MatchTest method testMatchModel.

@Test
public void testMatchModel() {
    assertNotNull(mMatch);
    assertEquals(mMatch.getKey(), "2014cmp_f1m1");
    assertEquals(mMatch.getMatchNumber().intValue(), 1);
    assertEquals(mMatch.getSetNumber().intValue(), 1);
    assertEquals(mMatch.getEventKey(), "2014cmp");
    assertNotNull(mMatch.getTime());
    assertEquals(mMatch.getTime().intValue(), 1398551880);
    assertNotNull(mMatch.getVideos());
    assertNotNull(mMatch.getAlliances());
    List<IMatchVideo> videos = mMatch.getVideos();
    assertEquals(videos.size(), 2);
    IMatchVideo video1 = videos.get(0);
    assertEquals(video1.getType(), "youtube");
    assertEquals(video1.getKey(), "jdJutaggCMk");
    IMatchAlliancesContainer alliances = mMatch.getAlliances();
    IMatchAlliance blueAlliance = alliances.getBlue();
    assertEquals(blueAlliance.getScore().intValue(), 361);
    List<String> blueTeams = blueAlliance.getTeamKeys();
    assertEquals(blueTeams.size(), 3);
    assertEquals(blueTeams.get(0), "frc469");
}
Also used : IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) IMatchAlliance(com.thebluealliance.api.model.IMatchAlliance) IMatchVideo(com.thebluealliance.api.model.IMatchVideo) Test(org.junit.Test)

Example 4 with IMatchAlliancesContainer

use of com.thebluealliance.api.model.IMatchAlliancesContainer in project the-blue-alliance-android by the-blue-alliance.

the class MatchBreakdownSubscriber method parseData.

@Override
public void parseData() {
    JsonObject scoreBreakdown;
    IMatchAlliancesContainer alliances;
    boolean shouldShowBreakdown = false;
    switch(mAPIData.getYear()) {
        case 2015:
        case 2016:
            shouldShowBreakdown = true;
            break;
        case 2017:
            shouldShowBreakdown = mConfig.getBoolean(SHOW_2017_KEY);
            TbaLogger.i("Showing 2017 breakdowns? " + shouldShowBreakdown);
            break;
        case 2018:
            shouldShowBreakdown = mConfig.getBoolean(SHOW_2018_KEY);
            TbaLogger.i("Showing 2018 breakdowns? " + shouldShowBreakdown);
            break;
        case 2019:
            shouldShowBreakdown = mConfig.getBoolean(SHOW_2019_KEY);
            TbaLogger.i("Showing 2019 breakdowns? " + shouldShowBreakdown);
            break;
        case 2020:
            shouldShowBreakdown = mConfig.getBoolean(SHOW_2020_KEY);
            TbaLogger.i("Showing 2020 breakdown? " + shouldShowBreakdown);
            break;
        case 2022:
            shouldShowBreakdown = mConfig.getBoolean(SHOW_2022_KEY);
            TbaLogger.i("Showing 2020 breakdown? " + shouldShowBreakdown);
            break;
    }
    mDataToBind = null;
    if (shouldShowBreakdown) {
        scoreBreakdown = mGson.fromJson(mAPIData.getScoreBreakdown(), JsonObject.class);
        alliances = mAPIData.getAlliances();
        if (scoreBreakdown == null || scoreBreakdown.entrySet().isEmpty() || alliances == null) {
            mDataToBind = null;
            return;
        }
        mDataToBind = new MatchBreakdownBinder.Model(mAPIData.getType(), mAPIData.getYear(), mAPIData.getWinningAlliance(), alliances, scoreBreakdown);
    }
}
Also used : MatchBreakdownBinder(com.thebluealliance.androidclient.binders.MatchBreakdownBinder) IMatchAlliancesContainer(com.thebluealliance.api.model.IMatchAlliancesContainer) JsonObject(com.google.gson.JsonObject)

Aggregations

IMatchAlliancesContainer (com.thebluealliance.api.model.IMatchAlliancesContainer)4 JsonObject (com.google.gson.JsonObject)3 MatchListElement (com.thebluealliance.androidclient.listitems.MatchListElement)2 IMatchVideo (com.thebluealliance.api.model.IMatchVideo)2 Notification (android.app.Notification)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Build (android.os.Build)1 TextUtils (android.text.TextUtils)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 TextView (android.widget.TextView)1 Nullable (androidx.annotation.Nullable)1 WorkerThread (androidx.annotation.WorkerThread)1 NotificationCompat (androidx.core.app.NotificationCompat)1 Predicate (com.google.common.base.Predicate)1 Gson (com.google.gson.Gson)1 JsonParseException (com.google.gson.JsonParseException)1 R (com.thebluealliance.androidclient.R)1