2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.admin;
|
2020-11-05 21:17:14 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import dev.morphia.Datastore;
|
|
|
|
import dev.morphia.query.Query;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
2022-01-04 03:04:39 +00:00
|
|
|
import dev.plex.rank.enums.Rank;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.storage.StorageType;
|
2020-11-05 21:17:14 +00:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2022-02-05 23:14:23 +00:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached storage for Admin objects
|
|
|
|
* @see Admin
|
|
|
|
*/
|
2020-11-05 21:17:14 +00:00
|
|
|
|
|
|
|
public class AdminList
|
|
|
|
{
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Key/Value storage, where the key is the unique ID of the admin
|
|
|
|
*/
|
2020-11-05 21:17:14 +00:00
|
|
|
private final Map<UUID, Admin> admins = Maps.newHashMap();
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Adds the admin to cache
|
|
|
|
* @param admin The admin object
|
|
|
|
*/
|
2020-11-05 21:17:14 +00:00
|
|
|
public void addToCache(Admin admin)
|
|
|
|
{
|
|
|
|
admins.put(admin.getUuid(), admin);
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Removes an admin from the cache
|
|
|
|
* @param uuid The unique ID of the admin
|
|
|
|
* @see UUID
|
|
|
|
*/
|
2020-11-05 21:17:14 +00:00
|
|
|
public void removeFromCache(UUID uuid)
|
|
|
|
{
|
|
|
|
admins.remove(uuid);
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Gathers every admin's username (cached and databsed)
|
|
|
|
* @return An array list of the names of every admin
|
|
|
|
*/
|
2020-11-05 21:17:14 +00:00
|
|
|
public List<String> getAllAdmins()
|
|
|
|
{
|
|
|
|
List<String> admins = Lists.newArrayList();
|
2021-01-03 07:21:15 +00:00
|
|
|
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
2020-11-05 21:17:14 +00:00
|
|
|
{
|
|
|
|
Datastore store = Plex.get().getMongoConnection().getDatastore();
|
|
|
|
Query<PlexPlayer> query = store.find(PlexPlayer.class);
|
2022-02-05 23:14:23 +00:00
|
|
|
admins.addAll(query.stream().filter(plexPlayer -> plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN)).map(PlexPlayer::getName).collect(Collectors.toList()));
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-05 21:17:14 +00:00
|
|
|
try (Connection con = Plex.get().getSqlConnection().getCon())
|
|
|
|
{
|
|
|
|
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE rank IN(?, ?, ?)");
|
|
|
|
statement.setString(1, Rank.ADMIN.name().toLowerCase());
|
|
|
|
statement.setString(2, Rank.SENIOR_ADMIN.name().toLowerCase());
|
|
|
|
statement.setString(3, Rank.EXECUTIVE.name().toLowerCase());
|
|
|
|
|
|
|
|
ResultSet set = statement.executeQuery();
|
|
|
|
while (set.next())
|
|
|
|
{
|
|
|
|
admins.add(set.getString("name"));
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
|
|
|
catch (SQLException throwables)
|
|
|
|
{
|
2020-11-05 21:17:14 +00:00
|
|
|
throwables.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return admins;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|