Refactored UsersTable#kicked to a transaction

This commit is contained in:
Rsl1122 2019-02-16 12:02:51 +02:00
parent 4c235b95e2
commit 70e83a12b4
9 changed files with 61 additions and 88 deletions

View File

@ -19,10 +19,7 @@ package com.djrapitops.plan.system.listeners.bukkit;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.events.GeoInfoStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.NicknameStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerServerRegisterTransaction;
import com.djrapitops.plan.db.access.transactions.events.WorldNameStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.*;
import com.djrapitops.plan.system.cache.GeolocationCache;
import com.djrapitops.plan.system.cache.NicknameCache;
import com.djrapitops.plan.system.cache.SessionCache;
@ -123,7 +120,7 @@ public class PlayerOnlineListener implements Listener {
return;
}
processing.submit(processors.player().kickProcessor(uuid));
dbSystem.getDatabase().executeTransaction(new KickStoreTransaction(uuid));
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);
}

View File

@ -0,0 +1,55 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.transactions.events;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.parsing.Sql.WHERE;
/**
* Transaction to store information in the database when a player is kicked from the server.
*
* @author Rsl1122
*/
public class KickStoreTransaction extends Transaction {
private final UUID playerUUID;
public KickStoreTransaction(UUID playerUUID) {
this.playerUUID = playerUUID;
}
@Override
protected void performOperations() {
String sql = "UPDATE " + UsersTable.TABLE_NAME + " SET "
+ UsersTable.TIMES_KICKED + "=" + UsersTable.TIMES_KICKED + "+ 1" +
WHERE + UsersTable.USER_UUID + "=?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
});
}
}

View File

@ -18,15 +18,10 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Insert;
import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
/**
* Table that is in charge of storing common player data for all servers.
* <p>
@ -59,17 +54,4 @@ public class UsersTable extends Table {
.column(TIMES_KICKED, Sql.INT).notNull().defaultValue("0")
.toString();
}
public void kicked(UUID uuid) {
String sql = "UPDATE " + tableName + " SET "
+ TIMES_KICKED + "=" + TIMES_KICKED + "+ 1" +
" WHERE " + USER_UUID + "=?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
});
}
}

View File

@ -70,9 +70,6 @@ public interface SaveOperations {
@Deprecated
void opStatus(UUID uuid, boolean op);
@Deprecated
void playerWasKicked(UUID uuid);
@Deprecated
void session(UUID uuid, Session session);

View File

@ -135,11 +135,6 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
userInfoTable.updateOpStatus(uuid, op);
}
@Override
public void playerWasKicked(UUID uuid) {
usersTable.kicked(uuid);
}
@Override
public void session(UUID uuid, Session session) {
db.executeTransaction(new Transaction() {

View File

@ -1,44 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.processing.processors.player;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.system.processing.CriticalRunnable;
import java.util.UUID;
/**
* Updates the Kick count of a user.
*
* @author Rsl1122
*/
public class KickProcessor implements CriticalRunnable {
private final UUID uuid;
private final Database database;
KickProcessor(UUID uuid, Database database) {
this.uuid = uuid;
this.database = database;
}
@Override
public void run() {
database.save().playerWasKicked(uuid);
}
}

View File

@ -52,8 +52,4 @@ public class PlayerProcessors {
public EndSessionProcessor endSessionProcessor(UUID uuid, long time) {
return new EndSessionProcessor(uuid, time, sessionCache.get());
}
public KickProcessor kickProcessor(UUID uuid) {
return new KickProcessor(uuid, dbSystem.get().getDatabase());
}
}

View File

@ -46,7 +46,6 @@ import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateTablesTransaction;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.tables.TPSTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.Server;
@ -238,7 +237,7 @@ public abstract class CommonDBTest {
private void saveUserOne() {
playerIsRegisteredToBothTables();
db.getUsersTable().kicked(playerUUID);
db.executeTransaction(new KickStoreTransaction(playerUUID));
}
private void saveUserTwo() {
@ -456,13 +455,12 @@ public abstract class CommonDBTest {
@Test
public void testUsersTableKickSaving() throws DBInitException {
saveUserOne();
UsersTable usersTable = db.getUsersTable();
OptionalAssert.equals(1, db.query(BaseUserQueries.fetchBaseUserOfPlayer(playerUUID)).map(BaseUser::getTimesKicked));
int random = new Random().nextInt(20);
for (int i = 0; i < random + 1; i++) {
usersTable.kicked(playerUUID);
db.executeTransaction(new KickStoreTransaction(playerUUID));
}
commitTest();
OptionalAssert.equals(random + 2, db.query(BaseUserQueries.fetchBaseUserOfPlayer(playerUUID)).map(BaseUser::getTimesKicked));

View File

@ -19,10 +19,7 @@ package com.djrapitops.plan.system.listeners.sponge;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.events.GeoInfoStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.NicknameStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerServerRegisterTransaction;
import com.djrapitops.plan.db.access.transactions.events.WorldNameStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.*;
import com.djrapitops.plan.system.cache.GeolocationCache;
import com.djrapitops.plan.system.cache.NicknameCache;
import com.djrapitops.plan.system.cache.SessionCache;
@ -118,7 +115,7 @@ public class SpongePlayerListener {
if (!status.areKicksCounted() || SpongeAFKListener.AFK_TRACKER.isAfk(uuid)) {
return;
}
processing.submit(processors.player().kickProcessor(uuid));
dbSystem.getDatabase().executeTransaction(new KickStoreTransaction(uuid));
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);
}