Tests for maximum activity index

This commit is contained in:
Rsl1122 2017-11-21 19:38:26 +02:00
parent dc495e0ca9
commit 23ae10238b
3 changed files with 144 additions and 12 deletions

View File

@ -100,19 +100,27 @@ public class PlayerProfile implements OfflinePlayer {
// Playtime per week multipliers, max out to avoid too high values.
double max = 4.0;
double weekPlay = (PlayerProfile.getPlaytime(sessionsWeek.stream()) * 1.0 / activePlayThreshold);
long playtimeWeek = PlayerProfile.getPlaytime(sessionsWeek.stream());
double weekPlay = (playtimeWeek * 1.0 / activePlayThreshold);
if (weekPlay > max) {
weekPlay = max;
}
double week2Play = (PlayerProfile.getPlaytime(sessionsWeek2.stream()) * 1.0 / activePlayThreshold);
long playtimeWeek2 = PlayerProfile.getPlaytime(sessionsWeek2.stream());
double week2Play = (playtimeWeek2 * 1.0 / activePlayThreshold);
if (week2Play > max) {
week2Play = max;
}
double week3Play = (PlayerProfile.getPlaytime(sessionsWeek3.stream()) * 1.0 / activePlayThreshold);
long playtimeWeek3 = PlayerProfile.getPlaytime(sessionsWeek3.stream());
double week3Play = (playtimeWeek3 * 1.0 / activePlayThreshold);
if (week3Play > max) {
week3Play = max;
}
double playtimeMultiplier = 1.0;
if (playtimeWeek + playtimeWeek2 + playtimeWeek3 > activeLoginThreshold * 3.0) {
playtimeMultiplier = 1.25;
}
// Reduce the harshness for new players and players who have had a vacation
if (weekPlay > 1 && week3Play > 1 && week2Play == 0.0) {
week2Play = 0.5;
@ -126,21 +134,20 @@ public class PlayerProfile implements OfflinePlayer {
double playAvg = (weekPlay + week2Play + week3Play) / 3.0;
double weekLogin = sessionsWeek.size() > activeLoginThreshold ? 1.0 : 0.5;
double week2Login = sessionsWeek2.size() > activeLoginThreshold ? 1.0 : 0.5;
double week3Login = sessionsWeek3.size() > activeLoginThreshold ? 1.0 : 0.5;
double weekLogin = sessionsWeek.size() >= activeLoginThreshold ? 1.0 : 0.5;
double week2Login = sessionsWeek2.size() >= activeLoginThreshold ? 1.0 : 0.5;
double week3Login = sessionsWeek3.size() >= activeLoginThreshold ? 1.0 : 0.5;
double extraMultiplier = 1.0;
double loginMultiplier = 1.0;
double loginTotal = weekLogin + week2Login + week3Login;
double loginAvg = loginTotal / 3.0;
if (loginTotal <= 2.0) {
// Reduce index for players that have not logged in the threshold amount for 2 weeks
extraMultiplier = 0.75;
loginMultiplier = 0.75;
}
activityIndx = playAvg * loginAvg * extraMultiplier;
activityIndx = playAvg * loginAvg * loginMultiplier * playtimeMultiplier;
activityIndex.put(date, activityIndx);
return activityIndx;
@ -292,7 +299,7 @@ public class PlayerProfile implements OfflinePlayer {
public Stream<Session> getSessions(long after, long before) {
return getAllSessions()
.filter(session -> session.getSessionStart() >= after && session.getSessionEnd() <= before);
.filter(session -> session.getSessionStart() >= after && session.getSessionStart() <= before);
}
public GeoInfo getMostRecentGeoInfo() {

View File

@ -0,0 +1,119 @@
package main.java.com.djrapitops.plan.data;
import com.djrapitops.plugin.api.TimeAmount;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.TestInit;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class PlayerProfileTest {
@Before
public void setUp() throws Exception {
TestInit.init();
}
@Test
public void testMaxActivityIndex() throws Exception {
PlayerProfile p = new PlayerProfile(null, null, 0L);
List<Session> sessions = new ArrayList<>();
long date = MiscUtils.getTime();
long week = TimeAmount.WEEK.ms();
long weekAgo = date - week;
long twoWeeksAgo = date - 2L * week;
long threeWeeksAgo = date - 3L * week;
long requiredPlaytime = Settings.ACTIVE_PLAY_THRESHOLD.getNumber() * TimeAmount.MINUTE.ms();
int requiredLogins = Settings.ACTIVE_LOGIN_THRESHOLD.getNumber();
for (int i = 0; i < requiredLogins; i++) {
sessions.add(new Session(0, weekAgo, weekAgo + requiredPlaytime * 4L, 0, 0));
sessions.add(new Session(0, twoWeeksAgo, twoWeeksAgo + requiredPlaytime * 4L, 0, 0));
sessions.add(new Session(0, threeWeeksAgo, threeWeeksAgo + requiredPlaytime * 4L, 0, 0));
}
p.setSessions(null, sessions);
assertEquals(5.0, p.getActivityIndex(date));
}
@Test
public void testMaxActivityIndex2() throws Exception {
PlayerProfile p = new PlayerProfile(null, null, 0L);
List<Session> sessions = new ArrayList<>();
long date = MiscUtils.getTime();
long week = TimeAmount.WEEK.ms();
long weekAgo = date - week;
long twoWeeksAgo = date - 2L * week;
long threeWeeksAgo = date - 3L * week;
long requiredPlaytime = Settings.ACTIVE_PLAY_THRESHOLD.getNumber() * TimeAmount.MINUTE.ms();
int requiredLogins = Settings.ACTIVE_LOGIN_THRESHOLD.getNumber();
for (int i = 0; i < requiredLogins * 2; i++) {
sessions.add(new Session(0, weekAgo, weekAgo + requiredPlaytime * 3L, 0, 0));
sessions.add(new Session(0, twoWeeksAgo, twoWeeksAgo + requiredPlaytime * 3L, 0, 0));
sessions.add(new Session(0, threeWeeksAgo, threeWeeksAgo + requiredPlaytime * 3L, 0, 0));
}
p.setSessions(null, sessions);
assertEquals(5.0, p.getActivityIndex(date));
}
@Test
public void testActivityIndexOne() throws Exception {
PlayerProfile p = new PlayerProfile(null, null, 0L);
List<Session> sessions = new ArrayList<>();
long date = MiscUtils.getTime();
long week = TimeAmount.WEEK.ms();
long weekAgo = date - week;
long twoWeeksAgo = date - 2L * week;
long threeWeeksAgo = date - 3L * week;
int requiredLogins = Settings.ACTIVE_LOGIN_THRESHOLD.getNumber();
long requiredPlaytime = Settings.ACTIVE_PLAY_THRESHOLD.getNumber() * TimeAmount.MINUTE.ms() / requiredLogins;
for (int i = 0; i < requiredLogins; i++) {
sessions.add(new Session(i, weekAgo, weekAgo + requiredPlaytime, 0, 0));
sessions.add(new Session(i * 2, twoWeeksAgo, twoWeeksAgo + requiredPlaytime, 0, 0));
sessions.add(new Session(i * 3, threeWeeksAgo, threeWeeksAgo + requiredPlaytime, 0, 0));
}
p.setSessions(null, sessions);
assertTrue(2.0 <= p.getActivityIndex(date));
}
@Test(timeout = 500)
public void testMethodTimeout() throws Exception {
PlayerProfile p = new PlayerProfile(null, null, 0L);
List<Session> sessions = new ArrayList<>();
long date = 0;
for (int i = 0; i < 5000; i++) {
sessions.add(new Session(0, 0, 0, 0, 0));
}
p.setSessions(null, sessions);
p.getActivityIndex(0);
}
}

View File

@ -24,7 +24,9 @@ import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@ -57,6 +59,10 @@ public class DatabaseTest {
private Database backup;
private int rows;
@Rule
public Timeout globalTimeout = Timeout.seconds(6); // 5 seconds max per method tested
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
@ -133,7 +139,7 @@ public class DatabaseTest {
assertEquals("MySQL", new MySQLDB(plan).getName());
}
@Test
@Test(timeout = 3000)
public void testSaveCommandUse() throws SQLException, DatabaseInitException {
CommandUseTable commandUseTable = db.getCommandUseTable();
Map<String, Integer> expected = new HashMap<>();