Browse Source

Begin refactor to remove abstraction.

master
MagicBot 2 years ago
parent
commit
c86ad3f2f6
  1. 244
      src/engine/db/handlers/dbAccountHandler.java
  2. 19
      src/engine/db/handlers/dbHandlerBase.java

244
src/engine/db/handlers/dbAccountHandler.java

@ -17,6 +17,8 @@ import engine.objects.Account;
import engine.objects.PlayerCharacter; import engine.objects.PlayerCharacter;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
@ -28,44 +30,65 @@ public class dbAccountHandler extends dbHandlerBase {
this.localObjectType = Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); this.localObjectType = Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
} }
public Account GET_ACCOUNT(int id) { public Account GET_ACCOUNT(int accountID) {
if (id == 0)
Account account;
if (accountID == 0)
return null; return null;
Account account = (Account) DbManager.getFromCache(GameObjectType.Account, id);
account = (Account) DbManager.getFromCache(GameObjectType.Account, accountID);
if (account != null) if (account != null)
return account; return account;
prepareCallable("SELECT * FROM `obj_account` WHERE `UID`=?"); try (Connection connection = DbManager.getConnection();
setLong(1, (long) id); PreparedStatement accountQuery = connection.prepareStatement("SELECT * FROM `obj_account` WHERE `UID`=?")) {
Account ac = null; accountQuery.setLong(1, accountID);
ac = (Account) getObjectSingle(id);
if (ac != null) ResultSet rs = accountQuery.executeQuery();
ac.runAfterLoad(); account = (Account) getObjectFromRs(rs);
return ac; } catch (SQLException e) {
Logger.error(e);
}
if (account != null)
account.runAfterLoad();
return account;
} }
public void WRITE_ADMIN_LOG(String adminName, String logEntry) { public void WRITE_ADMIN_LOG(String adminName, String logEntry) {
prepareCallable("INSERT INTO dyn_admin_log(`dateTime`, `charName`, `eventString`)" try (Connection connection = DbManager.getConnection();
+ " VALUES (?, ?, ?)"); PreparedStatement accountQuery = connection.prepareStatement("INSERT INTO dyn_admin_log(`dateTime`, `charName`, `eventString`)"
setTimeStamp(1, System.currentTimeMillis()); + " VALUES (?, ?, ?)")) {
setString(2, adminName);
setString(3, logEntry); accountQuery.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis()));
executeUpdate(); accountQuery.setString(2, adminName);
accountQuery.setString(3, logEntry);
accountQuery.execute();
} catch (SQLException e) {
Logger.error(e);
}
} }
public void SET_TRASH(String machineID) { public void SET_TRASH(String machineID) {
prepareCallable("INSERT INTO dyn_trash(`machineID`, `count`)" try (Connection connection = DbManager.getConnection();
+ " VALUES (?, 1) ON DUPLICATE KEY UPDATE `count` = `count` + 1;"); PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO dyn_trash(`machineID`, `count`)"
+ " VALUES (?, 1) ON DUPLICATE KEY UPDATE `count` = `count` + 1;")) {
preparedStatement.setString(1, machineID);
preparedStatement.execute();
setTimeStamp(4, System.currentTimeMillis()); } catch (SQLException e) {
setString(1, machineID); Logger.error(e);
executeUpdate(); }
} }
@ -73,39 +96,47 @@ public class dbAccountHandler extends dbHandlerBase {
ArrayList<String> machineList = new ArrayList<>(); ArrayList<String> machineList = new ArrayList<>();
prepareCallable("select `machineID` from `dyn_trash`"); try (Connection connection = DbManager.getConnection();
PreparedStatement accountQuery = connection.prepareStatement("select `machineID` from `dyn_trash`")) {
try { ResultSet rs = accountQuery.executeQuery();
ResultSet rs = executeQuery();
while (rs.next()) { while (rs.next())
machineList.add(rs.getString(1)); machineList.add(rs.getString(1));
}
} catch (SQLException e) { } catch (SQLException e) {
Logger.error(e); Logger.error(e);
} finally {
closeCallable();
} }
return machineList; return machineList;
} }
public boolean DELETE_VAULT_FOR_ACCOUNT(final int accountID) { public void DELETE_VAULT_FOR_ACCOUNT(final int accountID) {
prepareCallable("DELETE FROM `object` WHERE `parent`=? && `type`='item'");
setLong(1, (long) accountID); try (Connection connection = DbManager.getConnection();
return (executeUpdate() > 0); PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `object` WHERE `parent`=? && `type`='item'")) {
preparedStatement.setLong(1, accountID);
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
}
} }
public ArrayList<PlayerCharacter> GET_ALL_CHARS_FOR_MACHINE(String machineID) { public ArrayList<PlayerCharacter> GET_ALL_CHARS_FOR_MACHINE(String machineID) {
ArrayList<PlayerCharacter> trashList = new ArrayList<>(); ArrayList<PlayerCharacter> trashList = new ArrayList<>();
prepareCallable("select DISTINCT UID from object \n" + try (Connection connection = DbManager.getConnection();
PreparedStatement accountQuery = connection.prepareStatement("select DISTINCT UID from object \n" +
"where parent IN (select AccountID from dyn_login_history " + "where parent IN (select AccountID from dyn_login_history " +
" WHERE`machineID`=?)"); " WHERE`machineID`=?)")) {
setString(1, machineID);
accountQuery.setString(1, machineID);
ResultSet rs = accountQuery.executeQuery();
try {
ResultSet rs = executeQuery();
while (rs.next()) { while (rs.next()) {
PlayerCharacter trashPlayer; PlayerCharacter trashPlayer;
@ -115,52 +146,75 @@ public class dbAccountHandler extends dbHandlerBase {
trashPlayer = PlayerCharacter.getPlayerCharacter(playerID); trashPlayer = PlayerCharacter.getPlayerCharacter(playerID);
if (trashPlayer == null) if (trashPlayer == null)
continue;; continue;
if (trashPlayer.isDeleted() == false) if (trashPlayer.isDeleted() == false)
trashList.add(trashPlayer); trashList.add(trashPlayer);
} }
} catch (SQLException e) { } catch (SQLException e) {
Logger.error(e); Logger.error(e);
} finally {
closeCallable();
} }
return trashList; return trashList;
} }
public void CLEAR_TRASH_TABLE() { public void CLEAR_TRASH_TABLE() {
prepareCallable("DELETE FROM dyn_trash");
executeUpdate(); try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM dyn_trash")) {
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
}
} }
public void CREATE_SINGLE(String accountName, String password) { public void CREATE_SINGLE(String accountName, String password) {
prepareCallable("CALL singleAccountCreate(?,?)"); try (Connection connection = DbManager.getConnection();
setString(1, accountName); PreparedStatement preparedStatement = connection.prepareStatement("CALL singleAccountCreate(?,?)")) {
setString(2, password);
executeUpdate(); preparedStatement.setString(1, accountName);
preparedStatement.setString(2, password);
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
}
} }
public Account GET_ACCOUNT(String uname) { public Account GET_ACCOUNT(String uname) {
Account account = null;
if (Account.AccountsMap.get(uname) != null) if (Account.AccountsMap.get(uname) != null)
return this.GET_ACCOUNT(Account.AccountsMap.get(uname)); return this.GET_ACCOUNT(Account.AccountsMap.get(uname));
prepareCallable("SELECT * FROM `obj_account` WHERE `acct_uname`=?"); try (Connection connection = DbManager.getConnection();
setString(1, uname); PreparedStatement accountQuery = connection.prepareStatement("SELECT * FROM `obj_account` WHERE `acct_uname`=?")) {
ArrayList<Account> temp = getObjectList();
if (temp.isEmpty()) accountQuery.setString(1, uname);
return null;
if (temp.get(0) != null){ ResultSet rs = accountQuery.executeQuery();
temp.get(0).runAfterLoad(); account = (Account) getObjectFromRs(rs);
} catch (SQLException e) {
Logger.error(e);
}
if (account != null) {
account.runAfterLoad();
if (ConfigManager.serverType.equals(Enum.ServerType.LOGINSERVER)) if (ConfigManager.serverType.equals(Enum.ServerType.LOGINSERVER))
Account.AccountsMap.put(uname, temp.get(0).getObjectUUID()); Account.AccountsMap.put(uname, account.getObjectUUID());
} }
return temp.get(0); return account;
} }
public void SET_ACCOUNT_LOGIN(final Account acc, String playerName, final String ip, final String machineID) { public void SET_ACCOUNT_LOGIN(final Account acc, String playerName, final String ip, final String machineID) {
@ -168,55 +222,61 @@ public class dbAccountHandler extends dbHandlerBase {
if (acc.getObjectUUID() == 0 || ip == null || ip.length() == 0) if (acc.getObjectUUID() == 0 || ip == null || ip.length() == 0)
return; return;
prepareCallable("INSERT INTO dyn_login_history(`AccountID`, `accountName`, `characterName`, `ip`, `machineID`, `timeStamp`)" try (Connection connection = DbManager.getConnection();
+ " VALUES (?, ?, ?, ?, ?, ?)"); PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO dyn_login_history(`AccountID`, `accountName`, `characterName`, `ip`, `machineID`, `timeStamp`)"
+ " VALUES (?, ?, ?, ?, ?, ?)")) {
setInt(1, acc.getObjectUUID()); preparedStatement.setInt(1, acc.getObjectUUID());
setString(2, acc.getUname()); preparedStatement.setString(2, acc.getUname());
setString(3, playerName); preparedStatement.setString(3, playerName);
setString(4, ip); preparedStatement.setString(4, ip);
setString(5, machineID); preparedStatement.setString(5, machineID);
setTimeStamp(6, System.currentTimeMillis()); preparedStatement.setTimestamp(6, new java.sql.Timestamp(System.currentTimeMillis()));
executeUpdate();
}
public String SET_PROPERTY(final Account a, String name, Object new_value) { preparedStatement.execute();
prepareCallable("CALL account_SETPROP(?,?,?)");
setLong(1, (long) a.getObjectUUID());
setString(2, name);
setString(3, String.valueOf(new_value));
return getResult();
}
public String SET_PROPERTY(final Account a, String name, Object new_value, Object old_value) { } catch (SQLException e) {
prepareCallable("CALL account_GETSETPROP(?,?,?,?)"); Logger.error(e);
setLong(1, (long) a.getObjectUUID());
setString(2, name);
setString(3, String.valueOf(new_value));
setString(4, String.valueOf(old_value));
return getResult();
} }
}
public void updateDatabase(final Account acc) { public void updateDatabase(final Account acc) {
prepareCallable("UPDATE `obj_account` SET `acct_passwd`=?, "
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_account` SET `acct_passwd`=?, "
+ " `acct_lastCharUID`=?, `acct_salt`=?, `discordAccount`=?, " + + " `acct_lastCharUID`=?, `acct_salt`=?, `discordAccount`=?, " +
" status = ? WHERE `UID`=?"); " status = ? WHERE `UID`=?")) {
preparedStatement.setString(1, acc.getPasswd());
preparedStatement.setInt(2, acc.getLastCharIDUsed());
preparedStatement.setString(3, acc.getSalt());
preparedStatement.setString(4, acc.discordAccount);
preparedStatement.setString(5, acc.status.name());
preparedStatement.setInt(6, acc.getObjectUUID());
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
}
setString(1, acc.getPasswd());
setInt(2, acc.getLastCharIDUsed());
setString(3, acc.getSalt());
setString(4, acc.discordAccount);
setString(5, acc.status.name());
setInt(6, acc.getObjectUUID());
executeUpdate();
} }
public void INVALIDATE_LOGIN_CACHE(long accountUID, String objectType) { public void INVALIDATE_LOGIN_CACHE(long accountUID, String objectType) {
prepareCallable("INSERT IGNORE INTO login_cachelist (`UID`, `type`) VALUES(?,?);");
setLong(1, accountUID); try (Connection connection = DbManager.getConnection();
setString(2, objectType); PreparedStatement preparedStatement = connection.prepareStatement("INSERT IGNORE INTO login_cachelist (`UID`, `type`) VALUES(?,?);")) {
executeUpdate();
preparedStatement.setLong(1, accountUID);
preparedStatement.setString(2, objectType);
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
}
} }
} }

19
src/engine/db/handlers/dbHandlerBase.java

@ -286,7 +286,24 @@ public abstract class dbHandlerBase {
String errorMsg = rs.getString("errormsg"); String errorMsg = rs.getString("errormsg");
Logger.error("SQLError: errorNum: " + errorNum + ", errorMsg: " + errorMsg); Logger.error("SQLError: errorNum: " + errorNum + ", errorMsg: " + errorMsg);
logSQLCommand(); logSQLCommand();
} catch (SQLException e) {} } catch (SQLException e) {
}
}
protected <T extends AbstractGameObject> AbstractGameObject getObjectFromRs(ResultSet rs) {
AbstractGameObject abstractGameObject = null;
try {
if (rs.next()) {
abstractGameObject = localClass.getConstructor(ResultSet.class).newInstance(rs);
DbManager.addToCache(abstractGameObject);
}
} catch (Exception e) {
Logger.error(e);
}
return abstractGameObject;
} }
protected <T extends AbstractGameObject> AbstractGameObject getObjectSingle(int id) { protected <T extends AbstractGameObject> AbstractGameObject getObjectSingle(int id) {

Loading…
Cancel
Save