mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
fix saving of ignored players & left channels to db
This commit is contained in:
1 parent
f4f5f0d7e5
commit
470e506cb0
16 files changed
+51
-54
No files matched your search
@@ -36,6 +36,7 @@ import net.draycia.carbon.common.messaging.MessagingManager;
|
|||||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||||
import net.draycia.carbon.common.users.db.DatabaseUserManager;
|
import net.draycia.carbon.common.users.db.DatabaseUserManager;
|
||||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||||
|
import net.draycia.carbon.common.util.PlayerUtils;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
@@ -111,7 +112,8 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
|||||||
final CarbonPlayerCommon player = this.loadOrCreate(uuid);
|
final CarbonPlayerCommon player = this.loadOrCreate(uuid);
|
||||||
this.playerInjector.injectMembers(player);
|
this.playerInjector.injectMembers(player);
|
||||||
if (this instanceof DatabaseUserManager) {
|
if (this instanceof DatabaseUserManager) {
|
||||||
player.registerPropertyUpdateListener(() -> this.save(player));
|
player.registerPropertyUpdateListener(() ->
|
||||||
|
this.save(player).exceptionally(PlayerUtils.saveExceptionHandler(this.logger, player.username, uuid)));
|
||||||
}
|
}
|
||||||
return player;
|
return player;
|
||||||
}, this.executor);
|
}, this.executor);
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
|||||||
final boolean muted,
|
final boolean muted,
|
||||||
final boolean deafened,
|
final boolean deafened,
|
||||||
final @Nullable Key selectedChannel,
|
final @Nullable Key selectedChannel,
|
||||||
final String username,
|
final @Nullable String username, // will be resolved when requested
|
||||||
final UUID uuid,
|
final UUID uuid,
|
||||||
final @Nullable Component displayName,
|
final @Nullable Component displayName,
|
||||||
final @Nullable UUID lastWhisperTarget,
|
final @Nullable UUID lastWhisperTarget,
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ package net.draycia.carbon.common.users.db;
|
|||||||
|
|
||||||
import com.google.inject.MembersInjector;
|
import com.google.inject.MembersInjector;
|
||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||||
@@ -34,7 +36,6 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
|||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
import org.jdbi.v3.core.Jdbi;
|
import org.jdbi.v3.core.Jdbi;
|
||||||
import org.jdbi.v3.core.statement.PreparedBatch;
|
import org.jdbi.v3.core.statement.PreparedBatch;
|
||||||
import org.jdbi.v3.core.statement.Update;
|
|
||||||
|
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public abstract class DatabaseUserManager extends CachingUserManager {
|
public abstract class DatabaseUserManager extends CachingUserManager {
|
||||||
@@ -67,33 +68,46 @@ public abstract class DatabaseUserManager extends CachingUserManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void saveSync(final CarbonPlayerCommon player) {
|
public final void saveSync(final CarbonPlayerCommon player) {
|
||||||
this.jdbi.withHandle(handle -> {
|
this.jdbi.useTransaction(handle -> {
|
||||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player)
|
handle.createUpdate(this.locator.query("insert-player"))
|
||||||
|
.bind("id", player.uuid())
|
||||||
|
.bind("muted", player.muted())
|
||||||
|
.bind("deafened", player.deafened())
|
||||||
|
.bind("selectedchannel", player.selectedChannelKey())
|
||||||
|
.bind("username", player.username()) // todo remove from db?
|
||||||
|
.bind("displayname", player.displayNameRaw())
|
||||||
|
.bind("lastwhispertarget", player.lastWhisperTarget())
|
||||||
|
.bind("whisperreplytarget", player.whisperReplyTarget())
|
||||||
|
.bind("spying", player.spying())
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
if (!player.ignoredPlayers().isEmpty()) {
|
handle.createUpdate(this.locator.query("clear-ignores"))
|
||||||
final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-ignores"));
|
.bind("id", player.uuid())
|
||||||
|
.execute();
|
||||||
|
handle.createUpdate(this.locator.query("clear-leftchannels"))
|
||||||
|
.bind("id", player.uuid())
|
||||||
|
.execute();
|
||||||
|
|
||||||
for (final UUID ignoredPlayer : player.ignoredPlayers()) {
|
final Set<UUID> ignored = player.ignoredPlayers();
|
||||||
|
if (!ignored.isEmpty()) {
|
||||||
|
final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-ignores"));
|
||||||
|
for (final UUID ignoredPlayer : ignored) {
|
||||||
batch.bind("id", player.uuid()).bind("ignoredplayer", ignoredPlayer).add();
|
batch.bind("id", player.uuid()).bind("ignoredplayer", ignoredPlayer).add();
|
||||||
}
|
}
|
||||||
|
|
||||||
batch.execute();
|
batch.execute();
|
||||||
}
|
}
|
||||||
if (!player.leftChannels().isEmpty()) {
|
|
||||||
final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-leftchannels"));
|
|
||||||
|
|
||||||
for (final Key leftChannel : player.leftChannels()) {
|
final List<Key> left = player.leftChannels();
|
||||||
|
if (!left.isEmpty()) {
|
||||||
|
final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-leftchannels"));
|
||||||
|
for (final Key leftChannel : left) {
|
||||||
batch.bind("id", player.uuid()).bind("channel", leftChannel).add();
|
batch.bind("id", player.uuid()).bind("channel", leftChannel).add();
|
||||||
}
|
}
|
||||||
|
|
||||||
batch.execute();
|
batch.execute();
|
||||||
}
|
}
|
||||||
// TODO: save ignoredplayers
|
|
||||||
return null;
|
handle.commit();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -45,7 +45,7 @@ public class MySQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon> {
|
|||||||
rs.getBoolean("muted"),
|
rs.getBoolean("muted"),
|
||||||
rs.getBoolean("deafened"),
|
rs.getBoolean("deafened"),
|
||||||
selectedChannel == null ? null : Key.key(selectedChannel),
|
selectedChannel == null ? null : Key.key(selectedChannel),
|
||||||
Strings.trim(rs.getString("username")),
|
null,
|
||||||
UUID.fromString(id),
|
UUID.fromString(id),
|
||||||
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
|
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
|
||||||
lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget),
|
lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget),
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
import org.flywaydb.core.Flyway;
|
import org.flywaydb.core.Flyway;
|
||||||
import org.jdbi.v3.core.Jdbi;
|
import org.jdbi.v3.core.Jdbi;
|
||||||
import org.jdbi.v3.core.statement.Update;
|
|
||||||
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
||||||
|
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
@@ -110,20 +109,6 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player) {
|
|
||||||
return update
|
|
||||||
.bind("id", player.uuid())
|
|
||||||
.bind("muted", player.muted())
|
|
||||||
.bind("deafened", player.deafened())
|
|
||||||
.bind("selectedchannel", player.selectedChannelKey())
|
|
||||||
.bind("username", player.username())
|
|
||||||
.bind("displayname", player.displayNameRaw())
|
|
||||||
.bind("lastwhispertarget", player.lastWhisperTarget())
|
|
||||||
.bind("whisperreplytarget", player.whisperReplyTarget())
|
|
||||||
.bind("spying", player.spying());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class Factory {
|
public static final class Factory {
|
||||||
|
|
||||||
private final DatabaseSettings databaseSettings;
|
private final DatabaseSettings databaseSettings;
|
||||||
|
|||||||
+1
-1
@@ -42,7 +42,7 @@ public class PostgreSQLPlayerRowMapper implements RowMapper<CarbonPlayerCommon>
|
|||||||
rs.getBoolean("muted"),
|
rs.getBoolean("muted"),
|
||||||
rs.getBoolean("deafened"),
|
rs.getBoolean("deafened"),
|
||||||
selectedChannel == null ? null : Key.key(selectedChannel),
|
selectedChannel == null ? null : Key.key(selectedChannel),
|
||||||
Strings.trim(rs.getString("username")),
|
null,
|
||||||
rs.getObject("id", UUID.class),
|
rs.getObject("id", UUID.class),
|
||||||
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
|
displayName == null ? null : GsonComponentSerializer.gson().deserialize(displayName),
|
||||||
rs.getObject("lastwhispertarget", UUID.class),
|
rs.getObject("lastwhispertarget", UUID.class),
|
||||||
|
|||||||
-15
@@ -52,7 +52,6 @@ import org.flywaydb.core.Flyway;
|
|||||||
import org.flywaydb.core.internal.database.postgresql.PostgreSQLDatabaseType;
|
import org.flywaydb.core.internal.database.postgresql.PostgreSQLDatabaseType;
|
||||||
import org.flywaydb.core.internal.plugin.PluginRegister;
|
import org.flywaydb.core.internal.plugin.PluginRegister;
|
||||||
import org.jdbi.v3.core.Jdbi;
|
import org.jdbi.v3.core.Jdbi;
|
||||||
import org.jdbi.v3.core.statement.Update;
|
|
||||||
import org.jdbi.v3.postgres.PostgresPlugin;
|
import org.jdbi.v3.postgres.PostgresPlugin;
|
||||||
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
||||||
|
|
||||||
@@ -113,20 +112,6 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player) {
|
|
||||||
return update
|
|
||||||
.bind("id", player.uuid())
|
|
||||||
.bind("muted", player.muted())
|
|
||||||
.bind("deafened", player.deafened())
|
|
||||||
.bind("selectedchannel", player.selectedChannelKey())
|
|
||||||
.bind("username", player.username())
|
|
||||||
.bind("displayname", player.displayNameRaw())
|
|
||||||
.bind("lastwhispertarget", player.lastWhisperTarget())
|
|
||||||
.bind("whisperreplytarget", player.whisperReplyTarget())
|
|
||||||
.bind("spying", player.spying());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class Factory {
|
public static final class Factory {
|
||||||
|
|
||||||
private final DatabaseSettings databaseSettings;
|
private final DatabaseSettings databaseSettings;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public final class PlayerUtils {
|
|||||||
|
|
||||||
public static Function<Throwable, @Nullable Void> saveExceptionHandler(final Logger logger, final String username, final UUID uuid) {
|
public static Function<Throwable, @Nullable Void> saveExceptionHandler(final Logger logger, final String username, final UUID uuid) {
|
||||||
return thr -> {
|
return thr -> {
|
||||||
logger.warn("Exception saving data for player {} with uuid {}", username, uuid);
|
logger.warn("Exception saving data for player {} with uuid {}", username, uuid, thr);
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ CREATE TABLE carbon_users (
|
|||||||
`id` BINARY(16) NOT NULL PRIMARY KEY,
|
`id` BINARY(16) NOT NULL PRIMARY KEY,
|
||||||
`muted` BOOLEAN,
|
`muted` BOOLEAN,
|
||||||
`deafened` BOOLEAN,
|
`deafened` BOOLEAN,
|
||||||
`selectedchannel` varchar(256),
|
`selectedchannel` VARCHAR(256),
|
||||||
`username` varchar(20),
|
`username` VARCHAR(20),
|
||||||
`displayname` varchar(1024),
|
`displayname` VARCHAR(1024),
|
||||||
`lastwhispertarget` BINARY(16),
|
`lastwhispertarget` BINARY(16),
|
||||||
`whisperreplytarget` BINARY(16),
|
`whisperreplytarget` BINARY(16),
|
||||||
`spying` BOOLEAN
|
`spying` BOOLEAN
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
DROP TABLE carbon_leftchannels;
|
||||||
|
CREATE TABLE carbon_leftchannels (
|
||||||
|
`id` BINARY(16) NOT NULL,
|
||||||
|
`channel` VARCHAR(256) NOT NULL,
|
||||||
|
PRIMARY KEY (id, channel)
|
||||||
|
);
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
CREATE TABLE IF NOT EXISTS carbon_leftchannels (
|
CREATE TABLE IF NOT EXISTS carbon_leftchannels (
|
||||||
id uuid NOT NULL,
|
id uuid NOT NULL,
|
||||||
channel varchar(100) NOT NULL,
|
channel VARCHAR(100) NOT NULL,
|
||||||
PRIMARY KEY (id, channel)
|
PRIMARY KEY (id, channel)
|
||||||
);
|
);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE carbon_leftchannels ALTER COLUMN channel TYPE VARCHAR(256) NOT NULL;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DELETE FROM carbon_ignores WHERE (id = UNHEX(REPLACE(:id, '-', '')));
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DELETE FROM carbon_leftchannels WHERE (id = UNHEX(REPLACE(:id, '-', '')));
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DELETE FROM carbon_ignores WHERE (id = :id);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DELETE FROM carbon_leftchannels WHERE (id = :id);
|
||||||
Reference in new issue
Block a user