Added a terrible way to sync parties cus I don't have time to implement a proper solution atm

This commit is contained in:
2021-08-08 13:30:57 +02:00
parent 30cc4b3ead
commit 9551785f83
7 changed files with 132 additions and 87 deletions
@@ -1,14 +1,12 @@
package com.alttd.chat.database;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail;
import com.alttd.chat.objects.Party;
import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.util.ALogger;
import net.kyori.adventure.text.minimessage.MiniMessage;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -24,7 +22,7 @@ public class Queries {
List<String> tables = new ArrayList<>();
tables.add("CREATE TABLE IF NOT EXISTS ignored_users (`uuid` VARCHAR(36) NOT NULL, `ignored_uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`uuid`, `ignored_uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS parties (`id` INT NOT NULL AUTO_INCREMENT, `owner_uuid` VARCHAR(36) NOT NULL, `party_name` VARCHAR(36) NOT NULL, `password` VARCHAR(36), PRIMARY KEY (`id`))");
tables.add("CREATE TABLE IF NOT EXISTS chat_users (`uuid` VARCHAR(36) NOT NULL, `party_id` INT NOT NULL, `toggled_chat` BIT(1) DEFAULT b'0', `toggled_gc` BIT(1) DEFAULT b'0', PRIMARY KEY (`uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS chat_users (`uuid` VARCHAR(36) NOT NULL, `party_id` INT NOT NULL, `toggled_channel` VARCHAR(36) NULL DEFAULT NULL, PRIMARY KEY (`uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS mails (`id` INT NOT NULL AUTO_INCREMENT, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(36) NOT NULL, `message` VARCHAR(256) NOT NULL, `sendtime` BIGINT default 0, `readtime` BIGINT default 0, PRIMARY KEY (`id`))");
try {
@@ -197,6 +195,42 @@ public class Queries {
}
}
public static void loadPartyUsers(int id) {
String query = "SELECT chat_users.uuid, nicknames.nickname, utility_users.Username " +
"FROM chat_users " +
"LEFT OUTER JOIN nicknames ON chat_users.UUID = nicknames.uuid " +
"LEFT OUTER JOIN utility_users ON chat_users.uuid = utility_users.UUID " +
"WHERE party_id = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
Party party = PartyManager.getParty(id);
if (party == null) {
ALogger.warn("Tried to load invalid party");
return;
}
party.resetPartyUsers();
while (resultSet.next()) {
UUID uuid = UUID.fromString(resultSet.getString("uuid"));
// String displayName = resultSet.getString("nickname");
// if (displayName == null || displayName.isEmpty()) {
// displayName = resultSet.getString("Username");
// }
String displayName = resultSet.getString("Username"); // FIXME: 08/08/2021 only using display name till we can fix nickname colors
party.putUser(uuid, displayName);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Party addParty(UUID partyOwner, String partyName, String password) {
String query = "INSERT INTO parties (owner_uuid, party_name, password) VALUES (?, ?, ?)";
@@ -321,27 +355,6 @@ public class Queries {
//-----------------------------------------
public static void loadChatUsers() { //TODO Get parties from cache somewhere
String query = "SELECT * FROM chat_users WHERE party_id > -1";
try {
Connection connection = DatabaseConnection.getConnection();
ResultSet resultSet = connection.prepareStatement(query).executeQuery();
while (resultSet.next()) {
UUID uuid = UUID.fromString(resultSet.getString("uuid"));
int partyId = resultSet.getInt("party_id");
boolean toggled_chat = resultSet.getInt("toggled_chat") == 1;
boolean toggle_Gc = resultSet.getInt("toggled_gc") == 1;
ChatUserManager.addUser(new ChatUser(uuid, partyId, toggled_chat, toggle_Gc));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static ChatUser loadChatUser(UUID uuid) { //TODO Get parties from cache somewhere
String query = "SELECT * FROM chat_users WHERE uuid = ?";
ChatUser user = null;
@@ -356,9 +369,9 @@ public class Queries {
while (resultSet.next()) {
int partyId = resultSet.getInt("party_id");
boolean toggled_chat = resultSet.getInt("toggled_chat") == 1;
boolean toggle_Gc = resultSet.getInt("toggled_gc") == 1;
user = new ChatUser(uuid, partyId, toggled_chat, toggle_Gc);
String toggledChannel = resultSet.getString("toggled_channel");
Channel channel = toggledChannel == null ? null : Channel.getChatChannel(toggledChannel);
user = new ChatUser(uuid, partyId, channel);
}
} catch (SQLException e) {
@@ -367,20 +380,13 @@ public class Queries {
return user;
}
public static void setPartyChatState(boolean toggledChat, UUID uuid) {
setBitWhereId("UPDATE chat_users set toggled_chat = ? WHERE uuid = ?", toggledChat, uuid);
}
public static void setGlobalChatState(boolean globalChat, UUID uuid) {
setBitWhereId("UPDATE chat_users set toggled_gc = ? WHERE uuid = ?", globalChat, uuid);
}
private static void setBitWhereId(String query, boolean bool, UUID uuid) {
public static void setToggledChannel(Channel channel, UUID uuid) {
String sql = "UPDATE chat_users set toggled_channel = ? WHERE uuid = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, bool ? 1 : 0);
statement.setString(1, channel.getChannelName());
statement.setString(2, uuid.toString());
statement.execute();
@@ -418,19 +424,17 @@ public class Queries {
}
public static void saveUser(ChatUser user) {
String query = "INSERT INTO chat_users (uuid, party_id, toggled_chat, toggled_gc) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE party_id = ?, toggled_chat = ?, toggled_gc = ?";
String query = "INSERT INTO chat_users (uuid, party_id, toggled_channel) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE party_id = ?, toggled_channel = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
Channel toggledChannel = user.getToggledChannel();
statement.setString(1, user.getUuid().toString());
statement.setInt(2, user.getPartyId());
statement.setInt(3, user.toggledPartyChat() ? 1 : 0);
statement.setInt(4, 0);
statement.setInt(5, user.getPartyId());
statement.setInt(6, user.toggledPartyChat() ? 1 : 0);
statement.setInt(7, 0);
statement.setString(3, toggledChannel == null ? null : toggledChannel.getChannelName());
statement.setInt(4, user.getPartyId());
statement.setString(5, toggledChannel == null ? null : toggledChannel.getChannelName());
statement.execute();
} catch (SQLException e) {
@@ -1,8 +1,10 @@
package com.alttd.chat.objects;
import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.Component;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
@@ -11,7 +13,7 @@ import java.util.UUID;
public class ChatUser {
private final UUID uuid; // player uuid
private int partyId; // the party they are in
private boolean toggledPartyChat; // should chat messages instantly go to party chat when added, idk if this should be saved
private Channel toggledChannel;
private String name; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview
private Component displayName; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview
// private Component prefix; // doesn't need saving, we get this from luckperms
@@ -25,10 +27,10 @@ public class ChatUser {
private List<UUID> ignoredPlayers; // a list of UUID, a new table non unique, where one is is the player select * from ignores where ignoredby = thisplayer? where the result is the uuid of the player ignored by this player?
private List<UUID> ignoredBy; // a list of UUID, same table as above but select * from ignores where ignored = thisplayer? result should be the other user that ignored this player?
public ChatUser(UUID uuid, int partyId, boolean toggledChat, boolean toggleGc) {
public ChatUser(UUID uuid, int partyId, Channel toggledChannel) {
this.uuid = uuid;
this.partyId = partyId;
this.toggledPartyChat = toggledChat;
this.toggledChannel = toggledChannel;
name = Queries.getDisplayName(uuid);
if (name == null) {
@@ -41,7 +43,6 @@ public class ChatUser {
//
// prefixAll = Utility.getPrefix(uuid, false);
//this.toggleGc = toggleGc;
replyTarget = null;
gcCooldown = System.currentTimeMillis(); // players can't use gc for 30 seconds after logging in if we use this?
mails = Queries.getMails(uuid);
@@ -61,13 +62,13 @@ public class ChatUser {
this.partyId = partyId;
}
public boolean toggledPartyChat() {
return toggledPartyChat;
public Channel getToggledChannel() {
return toggledChannel;
}
public void togglePartyChat() {
toggledPartyChat = !toggledPartyChat;
Queries.setPartyChatState(toggledPartyChat, uuid); //TODO: Async pls - no CompleteableFuture<>!
public void setToggledChannel(Channel channel) {
toggledChannel = channel;
Queries.setToggledChannel(toggledChannel, uuid); //TODO: Async pls - no CompleteableFuture<>!
}
public Component getDisplayName() {
@@ -3,7 +3,6 @@ package com.alttd.chat.objects;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import java.util.*;
import java.util.List;
@@ -103,4 +102,8 @@ public class Party {
public String getUserDisplayName(UUID uuid) {
return partyUsers.get(uuid);
}
public void resetPartyUsers() { // FIXME: 08/08/2021 This is a temp solution until bungee messages take over updating parties
partyUsers.clear();
}
}