Add staff role mapping, display role in UI, and enhance staff playtime calculations
This commit is contained in:
+12
-4
@@ -1,6 +1,6 @@
|
||||
package com.alttd.altitudeweb.mappers;
|
||||
|
||||
import com.alttd.altitudeweb.database.luckperms.Player;
|
||||
import com.alttd.altitudeweb.database.luckperms.PlayerWithGroup;
|
||||
import com.alttd.altitudeweb.database.proxyplaytime.StaffPt;
|
||||
import com.alttd.altitudeweb.model.StaffPlaytimeDto;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -15,10 +15,10 @@ import java.util.concurrent.TimeUnit;
|
||||
public final class StaffPtToStaffPlaytimeMapper {
|
||||
private record PlaytimeInfo(long totalPlaytime, long lastPlayed) {}
|
||||
|
||||
public List<StaffPlaytimeDto> map(List<StaffPt> sessions, List<Player> staffMembers, long from, long to) {
|
||||
public List<StaffPlaytimeDto> map(List<StaffPt> sessions, List<PlayerWithGroup> staffMembers, long from, long to, HashMap<String, String> staffGroupsMap) {
|
||||
Map<UUID, PlaytimeInfo> playtimeData = getUuidPlaytimeInfoMap(sessions, from, to);
|
||||
|
||||
for (Player staffMember : staffMembers) {
|
||||
for (PlayerWithGroup staffMember : staffMembers) {
|
||||
if (!playtimeData.containsKey(staffMember.uuid())) {
|
||||
playtimeData.put(staffMember.uuid(), new PlaytimeInfo(0L, Long.MIN_VALUE));
|
||||
}
|
||||
@@ -28,14 +28,22 @@ public final class StaffPtToStaffPlaytimeMapper {
|
||||
for (Map.Entry<UUID, PlaytimeInfo> entry : playtimeData.entrySet()) {
|
||||
long lastPlayedMillis = entry.getValue().lastPlayed() == Long.MIN_VALUE ? 0L : entry.getValue().lastPlayed();
|
||||
StaffPlaytimeDto dto = new StaffPlaytimeDto();
|
||||
Optional<PlayerWithGroup> first = staffMembers.stream()
|
||||
.filter(player -> player.uuid().equals(entry.getKey())).findFirst();
|
||||
dto.setStaffMember(first.isPresent() ? first.get().username() : entry.getKey().toString());
|
||||
dto.setStaffMember(staffMembers.stream()
|
||||
.filter(player -> player.uuid().equals(entry.getKey()))
|
||||
.map(Player::username)
|
||||
.map(PlayerWithGroup::username)
|
||||
.findFirst()
|
||||
.orElse(entry.getKey().toString())
|
||||
);
|
||||
dto.setLastPlayed(OffsetDateTime.ofInstant(Instant.ofEpochMilli(lastPlayedMillis), ZoneOffset.UTC));
|
||||
dto.setPlaytime((int) TimeUnit.MILLISECONDS.toMinutes(entry.getValue().totalPlaytime()));
|
||||
if (first.isPresent()) {
|
||||
dto.setRole(staffGroupsMap.getOrDefault(first.get().group(), "Unknown"));
|
||||
} else {
|
||||
dto.setRole("Unknown");
|
||||
}
|
||||
results.add(dto);
|
||||
}
|
||||
return results;
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.alttd.altitudeweb.services.site;
|
||||
|
||||
import com.alttd.altitudeweb.database.Databases;
|
||||
import com.alttd.altitudeweb.database.luckperms.Player;
|
||||
import com.alttd.altitudeweb.database.luckperms.PlayerWithGroup;
|
||||
import com.alttd.altitudeweb.database.luckperms.TeamMemberMapper;
|
||||
import com.alttd.altitudeweb.database.proxyplaytime.StaffPlaytimeMapper;
|
||||
import com.alttd.altitudeweb.database.proxyplaytime.StaffPt;
|
||||
@@ -14,6 +15,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -23,17 +25,30 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class StaffPtService {
|
||||
private final static String STAFF_GROUPS = "'group.admin', 'group.developer', 'group.headmod', 'group.manager', 'group.moderator', 'group.owner', 'group.trainee'";
|
||||
private final static HashMap<String, String> STAFF_GROUPS_MAP = new HashMap<>();
|
||||
private final static String STAFF_GROUPS;
|
||||
static {
|
||||
STAFF_GROUPS_MAP.put("group.owner", "Owner");
|
||||
STAFF_GROUPS_MAP.put("group.manager", "Manager");
|
||||
STAFF_GROUPS_MAP.put("group.admin", "Admin");
|
||||
STAFF_GROUPS_MAP.put("group.headmod", "Head Mod");
|
||||
STAFF_GROUPS_MAP.put("group.moderator", "Moderator");
|
||||
STAFF_GROUPS_MAP.put("group.trainee", "Trainee");
|
||||
STAFF_GROUPS_MAP.put("group.developer", "Developer");
|
||||
STAFF_GROUPS = STAFF_GROUPS_MAP.keySet().stream()
|
||||
.map(group -> "'" + group + "'")
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
private final StaffPtToStaffPlaytimeMapper staffPtToStaffPlaytimeMapper;
|
||||
|
||||
public Optional<List<StaffPlaytimeDto>> getStaffPlaytime(Instant from, Instant to) {
|
||||
CompletableFuture<List<Player>> staffMembersFuture = new CompletableFuture<>();
|
||||
CompletableFuture<List<PlayerWithGroup>> staffMembersFuture = new CompletableFuture<>();
|
||||
CompletableFuture<List<StaffPt>> staffPlaytimeFuture = new CompletableFuture<>();
|
||||
Connection.getConnection(Databases.LUCK_PERMS)
|
||||
.runQuery(sqlSession -> {
|
||||
log.debug("Loading staff members");
|
||||
try {
|
||||
List<Player> staffMemberList = sqlSession.getMapper(TeamMemberMapper.class)
|
||||
List<PlayerWithGroup> staffMemberList = sqlSession.getMapper(TeamMemberMapper.class)
|
||||
.getTeamMembersOfGroupList(STAFF_GROUPS);
|
||||
staffMembersFuture.complete(staffMemberList);
|
||||
} catch (Exception e) {
|
||||
@@ -41,14 +56,14 @@ public class StaffPtService {
|
||||
staffMembersFuture.completeExceptionally(e);
|
||||
}
|
||||
});
|
||||
List<Player> staffMembers = staffMembersFuture.join().stream()
|
||||
List<PlayerWithGroup> staffMembers = staffMembersFuture.join().stream()
|
||||
.collect(Collectors.collectingAndThen(
|
||||
Collectors.toMap(Player::uuid, player -> player, (player1, player2) -> player1),
|
||||
Collectors.toMap(PlayerWithGroup::uuid, player -> player, (player1, player2) -> player1),
|
||||
m -> new ArrayList<>(m.values())));
|
||||
Connection.getConnection(Databases.PROXY_PLAYTIME)
|
||||
.runQuery(sqlSession -> {
|
||||
String staffUUIDs = staffMembers.stream()
|
||||
.map(Player::uuid)
|
||||
.map(PlayerWithGroup::uuid)
|
||||
.map(uuid -> "'" + uuid + "'")
|
||||
.collect(Collectors.joining(","));
|
||||
log.debug("Loading staff playtime for group");
|
||||
@@ -62,7 +77,7 @@ public class StaffPtService {
|
||||
}
|
||||
});
|
||||
List<StaffPt> join = staffPlaytimeFuture.join();
|
||||
|
||||
return Optional.of(staffPtToStaffPlaytimeMapper.map(join, staffMembers, from.toEpochMilli(), to.toEpochMilli()));
|
||||
HashMap<String, String> staffGroupsMap = new HashMap<>(STAFF_GROUPS_MAP);
|
||||
return Optional.of(staffPtToStaffPlaytimeMapper.map(join, staffMembers, from.toEpochMilli(), to.toEpochMilli(), staffGroupsMap));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user