use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class EventDepacker method run.
@Override
public void run() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
Log.d("RBS", "Executing EventDepacker task...");
ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
RSettings settings = io.loadSettings();
RSyncSettings cloudSettings = io.loadCloudSettings();
cloudSettings.setTeamSyncID(0);
cloudSettings.getCheckoutSyncIDs().clear();
cloudSettings.setPurgeRequested(false);
io.saveCloudSettings(cloudSettings);
Request r = new Request(settings.getServerIP());
CloudTeamRequest ctr = new CloudTeamRequest(r, settings.getCode());
if (teamNumber != -1) {
ctr.setCode("");
ctr.setTeamNumber(teamNumber);
}
CloudCheckoutRequest ccr = new CloudCheckoutRequest(r, settings.getCode());
if (teamNumber != -1) {
ccr.setTeamCode("");
ccr.setTeamNumber(teamNumber);
}
if (teamNumber == -1 && (settings.getCode() == null || settings.getCode().equals(""))) {
if (listener != null)
listener.errorOccurred("No team code found in settings. Unable to import event.");
return;
}
// Ping
if (!r.ping()) {
if (listener != null)
listener.errorOccurred("It appears as though the server is offline. Try again later.");
return;
}
if (!ctr.isActive()) {
if (listener != null)
listener.errorOccurred("No event found on Roblu Cloud.");
return;
}
/*
* Download everything
*
*/
CloudTeam team = ctr.getTeam(-1);
REvent event;
try {
// Create a new event
event = new REvent(io.getNewEventID(), team.getActiveEventName());
event.setKey(team.getTbaKey());
// should be -1 if cloud is not enabled
event.setReadOnlyTeamNumber(teamNumber);
event.setID(io.getNewEventID());
event.setCloudEnabled(true);
io.saveEvent(event);
settings.setTeamNumber((int) team.getNumber());
settings.setRui(mapper.readValue(team.getUi(), RUI.class));
io.saveSettings(settings);
RForm form = mapper.readValue(team.getForm(), RForm.class);
io.saveForm(event.getID(), form);
} catch (Exception e) {
Log.d("RBS", "Failed to download event");
listener.errorOccurred("Failed to import Roblu Cloud event.");
return;
}
/*
* Un-package checkouts into a teams array
*/
ArrayList<RCheckout> checkouts = new ArrayList<>();
try {
CloudCheckout[] pulledCheckouts = ccr.pullCheckouts(null, true);
for (CloudCheckout s : pulledCheckouts) checkouts.add(mapper.readValue(s.getContent(), RCheckout.class));
} catch (IOException e) {
Log.d("RBS", "Failed to de-package checkouts.");
listener.errorOccurred("Failed to import Roblu Cloud event.");
return;
}
/*
* Start sorting the checkouts into teams
*/
ArrayList<RTeam> teams = new ArrayList<>();
for (RCheckout checkout : checkouts) {
// First, check if the team has already been created
boolean found = false;
for (RTeam t : teams) {
if (t.getID() == checkout.getTeam().getID()) {
// Add the checkout information to the team
t.getTabs().addAll(checkout.getTeam().getTabs());
found = true;
break;
}
t.setLastEdit(checkout.getTime());
}
// If not found, create a new team
if (!found) {
RTeam newTeam = new RTeam(checkout.getTeam().getName(), checkout.getTeam().getNumber(), checkout.getTeam().getID());
newTeam.setTabs(new ArrayList<RTab>());
newTeam.getTabs().addAll(checkout.getTeam().getTabs());
teams.add(newTeam);
}
}
Log.d("RBS", "Created " + teams.size() + " teams");
/*
* Unpack images
*/
for (RCheckout checkout : checkouts) {
for (RTab tab : checkout.getTeam().getTabs()) {
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RGallery) {
for (int i = 0; ((RGallery) metric).getImages() != null && i < ((RGallery) metric).getImages().size(); i++) {
int picID = io.savePicture(event.getID(), ((RGallery) metric).getImages().get(i));
if (picID != -1) {
((RGallery) metric).setPictureIDs(new ArrayList<Integer>());
((RGallery) metric).getPictureIDs().add(picID);
}
}
if (((RGallery) metric).getImages() != null)
((RGallery) metric).getImages().clear();
}
}
}
}
/*
* Save teams
* -Teams don't need to be verified since the form has also been pulled from the server
*/
for (RTeam t : teams) {
Collections.sort(t.getTabs());
io.saveTeam(event.getID(), t);
}
// Remove all the other synced events
REvent[] events = io.loadEvents();
for (int i = 0; events != null && i < events.length; i++) {
events[i].setCloudEnabled(events[i].getID() == event.getID());
io.saveEvent(events[i]);
}
/*
* Add default sync ids
*/
for (RCheckout checkout : checkouts) {
cloudSettings.getCheckoutSyncIDs().put(checkout.getID(), 0L);
}
io.saveCloudSettings(cloudSettings);
if (listener != null) {
listener.success(event);
}
}
use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class CheckoutEncoder method decodeCheckout.
/**
* Decodes a checkout encoded in the CheckoutEncoder format
* @param string the string to deserialize
* @return an instantiated checkout from the string
*/
public RCheckout decodeCheckout(String string) {
try {
String[] lines = string.split("\n");
for (String s : lines) {
Log.d("RBS", "Line: " + s);
}
// Meta
RCheckout checkout = new RCheckout();
checkout.setID(Integer.parseInt(lines[0]));
checkout.setNameTag(lines[1]);
RTeam team = new RTeam();
team.setID(Integer.parseInt(lines[2]));
team.setLastEdit(Long.parseLong(lines[3]));
team.setTabs(new ArrayList<RTab>());
// Tabs
for (int i = 0; i < lines.length; i++) {
if (!lines[i].startsWith("TAB"))
continue;
// Tab meta
RTab tab = new RTab();
tab.setTitle(lines[i].substring(3));
tab.setWon(Boolean.parseBoolean(lines[i + 1]));
tab.setMetrics(new ArrayList<RMetric>());
String[] tokens = lines[i + 2].split(",");
LinkedHashMap<String, Long> edits = new LinkedHashMap<>();
for (int k = 1; k < tokens.length; k++) {
edits.put(tokens[1], Long.parseLong(tokens[2]));
}
tab.setEdits(edits);
// Metrics
for (int k = i + 1; k < lines.length; k++) {
if (lines[k].startsWith("TAB"))
break;
if (lines[i].startsWith("null"))
continue;
String[] mTokens = lines[k].split(String.valueOf(DELIMITER));
RMetric metric = null;
switch(mTokens[0]) {
case // boolean
"B":
metric = new RBoolean();
((RBoolean) metric).setValue(Boolean.parseBoolean(mTokens[4]));
break;
case "CH":
{
// checkbox
metric = new RCheckbox();
LinkedHashMap<String, Boolean> values = new LinkedHashMap<>();
for (int l = 4; l < mTokens.length; l++) {
values.put(mTokens[l].split(",")[0].substring(1), Boolean.parseBoolean(mTokens[l].split(",")[1].replace(")", "")));
}
((RCheckbox) metric).setValues(values);
break;
}
case "CO":
{
// chooser
metric = new RChooser();
((RChooser) metric).setSelectedIndex(Integer.parseInt(mTokens[4]));
// the amount of values, with the header info removed
String[] values = new String[mTokens.length - 6];
for (int l = 5; l < mTokens.length - 1; l++) {
if (mTokens[l] != null && !mTokens[l].equals(""))
values[l - 5] = mTokens[l];
}
((RChooser) metric).setValues(values);
break;
}
case // counter
"C":
metric = new RCounter();
((RCounter) metric).setVerboseInput(Boolean.parseBoolean(mTokens[4]));
((RCounter) metric).setValue(Double.parseDouble(mTokens[5]));
((RCounter) metric).setIncrement(Double.parseDouble(mTokens[6]));
break;
case // slider
"S":
metric = new RSlider();
((RSlider) metric).setValue(Integer.parseInt(mTokens[4]));
((RSlider) metric).setMin(Integer.parseInt(mTokens[5]));
((RSlider) metric).setMax(Integer.parseInt(mTokens[6]));
break;
case // stopwatch
"ST":
metric = new RStopwatch();
((RStopwatch) metric).setTime(Double.parseDouble(mTokens[4]));
((RStopwatch) metric).setTimes(new ArrayList<Double>());
for (int l = 5; l < mTokens.length; l++) {
if (!mTokens[l].equals(""))
((RStopwatch) metric).getTimes().add(Double.parseDouble(mTokens[5]));
}
break;
case // textfield
"T":
metric = new RTextfield();
((RTextfield) metric).setText((mTokens[4]));
break;
}
if (metric != null) {
metric.setID(Integer.parseInt(mTokens[1]));
metric.setTitle(mTokens[2]);
metric.setModified(Boolean.parseBoolean(mTokens[3]));
tab.getMetrics().add(metric);
// Adding metric
Log.d("RBS", "Adding metric " + metric.toString());
}
}
team.getTabs().add(tab);
}
checkout.setTeam(team);
return checkout;
} catch (Exception e) {
e.printStackTrace();
Log.d("RBS", "An error occurred while decoding a checkout. " + e.getMessage());
return null;
}
}
use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class Utils method randomizeTeamMetrics.
/**
* This code will randomize an event with random metric data for the purposes of show casing an app
* or testing
*/
public static void randomizeTeamMetrics(ArrayList<RTab> tabs) {
Random r = new Random();
if (tabs != null) {
for (RTab tab : tabs) {
for (RMetric metric : tab.getMetrics()) {
metric.setModified(true);
if (metric instanceof RSlider) {
((RSlider) metric).setMax(100);
((RSlider) metric).setValue(r.nextInt(100));
} else if (metric instanceof RCounter) {
((RCounter) metric).setValue(r.nextDouble() * 100);
} else if (metric instanceof RStopwatch) {
((RStopwatch) metric).setTime(r.nextDouble() * 10);
((RStopwatch) metric).setTimes(new ArrayList<Double>());
for (int i = 0; i < r.nextInt(5); i++) {
((RStopwatch) metric).getTimes().add(Utils.round(r.nextDouble() * 8.2, 2));
}
} else if (metric instanceof RBoolean) {
((RBoolean) metric).setValue(r.nextDouble() <= 0.5);
} else if (metric instanceof RCheckbox) {
for (Object o : ((RCheckbox) metric).getValues().keySet()) {
((RCheckbox) metric).getValues().put(o.toString(), r.nextDouble() <= 0.50);
}
} else if (metric instanceof RChooser) {
((RChooser) metric).setSelectedIndex(r.nextInt(((RChooser) metric).getValues().length - 1));
} else if (metric instanceof RTextfield) {
if (!((RTextfield) metric).isOneLine())
((RTextfield) metric).setText("RTextfield has been randomized to: " + getSaltString());
}
}
}
}
}
use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class MetricSortFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.metric_tab, container, false);
/*
* Receive parameters
*/
Bundle bundle = this.getArguments();
metrics = (ArrayList<RMetric>) bundle.getSerializable("metrics");
processMethod = bundle.getInt("processMethod");
eventID = bundle.getInt("eventID");
/*
* Attach metrics to the RecyclerView
*/
rv = view.findViewById(R.id.metric_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(linearLayoutManager);
((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false);
FormRecyclerAdapter adapter = new FormRecyclerAdapter(view.getContext(), this);
rv.setAdapter(adapter);
// setup gesture listener
ItemTouchHelper.Callback callback = new FormRecyclerTouchHelper(adapter, true);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(rv);
adapter.setMetrics(metrics);
return view;
}
use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class RTeam method verify.
/**
* verify() makes sure that the form and team are synchronized. Here's what it does:
* <p>
* PIT:
* -If the user modified the form and ADDED elements, then we'll make sure to add them to this team's form copy
* -If the user modified the form and REMOVED elements, then we'll make sure to remove them from this team's form copy
* -If the user changed any item titles, change them right away
* -If the user changed any default values, reset all the values on all elements that have NOT been modified
* -If the user changed the order of any elements, change the order
* <p>
* MATCH:
* -If the user modified the match form and ADDED elements, then we'll add those to EVERY match profile
* -If the user modified the match form and REMOVED elements, then we'll remove those from EVERY match profile
* -If the user changed any item titles, change them on ALL match profiles
* -If the user changed any default values, reset all the values of EVERY match that have NOT been modified
* -If the user changed the order of any elements, change the order
* <p>
* PREMISE:
* -PIT and MATCH form arrays may NOT be null, only empty
* <p>
* NULLS to check for:
* -If the team has never been opened before, set the PIT values, matches don't need to be set until creation.
*/
public void verify(RForm form) {
// Check for null or missing Pit & Predictions tabs
if (this.tabs == null || this.tabs.size() == 0) {
this.tabs = new ArrayList<>();
addTab(new RTab(number, "Pit", Utils.duplicateRMetricArray(form.getPit()), false, false, 0));
addTab(new RTab(number, "Predictions", Utils.duplicateRMetricArray(form.getMatch()), false, false, 0));
// Check to make sure the team name and number have been inserted into the form
for (RMetric m : this.getTabs().get(0).getMetrics()) {
if (// team name
m.getID() == 0)
// team name
((RTextfield) m).setText(name);
else // team number
if (m.getID() == 1)
((RTextfield) m).setText(String.valueOf(number));
}
return;
}
// Remove elements that aren't on the form
// less if statements, just switches between PIT or MATCH depending on what needs to be verified
ArrayList<RMetric> temp = form.getPit();
for (int i = 0; i < tabs.size(); i++) {
if (!tabs.get(i).getTitle().equalsIgnoreCase("Pit"))
temp = form.getMatch();
for (int j = 0; j < tabs.get(i).getMetrics().size(); j++) {
boolean found = false;
if (temp.size() == 0) {
tabs.get(i).getMetrics().clear();
break;
}
for (int k = 0; k < temp.size(); k++) {
if (tabs.get(i).getMetrics().get(j).getID() == temp.get(k).getID())
found = true;
if (k == temp.size() - 1 && !found) {
tabs.get(i).getMetrics().remove(j);
j = 0;
break;
}
}
}
}
/*
* Alright, so we removed old elements, but we're still very suspicious. For example,
* let's say the user edited the form (by removing an element), didn't re-verify any teams (by not opening them),
* and then added a new element in the position of the old one. What will happen is that the above method will
* NOT remove the old metric instance, and instead below the metrics name will not get updated. So, here we will
* make sure that both the ID and INSTANCE TYPE of each metric (in the form and team) match, otherwise, the metric
* gets booted.
*/
temp = form.getPit();
for (int i = 0; i < tabs.size(); i++) {
if (!tabs.get(i).getTitle().equalsIgnoreCase("Pit"))
temp = form.getMatch();
for (int j = 0; j < temp.size(); j++) {
for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
if (tabs.get(i).getMetrics().get(k).getID() == temp.get(j).getID()) {
if (!temp.get(j).getClass().equals(tabs.get(i).getMetrics().get(k).getClass())) {
tabs.get(i).getMetrics().remove(k);
j = 0;
k = 0;
}
}
}
}
}
// Add elements that are on the form, but not in this team
temp = form.getPit();
for (int i = 0; i < tabs.size(); i++) {
if (!tabs.get(i).getTitle().equalsIgnoreCase("Pit"))
temp = form.getMatch();
for (int j = 0; j < temp.size(); j++) {
boolean found = false;
if (tabs.get(i).getMetrics().size() == 0) {
tabs.get(i).getMetrics().add(temp.get(j).clone());
continue;
}
for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
if (tabs.get(i).getMetrics().get(k).getID() == temp.get(j).getID())
found = true;
if (k == tabs.get(i).getMetrics().size() - 1 && !found) {
tabs.get(i).getMetrics().add(temp.get(j).clone());
j = 0;
break;
}
}
}
}
// Update item names
temp = form.getPit();
for (int i = 0; i < tabs.size(); i++) {
if (!tabs.get(i).getTitle().equalsIgnoreCase("PIT"))
temp = form.getMatch();
for (int j = 0; j < temp.size(); j++) {
for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
if (temp.get(j).getID() == tabs.get(i).getMetrics().get(k).getID()) {
tabs.get(i).getMetrics().get(k).setTitle(temp.get(j).getTitle());
break;
}
}
}
}
// Update default values for non-modified values, also check for some weird scenario
temp = form.getPit();
for (int i = 0; i < tabs.size(); i++) {
if (!tabs.get(i).getTitle().equalsIgnoreCase("PIT"))
temp = form.getMatch();
for (int j = 0; j < temp.size(); j++) {
for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
if (temp.get(j).getID() == tabs.get(i).getMetrics().get(k).getID()) {
RMetric e = temp.get(j);
RMetric s = tabs.get(i).getMetrics().get(k);
if (e instanceof RBoolean && !s.isModified() && s instanceof RBoolean)
((RBoolean) s).setValue(((RBoolean) e).isValue());
else if (e instanceof RCounter && !s.isModified() && s instanceof RCounter) {
((RCounter) s).setValue(((RCounter) e).getValue());
} else if (e instanceof RCalculation && s instanceof RCalculation) {
((RCalculation) s).setCalculation(((RCalculation) e).getCalculation());
} else if (e instanceof RCheckbox && s instanceof RCheckbox) {
// update the titles always, rely on position for this one
LinkedHashMap<String, Boolean> newHash = new LinkedHashMap<>();
// get old values
ArrayList<Boolean> values = new ArrayList<>();
// put values from team checkbox into this array
for (String oKey : ((RCheckbox) s).getValues().keySet()) {
values.add(((RCheckbox) s).getValues().get(oKey));
}
// copy values to new linked hash map, replacing the new keys
int index = 0;
for (String oKey : ((RCheckbox) e).getValues().keySet()) {
newHash.put(oKey, values.get(index));
index++;
}
// set new array back to team metric
((RCheckbox) s).setValues(newHash);
// if the checkbox is not modified, reset the boolean values
if (!s.isModified()) {
for (String key : ((RCheckbox) e).getValues().keySet()) ((RCheckbox) s).getValues().put(key, ((RCheckbox) e).getValues().get(key));
}
} else // if one line is true, it means its the team name or number metric and its value shouldn't be overrided
if (e instanceof RTextfield && !s.isModified() && !((RTextfield) e).isOneLine())
((RTextfield) s).setText(((RTextfield) e).getText());
else if (e instanceof RChooser && s instanceof RChooser) {
// Always update the title
if (!Arrays.equals(((RChooser) s).getValues(), ((RChooser) e).getValues())) {
((RChooser) s).setValues(((RChooser) e).getValues());
}
// if the chooser is not modified, reset the chooser values
if (!s.isModified())
((RChooser) s).setSelectedIndex(((RChooser) e).getSelectedIndex());
} else if (e instanceof RStopwatch && !s.isModified() && s instanceof RStopwatch)
((RStopwatch) s).setTime(((RStopwatch) e).getTime());
else if (e instanceof RSlider && !s.isModified() && s instanceof RSlider) {
((RSlider) s).setMax(((RSlider) e).getMax());
((RSlider) s).setMin(((RSlider) e).getMin());
((RSlider) s).setValue(((RSlider) e).getValue());
} else if (e instanceof RCounter && s instanceof RCounter) {
((RCounter) s).setIncrement(((RCounter) e).getIncrement());
((RCounter) s).setVerboseInput(((RCounter) e).isVerboseInput());
if (!s.isModified())
((RCounter) s).setValue(((RCounter) e).getValue());
}
break;
}
}
}
}
}
Aggregations