Search in sources :

Example 21 with RMetric

use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.

the class CheckoutEncoder method encodeCheckout.

/**
 * This method will encode a checkout with as little overhead as possible (as opposed to JSON
 * serialization). This method is used for QR serialization to minimize the data transfer, since
 * compression isn't supported well by QR. This could be expanded for the whole project, but isn't really versatile enough yet.
 * @return The encoded string
 */
public String encodeCheckout(String nameTag, RCheckout checkout) {
    StringBuilder builder = new StringBuilder();
    // Checkout components
    builder.append(checkout.getID()).append("\n");
    builder.append(nameTag).append("\n");
    // Team meta
    builder.append(checkout.getTeam().getID()).append("\n");
    builder.append(checkout.getTeam().getLastEdit()).append("\n");
    // Tabs!
    for (RTab tab : checkout.getTeam().getTabs()) {
        builder.append("TAB").append(tab.getTitle()).append("\n");
        builder.append(tab.isWon()).append("\n");
        // Edits
        builder.append("EDITS");
        if (tab.getEdits() != null) {
            for (Object o : tab.getEdits().keySet()) {
                builder.append(",");
                if (o.toString().equals(""))
                    builder.append("Unknown");
                else
                    builder.append(o.toString());
                // EDITS,will:120391823,john,12039123
                builder.append(",").append(tab.getEdits().get(o.toString()));
            }
        }
        builder.append("\n");
        // -RDivider and RCalculation don't need to be encoded since they don't contain any user information
        for (RMetric metric : tab.getMetrics()) {
            builder.append(getMetricType(metric)).append(DELIMITER).append(metric.getID()).append(DELIMITER).append(metric.getTitle()).append(DELIMITER).append(metric.isModified()).append(DELIMITER);
            if (metric instanceof RBoolean)
                builder.append(((RBoolean) metric).isValue());
            else if (metric instanceof RCheckbox) {
                if (((RCheckbox) metric).getValues() != null) {
                    for (Object o : ((RCheckbox) metric).getValues().keySet()) {
                        // :(title,value):(title,value):
                        builder.append("(").append(o.toString()).append(",").append(((RCheckbox) metric).getValues().get(o.toString())).append(")").append(DELIMITER);
                    }
                }
            } else if (metric instanceof RChooser) {
                builder.append(((RChooser) metric).getSelectedIndex()).append(DELIMITER);
                if (((RChooser) metric).getValues() != null) {
                    for (String s : ((RChooser) metric).getValues()) {
                        // :1:option:option:option:
                        builder.append(s).append(DELIMITER);
                    }
                }
            } else if (metric instanceof RCounter)
                builder.append(((RCounter) metric).isVerboseInput()).append(DELIMITER).append(((RCounter) metric).getValue()).append(DELIMITER).append(((RCounter) metric).getIncrement());
            else if (metric instanceof RSlider)
                builder.append(((RSlider) metric).getValue()).append(DELIMITER).append(((RSlider) metric).getMin()).append(DELIMITER).append(((RSlider) metric).getMax());
            else if (metric instanceof RStopwatch) {
                builder.append(((RStopwatch) metric).getTime()).append(DELIMITER);
                if (((RStopwatch) metric).getTimes() != null) {
                    for (Double t : ((RStopwatch) metric).getTimes()) {
                        // :curr:1:2:3:
                        builder.append(t).append(DELIMITER);
                    }
                }
            } else if (metric instanceof RTextfield)
                builder.append(((RTextfield) metric).getText());
            builder.append("\n");
        }
    }
    return builder.toString();
}
Also used : RBoolean(com.cpjd.roblu.models.metrics.RBoolean) RChooser(com.cpjd.roblu.models.metrics.RChooser) RTab(com.cpjd.roblu.models.RTab) RTextfield(com.cpjd.roblu.models.metrics.RTextfield) RMetric(com.cpjd.roblu.models.metrics.RMetric) RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) RSlider(com.cpjd.roblu.models.metrics.RSlider) RCounter(com.cpjd.roblu.models.metrics.RCounter)

Example 22 with RMetric

use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.

the class EventMergeTask method run.

