2020-10-27 20:12:38 +00:00
|
|
|
package me.totalfreedom.plex.cache;
|
|
|
|
|
2020-10-28 04:11:23 +00:00
|
|
|
import dev.morphia.Datastore;
|
|
|
|
import dev.morphia.query.Query;
|
|
|
|
import dev.morphia.query.UpdateOperations;
|
2020-10-27 20:12:38 +00:00
|
|
|
import me.totalfreedom.plex.Plex;
|
|
|
|
import me.totalfreedom.plex.player.PlexPlayer;
|
2020-10-28 04:11:23 +00:00
|
|
|
|
|
|
|
import java.util.UUID;
|
2020-10-27 20:12:38 +00:00
|
|
|
|
|
|
|
public class MongoPlayerData
|
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
private final Datastore datastore;
|
2020-10-27 20:12:38 +00:00
|
|
|
|
|
|
|
public MongoPlayerData()
|
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
this.datastore = Plex.get().getMongoConnection().getDatastore();
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:49:56 +00:00
|
|
|
public boolean exists(UUID uuid)
|
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
Query<PlexPlayer> query = datastore.createQuery(PlexPlayer.class);
|
2020-10-27 20:12:38 +00:00
|
|
|
|
2020-10-28 04:11:23 +00:00
|
|
|
return query.field("uuid").exists().field("uuid").equal(uuid.toString()).first() != null;
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:49:56 +00:00
|
|
|
public PlexPlayer getByUUID(UUID uuid)
|
|
|
|
{
|
2020-10-27 20:12:38 +00:00
|
|
|
|
2020-10-27 21:04:05 +00:00
|
|
|
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
2020-10-27 20:12:38 +00:00
|
|
|
{
|
2020-10-27 21:04:05 +00:00
|
|
|
return PlayerCache.getPlexPlayerMap().get(uuid);
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
2020-10-28 04:11:23 +00:00
|
|
|
Query<PlexPlayer> query2 = datastore.createQuery(PlexPlayer.class).field("uuid").exists().field("uuid").equal(uuid.toString());
|
|
|
|
return query2.first();
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void update(PlexPlayer player)
|
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
Query<PlexPlayer> filter = datastore.createQuery(PlexPlayer.class)
|
2020-10-28 03:49:56 +00:00
|
|
|
.field("uuid").equal(player.getUuid());
|
2020-10-27 20:12:38 +00:00
|
|
|
|
2020-10-28 04:11:23 +00:00
|
|
|
UpdateOperations<PlexPlayer> updateOps = datastore.createUpdateOperations(PlexPlayer.class);
|
2020-10-27 20:12:38 +00:00
|
|
|
|
|
|
|
updateOps.set("name", player.getName());
|
|
|
|
updateOps.set("loginMSG", player.getLoginMSG());
|
|
|
|
updateOps.set("prefix", player.getPrefix());
|
2020-10-28 03:55:35 +00:00
|
|
|
updateOps.set("rank", player.getRank().toLowerCase());
|
2020-10-27 20:12:38 +00:00
|
|
|
updateOps.set("ips", player.getIps());
|
2020-10-27 21:30:57 +00:00
|
|
|
updateOps.set("coins", player.getCoins());
|
2020-10-28 04:11:23 +00:00
|
|
|
datastore.update(filter, updateOps);
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 04:11:23 +00:00
|
|
|
public void save(PlexPlayer plexPlayer)
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
datastore.save(plexPlayer);
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
2020-10-28 04:11:23 +00:00
|
|
|
|
2020-10-28 03:55:35 +00:00
|
|
|
}
|