Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void updateProgress(Progress progress, int[] cubesInStage, int finished, int skipped) {
- int[] actualStages = new int[cubesInStage.length + 1];
- System.arraycopy(cubesInStage, 0, actualStages, 0, cubesInStage.length);
- actualStages[actualStages.length - 1] = finished + skipped;
- int totalKnownUnfinished = 0;
- //the amount of cubes that went through 0-th stage
- totalKnownUnfinished += cubesInStage[0];
- for (int n = 1; n < cubesInStage.length; n++) {
- //the amount if cubes that went through n-th stage but didn't go through (n-1)-th stage.
- //TODO: is it accurate enough?
- totalKnownUnfinished += Math.max(0, cubesInStage[n] - cubesInStage[n - 1]);
- }
- int unknown = toUpdate.size() + finished + skipped - totalKnownUnfinished;
- if (unknown < 0) {
- unknown = 0;
- }
- //if unknown, assume 0
- actualStages[0] += unknown;
- for (int i = 0; i < actualStages.length; i++) {
- int currentStage = (i == actualStages.length - 1) ?
- actualStages[i] :
- actualStages[i] - actualStages[i + 1];
- if (currentStage < 0) {
- currentStage = 0;
- }
- actualStages[i] = currentStage;
- }
- int total = 0;
- for (int num : actualStages) {
- total += num;
- }
- double totalProgress = 0;
- for (int i = 0; i < actualStages.length; i++) {
- double progressPart = actualStages[i]/(double) total;
- double weightedProgress = progressPart*i/(double) (actualStages.length - 1);
- totalProgress += weightedProgress;
- }
- assert totalProgress < 1.00001 && totalProgress >= 0;
- if (totalProgress > 1) {
- totalProgress = 1;
- }
- int progressInt = (int) (totalProgress*Integer.MAX_VALUE);
- progress.setProgress(progressInt);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement