From f489264ca3175ac4a69f801a63c4f80ad51b39d6 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 22 Jul 2019 15:33:47 +0300 Subject: [PATCH] Session calendar for player page --- .../plan/system/json/PlayerJSONParser.java | 2 + .../html/graphs/calendar/CalendarEntry.java | 78 ++++++++++++++ .../html/graphs/calendar/PlayerCalendar.java | 101 +++++++++--------- .../resources/assets/plan/web/player.html | 7 +- 4 files changed, 131 insertions(+), 57 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/CalendarEntry.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/json/PlayerJSONParser.java b/Plan/common/src/main/java/com/djrapitops/plan/system/json/PlayerJSONParser.java index e7d661f9d..e63528158 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/json/PlayerJSONParser.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/json/PlayerJSONParser.java @@ -95,6 +95,8 @@ public class PlayerJSONParser { WorldPie worldPie = graphs.pie().worldPie(player.getValue(PlayerKeys.WORLD_TIMES).orElse(new WorldTimes())); data.put("world_pie_series", worldPie.getSlices()); data.put("gm_series", worldPie.toHighChartsDrillDownMaps()); + data.put("calendar_series", graphs.calendar().playerCalendar(player).getEntries()); + data.put("first_day", 1); // Monday return data; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/CalendarEntry.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/CalendarEntry.java new file mode 100644 index 000000000..daf54c2a4 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/CalendarEntry.java @@ -0,0 +1,78 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.html.graphs.calendar; + +import java.io.Serializable; +import java.util.Optional; + +/** + * Represents an entry for calendar. + * + * @author Rsl1122 + */ +public class CalendarEntry { + + private final String title; + private final Serializable start; + private Serializable end; + private String color; + + private CalendarEntry(String title, Serializable start) { + this.title = title; + this.start = start; + } + + public static CalendarEntry of(String title, Serializable start) { + return new CalendarEntry(title, start); + } + + public CalendarEntry withEnd(Serializable end) { + this.end = end; + return this; + } + + public CalendarEntry withColor(String color) { + this.color = color; + return this; + } + + public String getTitle() { + return title; + } + + public Serializable getStart() { + return start; + } + + public Optional getEnd() { + return Optional.ofNullable(end); + } + + public Optional getColor() { + return Optional.ofNullable(color); + } + + @Override + public String toString() { + return "{" + + "title:'" + title + '\'' + + ", start:'" + start + '\'' + + (end != null ? ", end='" + end + '\'' : "") + + (color != null ? ", color='" + color + '\'' : "") + + '}'; + } +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/PlayerCalendar.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/PlayerCalendar.java index 56d3f0ca4..5b449df95 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/PlayerCalendar.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/calendar/PlayerCalendar.java @@ -35,8 +35,8 @@ import java.util.concurrent.TimeUnit; */ public class PlayerCalendar { - private final Formatter timeAmountFormatter; - private final Formatter yearLongFormatter; + private final Formatter timeAmount; + private final Formatter year; private final Formatter iso8601Formatter; private final Theme theme; private final TimeZone timeZone; @@ -46,8 +46,8 @@ public class PlayerCalendar { PlayerCalendar( PlayerContainer container, - Formatter timeAmountFormatter, - Formatter yearLongFormatter, + Formatter timeAmount, + Formatter year, Formatter iso8601Formatter, Theme theme, TimeZone timeZone @@ -55,24 +55,22 @@ public class PlayerCalendar { this.allSessions = container.getValue(PlayerKeys.SESSIONS).orElse(new ArrayList<>()); this.registered = container.getValue(PlayerKeys.REGISTERED).orElse(0L); - this.timeAmountFormatter = timeAmountFormatter; - this.yearLongFormatter = yearLongFormatter; + this.timeAmount = timeAmount; + this.year = year; this.iso8601Formatter = iso8601Formatter; this.theme = theme; this.timeZone = timeZone; } - public String toCalendarSeries() { - StringBuilder series = new StringBuilder("["); + public List getEntries() { + List entries = new ArrayList<>(); - appendRegister(series); - appendDailyPlaytime(series); - appendSessionsAndKills(series); + entries.add(CalendarEntry + .of("Registered: " + year.apply(registered), + registered + ).withColor(theme.getValue(ThemeVal.LIGHT_GREEN)) + ); - return series.append("]").toString(); - } - - private void appendDailyPlaytime(StringBuilder series) { Map> sessionsByDay = getSessionsByDay(); for (Map.Entry> entry : sessionsByDay.entrySet()) { @@ -82,15 +80,42 @@ public class PlayerCalendar { int sessionCount = sessions.size(); long playtime = sessions.stream().mapToLong(Session::getLength).sum(); - series.append(",{title: 'Playtime: ").append(timeAmountFormatter.apply(playtime)) - .append("',start:'").append(day) - .append("',color: '").append(theme.getValue(ThemeVal.GREEN)).append("'") - .append("}"); - - series.append(",{title: 'Sessions: ").append(sessionCount) - .append("',start:'").append(day) - .append("'}"); + entries.add(CalendarEntry + .of("Playtime: " + timeAmount.apply(playtime), day) + .withColor(theme.getValue(ThemeVal.GREEN)) + ); + entries.add(CalendarEntry.of("Sessions: " + sessionCount, day)); } + + long fiveMinutes = TimeUnit.MINUTES.toMillis(5L); + + for (Session session : allSessions) { + String length = timeAmount.apply(session.getLength()); + Long start = session.getUnsafe(SessionKeys.START); + Long end = session.getValue(SessionKeys.END).orElse(System.currentTimeMillis()); + + entries.add(CalendarEntry + .of("Session: " + length, + start + timeZone.getOffset(start)) + .withEnd(end + timeZone.getOffset(end)) + ); + + for (PlayerKill kill : session.getPlayerKills()) { + long time = kill.getDate(); + String victim = kill.getVictimName().orElse(kill.getVictim().toString()); + entries.add(CalendarEntry + .of("Killed: " + victim, time) + .withEnd(time + fiveMinutes) + .withColor(theme.getValue(ThemeVal.RED)) + ); + } + } + + return entries; + } + + public String toCalendarSeries() { + return getEntries().toString(); } private Map> getSessionsByDay() { @@ -104,34 +129,4 @@ public class PlayerCalendar { } return sessionsByDay; } - - private void appendSessionsAndKills(StringBuilder series) { - long fiveMinutes = TimeUnit.MINUTES.toMillis(5L); - - for (Session session : allSessions) { - String length = timeAmountFormatter.apply(session.getLength()); - Long start = session.getUnsafe(SessionKeys.START); - Long end = session.getValue(SessionKeys.END).orElse(System.currentTimeMillis()); - - series.append(",{title: 'Session: ").append(length) - .append("',start:").append(start + timeZone.getOffset(start)) - .append(",end:").append(end + timeZone.getOffset(end)) - .append("}"); - - for (PlayerKill kill : session.getPlayerKills()) { - long time = kill.getDate(); - - series.append(",{title: 'Killed: ").append(kill.getVictimName().orElse(kill.getVictim().toString())) - .append("',start:").append(time) - .append(",end:").append(time + fiveMinutes) - .append(",color: '").append(theme.getValue(ThemeVal.RED)).append("'") - .append("}"); - } - } - } - - private void appendRegister(StringBuilder series) { - series.append("{title: 'Registered: ").append(yearLongFormatter.apply(registered)).append("'," + - "start: ").append(this.registered).append(",color: '").append(theme.getValue(ThemeVal.LIGHT_GREEN)).append("'}"); - } } diff --git a/Plan/common/src/main/resources/assets/plan/web/player.html b/Plan/common/src/main/resources/assets/plan/web/player.html index dfb9d7189..4c1f991d0 100644 --- a/Plan/common/src/main/resources/assets/plan/web/player.html +++ b/Plan/common/src/main/resources/assets/plan/web/player.html @@ -349,9 +349,7 @@ class="far fa-fw fa-calendar-alt col-teal"> Session Calendar -
- -
+
@@ -1333,7 +1331,8 @@ } }; punchCard("punchCard", series.punchCard); - worldPie("worldPie", series.worldPie, series.worldPieGMs) + worldPie("worldPie", series.worldPie, series.worldPieGMs); + sessionCalendar("#sessionCalendar", json.calendar_series, json.first_day); } else if (error) { $('#punchCard').text("Failed to load graph data: " + error) }