use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class Overview method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview_tab, container, false);
Bundle bundle = this.getArguments();
layout = view.findViewById(R.id.overview_layout);
REvent event = (REvent) getArguments().getSerializable("event");
RTeam team = new IO(getActivity()).loadTeam(event.getID(), TeamViewer.team.getID());
rMetricToUI = new RMetricToUI(getActivity(), new IO(getActivity()).loadSettings().getRui(), true);
try {
/*
* Do statistics generation, this will generate graphs for certain metrics
*/
// Stores pie chart values, with the sub linked hash map <item,occurrences>, this will need to be processed later into a percent
LinkedHashMap<String, LinkedHashMap<String, Double>> pieValues = new LinkedHashMap<>();
// Stores line chart values, with the sub linked hash map <matchName,value>
LinkedHashMap<String, LinkedHashMap<String, Double>> lineValues = new LinkedHashMap<>();
// This isn't directly related, more of a side project
ArrayList<RGallery> galleries = new ArrayList<>();
for (RTab tab : team.getTabs()) {
// Rule out disallowed tabs
if (tab.getTitle().equalsIgnoreCase("PIT"))
continue;
// Start processing metrics
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RGallery) {
galleries.add((RGallery) metric);
}
if (!metric.isModified())
continue;
// Pie graph metrics, scan these here
if (metric instanceof RBoolean) {
LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
String key = ((RBoolean) metric).isValue() ? "Yes" : "No";
if (temp.get(key) == null)
temp.put(key, 1.0);
else
temp.put(key, temp.get(key) + 1);
pieValues.put(metric.getTitle(), temp);
} else if (metric instanceof RCheckbox) {
if (((RCheckbox) metric).getValues() != null) {
for (Object key : ((RCheckbox) metric).getValues().keySet()) {
LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
if (temp.get(key.toString()) == null)
temp.put(key.toString(), 1.0);
else
temp.put(key.toString(), temp.get(key.toString()) + 1);
pieValues.put(metric.getTitle(), temp);
}
}
} else if (metric instanceof RChooser) {
LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
if (temp.get(metric.toString()) == null)
temp.put(metric.toString(), 1.0);
else
temp.put(metric.toString(), temp.get(metric.toString()) + 1);
pieValues.put(metric.getTitle(), temp);
} else // Line chart metrics
if (metric instanceof RCounter || metric instanceof RSlider || metric instanceof RStopwatch || metric instanceof RCalculation) {
LinkedHashMap<String, Double> temp = lineValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
temp.put(tab.getTitle(), Double.parseDouble(metric.toString()));
lineValues.put(metric.getTitle(), temp);
}
}
}
// Add the divider metrics by position, -1 if no metric after it, or at the end
ArrayList<RDivider> addedDividers = new ArrayList<>();
/*
* Add the charts!
*/
for (Object key : lineValues.keySet()) {
if (lineValues.get(key.toString()).size() >= 2) {
loop: for (RTab tab : team.getTabs()) {
for (int i = 0; i < tab.getMetrics().size(); i++) {
if (tab.getMetrics().get(i).getTitle().equals(key.toString())) {
// See if there is a RDivider hiding above this metric
for (int j = i; j >= 0; j--) {
if (tab.getMetrics().get(j) instanceof RDivider && !addedDividers.contains(tab.getMetrics().get(j))) {
layout.addView(rMetricToUI.getDivider((RDivider) tab.getMetrics().get(j)));
addedDividers.add((RDivider) tab.getMetrics().get(j));
break loop;
}
}
}
}
}
layout.addView(rMetricToUI.generateLineChart(key.toString(), lineValues.get(key.toString())));
}
}
// Process the pie charts
for (Object key : pieValues.keySet()) {
if (pieValues.get(key.toString()).size() <= 1)
continue;
int metricID = 0;
loop: for (RTab tab : team.getTabs()) {
for (int i = 0; i < tab.getMetrics().size(); i++) {
if (tab.getMetrics().get(i).getTitle().equals(key.toString())) {
metricID = tab.getMetrics().get(i).getID();
// See if there is a RDivider hiding above this metric
for (int j = i; j >= 0; j--) {
if (tab.getMetrics().get(j) instanceof RDivider && !addedDividers.contains(tab.getMetrics().get(j))) {
layout.addView(rMetricToUI.getDivider((RDivider) tab.getMetrics().get(j)));
addedDividers.add((RDivider) tab.getMetrics().get(j));
break loop;
}
}
}
}
}
for (Object key2 : pieValues.get(key.toString()).keySet()) {
if (numModified(team.getTabs(), metricID) != 0)
pieValues.get(key.toString()).put(key2.toString(), pieValues.get(key.toString()).get(key2.toString()) / (double) numModified(team.getTabs(), metricID));
}
layout.addView(rMetricToUI.generatePieChart(key.toString(), pieValues.get(key.toString())));
}
/*
* Find the image with the most entropy, and add
* it as the "featured" image
*/
galleryLoop: for (int j = galleries.size() - 1; j >= 0; j--) {
if (galleries.get(j).getImages() != null && galleries.get(j).getImages().size() > 0) {
for (int i = galleries.get(j).getImages().size() - 1; i >= 0; i--) {
try {
layout.addView(rMetricToUI.getImageView("Featured image", BitmapFactory.decodeByteArray(galleries.get(j).getImages().get(i), 0, galleries.get(j).getImages().get(i).length)));
break galleryLoop;
} catch (Exception e) {
Log.d("RBS", "Failed to load featured image: " + e.getMessage());
}
}
}
}
} catch (Exception e) {
Log.d("RBS", "Failed to generate graphs for this team profile.");
}
/*
* Attempt to download TBA info for this team
*/
if (!team.hasTBAInfo()) {
if (event.getKey() != null && event.getKey().length() >= 4)
new TBATeamInfoTask(view.getContext(), team.getNumber(), event.getKey().substring(0, 4), this);
} else {
// TBA info card
layout.addView(rMetricToUI.getInfoField("TBA.com information", TeamViewer.team.getTbaInfo(), TeamViewer.team.getWebsite(), TeamViewer.team.getNumber()), 0);
if (TeamViewer.team.getImage() != null) {
// Image view
Bitmap bitmap = BitmapFactory.decodeByteArray(TeamViewer.team.getImage(), 0, TeamViewer.team.getImage().length);
layout.addView(rMetricToUI.getImageView("Robot", bitmap));
}
}
/*
* Add UI cards to the layout
*/
// "Other" card
layout.addView(rMetricToUI.getInfoField("Other", "Last edited: " + Utils.convertTime(team.getLastEdit()) + "\nSize on disk: " + new IO(view.getContext()).getTeamSize(bundle.getInt("eventID"), team.getID()) + " KB", "", 0));
return view;
}
use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class TeamTabAdapter method createMatch.
/**
* Creates a new match with the specified parameter,
* this method is used by the manual match creator, however,
* most users will likely just use TBA.com importing.
* @param name the name of the match
* @param isRed true if this team is on the red alliance
* @return the position of the sorted, created match
*/
public int createMatch(String name, boolean isRed) {
RTab tab = new RTab(TeamViewer.team.getNumber(), name, Utils.duplicateRMetricArray(form.getMatch()), isRed, false, 0);
int position = TeamViewer.team.addTab(tab);
TeamViewer.team.setLastEdit(System.currentTimeMillis());
new IO(context).saveTeam(event.getID(), TeamViewer.team);
// If these event is cloud synced, a new checkout needs to be packaged
if (event.isCloudEnabled()) {
RTeam newTeam = new RTeam(TeamViewer.team.getName(), TeamViewer.team.getNumber(), TeamViewer.team.getID());
newTeam.addTab(tab);
RCheckout checkout = new RCheckout(newTeam);
/*
* It would require a lot more code to check all devices and be sure that a new ID is
* valid, so generate a random one. The chances of an error occurring are so low, this is acceptable (somewhat :\)
*/
checkout.setID(new Random().nextInt(Integer.MAX_VALUE - 50_000) + 20_000);
checkout.setStatus(HandoffStatus.AVAILABLE);
new IO(context).savePendingCheckout(checkout);
}
Bundle bundle = new Bundle();
bundle.putSerializable("event", event);
bundle.putSerializable("form", form);
bundle.putBoolean("readOnly", false);
bundle.putInt("position", position);
Match match = new Match();
match.setArguments(bundle);
notifyDataSetChanged();
return position + 1;
}
use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class UnpackTBAEvent method doInBackground.
protected Void doInBackground(Void... params) {
/*
* No teams were contained within the event, so exit, nothing here is relevant
* to a TBA event that doesn't contain any team models
*/
if (event.teams == null || event.teams.length == 0)
return null;
/*
* Create an array of team models from the ones contained in the event
*/
ArrayList<RTeam> teams = new ArrayList<>();
for (int i = 0; i < event.teams.length; i++) {
// i can be used as the ID because we are creating a fresh event, io.getNewTeamID is irrelevant
teams.add(new RTeam(event.teams[i].nickname, (int) event.teams[i].team_number, i));
}
/*
* Sort the matches in the event
*/
Collections.sort(Arrays.asList(event.matches));
/*
* Add the matches to the respective team models
*/
IO io = new IO(activityWeakReference.get());
RForm form = io.loadForm(eventID);
int result;
for (RTeam t : teams) {
t.verify(form);
for (int j = 0; j < event.matches.length; j++) {
result = event.matches[j].doesMatchContainTeam(t.getNumber());
if (result > 0) {
String name = "Match";
// process the correct match name
switch(event.matches[j].comp_level) {
case "qm":
name = "Quals " + event.matches[j].match_number;
break;
case "qf":
name = "Quarters " + event.matches[j].set_number + " Match " + event.matches[j].match_number;
break;
case "sf":
name = "Semis " + event.matches[j].set_number + " Match " + event.matches[j].match_number;
break;
case "f":
name = "Finals " + event.matches[j].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(t.getNumber(), name, Utils.duplicateRMetricArray(form.getMatch()), isRed, event.matches[j].isOnWinningAlliance(t.getNumber()), event.matches[j].time * 1000);
// set the match position, if possible
tab.setAlliancePosition(event.matches[j].getTeamPosition(t.getNumber()));
// Check for FieldData metrics
if (tab.getMetrics() != null) {
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RFieldData) {
if (((RFieldData) metric).getData() == null)
((RFieldData) metric).setData(new LinkedHashMap<String, ArrayList<RMetric>>());
for (int i = 0; i < event.matches[j].scorableItems.length; i++) {
Log.d("RBS", "Metric name: " + event.matches[j].scorableItems[i] + ", " + "Red value: " + event.matches[j].redValues[i] + ", Blue value: " + event.matches[j].blueValues[i]);
ArrayList<RMetric> metrics = new ArrayList<>();
try {
metrics.add(new RCounter(0, "", 0, Double.parseDouble(event.matches[j].redValues[i])));
} catch (Exception e) {
metrics.add(new RTextfield(0, "", (event.matches[j].redValues[i])));
}
try {
metrics.add(new RCounter(0, "", 0, Double.parseDouble(event.matches[j].blueValues[i])));
} catch (Exception e) {
metrics.add(new RTextfield(0, "", (event.matches[j].blueValues[i])));
}
if (event.matches[j].scorableItems[i] != null && metrics.size() > 0)
((RFieldData) metric).getData().put(event.matches[j].scorableItems[i], metrics);
}
}
}
}
t.addTab(tab);
}
}
/*
* This is where the merge decision comes into play
*/
if (randomize) {
t.setLastEdit(System.currentTimeMillis());
Utils.randomizeTeamMetrics(t.getTabs());
}
io.saveTeam(eventID, t);
}
return null;
}
use of com.cpjd.roblu.models.RTab 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.RTab 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;
}
}
Aggregations