Search in sources :

Example 11 with RTab

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

the class InitPacker method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    /*
         * Make sure this thread has network permissions
         */
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
    Log.d("RBS", "Executing InitPacker task...");
    IO io = ioWeakReference.get();
    RSettings settings = io.loadSettings();
    RSyncSettings cloudSettings = io.loadCloudSettings();
    cloudSettings.setPurgeRequested(false);
    io.saveCloudSettings(cloudSettings);
    io.saveSettings(settings);
    Request r = new Request(settings.getServerIP());
    if (!r.ping()) {
        listener.statusUpdate("It appears as though the server is offline. Try again later.");
        return false;
    }
    /*
         * Load all teams from the event, also make sure that that the teams are verified
         */
    REvent event = io.loadEvent(eventID);
    event.setReadOnlyTeamNumber(-1);
    RForm form = io.loadForm(eventID);
    RTeam[] teams = io.loadTeams(eventID);
    if (event == null || form == null || teams == null || teams.length == 0) {
        Log.d("RBS", "Not enough data to warrant an event upload.");
        listener.statusUpdate("This event doesn't contain any teams or sufficient data to upload to the server. Create some teams!");
        return false;
    }
    // Generate the checkouts
    SyncHelper syncHelper = new SyncHelper(io, event, SyncHelper.MODES.NETWORK);
    ArrayList<RCheckout> checkouts = syncHelper.generateCheckoutsFromEvent(teams, -1);
    // Remove field data
    try {
        for (RCheckout checkout : checkouts) {
            for (RTab tab : checkout.getTeam().getTabs()) {
                for (RMetric metric : tab.getMetrics()) {
                    if (metric instanceof RFieldData) {
                        ((RFieldData) metric).setData(null);
                    }
                }
            }
        }
    } catch (Exception e) {
    // Doesn't matter
    }
    /*
         * Convert into JSON and upload
         */
    ObjectMapper mapper = new ObjectMapper();
    try {
        // serialization all the checkouts and pack them in an json array, this will be processed by the server
        String serializedCheckouts = syncHelper.packCheckouts(checkouts);
        String serializedForm = mapper.writeValueAsString(form);
        String serializedUI = mapper.writeValueAsString(settings.getRui());
        String eventName = event.getName();
        if (eventName == null)
            eventName = "";
        if (event.getKey() == null)
            event.setKey("");
        CloudCheckoutRequest ccr = new CloudCheckoutRequest(r, settings.getCode());
        Log.d("RBS", "Initializing init packer upload...");
        boolean success = ccr.init(settings.getTeamNumber(), eventName, serializedForm, serializedUI, serializedCheckouts, event.getKey());
        /*
             * Disable all other events with cloud syncing enabled
             */
        if (success) {
            REvent[] events = io.loadEvents();
            for (int i = 0; events != null && i < events.length; i++) {
                events[i].setCloudEnabled(events[i].getID() == eventID);
                io.saveEvent(events[i]);
            }
            cloudSettings.getCheckoutSyncIDs().clear();
            /*
                 * Add default sync ids
                 */
            for (RCheckout checkout : checkouts) {
                cloudSettings.getCheckoutSyncIDs().put(checkout.getID(), 0L);
            }
            io.saveCloudSettings(cloudSettings);
            io.saveSettings(settings);
        } else
            listener.statusUpdate("An error occurred. Event was not uploaded.");
        return success;
    } catch (Exception e) {
        Log.d("RBS", "An error occurred in InitPacker: " + e.getMessage());
        listener.statusUpdate("An error occurred. Event was not uploaded.");
        return false;
    } finally {
        /*
             * Set all images to null to return memory to normal
             */
        for (RCheckout checkout : checkouts) {
            for (RTab tab : checkout.getTeam().getTabs()) {
                for (RMetric metric : tab.getMetrics()) {
                    if (metric instanceof RGallery) {
                        ((RGallery) metric).setImages(null);
                    }
                }
            }
        }
    }
}
Also used : RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RMetric(com.cpjd.roblu.models.metrics.RMetric) StrictMode(android.os.StrictMode) RForm(com.cpjd.roblu.models.RForm) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper) RCheckout(com.cpjd.roblu.models.RCheckout) RSyncSettings(com.cpjd.roblu.models.RSyncSettings) RSettings(com.cpjd.roblu.models.RSettings) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest) RTeam(com.cpjd.roblu.models.RTeam) IO(com.cpjd.roblu.io.IO) Request(com.cpjd.http.Request) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest)

