Session calendar for player page

This commit is contained in:
Rsl1122 2019-07-22 15:33:47 +03:00
parent 024abf26b4
commit f489264ca3
4 changed files with 131 additions and 57 deletions

View File

@ -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;
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Serializable> getEnd() {
return Optional.ofNullable(end);
}
public Optional<String> getColor() {
return Optional.ofNullable(color);
}
@Override
public String toString() {
return "{" +
"title:'" + title + '\'' +
", start:'" + start + '\'' +
(end != null ? ", end='" + end + '\'' : "") +
(color != null ? ", color='" + color + '\'' : "") +
'}';
}
}

View File

@ -35,8 +35,8 @@ import java.util.concurrent.TimeUnit;
*/
public class PlayerCalendar {
private final Formatter<Long> timeAmountFormatter;
private final Formatter<Long> yearLongFormatter;
private final Formatter<Long> timeAmount;
private final Formatter<Long> year;
private final Formatter<Long> iso8601Formatter;
private final Theme theme;
private final TimeZone timeZone;
@ -46,8 +46,8 @@ public class PlayerCalendar {
PlayerCalendar(
PlayerContainer container,
Formatter<Long> timeAmountFormatter,
Formatter<Long> yearLongFormatter,
Formatter<Long> timeAmount,
Formatter<Long> year,
Formatter<Long> 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<CalendarEntry> getEntries() {
List<CalendarEntry> 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<String, List<Session>> sessionsByDay = getSessionsByDay();
for (Map.Entry<String, List<Session>> 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<String, List<Session>> 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("'}");
}
}

View File

@ -349,9 +349,7 @@
class="far fa-fw fa-calendar-alt col-teal"></i>
Session Calendar</h6>
</div>
<div class="chart-area">
<canvas id="myAreaChart2"></canvas>
</div>
<div class="chart-area" id="sessionCalendar"></div>
</div>
<!-- Most Recent Sessions -->
@ -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)
}