Compare commits
3 commits
905fc4ba35
...
51115e1a2a
Author | SHA1 | Date | |
---|---|---|---|
51115e1a2a | |||
a83de958d7 | |||
04cd9e2ff2 |
7 changed files with 100 additions and 13 deletions
8
pom.xml
8
pom.xml
|
@ -10,10 +10,10 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>18</maven.compiler.source>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
|
||||
<junit.jupiter.version>5.9.0</junit.jupiter.version>
|
||||
<mockito.version>4.8.0</mockito.version>
|
||||
<junit.jupiter.version>5.10.0</junit.jupiter.version>
|
||||
<mockito.version>5.4.0</mockito.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
@ -27,7 +27,7 @@
|
|||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.19.2-R0.1-SNAPSHOT</version>
|
||||
<version>1.20.1-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package de.hhhammer.playtime.ng;
|
||||
|
||||
import de.hhhammer.playtime.ng.player.Player;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class PlaytimeCalculator {
|
||||
|
||||
private final Clock clock;
|
||||
|
||||
public PlaytimeCalculator(Clock clock) {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
public PlaytimeCalculator() {
|
||||
this(Clock.systemDefaultZone());
|
||||
}
|
||||
|
||||
public Duration calculatePlaytime(Player player) {
|
||||
Duration playTime = timePassedBetweenSaveOrJoin(player);
|
||||
return Duration.from(player.playtime()).plus(playTime);
|
||||
}
|
||||
|
||||
private Duration timePassedBetweenSaveOrJoin(Player player) {
|
||||
if (player.saveTime().isPresent() && Duration.between(player.saveTime().get(), player.joinTime()).isNegative()) {
|
||||
return Duration.between(player.saveTime().get(), LocalDateTime.now(clock));
|
||||
}
|
||||
return Duration.between(player.joinTime(), LocalDateTime.now(clock));
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package de.hhhammer.playtime.ng;
|
||||
|
||||
import de.hhhammer.playtime.ng.persistence.FileSystemDB;
|
||||
import de.hhhammer.playtime.ng.persistence.FileSystemPlayerTimeDB;
|
||||
import de.hhhammer.playtime.ng.serialization.JsonSerializationFactory;
|
||||
import de.hhhammer.playtime.ng.serialization.SerializationFactory;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
@ -9,12 +9,12 @@ import java.io.File;
|
|||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public final class Plugin extends JavaPlugin {
|
||||
private final FileSystemDB persistence;
|
||||
private final FileSystemPlayerTimeDB persistence;
|
||||
|
||||
public Plugin() {
|
||||
super();
|
||||
final SerializationFactory serializationFactory = new JsonSerializationFactory(DateTimeFormatter.ISO_DATE_TIME);
|
||||
this.persistence = new FileSystemDB(new File("plugins/PlayTimeNG"), serializationFactory);
|
||||
this.persistence = new FileSystemPlayerTimeDB(new File("plugins/PlayTimeNG"), serializationFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -12,12 +12,12 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class FileSystemDB implements PersistPlayer, FindPlayer {
|
||||
public final class FileSystemPlayerTimeDB implements PlayerTimeDB {
|
||||
|
||||
private final File saveDirectory;
|
||||
private final SerializationFactory serializationFactory;
|
||||
|
||||
public FileSystemDB(final File saveDirectory, final SerializationFactory serializationFactory) {
|
||||
public FileSystemPlayerTimeDB(final File saveDirectory, final SerializationFactory serializationFactory) {
|
||||
this.saveDirectory = saveDirectory;
|
||||
this.serializationFactory = serializationFactory;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package de.hhhammer.playtime.ng.persistence;
|
||||
|
||||
public interface PlayerTimeDB extends FindPlayer, PersistPlayer{
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package de.hhhammer.playtime.ng;
|
||||
|
||||
import de.hhhammer.playtime.ng.player.PlayTimePlayer;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.*;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PlaytimeCalculatorTest {
|
||||
|
||||
@Nested
|
||||
class CalculatePlaytimeTest {
|
||||
private final Clock clock = Clock.fixed(Instant.parse("2018-08-22T10:00:00Z"), ZoneId.systemDefault());
|
||||
|
||||
@Test
|
||||
void shouldReturnTimeBetweenJoinAndNowWhenNotSaved() {
|
||||
var playtimeCalculator = new PlaytimeCalculator(clock);
|
||||
var player = new PlayTimePlayer(UUID.randomUUID(), "Name", Duration.ofHours(1), LocalDateTime.now(clock).minusMinutes(5));
|
||||
|
||||
Duration playtime = playtimeCalculator.calculatePlaytime(player);
|
||||
assertEquals(Duration.ofHours(1)
|
||||
.plus(Duration.ofMinutes(5)),
|
||||
playtime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnTimeBetweenJoinAndNowWhenSavedBeforeJoin() {
|
||||
var playtimeCalculator = new PlaytimeCalculator(clock);
|
||||
var player = new PlayTimePlayer(UUID.randomUUID(), "Name", Duration.ofHours(1), LocalDateTime.now(clock).minusMinutes(5));
|
||||
|
||||
Duration playtime = playtimeCalculator.calculatePlaytime(player);
|
||||
assertEquals(Duration.ofHours(1)
|
||||
.plus(Duration.ofMinutes(5)),
|
||||
playtime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnTimeBetweenSaveAndNowWhenSavedAfterJoin() {
|
||||
var playtimeCalculator = new PlaytimeCalculator(clock);
|
||||
var player = new PlayTimePlayer(UUID.randomUUID(), "Name", Duration.ofHours(1), LocalDateTime.now(clock).minusMinutes(5), LocalDateTime.now(clock).minusMinutes(1));
|
||||
|
||||
Duration playtime = playtimeCalculator.calculatePlaytime(player);
|
||||
assertEquals(Duration.ofHours(1)
|
||||
.plus(Duration.ofMinutes(1)),
|
||||
playtime);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,7 +25,7 @@ import static org.mockito.ArgumentMatchers.anyString;
|
|||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class FileSystemDBTest {
|
||||
class FileSystemPlayerTimeDBTest {
|
||||
|
||||
@Mock
|
||||
private SerializationFactory mockSerializationFactory;
|
||||
|
@ -34,13 +34,13 @@ class FileSystemDBTest {
|
|||
@Mock
|
||||
private Serializer mockSerializer;
|
||||
private File testDirectory;
|
||||
private FileSystemDB fileSystemDB;
|
||||
private FileSystemPlayerTimeDB fileSystemDB;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws IOException {
|
||||
final Path path = Files.createTempDirectory("playtime").toAbsolutePath();
|
||||
this.testDirectory = path.toFile();
|
||||
this.fileSystemDB = new FileSystemDB(testDirectory, mockSerializationFactory);
|
||||
this.fileSystemDB = new FileSystemPlayerTimeDB(testDirectory, mockSerializationFactory);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
@ -100,7 +100,7 @@ class FileSystemDBTest {
|
|||
@Test
|
||||
void shouldThrowIfDirectoryIsAFile() {
|
||||
final var notADir = new File(testDirectory, "not_a_dir");
|
||||
final var fileSystemDB = new FileSystemDB(notADir, mockSerializationFactory);
|
||||
final var fileSystemDB = new FileSystemPlayerTimeDB(notADir, mockSerializationFactory);
|
||||
final Exception exception = assertThrows(RuntimeException.class, () -> fileSystemDB.findById(UUID.randomUUID()));
|
||||
assertEquals("Could not find files in: " + notADir.getAbsolutePath(), exception.getMessage());
|
||||
}
|
Loading…
Reference in a new issue