Fixed time-based statistics unit configurability

This commit is contained in:
Artemis-the-gr8 2022-07-04 02:17:31 +02:00
parent 5b96f0b292
commit 7e9f7ee549
2 changed files with 50 additions and 15 deletions

View File

@ -12,22 +12,33 @@ public enum Unit {
HP (Type.DAMAGE),
HEART (Type.DAMAGE),
TICK (Type.TIME),
SECOND (Type.TIME),
MINUTE (Type.TIME),
HOUR (Type.TIME),
DAY (Type.TIME),
WEEK (Type.TIME);
SECOND (Type.TIME, 1),
MINUTE (Type.TIME, 60),
HOUR (Type.TIME, 3600),
DAY (Type.TIME, 86400),
WEEK (Type.TIME, 604800);
private final Type type;
private final int seconds;
Unit(Type type) {
this(type, -1);
}
Unit(Type type, int seconds) {
this.type = type;
this.seconds = seconds;
}
public Type getType() {
return type;
}
/** Returns the given Unit in seconds, or -1 if the Unit is not a TimeUnit.*/
public int getTimeInSeconds() {
return this.seconds;
}
/** Returns a pretty name belonging to this enum constant. If the Unit is
NUMBER, it will return an empty String. */
public @NotNull String getName() throws NullPointerException {

View File

@ -1,7 +1,6 @@
package com.gmail.artemis.the.gr8.playerstats.msg;
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
import java.text.DecimalFormat;
public class NumberFormatter {
@ -68,37 +67,62 @@ public class NumberFormatter {
}
}
//TODO work in min-max
//TODO Fix spaces
/** The unit of time-based statistics is ticks by default.*/
private String formatTime(long number, Unit statUnit, Unit timeMinimumUnit) { //5 statistics
private String formatTime(long number, Unit maxUnit, Unit minUnit) { //5 statistics
if (number == 0) {
return "-";
}
StringBuilder output = new StringBuilder();
int max = maxUnit.getTimeInSeconds();
int min = minUnit.getTimeInSeconds();
double leftover = number / 20.0;
if (leftover >= 86400) {
if (isInRange(max, min, 604800) && leftover >= 604800) {
double weeks = leftover / 7 / 60 / 60 / 24;
leftover = leftover % (7 * 60 * 60 * 24);
if (maxUnit == Unit.WEEK && leftover >= 302400) {
weeks++;
}
output.append(format.format(Math.round(weeks)))
.append("w ");
}
if (isInRange(max, min, 86400) && leftover >= 86400) {
double days = leftover / 60 / 60 / 24;
leftover = leftover % (60 * 60 * 24);
if (maxUnit == Unit.DAY) {
days++;
}
output.append(format.format(Math.round(days)))
.append("d ");
leftover = leftover % (60 * 60 * 24);
}
if (leftover >= 3600) {
if (isInRange(max, min, 3600) && leftover >= 3600) {
double hours = leftover / 60 / 60;
leftover = leftover % (60 * 60);
if (maxUnit == Unit.HOUR) {
hours++;
}
output.append(format.format(Math.round(hours)))
.append("h ");
leftover = leftover % (60 * 60);
}
if (leftover >= 60) {
if (isInRange(max, min, 60) && leftover >= 60) {
double minutes = leftover / 60;
leftover = leftover % 60;
if (maxUnit == Unit.MINUTE) {
minutes++;
}
output.append(format.format(Math.round(minutes)))
.append("m ");
leftover = leftover % 60;
}
if (leftover > 0) {
if (isInRange(max, min, 1) && leftover > 0) {
output.append(format.format(Math.round(leftover)))
.append("s");
}
return output.toString();
}
private boolean isInRange(int maxUnit, int minUnit, int unitToEvaluate) {
return maxUnit >= unitToEvaluate && unitToEvaluate >= minUnit;
}
}