Browse Source

Refactor to remove abstraction.

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

146
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());
setInt(2, toAdd.getItemBaseID());
setInt(3, toAdd.getChargesRemaining());
setInt(4, toAdd.getDurabilityCurrent());
setInt(5, toAdd.getDurabilityMax());
if (toAdd.getNumOfItems() < 1)
setInt(6, 1);
else
setInt(6, toAdd.getNumOfItems());
switch (toAdd.containerType) { try (Connection connection = DbManager.getConnection();
case INVENTORY: PreparedStatement preparedStatement = connection.prepareStatement("CALL `item_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?,?);")) {
setString(7, "inventory");
break; preparedStatement.setInt(1, toAdd.getOwnerID());
case EQUIPPED: preparedStatement.setInt(2, toAdd.getItemBaseID());
setString(7, "equip"); preparedStatement.setInt(3, toAdd.getChargesRemaining());
break; preparedStatement.setInt(4, toAdd.getDurabilityCurrent());
case BANK: preparedStatement.setInt(5, toAdd.getDurabilityMax());
setString(7, "bank");
break; if (toAdd.getNumOfItems() < 1)
case VAULT: preparedStatement.setInt(6, 1);
setString(7, "vault"); else
break; preparedStatement.setInt(6, toAdd.getNumOfItems());
case FORGE:
setString(7, "forge"); switch (toAdd.containerType) {
break; case INVENTORY:
preparedStatement.setString(7, "inventory");
break;
case EQUIPPED:
preparedStatement.setString(7, "equip");
break;
case BANK:
preparedStatement.setString(7, "bank");
break;
case VAULT:
preparedStatement.setString(7, "vault");
break;
case FORGE:
preparedStatement.setString(7, "forge");
break;
default: default:
setString(7, "none"); //Shouldn't be here preparedStatement.setString(7, "none"); //Shouldn't be here
break; break;
} }
preparedStatement.setByte(8, toAdd.getEquipSlot());
preparedStatement.setInt(9, toAdd.getFlags());
preparedStatement.setString(10, toAdd.getCustomName());
setByte(8, toAdd.getEquipSlot()); ResultSet rs = preparedStatement.executeQuery();
setInt(9, toAdd.getFlags());
setString(10, toAdd.getCustomName()); int objectUUID = (int) rs.getLong("UID");
int objectUUID = (int) getUUID();
if (objectUUID > 0)
return GET_ITEM(objectUUID);
} catch (SQLException e) {
Logger.error(e);
}
if (objectUUID > 0)
return GET_ITEM(objectUUID);
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,35 +112,48 @@ 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