Advertisement
Barteks2x

Untitled

Jun 22nd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1.     private void updateProgress(Progress progress, int[] cubesInStage, int finished, int skipped) {
  2.         int[] actualStages = new int[cubesInStage.length + 1];
  3.         System.arraycopy(cubesInStage, 0, actualStages, 0, cubesInStage.length);
  4.         actualStages[actualStages.length - 1] = finished + skipped;
  5.         int totalKnownUnfinished = 0;
  6.         //the amount of cubes that went through 0-th stage
  7.         totalKnownUnfinished += cubesInStage[0];
  8.         for (int n = 1; n < cubesInStage.length; n++) {
  9.             //the amount if cubes that went through n-th stage but didn't go through (n-1)-th stage.
  10.             //TODO: is it accurate enough?
  11.             totalKnownUnfinished += Math.max(0, cubesInStage[n] - cubesInStage[n - 1]);
  12.         }
  13.         int unknown = toUpdate.size() + finished + skipped - totalKnownUnfinished;
  14.         if (unknown < 0) {
  15.             unknown = 0;
  16.         }
  17.         //if unknown, assume 0
  18.         actualStages[0] += unknown;
  19.         for (int i = 0; i < actualStages.length; i++) {
  20.             int currentStage = (i == actualStages.length - 1) ?
  21.                     actualStages[i] :
  22.                     actualStages[i] - actualStages[i + 1];
  23.             if (currentStage < 0) {
  24.                 currentStage = 0;
  25.             }
  26.             actualStages[i] = currentStage;
  27.         }
  28.         int total = 0;
  29.         for (int num : actualStages) {
  30.             total += num;
  31.         }
  32.         double totalProgress = 0;
  33.         for (int i = 0; i < actualStages.length; i++) {
  34.             double progressPart = actualStages[i]/(double) total;
  35.             double weightedProgress = progressPart*i/(double) (actualStages.length - 1);
  36.             totalProgress += weightedProgress;
  37.         }
  38.         assert totalProgress < 1.00001 && totalProgress >= 0;
  39.         if (totalProgress > 1) {
  40.             totalProgress = 1;
  41.         }
  42.         int progressInt = (int) (totalProgress*Integer.MAX_VALUE);
  43.         progress.setProgress(progressInt);
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement