2022-05-06 02:28:39 +00:00
|
|
|
package dev.plex.data;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.common.reflect.TypeToken;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.guild.Guild;
|
2022-05-08 23:34:03 +00:00
|
|
|
import dev.plex.guild.data.Member;
|
2022-05-06 02:28:39 +00:00
|
|
|
import dev.plex.util.CustomLocation;
|
|
|
|
import dev.plex.util.GuildUtil;
|
2022-05-08 23:34:03 +00:00
|
|
|
import dev.plex.util.PlexLog;
|
2022-05-06 02:28:39 +00:00
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.ZoneId;
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.util.List;
|
2022-05-08 23:34:03 +00:00
|
|
|
import java.util.Map;
|
2022-05-06 02:28:39 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
public class SQLGuildManager
|
|
|
|
{
|
|
|
|
private static final Gson GSON = new Gson();
|
|
|
|
|
|
|
|
private static final String SELECT_GUILD = "SELECT * FROM `guilds`";
|
2022-05-08 23:34:03 +00:00
|
|
|
private static final String INSERT_GUILD = "INSERT INTO `guilds` (`guildUuid`, `name`, `owner`, `createdAt`, `members`, `moderators`, `prefix`, `motd`, `ranks`, `defaultRank`, `warps`, `home`, `tagEnabled`, `isPublic`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
|
private static final String DELETE_GUILD = "DELETE FROM `guilds` WHERE guildUuid=?";
|
|
|
|
private static final String UPDATE_GUILD = "UPDATE `guilds` SET name=?, owner=?, members=?, moderators=?, prefix=?, motd=?, ranks=?, defaultRank=?, home=?, warps=?, tagEnabled=?, isPublic=? WHERE guildUuid=?";
|
2022-05-06 02:28:39 +00:00
|
|
|
|
|
|
|
public CompletableFuture<Guild> insertGuild(Guild guild)
|
|
|
|
{
|
|
|
|
return CompletableFuture.supplyAsync(() ->
|
|
|
|
{
|
|
|
|
try (Connection connection = Plex.get().getSqlConnection().getCon())
|
|
|
|
{
|
|
|
|
PreparedStatement statement = connection.prepareStatement(INSERT_GUILD);
|
2022-05-08 05:54:22 +00:00
|
|
|
statement.setString(1, guild.getGuildUuid().toString());
|
|
|
|
statement.setString(2, guild.getName());
|
2022-05-08 23:34:03 +00:00
|
|
|
statement.setString(3, GSON.toJson(guild.getOwner()));
|
2022-05-08 05:54:22 +00:00
|
|
|
statement.setLong(4, guild.getCreatedAt().toInstant().toEpochMilli());
|
|
|
|
statement.setString(5, GSON.toJson(guild.getMembers()));
|
|
|
|
statement.setString(6, GSON.toJson(guild.getModerators().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
|
|
statement.setString(7, guild.getPrefix());
|
|
|
|
statement.setString(8, guild.getMotd());
|
|
|
|
statement.setString(9, GSON.toJson(guild.getRanks()));
|
|
|
|
statement.setString(10, GSON.toJson(guild.getDefaultRank()));
|
2022-05-08 23:34:03 +00:00
|
|
|
statement.setString(11, GSON.toJson(guild.getWarps()));
|
|
|
|
statement.setString(12, GSON.toJson(guild.getHome()));
|
|
|
|
statement.setBoolean(13, guild.isTagEnabled());
|
|
|
|
statement.setBoolean(14, guild.isPublic());
|
2022-05-08 05:54:22 +00:00
|
|
|
statement.execute();
|
|
|
|
return guild;
|
|
|
|
} catch (SQLException e)
|
|
|
|
{
|
|
|
|
GuildUtil.throwExceptionSync(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompletableFuture<Guild> updateGuild(Guild guild)
|
|
|
|
{
|
|
|
|
return CompletableFuture.supplyAsync(() ->
|
|
|
|
{
|
|
|
|
try (Connection connection = Plex.get().getSqlConnection().getCon())
|
|
|
|
{
|
|
|
|
PreparedStatement statement = connection.prepareStatement(UPDATE_GUILD);
|
2022-05-06 02:28:39 +00:00
|
|
|
statement.setString(1, guild.getName());
|
2022-05-08 23:34:03 +00:00
|
|
|
statement.setString(2, GSON.toJson(guild.getOwner()));
|
2022-05-08 05:54:22 +00:00
|
|
|
statement.setString(3, GSON.toJson(guild.getMembers()));
|
|
|
|
statement.setString(4, GSON.toJson(guild.getModerators().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
|
|
statement.setString(5, guild.getPrefix());
|
|
|
|
statement.setString(6, guild.getMotd());
|
|
|
|
statement.setString(7, GSON.toJson(guild.getRanks()));
|
|
|
|
statement.setString(8, GSON.toJson(guild.getDefaultRank()));
|
|
|
|
statement.setString(9, GSON.toJson(guild.getHome()));
|
2022-05-08 23:34:03 +00:00
|
|
|
statement.setString(10, GSON.toJson(guild.getWarps()));
|
|
|
|
statement.setBoolean(11, guild.isTagEnabled());
|
|
|
|
statement.setBoolean(12, guild.isPublic());
|
|
|
|
statement.setString(13, guild.getGuildUuid().toString());
|
2022-05-08 05:54:22 +00:00
|
|
|
statement.executeUpdate();
|
2022-05-06 02:28:39 +00:00
|
|
|
return guild;
|
|
|
|
} catch (SQLException e)
|
|
|
|
{
|
|
|
|
GuildUtil.throwExceptionSync(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompletableFuture<List<Guild>> getGuilds()
|
|
|
|
{
|
|
|
|
return CompletableFuture.supplyAsync(() ->
|
|
|
|
{
|
|
|
|
List<Guild> guilds = Lists.newArrayList();
|
|
|
|
try (Connection connection = Plex.get().getSqlConnection().getCon())
|
|
|
|
{
|
|
|
|
PreparedStatement statement = connection.prepareStatement(SELECT_GUILD);
|
|
|
|
ResultSet set = statement.executeQuery();
|
|
|
|
while (set.next())
|
|
|
|
{
|
2022-05-08 05:54:22 +00:00
|
|
|
Guild guild = new Guild(UUID.fromString(set.getString("guildUuid")),
|
2022-05-06 02:28:39 +00:00
|
|
|
set.getString("name"),
|
2022-05-08 23:34:03 +00:00
|
|
|
GSON.fromJson(set.getString("owner"), Member.class),
|
2022-05-06 02:28:39 +00:00
|
|
|
ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("createdAt")), ZoneId.of(Plex.get().config.getString("server.timezone")).getRules().getOffset(Instant.now())));
|
|
|
|
guild.getMembers().addAll(new Gson().fromJson(set.getString("members"), new TypeToken<List<String>>(){}.getType()));
|
|
|
|
guild.getModerators().addAll(new Gson().fromJson(set.getString("moderators"), new TypeToken<List<String>>(){}.getType()));
|
|
|
|
guild.setPrefix(set.getString("prefix"));
|
|
|
|
guild.setMotd(set.getString("motd"));
|
|
|
|
guild.setHome(GSON.fromJson(set.getString("home"), CustomLocation.class));
|
|
|
|
guild.setTagEnabled(set.getBoolean("tagEnabled"));
|
2022-05-08 23:34:03 +00:00
|
|
|
Map<String, CustomLocation> warps = GSON.fromJson(set.getString("warps"), new TypeToken<Map<String, CustomLocation>>(){}.getType());
|
|
|
|
PlexLog.debug("Loaded {0} warps for {1} guild", warps.size(), guild.getName());
|
|
|
|
guild.getWarps().putAll(GSON.fromJson(set.getString("warps"), new TypeToken<Map<String, CustomLocation>>(){}.getType()));
|
|
|
|
guild.setPublic(set.getBoolean("isPublic"));
|
2022-05-06 02:28:39 +00:00
|
|
|
guilds.add(guild);
|
|
|
|
}
|
|
|
|
} catch (SQLException e)
|
|
|
|
{
|
|
|
|
GuildUtil.throwExceptionSync(e);
|
|
|
|
}
|
|
|
|
return guilds;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|