Browse Source

Refactor to remove abstraction.

master
MagicBot 2 years ago
parent
commit
a70c981b47
  1. 112
      src/engine/db/handlers/dbItemHandler.java

112
src/engine/db/handlers/dbItemHandler.java

@ -11,10 +11,16 @@ package engine.db.handlers;
import engine.Enum.ItemContainerType; import engine.Enum.ItemContainerType;
import engine.Enum.ItemType; import engine.Enum.ItemType;
import engine.gameManager.DbManager;
import engine.objects.AbstractCharacter; import engine.objects.AbstractCharacter;
import engine.objects.CharacterItemManager; import engine.objects.CharacterItemManager;
import engine.objects.Item; import engine.objects.Item;
import org.pmw.tinylog.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -27,52 +33,77 @@ public class dbItemHandler extends dbHandlerBase {
} }
public Item ADD_ITEM(Item toAdd) { public Item ADD_ITEM(Item toAdd) {
prepareCallable("CALL `item_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?,?);");
setInt(1, toAdd.getOwnerID()); try (Connection connection = DbManager.getConnection();
setInt(2, toAdd.getItemBaseID()); PreparedStatement preparedStatement = connection.prepareStatement("CALL `item_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?,?);")) {
setInt(3, toAdd.getChargesRemaining());
setInt(4, toAdd.getDurabilityCurrent()); preparedStatement.setInt(1, toAdd.getOwnerID());
setInt(5, toAdd.getDurabilityMax()); preparedStatement.setInt(2, toAdd.getItemBaseID());
preparedStatement.setInt(3, toAdd.getChargesRemaining());
preparedStatement.setInt(4, toAdd.getDurabilityCurrent());
preparedStatement.setInt(5, toAdd.getDurabilityMax());
if (toAdd.getNumOfItems() < 1) if (toAdd.getNumOfItems() < 1)
setInt(6, 1); preparedStatement.setInt(6, 1);
else else
setInt(6, toAdd.getNumOfItems()); preparedStatement.setInt(6, toAdd.getNumOfItems());
switch (toAdd.containerType) { switch (toAdd.containerType) {
case INVENTORY: case INVENTORY:
setString(7, "inventory"); preparedStatement.setString(7, "inventory");
break; break;
case EQUIPPED: case EQUIPPED:
setString(7, "equip"); preparedStatement.setString(7, "equip");
break; break;
case BANK: case BANK:
setString(7, "bank"); preparedStatement.setString(7, "bank");
break; break;
case VAULT: case VAULT:
setString(7, "vault"); preparedStatement.setString(7, "vault");
break; break;
case FORGE: case FORGE:
setString(7, "forge"); preparedStatement.setString(7, "forge");
break; break;
default: default:
setString(7, "none"); //Shouldn't be here preparedStatement.setString(7, "none"); //Shouldn't be here
break; break;
} }
setByte(8, toAdd.getEquipSlot()); preparedStatement.setByte(8, toAdd.getEquipSlot());
setInt(9, toAdd.getFlags()); preparedStatement.setInt(9, toAdd.getFlags());
setString(10, toAdd.getCustomName()); preparedStatement.setString(10, toAdd.getCustomName());
int objectUUID = (int) getUUID();
ResultSet rs = preparedStatement.executeQuery();
int objectUUID = (int) rs.getLong("UID");
if (objectUUID > 0) if (objectUUID > 0)
return GET_ITEM(objectUUID); return GET_ITEM(objectUUID);
} catch (SQLException e) {
Logger.error(e);
}
return null; return null;
} }
public String GET_OWNER(int ownerID) { public String GET_OWNER(int ownerID) {
prepareCallable("SELECT `type` FROM `object` WHERE `UID`=?");
setLong(1, (long) ownerID); String ownerType = "";
return getString("type");
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `type` FROM `object` WHERE `UID`=?")) {
preparedStatement.setInt(1, ownerID);
ResultSet rs = preparedStatement.executeQuery();
ownerType = rs.getString("type");
} catch (SQLException e) {
Logger.error(e);
}
return ownerType;
} }
public boolean DO_TRADE(HashSet<Integer> from1, HashSet<Integer> from2, public boolean DO_TRADE(HashSet<Integer> from1, HashSet<Integer> from2,
@ -81,34 +112,47 @@ public class dbItemHandler extends dbHandlerBase {
AbstractCharacter ac1 = man1.getOwner(); AbstractCharacter ac1 = man1.getOwner();
AbstractCharacter ac2 = man2.getOwner(); AbstractCharacter ac2 = man2.getOwner();
if (ac1 == null || ac2 == null || inventoryGold1 == null || inventoryGold2 == null) if (ac1 == null || ac2 == null || inventoryGold1 == null || inventoryGold2 == null)
return false; return false;
prepareCallable("CALL `item_TRADE`(?, ?, ?, ?, ?, ?, ?, ?)"); try (Connection connection = DbManager.getConnection();
setString(1, formatTradeString(from1)); PreparedStatement preparedStatement = connection.prepareStatement("CALL `item_TRADE`(?, ?, ?, ?, ?, ?, ?, ?)")) {
setLong(2, (long) ac1.getObjectUUID());
setString(3, formatTradeString(from2)); preparedStatement.setString(1, formatTradeString(from1));
setLong(4, (long) ac2.getObjectUUID()); preparedStatement.setLong(2, (long) ac1.getObjectUUID());
setInt(5, goldFrom1); preparedStatement.setString(3, formatTradeString(from2));
setLong(6, (long) inventoryGold1.getObjectUUID()); preparedStatement.setLong(4, (long) ac2.getObjectUUID());
setInt(7, goldFrom2); preparedStatement.setInt(5, goldFrom1);
setLong(8, (long) inventoryGold2.getObjectUUID()); preparedStatement.setLong(6, (long) inventoryGold1.getObjectUUID());
return worked(); preparedStatement.setInt(7, goldFrom2);
preparedStatement.setLong(8, (long) inventoryGold2.getObjectUUID());
ResultSet rs = preparedStatement.executeQuery();
return rs.getBoolean("result");
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
private static String formatTradeString(HashSet<Integer> list) { private static String formatTradeString(HashSet<Integer> list) {
int size = list.size(); int size = list.size();
String ret = ""; String ret = "";
if (size == 0) if (size == 0)
return ret; return ret;
boolean start = true; boolean start = true;
for (int i : list) { for (int i : list) {
if (start){ if (start) {
ret += i; ret += i;
start = false; start = false;
} } else
else
ret += "," + i; ret += "," + i;
} }
return ret; return ret;

Loading…
Cancel
Save