Example 12 with RTab

use of com.cpjd.roblu.models.RTab 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 13 with RTab

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

the class SyncTBAEvent method syncTeam.

private void syncTeam(RTeam team) {
    team.verify(form);
    for (int i = 0; i < event.matches.length; i++) {
        if (event.matches[i].doesMatchContainTeam(team.getNumber()) == -1)
            continue;
        RTab newTab = matchModelToTab(i, team.getNumber());
        if (newTab == null)
            continue;
        // Search for the match within the team
        boolean found = false;
        for (RTab t : team.getTabs()) {
            if (t.getTitle().equalsIgnoreCase(newTab.getTitle())) {
                newTab = t;
                found = true;
                break;
            }
        }
        // Add the new match
        if (!found) {
            team.addTab(newTab);
            Collections.sort(team.getTabs());
        }
        /*
            * Process field data
            */
        if (team.getTabs() != null) {
            insertFieldData(i, newTab);
        }
    }
    // Save the team
    io.saveTeam(eventID, team);
}
Also used : RTab(com.cpjd.roblu.models.RTab)

Example 14 with RTab

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

the class SyncTBAEvent method matchModelToTab.

private RTab matchModelToTab(int index, int teamNumber) {
    int result = event.matches[index].doesMatchContainTeam(teamNumber);
    if (result > 0) {
        String name = "Match";
        // process the correct match name
        switch(event.matches[index].comp_level) {
            case "qm":
                name = "Quals " + event.matches[index].match_number;
                break;
            case "qf":
                name = "Quarters " + event.matches[index].set_number + " Match " + event.matches[index].match_number;
                break;
            case "sf":
                name = "Semis " + event.matches[index].set_number + " Match " + event.matches[index].match_number;
                break;
            case "f":
                name = "Finals " + event.matches[index].match_number;
        }
        boolean isRed = result == com.cpjd.main.Constants.CONTAINS_TEAM_RED;
        // add the match to the team, make sure to multiple the Event model's matches times by 1000 (seconds to milliseconds, Roblu works with milliseconds!)
        RTab tab = new RTab(teamNumber, name, Utils.duplicateRMetricArray(form.getMatch()), isRed, event.matches[index].isOnWinningAlliance(teamNumber), event.matches[index].time * 1000);
        // set the match position, if possible
        tab.setAlliancePosition(event.matches[index].getTeamPosition(teamNumber));
        return tab;
    }
    return null;
}
Also used : RTab(com.cpjd.roblu.models.RTab)

Example 15 with RTab

use of com.cpjd.roblu.models.RTab 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)

Aggregations

RTab (com.cpjd.roblu.models.RTab)18 RTeam (com.cpjd.roblu.models.RTeam)13 RMetric (com.cpjd.roblu.models.metrics.RMetric)12 IO (com.cpjd.roblu.io.IO)8 ArrayList (java.util.ArrayList)8 RCheckout (com.cpjd.roblu.models.RCheckout)6 RForm (com.cpjd.roblu.models.RForm)6 RGallery (com.cpjd.roblu.models.metrics.RGallery)6 RCounter (com.cpjd.roblu.models.metrics.RCounter)5 RFieldData (com.cpjd.roblu.models.metrics.RFieldData)5 REvent (com.cpjd.roblu.models.REvent)4 RBoolean (com.cpjd.roblu.models.metrics.RBoolean)4 RCheckbox (com.cpjd.roblu.models.metrics.RCheckbox)4 RChooser (com.cpjd.roblu.models.metrics.RChooser)4 RSlider (com.cpjd.roblu.models.metrics.RSlider)4 RStopwatch (com.cpjd.roblu.models.metrics.RStopwatch)4 RTextfield (com.cpjd.roblu.models.metrics.RTextfield)4 Bundle (android.os.Bundle)3 LinkedHashMap (java.util.LinkedHashMap)3 Intent (android.content.Intent)2