Added activity index queries and placeholders.

- Added CommonQueries#fetchActivityIndexOf
- Added CommonQueries#getActivityGroupForIndex

- Added %plan_player_activity_index%
- Added %plan_player_activity_group%

Affects issues:
- Close #1663
This commit is contained in:
Risto Lahtela 2021-01-20 16:58:46 +02:00
parent e220e40a8c
commit 15c9325eb4
4 changed files with 57 additions and 2 deletions

View File

@ -71,4 +71,21 @@ public interface CommonQueries {
boolean doesDBHaveTable(String table);
boolean doesDBHaveTableColumn(String table, String column);
/**
* Calculates the activity index for the player at a specific date.
*
* @param playerUUID UUID of the player.
* @param epochMs Epoch millisecond to use for calculation
* @return a double between 0.0 and 5.0.
*/
double fetchActivityIndexOf(UUID playerUUID, long epochMs);
/**
* Get a String that represents the Activity group for an index.
*
* @param activityIndex a double between 0.0 and 5.0.
* @return Name of the group (in English) that this activityIndex falls within.
*/
String getActivityGroupForIndex(double activityIndex);
}

View File

@ -25,6 +25,8 @@ import com.djrapitops.plan.delivery.formatting.Formatter;
import com.djrapitops.plan.delivery.formatting.Formatters;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.TimeSettings;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.utilities.Predicates;
@ -42,16 +44,19 @@ import static com.djrapitops.plan.utilities.MiscUtils.*;
@Singleton
public class PlayerPlaceHolders implements Placeholders {
private final PlanConfig config;
private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final Formatters formatters;
@Inject
public PlayerPlaceHolders(
PlanConfig config,
DBSystem dbSystem,
ServerInfo serverInfo,
Formatters formatters
) {
this.config = config;
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.formatters = formatters;
@ -209,5 +214,18 @@ public class PlayerPlaceHolders implements Placeholders {
.map(Server::getName)
.orElse("-")
);
placeholders.register("player_activity_index",
player -> player.getActivityIndex(
now(),
config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD)
).getValue()
);
placeholders.register("player_activity_group",
player -> player.getActivityIndex(
now(),
config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD)
).getGroup()
);
}
}

View File

@ -16,10 +16,14 @@
*/
package com.djrapitops.plan.query;
import com.djrapitops.plan.delivery.domain.mutators.ActivityIndex;
import com.djrapitops.plan.gathering.cache.SessionCache;
import com.djrapitops.plan.gathering.domain.Session;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.TimeSettings;
import com.djrapitops.plan.storage.database.DBType;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.containers.PlayerContainerQuery;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.storage.database.queries.objects.SessionQueries;
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
@ -34,9 +38,11 @@ import java.util.UUID;
public class CommonQueriesImplementation implements CommonQueries {
private final Database db;
private final PlanConfig config;
CommonQueriesImplementation(Database db) {
CommonQueriesImplementation(Database db, PlanConfig config) {
this.db = db;
this.config = config;
}
@Override
@ -98,4 +104,14 @@ public class CommonQueriesImplementation implements CommonQueries {
throw new IllegalStateException("Unsupported Database Type: " + dbType.getName());
}
}
@Override
public double fetchActivityIndexOf(UUID playerUUID, long epochMs) {
return db.query(new PlayerContainerQuery(playerUUID)).getActivityIndex(epochMs, config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD)).getValue();
}
@Override
public String getActivityGroupForIndex(double activityIndex) {
return ActivityIndex.getGroup(activityIndex);
}
}

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.query;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.QueryAPIExecutable;
@ -40,6 +41,7 @@ import java.util.function.Consumer;
@Singleton
public class QuerySvc implements QueryService {
private final PlanConfig config;
private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final ErrorLogger errorLogger;
@ -49,10 +51,12 @@ public class QuerySvc implements QueryService {
@Inject
public QuerySvc(
PlanConfig config,
DBSystem dbSystem,
ServerInfo serverInfo,
ErrorLogger errorLogger
) {
this.config = config;
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.errorLogger = errorLogger;
@ -132,6 +136,6 @@ public class QuerySvc implements QueryService {
public CommonQueries getCommonQueries() {
Database database = dbSystem.getDatabase();
if (database == null) throw new IllegalStateException("Database has not been initialized.");
return new CommonQueriesImplementation(database);
return new CommonQueriesImplementation(database, config);
}
}