Plex/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java

58 lines
1.7 KiB
Java
Raw Normal View History

package me.totalfreedom.plex.cache;
2020-10-28 03:49:56 +00:00
import dev.morphia.query.Query;
import dev.morphia.query.UpdateOperations;
import java.util.UUID;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
public class MongoPlayerData
{
2020-10-28 03:49:56 +00:00
private final PlexPlayerDAO plexPlayerDAO;
public MongoPlayerData()
{
this.plexPlayerDAO = new PlexPlayerDAO(PlexPlayer.class, Plex.get().getMongoConnection().getDatastore());
}
2020-10-28 03:49:56 +00:00
public boolean exists(UUID uuid)
{
Query<PlexPlayer> query = plexPlayerDAO.createQuery();
2020-10-28 03:49:56 +00:00
return query.field("uuid").exists().field("uuid").equal(uuid.toString()).find().tryNext() != null;
}
2020-10-28 03:49:56 +00:00
public PlexPlayer getByUUID(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
Query<PlexPlayer> query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString());
2020-10-28 03:49:56 +00:00
return query2.first();
}
public void update(PlexPlayer player)
{
Query<PlexPlayer> filter = plexPlayerDAO.createQuery()
2020-10-28 03:49:56 +00:00
.field("uuid").equal(player.getUuid());
UpdateOperations<PlexPlayer> updateOps = plexPlayerDAO.createUpdateOperations();
updateOps.set("name", player.getName());
updateOps.set("loginMSG", player.getLoginMSG());
updateOps.set("prefix", player.getPrefix());
2020-10-28 03:49:56 +00:00
updateOps.set("rank", player.getRank() == null ? "" : player.getRank().name().toLowerCase());
updateOps.set("ips", player.getIps());
updateOps.set("coins", player.getCoins());
plexPlayerDAO.update(filter, updateOps);
}
2020-10-28 03:49:56 +00:00
public PlexPlayerDAO getPlexPlayerDAO()
{
return plexPlayerDAO;
}
}