From ecb7eeb62b8ba2255fc555779ba7ca1e0a2b9236 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 13 Aug 2019 09:35:20 +0300 Subject: [PATCH] Added new constructor to ActivityIndex --- .../data/store/mutators/ActivityIndex.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/data/store/mutators/ActivityIndex.java b/Plan/common/src/main/java/com/djrapitops/plan/data/store/mutators/ActivityIndex.java index 0160e563d..e9bfd524e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/data/store/mutators/ActivityIndex.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/data/store/mutators/ActivityIndex.java @@ -67,14 +67,18 @@ public class ActivityIndex { private long playtimeMsThreshold; - public ActivityIndex( - DataContainer container, long date, - long playtimeMsThreshold - ) { + public ActivityIndex(DataContainer container, long date, long playtimeMsThreshold) { this.playtimeMsThreshold = playtimeMsThreshold; - value = calculate(container, date); this.date = date; + value = calculate(container); + } + + public ActivityIndex(List sessions, long date, long playtimeMsThreshold) { + this.playtimeMsThreshold = playtimeMsThreshold; + + this.date = date; + value = calculate(new SessionsMutator(sessions)); } public ActivityIndex(double value, long date) { @@ -86,21 +90,23 @@ public class ActivityIndex { return new String[]{"Very Active", "Active", "Regular", "Irregular", "Inactive"}; } - private double calculate(DataContainer container, long date) { + private double calculate(DataContainer container) { + Optional> sessionsValue = container.getValue(PlayerKeys.SESSIONS); + return sessionsValue + .map(sessions -> calculate(new SessionsMutator(sessions))) + .orElse(0.0); + } + + private double calculate(SessionsMutator sessionsMutator) { + if (sessionsMutator.all().isEmpty()) { + return 0.0; + } + long week = TimeAmount.WEEK.toMillis(1L); long weekAgo = date - week; long twoWeeksAgo = date - 2L * week; long threeWeeksAgo = date - 3L * week; - Optional> sessionsValue = container.getValue(PlayerKeys.SESSIONS); - if (!sessionsValue.isPresent()) { - return 0.0; - } - SessionsMutator sessionsMutator = new SessionsMutator(sessionsValue.get()); - if (sessionsMutator.all().isEmpty()) { - return 0.0; - } - SessionsMutator weekOne = sessionsMutator.filterSessionsBetween(weekAgo, date); SessionsMutator weekTwo = sessionsMutator.filterSessionsBetween(twoWeeksAgo, weekAgo); SessionsMutator weekThree = sessionsMutator.filterSessionsBetween(threeWeeksAgo, twoWeeksAgo);