@Override
public void run() {
    try {
        // Load teams for each event
        RTeam[] localTeams = io.loadTeams(localEventID);
        RTeam[] targetTeams = io.loadTeams(targetEventID);
        RForm localForm = io.loadForm(localEventID);
        RForm targetForm = io.loadForm(targetEventID);
        for (RTeam team : localTeams) team.verify(localForm);
        for (RTeam team : targetTeams) team.verify(targetForm);
        // Start merging
        for (RTeam local : localTeams) {
            // Find the team
            for (RTeam target : targetTeams) {
                if (local.getNumber() == target.getNumber()) {
                    for (RTab localTab : local.getTabs()) {
                        // Find tab
                        for (RTab targetTab : target.getTabs()) {
                            if (targetTab.getTitle().equalsIgnoreCase(localTab.getTitle())) {
                                // Match found, start merging metrics
                                for (RMetric localMetric : localTab.getMetrics()) {
                                    // Find metric
                                    for (RMetric targetMetric : targetTab.getMetrics()) {
                                        /*
                                         * Okay, the forms might be different, so we need to run some checks before we override the localMetric:
                                         * -Same instanceof type (obviously)
                                         * -Same title (within reason, do a little trimming, and ignore caps)
                                         * -ID - ID should not be considered, they might not be equal
                                         *
                                         * If we find a good candidate for the metric, override the local. Conditions
                                         * -Target is not modified: do nothing
                                         * -Target is modified: overwrite if local not modified
                                         * -Both modified: Compare team last edit time stamp
                                         */
                                        if ((localMetric.getClass().equals(targetMetric.getClass())) && localMetric.getTitle().toLowerCase().replaceAll(" ", "").equals(targetMetric.getTitle().toLowerCase().replaceAll(" ", ""))) {
                                            if (localMetric instanceof RGallery) {
                                                // Just add the images
                                                if (((RGallery) localMetric).getPictureIDs() == null)
                                                    ((RGallery) localMetric).setPictureIDs(new ArrayList<Integer>());
                                                if (((RGallery) localMetric).getImages() != null) {
                                                    // Add images to the current gallery
                                                    for (int i = 0; i < ((RGallery) targetMetric).getImages().size(); i++) {
                                                        ((RGallery) localMetric).getPictureIDs().add(io.savePicture(localEventID, ((RGallery) targetMetric).getImages().get(i)));
                                                    }
                                                }
                                                // Don't forget to clear the pictures from memory after they've been merged
                                                ((RGallery) targetMetric).setImages(null);
                                                local.setLastEdit(target.getLastEdit());
                                            } else // Alright, looks like we can do the checks now
                                            if (targetMetric.isModified() && !localMetric.isModified()) {
                                                int tabIndex = local.getTabs().indexOf(localTab);
                                                int metricIndex = local.getTabs().get(tabIndex).getMetrics().indexOf(localMetric);
                                                local.getTabs().get(tabIndex).getMetrics().set(metricIndex, targetMetric);
                                                local.setLastEdit(target.getLastEdit());
                                            } else if (targetMetric.isModified() && localMetric.isModified() && target.getLastEdit() > local.getLastEdit()) {
                                                int tabIndex = local.getTabs().indexOf(localTab);
                                                int metricIndex = local.getTabs().get(tabIndex).getMetrics().indexOf(localMetric);
                                                local.getTabs().get(tabIndex).getMetrics().set(metricIndex, targetMetric);
                                                local.setLastEdit(target.getLastEdit());
                                            }
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            io.saveTeam(localEventID, local);
        }
        listener.success();
    } catch (Exception e) {
        Log.d("RBS", "Error occurred in EventMergeTask: " + e.getMessage());
        listener.error();
    }
}
Also used : RForm(com.cpjd.roblu.models.RForm) RTeam(com.cpjd.roblu.models.RTeam) RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 23 with RMetric

use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.

the class Utils method createEmpty.

/**
 * Creates an empty form for an event (not technically empty).
 *
 * Makes sure that nothing is null and that we have the required
 * team name and team number fields
 * @return creates an empty RForm
 */
public static RForm createEmpty() {
    ArrayList<RMetric> pit = new ArrayList<>();
    ArrayList<RMetric> matches = new ArrayList<>();
    RTextfield name = new RTextfield(0, "Team name", false, true, "");
    RTextfield number = new RTextfield(1, "Team number", true, true, "");
    pit.add(name);
    pit.add(number);
    return new RForm(pit, matches);
}
Also used : RForm(com.cpjd.roblu.models.RForm) RTextfield(com.cpjd.roblu.models.metrics.RTextfield) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 24 with RMetric

use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.

the class SyncHelper method mergeCheckout.

/**
 * Merges the checkout with the active event.
 * Creates new team / match if they don't already exist
 * @param checkout the checkout to merge
 */
public void mergeCheckout(RCheckout checkout) {
    // Verify the target checkout, to make sure it's in sync with the local form
    checkout.getTeam().verify(form);
    // Check to see if there is a local team matching this checkout
    RTeam team = io.loadTeam(activeEvent.getID(), checkout.getTeam().getID());
    // The team was found, so do a merge
    if (team != null) {
        team.verify(form);
        boolean shouldOverrideLastEdited = false;
        for (RTab downloadedTab : checkout.getTeam().getTabs()) {
            boolean matchLocated = false;
            for (RTab localTab : team.getTabs()) {
                localTab.setWon(downloadedTab.isWon());
                // Found the match, start merging
                if (localTab.getTitle().equalsIgnoreCase(downloadedTab.getTitle())) {
                    /*
                         * Copy over the edit tabs
                         */
                    if (downloadedTab.getEdits() != null)
                        localTab.setEdits(downloadedTab.getEdits());
                    for (RMetric downloadedMetric : downloadedTab.getMetrics()) {
                        if (!(downloadedMetric instanceof RCalculation) && !(downloadedMetric instanceof RFieldData) && downloadedMetric.isModified())
                            shouldOverrideLastEdited = true;
                        for (RMetric localMetric : localTab.getMetrics()) {
                            // Found the metric, determine if a merge needs to occur
                            if (downloadedMetric.getID() == localMetric.getID()) {
                                // Ignore imports from this metric
                                if (downloadedMetric instanceof RFieldData)
                                    break;
                                /*
                                     * We have to deal with one special case scenario - the gallery.
                                     * The gallery should never be overrided, just added to
                                     */
                                if (downloadedMetric instanceof RGallery && localMetric instanceof RGallery) {
                                    if (((RGallery) localMetric).getPictureIDs() == null)
                                        ((RGallery) localMetric).setPictureIDs(new ArrayList<Integer>());
                                    if (((RGallery) downloadedMetric).getImages() != null) {
                                        // Add images to the current gallery
                                        for (int i = 0; i < ((RGallery) downloadedMetric).getImages().size(); i++) {
                                            ((RGallery) localMetric).getPictureIDs().add(io.savePicture(activeEvent.getID(), ((RGallery) downloadedMetric).getImages().get(i)));
                                        }
                                    }
                                    // Don't forget to clear the pictures from memory after they've been merged
                                    ((RGallery) downloadedMetric).setImages(null);
                                    break;
                                }
                                // If the local metric is already edited, keep whichever data is newest
                                if (localMetric.isModified()) {
                                    if (checkout.getTeam().getLastEdit() >= team.getLastEdit()) {
                                        int replaceIndex = localTab.getMetrics().indexOf(localMetric);
                                        localTab.getMetrics().set(replaceIndex, downloadedMetric);
                                    }
                                } else // Otherwise, just do a straight override
                                {
                                    int replaceIndex = localTab.getMetrics().indexOf(localMetric);
                                    localTab.getMetrics().set(replaceIndex, downloadedMetric);
                                }
                                break;
                            }
                        }
                    }
                    matchLocated = true;
                    break;
                }
            }
            if (!matchLocated) {
                // Add as a new match if a merge wasn't performed
                team.addTab(checkout.getTeam().getTabs().get(0));
                Collections.sort(team.getTabs());
            }
        }
        if (shouldOverrideLastEdited)
            team.setLastEdit(checkout.getTeam().getLastEdit());
    } else // The team was not found locally, create a new one
    {
        Log.d("RBS", "Team was not found, creating a new one.");
        team = new RTeam(checkout.getTeam().getName(), checkout.getTeam().getNumber(), checkout.getTeam().getID());
        team.setLastEdit(checkout.getTeam().getLastEdit());
        team.verify(form);
        if (checkout.getTeam().getTabs().size() > 1) {
            // this means the downloaded team was a PIT tab, so override the new team's tabs
            team.setTabs(checkout.getTeam().getTabs());
        } else {
            // otherwise just add them
            team.addTab(checkout.getTeam().getTabs().get(0));
        }
    }
    // Save the team
    io.saveTeam(activeEvent.getID(), team);
    // Request a UI refresh
    Utils.requestTeamViewerRefresh(context, team.getID());
    Log.d("RBS-Service", "Merged the team: " + checkout.getTeam().getName());
}
Also used : RTeam(com.cpjd.roblu.models.RTeam) RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) RCalculation(com.cpjd.roblu.models.metrics.RCalculation)

Example 25 with RMetric

use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.

the class FieldData method generateSheet.

@Override
public void generateSheet(XSSFSheet sheet, REvent event, RForm form, RTeam[] teams, ArrayList<RCheckout> checkouts) {
    Row one = createRow(sheet);
    createCell(one, 0, "Team#");
    createCell(one, 1, "Match#");
    // Find a random RFieldData reference
    RFieldData fieldData = null;
    try {
        mainLoop: for (RTab tab : teams[0].getTabs()) {
            if (tab.getTitle().equalsIgnoreCase("PIT") || tab.getTitle().equalsIgnoreCase("PREDICTIONS"))
                continue;
            for (RMetric metric2 : tab.getMetrics()) {
                if (metric2 instanceof RFieldData) {
                    fieldData = (RFieldData) metric2;
                    break mainLoop;
                }
            }
        }
    } catch (Exception e) {
    // }
    }
    // Copy the metrics over
    int index = 2;
    for (Object key : fieldData.getData().keySet()) {
        createCell(one, index, key.toString());
        index++;
    }
    // Start copying data
    for (RCheckout checkout : checkouts) {
        if (!checkout.getTeam().getTabs().get(0).getTitle().startsWith("Quals"))
            continue;
        Row row = createRow(sheet);
        createCell(row, 0, String.valueOf(checkout.getTeam().getNumber()));
        createCell(row, 1, checkout.getTeam().getTabs().get(0).getTitle());
        index = 0;
        mainLoop: for (RTab tab : checkout.getTeam().getTabs()) {
            for (RMetric metric2 : tab.getMetrics()) {
                if (metric2 instanceof RFieldData) {
                    try {
                        for (Object key : ((RFieldData) metric2).getData().keySet()) {
                            createCell(row, index + 2, ((RFieldData) metric2).getData().get(key).get(tab.isRedAlliance() ? 0 : 1).toString());
                            index++;
                        }
                        break mainLoop;
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
}
Also used : RTab(com.cpjd.roblu.models.RTab) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RCheckout(com.cpjd.roblu.models.RCheckout) Row(org.apache.poi.ss.usermodel.Row) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Aggregations

RMetric (com.cpjd.roblu.models.metrics.RMetric)26 RTab (com.cpjd.roblu.models.RTab)12 RTeam (com.cpjd.roblu.models.RTeam)10 RStopwatch (com.cpjd.roblu.models.metrics.RStopwatch)10 RTextfield (com.cpjd.roblu.models.metrics.RTextfield)10 ArrayList (java.util.ArrayList)10 RCounter (com.cpjd.roblu.models.metrics.RCounter)9 RGallery (com.cpjd.roblu.models.metrics.RGallery)9 IO (com.cpjd.roblu.io.IO)8 RCheckbox (com.cpjd.roblu.models.metrics.RCheckbox)8 RChooser (com.cpjd.roblu.models.metrics.RChooser)8 RSlider (com.cpjd.roblu.models.metrics.RSlider)8 RForm (com.cpjd.roblu.models.RForm)7 RBoolean (com.cpjd.roblu.models.metrics.RBoolean)7 RFieldData (com.cpjd.roblu.models.metrics.RFieldData)7 RCalculation (com.cpjd.roblu.models.metrics.RCalculation)6 RCheckout (com.cpjd.roblu.models.RCheckout)5 LinkedHashMap (java.util.LinkedHashMap)5 Bundle (android.os.Bundle)4 View (android.view.View)4