so far it is working but the only thing preventing me from going to fully asynchronous calls for punishments is httpd...

This commit is contained in:
Taah
2022-04-04 01:36:50 -07:00
parent 35d436bb61
commit 6f4bc5aec1
38 changed files with 483 additions and 528 deletions

View File

@ -0,0 +1,124 @@
package dev.plex.cache.player;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
import dev.morphia.query.Update;
import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
* Mongo fetching utilities for players
*/
public class MongoPlayerData
{
/**
* The datastore object / database
*/
private final Datastore datastore;
/**
* Creates an instance of the player data
*/
public MongoPlayerData()
{
this.datastore = Plex.get().getMongoConnection().getDatastore();
}
/**
* Checks whether the player exists in mongo's database
*
* @param uuid The unique ID of the player
* @return true if the player was found
*/
public boolean exists(UUID uuid)
{
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
.filter(Filters.eq("uuid", uuid.toString()));
return query.first() != null;
}
/**
* Gets the player from cache or from mongo's database
*
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByUUID(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid.toString()));
return query2.first();
}
/**
* Gets the player from cache or from mongo's database
*
* @param ip The IP address of the player.
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.in("ips", Collections.singleton(ip)));
return query2.first();
}
/**
* Updates a player's information in the mongo database
*
* @param player The PlexPlayer object
* @see PlexPlayer
*/
public void update(PlexPlayer player)
{
Query<PlexPlayer> filter = datastore.find(PlexPlayer.class)
.filter(Filters.eq("uuid", player.getUuid()));
Update<PlexPlayer> updateOps = filter
.update(
UpdateOperators.set("name", player.getName()),
UpdateOperators.set("loginMSG", player.getLoginMessage()),
UpdateOperators.set("prefix", player.getPrefix()),
UpdateOperators.set("rank", player.getRank().toLowerCase()),
UpdateOperators.set("ips", player.getIps()),
UpdateOperators.set("coins", player.getCoins()),
UpdateOperators.set("vanished", player.isVanished()),
UpdateOperators.set("commandspy", player.isCommandSpy()));
updateOps.execute();
}
public List<PlexPlayer> getPlayers()
{
return datastore.find(PlexPlayer.class).stream().toList();
}
/**
* Saves the player's information in the database
*
* @param plexPlayer The PlexPlayer object
* @see PlexPlayer
*/
public void save(PlexPlayer plexPlayer)
{
datastore.save(plexPlayer);
}
}

View File

@ -0,0 +1,48 @@
package dev.plex.cache.player;
import com.google.common.collect.Maps;
import dev.plex.player.PlexPlayer;
import java.util.Map;
import java.util.UUID;
/**
* Cache storage
*/
public class PlayerCache
{
/**
* A key/value pair where the key is the unique ID of the Plex Player
*/
private static final Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
/**
* A key/value pair where the key is the unique ID of the Punished Player
*/
// private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
// public static Map<UUID, PunishedPlayer> getPunishedPlayerMap()
// {
// return punishedPlayerMap;
// }
public static Map<UUID, PlexPlayer> getPlexPlayerMap()
{
return plexPlayerMap;
}
/*public static PunishedPlayer getPunishedPlayer(UUID uuid)
{
if (!getPunishedPlayerMap().containsKey(uuid))
{
getPunishedPlayerMap().put(uuid, new PunishedPlayer(uuid));
}
return getPunishedPlayerMap().get(uuid);
}
*/
public static PlexPlayer getPlexPlayer(UUID uuid)
{
return getPlexPlayerMap().get(uuid);
}
}

View File

@ -0,0 +1,205 @@
package dev.plex.cache.player;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
/**
* SQL fetching utilities for players
*/
public class SQLPlayerData
{
private final String SELECT = "SELECT * FROM `players` WHERE uuid=?";
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=?, coins=?, vanished=?, commandspy=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`, `coins`, `vanished`, `commandspy`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
/**
* Checks if a player exists in the SQL database
*
* @param uuid The unique ID of the player
* @return true if the player was found in the database
*/
public boolean exists(UUID uuid)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return false;
}
/**
* Gets the player from cache or from the SQL database
*
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByUUID(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
PlexPlayer plexPlayer = new PlexPlayer(uuid);
while (set.next())
{
String name = set.getString("name");
String loginMSG = set.getString("login_msg");
String prefix = set.getString("prefix");
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
boolean vanished = set.getBoolean("vanished");
boolean commandspy = set.getBoolean("commandspy");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
plexPlayer.setName(name);
plexPlayer.setLoginMessage(loginMSG);
plexPlayer.setPrefix(prefix);
plexPlayer.setRank(rankName);
plexPlayer.setIps(ips);
plexPlayer.setCoins(coins);
plexPlayer.setVanished(vanished);
plexPlayer.setCommandSpy(commandspy);
}
return plexPlayer;
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return null;
}
/**
* Gets the player from cache or from the SQL database
*
* @param ip The IP address of the player.
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;
}
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("select * from `players` where json_search(ips, ?, ?) IS NOT NULL LIMIT 1");
statement.setString(1, "one");
statement.setString(2, ip);
ResultSet set = statement.executeQuery();
PlexPlayer plexPlayer = null;
while (set.next())
{
String uuid = set.getString("uuid");
String name = set.getString("name");
String loginMSG = set.getString("login_msg");
String prefix = set.getString("prefix");
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
boolean vanished = set.getBoolean("vanished");
boolean commandspy = set.getBoolean("commandspy");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
plexPlayer = new PlexPlayer(UUID.fromString(uuid));
plexPlayer.setName(name);
plexPlayer.setLoginMessage(loginMSG);
plexPlayer.setPrefix(prefix);
plexPlayer.setRank(rankName);
plexPlayer.setIps(ips);
plexPlayer.setCoins(coins);
plexPlayer.setVanished(vanished);
plexPlayer.setCommandSpy(commandspy);
}
return plexPlayer;
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return null;
}
/**
* Updates a player's information in the SQL database
*
* @param player The PlexPlayer object
* @see PlexPlayer
*/
public void update(PlexPlayer player)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(UPDATE);
statement.setString(1, player.getName());
statement.setString(2, player.getLoginMessage());
statement.setString(3, player.getPrefix());
statement.setString(4, player.getRank().toLowerCase());
statement.setString(5, new Gson().toJson(player.getIps()));
statement.setLong(6, player.getCoins());
statement.setBoolean(7, player.isVanished());
statement.setBoolean(8, player.isCommandSpy());
statement.setString(9, player.getUuid());
statement.executeUpdate();
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
}
/**
* Inserts the player's information in the database
*
* @param player The PlexPlayer object
* @see PlexPlayer
*/
public void insert(PlexPlayer player)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(INSERT);
statement.setString(1, player.getUuid());
statement.setString(2, player.getName());
statement.setString(3, player.getLoginMessage());
statement.setString(4, player.getPrefix());
statement.setString(5, player.getRank().toLowerCase());
statement.setString(6, new Gson().toJson(player.getIps()));
statement.setLong(7, player.getCoins());
statement.setBoolean(8, player.isVanished());
statement.setBoolean(9, player.isCommandSpy());
statement.execute();
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
}
}