Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

643 lines
22 KiB

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.objects;
import engine.Enum;
10 months ago
import engine.gameManager.ChatManager;
import engine.gameManager.DbManager;
import engine.net.Dispatch;
import engine.net.DispatchMessage;
import engine.net.client.ClientConnection;
import engine.net.client.msg.*;
import engine.server.MBServerStatics;
import org.joda.time.DateTime;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.pmw.tinylog.Logger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class Warehouse {
public EnumSet<Enum.ResourceType> locked = EnumSet.noneOf(Enum.ResourceType.class);
public Building building;
public City city;
10 months ago
public ArrayList<Transaction> transactions = new ArrayList<>();
public ConcurrentHashMap<Enum.ResourceType, Integer> resources = new ConcurrentHashMap<>();
public Warehouse(Building building) {
this.building = building;
this.city = building.getCity();
// New empty warehouse
for (Enum.ResourceType resourceType : EnumSet.allOf(Enum.ResourceType.class))
this.resources.put(resourceType, 0);
// With no locks
this.locked = EnumSet.noneOf(Enum.ResourceType.class);
}
public Warehouse(JSONObject warehouse) throws SQLException {
JSONObject resources = (JSONObject) warehouse.get("resources");
for (Object key : resources.keySet()) {
Enum.ResourceType resourceType = Enum.ResourceType.valueOf((String) key);
int value = ((Long) resources.get(key)).intValue();
this.resources.put(resourceType, value);
}
8 months ago
JSONArray lockedResources = (JSONArray) warehouse.get("locked");
if (lockedResources.isEmpty() == false)
for (Object o : lockedResources)
this.locked.add(Enum.ResourceType.valueOf((String) o));
10 months ago
}
public static HashMap<Enum.ResourceType, Integer>
calculateWarehouseOverdraft(Warehouse warehouse, HashMap<Enum.ResourceType, Integer> costMap) {
HashMap<Enum.ResourceType, Integer> overdraft = new HashMap<>();
for (Enum.ResourceType resourceType : costMap.keySet()) {
int cost = costMap.get(resourceType);
if (cost > warehouse.resources.get(resourceType))
overdraft.put(resourceType, cost - warehouse.resources.get(resourceType));
}
return overdraft;
}
10 months ago
public static void warehouseDeposit(MerchantMsg msg, PlayerCharacter player, NPC npc) {
Building warehouseBuilding;
Warehouse warehouse;
int depositAmount;
Dispatch dispatch;
Item resource = Item.getFromCache(msg.getItemID());
if (resource == null)
return;
depositAmount = msg.getAmount();
CharacterItemManager itemMan = player.charItemManager;
10 months ago
if (!itemMan.doesCharOwnThisItem(resource.getObjectUUID()))
return;
warehouseBuilding = npc.getBuilding();
if (warehouseBuilding == null)
return;
warehouse = warehouseBuilding.getCity().warehouse;
10 months ago
if (warehouse == null)
return;
if (!deposit(player, resource, depositAmount, true, true, warehouse)) {
10 months ago
return;
}
ViewResourcesMsg vrm = new ViewResourcesMsg(player);
10 months ago
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouseBuilding);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
}
public static void warehouseWithdraw(MerchantMsg msg, PlayerCharacter player, NPC npc) {
int withdrawAmount;
Building warehouseBuilding;
Warehouse warehouse;
Dispatch dispatch;
withdrawAmount = msg.getAmount();
warehouseBuilding = npc.getBuilding();
if (warehouseBuilding == null)
return;
if (player.getGuild() != warehouseBuilding.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus()))
return;
City city = warehouseBuilding.getCity();
if (city == null)
return;
warehouse = city.warehouse;
10 months ago
if (warehouse == null)
return;
Enum.ResourceType resourceType = Enum.ResourceType.hashLookup.get(msg.getHashID());
10 months ago
if (isResourceLocked(warehouse, resourceType)) {
10 months ago
ChatManager.chatSystemInfo(player, "You cannot withdrawl a locked resource.");
return;
}
if (!withdraw(warehouse, player, resourceType, withdrawAmount, true, true)) {
ChatManager.chatGuildError(player, "Failed to withdrawl " + resourceType.name() + '.');
Logger.debug(player.getName() + " Failed to withdrawl =" + resourceType.name() + " from Warehouse With ID = " + warehouseBuilding.getObjectUUID());
10 months ago
return;
}
ViewResourcesMsg vrm = new ViewResourcesMsg(player);
10 months ago
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouseBuilding);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
}
public static void warehouseLock(MerchantMsg msg, PlayerCharacter player, NPC npc) {
Building warehouseBuilding;
Warehouse warehouse;
10 months ago
int hashID;
Dispatch dispatch;
hashID = msg.getHashID();
warehouseBuilding = npc.getBuilding();
10 months ago
if (warehouseBuilding == null)
10 months ago
return;
if (player.getGuild() != warehouseBuilding.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus()))
10 months ago
return;
City city = warehouseBuilding.getCity();
10 months ago
if (city == null)
10 months ago
return;
warehouse = city.warehouse;
10 months ago
Enum.ResourceType resourceType = Enum.ResourceType.hashLookup.get(hashID);
10 months ago
// toggle lock
10 months ago
if (warehouse.locked.contains(resourceType)) {
10 months ago
boolean worked;
warehouse.locked.remove(resourceType);
worked = DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse);
10 months ago
if (worked) {
ViewResourcesMsg vrm = new ViewResourcesMsg(player);
10 months ago
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouseBuilding);
10 months ago
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
} else
warehouse.locked.add(resourceType);
10 months ago
return;
}
10 months ago
boolean worked;
warehouse.locked.add(resourceType);
worked = DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse);
if (worked) {
ViewResourcesMsg vrm = new ViewResourcesMsg(player);
vrm.setGuild(player.getGuild());
vrm.setWarehouseBuilding(warehouseBuilding);
vrm.configure();
dispatch = Dispatch.borrow(player, vrm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
} else
warehouse.locked.remove(resourceType);
10 months ago
}
public static synchronized boolean deposit(PlayerCharacter pc, Item resource, int amount, boolean removeFromInventory, boolean transaction, Warehouse warehouse) {
ClientConnection origin = pc.getClientConnection();
10 months ago
if (origin == null)
return false;
if (amount < 0) {
Logger.info(pc.getFirstName() + " Attempting to Dupe!!!!!!");
return false;
}
Enum.ResourceType resourceType = Enum.ResourceType.resourceLookup.get(resource.templateID);
10 months ago
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
CharacterItemManager itemMan = pc.charItemManager;
10 months ago
if (itemMan == null)
return false;
if (itemMan.getGoldTrading() > 0) {
ErrorPopupMsg.sendErrorPopup(pc, 195);
return false;
}
if (!itemMan.doesCharOwnThisItem(resource.getObjectUUID()))
return false;
if (!resource.validForInventory(origin, pc, itemMan))
return false;
if (resource.getNumOfItems() < amount)
return false;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > Enum.ResourceType.resourceLookup.get(resource.templateID).deposit_limit)
10 months ago
return false;
if (removeFromInventory) {
if (resourceType.equals(Enum.ResourceType.GOLD)) {
10 months ago
if (itemMan.getGoldInventory().getNumOfItems() - amount < 0)
return false;
if (itemMan.getGoldInventory().getNumOfItems() - amount > MBServerStatics.PLAYER_GOLD_LIMIT)
return false;
if (!itemMan.modifyInventoryGold(-amount))
10 months ago
return false;
UpdateGoldMsg ugm = new UpdateGoldMsg(pc);
ugm.configure();
Dispatch dispatch = Dispatch.borrow(pc, ugm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
itemMan.updateInventory();
} else {
itemMan.delete(resource);
itemMan.updateInventory();
}
}
10 months ago
itemMan.updateInventory();
if (newAmount > resourceType.deposit_limit)
10 months ago
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
warehouse.resources.put(resourceType, oldAmount);
return false;
}
if (resource.template.item_type.equals(Enum.ItemType.GOLD))
resourceType = Enum.ResourceType.GOLD;
10 months ago
else
resourceType = Enum.ResourceType.valueOf(ItemTemplate.templates.get(resource.getTemplateID()).item_base_name.toUpperCase());
10 months ago
if (transaction)
AddTransactionToWarehouse(warehouse, pc.getObjectType(), pc.getObjectUUID(), Enum.TransactionType.DEPOSIT, resourceType, amount);
return true;
}
public static synchronized boolean depositFromMine(Mine mine, Enum.ResourceType resourceType, int amount, Warehouse warehouse) {
10 months ago
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > resourceType.deposit_limit)
10 months ago
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
warehouse.resources.put(resourceType, oldAmount);
return false;
}
10 months ago
if (mine != null)
AddTransactionToWarehouse(warehouse, Enum.GameObjectType.Building, mine.getBuildingID(), Enum.TransactionType.MINE, resourceType, amount);
return true;
}
public static synchronized void depositRealmTaxes(PlayerCharacter taxer, Enum.ResourceType resourceType, int amount, Warehouse warehouse) {
10 months ago
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
warehouse.resources.put(resourceType, oldAmount);
return;
}
10 months ago
AddTransactionToWarehouse(warehouse, taxer.getObjectType(), taxer.getObjectUUID(), Enum.TransactionType.TAXRESOURCEDEPOSIT, resourceType, amount);
}
public static synchronized void depositProfitTax(Enum.ResourceType resourceType, int amount, Building building, Warehouse warehouse) {
if (warehouse.resources.get(resourceType) == null)
10 months ago
return;
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount + amount;
if (newAmount > resourceType.deposit_limit)
10 months ago
return;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
warehouse.resources.put(resourceType, oldAmount);
return;
}
10 months ago
if (building != null)
AddTransactionToWarehouse(warehouse, Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.DEPOSIT, resourceType, amount);
}
public static synchronized boolean withdraw(Warehouse warehouse, NPC npc, Enum.ResourceType resourceType, int amount, boolean transaction) {
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
int newAmount = oldAmount - amount;
10 months ago
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
if (amount <= 0)
return false;
if (oldAmount < amount)
return false;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
warehouse.resources.put(resourceType, oldAmount);
10 months ago
return false;
}
10 months ago
if (transaction)
AddTransactionToWarehouse(warehouse, npc.getObjectType(), npc.getObjectUUID(), Enum.TransactionType.WITHDRAWL, resourceType, amount);
return true;
}
public static synchronized void transferResources(Warehouse warehouse, PlayerCharacter taxer, TaxResourcesMsg msg, ArrayList<Enum.ResourceType> realmResources, float taxPercent) {
10 months ago
for (Enum.ResourceType resourceType : realmResources) {
if (warehouse.resources.get(resourceType) == null)
10 months ago
return;
int amount = (int) (warehouse.resources.get(resourceType) * taxPercent);
10 months ago
if (amount <= 0) {
msg.getResources().put(resourceType.hash, 0);
10 months ago
continue;
}
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
amount = oldAmount;
int newAmount = oldAmount - amount;
if (newAmount < amount)
continue;
msg.getResources().put(resourceType.hash, amount);
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
msg.getResources().put(resourceType.hash, 0);
warehouse.resources.put(resourceType, oldAmount);
10 months ago
continue;
}
warehouse.resources.put(resourceType, newAmount);
depositRealmTaxes(taxer, resourceType, amount, warehouse);
Enum.ResourceType resource;
10 months ago
AddTransactionToWarehouse(warehouse, taxer.getObjectType(), taxer.getObjectUUID(), Enum.TransactionType.TAXRESOURCE, resourceType, amount);
10 months ago
}
}
public static synchronized boolean withdraw(Warehouse warehouse, PlayerCharacter pc, Enum.ResourceType resourceType, int amount, boolean addToInventory, boolean transaction) {
10 months ago
if (pc == null)
return false;
ItemTemplate template = ItemTemplate.templates.get(resourceType.templateID);
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
if (amount <= 0)
return false;
CharacterItemManager itemMan = pc.charItemManager;
10 months ago
if (itemMan == null)
return false;
if (addToInventory)
8 months ago
if (!itemMan.hasRoomInventory(template.item_wt * amount)) {
10 months ago
ChatManager.chatSystemInfo(pc, "You can not carry any more of that item.");
return false;
}
if (addToInventory && resourceType.equals(Enum.ResourceType.GOLD)) {
if (pc.charItemManager.getGoldInventory().getNumOfItems() + amount > MBServerStatics.PLAYER_GOLD_LIMIT)
10 months ago
return false;
if (pc.charItemManager.getGoldInventory().getNumOfItems() + amount < 0)
10 months ago
return false;
}
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
return false;
int newAmount = oldAmount - amount;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (!DbManager.WarehouseQueries.UPDATE_WAREHOUSE(warehouse)) {
warehouse.resources.put(resourceType, oldAmount);
10 months ago
return false;
}
10 months ago
if (addToInventory) {
if (resourceType.equals(Enum.ResourceType.GOLD)) {
10 months ago
itemMan.addGoldToInventory(amount, false);
UpdateGoldMsg ugm = new UpdateGoldMsg(pc);
ugm.configure();
Dispatch dispatch = Dispatch.borrow(pc, ugm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
itemMan.updateInventory();
} else {
boolean itemWorked = false;
Item item = new Item(resourceType.templateID);
item.ownerID = pc.getObjectUUID();
item.ownerType = Enum.OwnerType.PlayerCharacter;
10 months ago
item.containerType = Enum.ItemContainerType.INVENTORY;
item.numberOfItems = amount;
10 months ago
try {
item = DbManager.ItemQueries.PERSIST(item);
10 months ago
itemWorked = true;
} catch (Exception e) {
Logger.error(e);
}
if (itemWorked) {
itemMan.addItemToInventory(item);
itemMan.updateInventory();
}
}
}
if (transaction)
AddTransactionToWarehouse(warehouse, pc.getObjectType(), pc.getObjectUUID(), Enum.TransactionType.WITHDRAWL, resourceType, amount);
return true;
}
public static synchronized boolean loot(Warehouse warehouse, PlayerCharacter pc, Enum.ResourceType resourceType, int amount, boolean addToInventory) {
10 months ago
if (pc == null)
return false;
ItemTemplate template = ItemTemplate.templates.get(resourceType);
if (template == null)
10 months ago
return false;
if (warehouse.resources.get(resourceType) == null)
10 months ago
return false;
if (amount <= 0)
return false;
CharacterItemManager itemMan = pc.charItemManager;
10 months ago
if (itemMan == null)
return false;
if (!itemMan.hasRoomInventory(template.item_wt)) {
10 months ago
ChatManager.chatSystemInfo(pc, "You can not carry any more of that item.");
return false;
}
int oldAmount = warehouse.resources.get(resourceType);
10 months ago
if (oldAmount < amount)
return false;
int newAmount = oldAmount - amount;
warehouse.resources.put(resourceType, newAmount);
10 months ago
if (addToInventory) {
if (resourceType.equals(Enum.ResourceType.GOLD)) {
10 months ago
itemMan.addGoldToInventory(amount, false);
UpdateGoldMsg ugm = new UpdateGoldMsg(pc);
ugm.configure();
Dispatch dispatch = Dispatch.borrow(pc, ugm);
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
itemMan.updateInventory();
} else {
boolean itemWorked = false;
Item item = new Item(resourceType.templateID);
item.ownerID = pc.getObjectUUID();
item.ownerType = Enum.OwnerType.PlayerCharacter;
10 months ago
item.containerType = Enum.ItemContainerType.INVENTORY;
item.numberOfItems = amount;
10 months ago
try {
item = DbManager.ItemQueries.PERSIST(item);
10 months ago
itemWorked = true;
} catch (Exception e) {
Logger.error(e);
}
if (itemWorked) {
itemMan.addItemToInventory(item);
itemMan.updateInventory();
}
}
}
return true;
}
public static boolean isEmpty(Warehouse warehouse) {
int amount = 0;
for (Enum.ResourceType resourceType : EnumSet.allOf(Enum.ResourceType.class)) {
amount += warehouse.resources.get(resourceType);
10 months ago
if (amount > 0)
return false;
}
return true;
}
public static void loadAllTransactions(Warehouse warehouse) {
warehouse.transactions = DbManager.WarehouseQueries.GET_TRANSACTIONS_FOR_WAREHOUSE(warehouse.building.getObjectUUID());
10 months ago
}
public static void AddTransactionToWarehouse(Warehouse warehouse, Enum.GameObjectType targetType, int targetUUID, Enum.TransactionType transactionType, Enum.ResourceType resource, int amount) {
10 months ago
if (!DbManager.WarehouseQueries.CREATE_TRANSACTION(warehouse.building.getObjectUUID(), targetType, targetUUID, transactionType, resource, amount, DateTime.now()))
10 months ago
return;
Transaction transaction = new Transaction(warehouse.building.getObjectUUID(), targetType, targetUUID, transactionType, resource, amount, DateTime.now());
10 months ago
warehouse.transactions.add(transaction);
}
public static boolean isAboveCap(Warehouse warehouse, Enum.ResourceType resourceType, int deposit) {
int newAmount = warehouse.resources.get(resourceType) + deposit;
return newAmount > resourceType.deposit_limit;
10 months ago
}
public static boolean isResourceLocked(Warehouse warehouse, Enum.ResourceType resourceType) {
return resourceType.elementOf(warehouse.locked);
}
10 months ago
}