mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-21 22:41:06 +01:00
fix: remove record if balance is < 0
before, there was easy duplication bug
This commit is contained in:
parent
bc6fbb6aaf
commit
e38c5977ec
4 changed files with 101 additions and 41 deletions
|
@ -25,6 +25,8 @@ public class SqlStorageProvider implements StorageProvider {
|
||||||
private static String LOAD_WHOLE_USER;
|
private static String LOAD_WHOLE_USER;
|
||||||
private static final String GET_TOP_X_USERS_LOCAL = "SELECT uuid, balance FROM {prefix}_{context}_users WHERE currency_identifier = ? ORDER BY balance DESC LIMIT ?;";
|
private static final String GET_TOP_X_USERS_LOCAL = "SELECT uuid, balance FROM {prefix}_{context}_users WHERE currency_identifier = ? ORDER BY balance DESC LIMIT ?;";
|
||||||
private static final String GET_TOP_X_USERS_GLOBAL = "SELECT uuid, balance FROM {prefix}_users WHERE currency_identifier = ? ORDER BY balance DESC LIMIT ?;";
|
private static final String GET_TOP_X_USERS_GLOBAL = "SELECT uuid, balance FROM {prefix}_users WHERE currency_identifier = ? ORDER BY balance DESC LIMIT ?;";
|
||||||
|
private static final String DELETE_GLOBAL_USER_IF_BALANCE = "DELETE FROM {prefix}_{context}_users WHERE uuid = ? AND currency_identifier = ? AND balance = ?;";
|
||||||
|
private static final String DELETE_LOCAL_USER_IF_BALANCE = "DELETE FROM {prefix}_{context}_users WHERE uuid = ? AND currency_identifier = ? AND balance = ?;";
|
||||||
|
|
||||||
private final LightEcoPlugin plugin;
|
private final LightEcoPlugin plugin;
|
||||||
private final ConnectionFactory connectionFactory;
|
private final ConnectionFactory connectionFactory;
|
||||||
|
@ -110,13 +112,8 @@ public class SqlStorageProvider implements StorageProvider {
|
||||||
String uniqueIdString = user.getUniqueId().toString();
|
String uniqueIdString = user.getUniqueId().toString();
|
||||||
|
|
||||||
try (Connection c = this.connectionFactory.getConnection()) {
|
try (Connection c = this.connectionFactory.getConnection()) {
|
||||||
try (PreparedStatement psGlobal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_GLOBAL_CURRENCY));
|
try {
|
||||||
PreparedStatement psLocal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_LOCAL_CURRENCY))) {
|
saveBalances(c, user, uniqueIdString);
|
||||||
|
|
||||||
saveBalances(psGlobal, psLocal, user, uniqueIdString);
|
|
||||||
|
|
||||||
psGlobal.executeBatch();
|
|
||||||
psLocal.executeBatch();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new SQLException("Failed to save user " + user.getUniqueId(), e);
|
throw new SQLException("Failed to save user " + user.getUniqueId(), e);
|
||||||
}
|
}
|
||||||
|
@ -130,19 +127,10 @@ public class SqlStorageProvider implements StorageProvider {
|
||||||
try {
|
try {
|
||||||
c.setAutoCommit(false);
|
c.setAutoCommit(false);
|
||||||
|
|
||||||
try (PreparedStatement psGlobal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_GLOBAL_CURRENCY));
|
|
||||||
PreparedStatement psLocal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_LOCAL_CURRENCY))) {
|
|
||||||
|
|
||||||
for (User user : users) {
|
for (User user : users) {
|
||||||
String uniqueIdString = user.getUniqueId().toString();
|
String uniqueIdString = user.getUniqueId().toString();
|
||||||
|
|
||||||
saveBalances(psGlobal, psLocal, user, uniqueIdString);
|
saveBalances(c, user, uniqueIdString);
|
||||||
}
|
|
||||||
|
|
||||||
psGlobal.executeBatch();
|
|
||||||
psLocal.executeBatch();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new SQLException("Failed to save users", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.commit();
|
c.commit();
|
||||||
|
@ -185,11 +173,35 @@ public class SqlStorageProvider implements StorageProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveBalances(PreparedStatement psGlobal, PreparedStatement psLocal, User user, String uniqueIdString) throws SQLException {
|
private void saveBalances(Connection c, User user, String uniqueIdString) throws SQLException {
|
||||||
|
try (PreparedStatement psGlobal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_GLOBAL_CURRENCY));
|
||||||
|
PreparedStatement psLocal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_LOCAL_CURRENCY));
|
||||||
|
PreparedStatement psDeleteGlobal = c.prepareStatement(this.statementProcessor.apply(DELETE_GLOBAL_USER_IF_BALANCE));
|
||||||
|
PreparedStatement psDeleteLocal = c.prepareStatement(this.statementProcessor.apply(DELETE_LOCAL_USER_IF_BALANCE))) {
|
||||||
|
|
||||||
for (Currency currency : this.plugin.getCurrencyManager().getRegisteredCurrencies()) {
|
for (Currency currency : this.plugin.getCurrencyManager().getRegisteredCurrencies()) {
|
||||||
BigDecimal balance = user.getBalance(currency.getProxy());
|
BigDecimal balance = user.getBalance(currency.getProxy());
|
||||||
|
|
||||||
if (balance.compareTo(BigDecimal.ZERO) == 0) continue;
|
if (balance.compareTo(BigDecimal.ZERO) == 0) {
|
||||||
|
switch (currency.getType()) {
|
||||||
|
case GLOBAL -> {
|
||||||
|
psDeleteGlobal.setString(1, uniqueIdString);
|
||||||
|
psDeleteGlobal.setString(2, currency.getIdentifier());
|
||||||
|
psDeleteGlobal.setBigDecimal(3, balance);
|
||||||
|
|
||||||
|
psDeleteGlobal.addBatch();
|
||||||
|
}
|
||||||
|
case LOCAL -> {
|
||||||
|
psDeleteLocal.setString(1, uniqueIdString);
|
||||||
|
psDeleteLocal.setString(2, currency.getIdentifier());
|
||||||
|
psDeleteLocal.setBigDecimal(3, balance);
|
||||||
|
|
||||||
|
psDeleteLocal.addBatch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
switch (currency.getType()) {
|
switch (currency.getType()) {
|
||||||
case GLOBAL -> {
|
case GLOBAL -> {
|
||||||
|
@ -212,5 +224,13 @@ public class SqlStorageProvider implements StorageProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
psGlobal.executeBatch();
|
||||||
|
psLocal.executeBatch();
|
||||||
|
psDeleteGlobal.executeBatch();
|
||||||
|
psDeleteLocal.executeBatch();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new SQLException("Failed to save user " + user.getUniqueId(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ public class UserSaveTask implements Runnable {
|
||||||
.filter(User::isDirty)
|
.filter(User::isDirty)
|
||||||
.toArray(User[]::new);
|
.toArray(User[]::new);
|
||||||
|
|
||||||
|
System.out.println("Saving " + users.length + " users");
|
||||||
|
|
||||||
if (users.length == 0) {
|
if (users.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
package dev.xhyrom.lighteco.sponge;
|
package dev.xhyrom.lighteco.sponge;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap;
|
||||||
import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap;
|
import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
|
||||||
|
import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.spongepowered.plugin.builtin.jvm.Plugin;
|
import org.spongepowered.plugin.builtin.jvm.Plugin;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Plugin("lighteco-sponge")
|
@Plugin("lighteco-sponge")
|
||||||
public class SpongeLightEcoBootstrap implements LoaderBootstrap {
|
public class SpongeLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap {
|
||||||
private final SpongeLightEcoPlugin plugin;
|
private final SpongeLightEcoPlugin plugin;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
|
@ -30,4 +38,34 @@ public class SpongeLightEcoBootstrap implements LoaderBootstrap {
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
this.plugin.disable();
|
this.plugin.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getLoader() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PluginLogger getLogger() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SchedulerAdapter getScheduler() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Path getDataDirectory() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<UUID> getOnlinePlayers() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getResourceStream(String filename) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package dev.xhyrom.lighteco.sponge;
|
package dev.xhyrom.lighteco.sponge;
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.api.LightEco;
|
import dev.xhyrom.lighteco.api.LightEco;
|
||||||
import dev.xhyrom.lighteco.api.manager.CommandManager;
|
|
||||||
import dev.xhyrom.lighteco.api.manager.ContextManager;
|
import dev.xhyrom.lighteco.api.manager.ContextManager;
|
||||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||||
|
import dev.xhyrom.lighteco.common.manager.command.CommandManager;
|
||||||
import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager;
|
import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager;
|
||||||
import dev.xhyrom.lighteco.common.manager.user.StandardUserManager;
|
import dev.xhyrom.lighteco.common.manager.user.StandardUserManager;
|
||||||
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
|
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
|
||||||
|
@ -11,8 +11,8 @@ import lombok.Getter;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.spongepowered.api.entity.living.player.Player;
|
import org.spongepowered.api.entity.living.player.Player;
|
||||||
|
|
||||||
public class SpongeLightEcoPlugin extends AbstractLightEcoPlugin {
|
|
||||||
@Getter
|
@Getter
|
||||||
|
public class SpongeLightEcoPlugin extends AbstractLightEcoPlugin {
|
||||||
private final SpongeLightEcoBootstrap bootstrap;
|
private final SpongeLightEcoBootstrap bootstrap;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
|
Loading…
Reference in a new issue