Browse Source

junk, complete and produce wrapped with same lock as warehouse.

combat-2
MagicBot 7 months ago
parent
commit
364fb40e0b
  1. 16
      src/engine/gameManager/ForgeManager.java
  2. 148
      src/engine/net/client/handlers/ItemProductionMsgHandler.java

16
src/engine/gameManager/ForgeManager.java

@ -14,10 +14,7 @@ import engine.loot.WorkOrder;
import engine.mbEnums; import engine.mbEnums;
import engine.net.DispatchMessage; import engine.net.DispatchMessage;
import engine.net.client.msg.ItemProductionMsg; import engine.net.client.msg.ItemProductionMsg;
import engine.objects.Item; import engine.objects.*;
import engine.objects.ItemTemplate;
import engine.objects.NPC;
import engine.objects.Warehouse;
import engine.powers.EffectsBase; import engine.powers.EffectsBase;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
@ -108,6 +105,10 @@ public enum ForgeManager implements Runnable {
if (validation_result != 0) if (validation_result != 0)
return validation_result; return validation_result;
City city = workOrder.vendor.building.getCity();
city.transactionLock.writeLock().lock();
try {
// Configure this production run. // Configure this production run.
workOrder.workOrderID = wordOrderCounter.incrementAndGet(); workOrder.workOrderID = wordOrderCounter.incrementAndGet();
@ -137,8 +138,11 @@ public enum ForgeManager implements Runnable {
forge.add(workOrder); forge.add(workOrder);
Logger.info(workOrder.toString()); } catch (Exception e) {
Logger.error(e);
} finally {
city.transactionLock.writeLock().unlock();
}
return validation_result; return validation_result;
} }

148
src/engine/net/client/handlers/ItemProductionMsgHandler.java

@ -24,10 +24,7 @@ import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.ErrorPopupMsg; import engine.net.client.msg.ErrorPopupMsg;
import engine.net.client.msg.ItemProductionMsg; import engine.net.client.msg.ItemProductionMsg;
import engine.net.client.msg.ManageNPCMsg; import engine.net.client.msg.ManageNPCMsg;
import engine.objects.CharacterItemManager; import engine.objects.*;
import engine.objects.Item;
import engine.objects.NPC;
import engine.objects.PlayerCharacter;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.util.HashMap; import java.util.HashMap;
@ -132,44 +129,54 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler {
Item virtualItem = Item.getFromCache(itemUUID); Item virtualItem = Item.getFromCache(itemUUID);
WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(virtualItem); WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(virtualItem);
// Refresh the window. SetPrice is used only to force a window refresh. City city = workOrder.vendor.building.getCity();
// What controls items displayed are the contents of the workOrders. (ManageNPCMsg) city.transactionLock.writeLock().lock();
ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, virtualItem, mbEnums.ProductionActionType.CONFIRM_SETPRICE, true); try {
DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
// Remove virtualItem from collections // Refresh the window. SetPrice is used only to force a window refresh.
// What controls items displayed are the contents of the workOrders. (ManageNPCMsg)
workOrder.cooking.remove(virtualItem); ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, virtualItem, mbEnums.ProductionActionType.CONFIRM_SETPRICE, true);
ForgeManager.itemWorkOrderLookup.remove(virtualItem); DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
DbManager.removeFromCache(virtualItem);
// WorkOrder exists until all items are completed or junked // Remove virtualItem from collections
// this is to hold a reference to the virtual item for serialization (NPCMsgHandler)
workOrder.slots_used = workOrder.slots_used - 1; workOrder.cooking.remove(virtualItem);
ForgeManager.itemWorkOrderLookup.remove(virtualItem);
DbManager.removeFromCache(virtualItem);
if (workOrder.slots_used == 0) // WorkOrder exists until all items are completed or junked
ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder); // this is to hold a reference to the virtual item for serialization (NPCMsgHandler)
// Persist item and add to vendor inventory workOrder.slots_used = workOrder.slots_used - 1;
Item completedItem = DbManager.ItemQueries.PERSIST(virtualItem); if (workOrder.slots_used == 0)
ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder);
// Apply Item effects for Prefix and Suffix tokens // Persist item and add to vendor inventory
completedItem.prefixToken = virtualItem.prefixToken; Item completedItem = DbManager.ItemQueries.PERSIST(virtualItem);
completedItem.suffixToken = virtualItem.suffixToken;
ItemManager.applyItemEffects(completedItem); // Apply Item effects for Prefix and Suffix tokens
vendor.charItemManager.addItemToInventory(completedItem); completedItem.prefixToken = virtualItem.prefixToken;
completedItem.suffixToken = virtualItem.suffixToken;
ItemProductionMsg outMsg1 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.DEPOSIT, true); ItemManager.applyItemEffects(completedItem);
DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg1, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
ItemProductionMsg outMsg2 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.CONFIRM_DEPOSIT, true);
DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg2, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
vendor.charItemManager.addItemToInventory(completedItem);
ItemProductionMsg outMsg1 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.DEPOSIT, true);
DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg1, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
ItemProductionMsg outMsg2 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.CONFIRM_DEPOSIT, true);
DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg2, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
} catch (Exception e) {
Logger.error(e);
} finally {
city.transactionLock.writeLock().unlock();
}
} }
private static void setItemPrice(int itemUUID, int itemPrice, NPC vendor, ClientConnection origin) { private static void setItemPrice(int itemUUID, int itemPrice, NPC vendor, ClientConnection origin) {
@ -189,18 +196,18 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler {
return; return;
if (!DbManager.ItemQueries.UPDATE_VALUE(targetItem, itemPrice)) { if (!DbManager.ItemQueries.UPDATE_VALUE(targetItem, itemPrice)) {
ChatManager.chatInfoError(origin.getPlayerCharacter(), "Failed to set price! Contact CCR For help."); ChatManager.chatInfoError(origin.getPlayerCharacter(), "Failed to set price! Contact CCR For help.");
return; return;
} }
targetItem.setValue(itemPrice); targetItem.setValue(itemPrice);
outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.DEPOSIT, true); outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.DEPOSIT, true);
dispatch = Dispatch.borrow(player, outMsg); dispatch = Dispatch.borrow(player, outMsg);
DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.SETPRICE, true); outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.SETPRICE, true);
dispatch = Dispatch.borrow(player, outMsg); dispatch = Dispatch.borrow(player, outMsg);
DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
} }
@ -312,7 +319,7 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler {
if (!vendor.charItemManager.doesCharOwnThisItem(targetItem.getObjectUUID())) if (!vendor.charItemManager.doesCharOwnThisItem(targetItem.getObjectUUID()))
continue; continue;
if (vendor.charItemManager.inventoryContains(targetItem) == false) if (!vendor.charItemManager.inventoryContains(targetItem))
continue; continue;
itemValue = targetItem.template.item_value; itemValue = targetItem.template.item_value;
@ -361,48 +368,51 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler {
ManageNPCMsg outMsg; ManageNPCMsg outMsg;
Dispatch dispatch; Dispatch dispatch;
if (origin.sellLock.tryLock()) { targetItem = Item.getFromCache(itemUUID);
try { PlayerCharacter player = origin.getPlayerCharacter();
targetItem = Item.getFromCache(itemUUID); if (player == null)
return;
PlayerCharacter player = origin.getPlayerCharacter(); // junk nothing?
if (player == null) if (targetItem == null)
return; return;
// junk nothing? // Cannot junk items without a forge!
if (targetItem == null) if (vendor.getBuilding() == null)
return; return;
// Cannot junk items without a forge! WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(targetItem);
if (vendor.getBuilding() == null) City city = workOrder.vendor.building.getCity();
return; city.transactionLock.writeLock().lock();
WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(targetItem); try {
workOrder.cooking.remove(targetItem);
DbManager.removeFromCache(targetItem);
// WorkOrder exists until all items are completed or junked workOrder.cooking.remove(targetItem);
// this is to hold a reference to the virtual item for serialization (NPCMsgHandler) DbManager.removeFromCache(targetItem);
workOrder.slots_used = workOrder.slots_used - 1; // WorkOrder exists until all items are completed or junked
// this is to hold a reference to the virtual item for serialization (NPCMsgHandler)
if (workOrder.slots_used == 0) workOrder.slots_used = workOrder.slots_used - 1;
ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder);
// Refresh vendor's inventory to client if (workOrder.slots_used == 0)
ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder);
outMsg = new ManageNPCMsg(vendor); // Refresh vendor's inventory to client
outMsg.setMessageType(1);
dispatch = Dispatch.borrow(player, outMsg); outMsg = new ManageNPCMsg(vendor);
DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); outMsg.setMessageType(1);
} finally { dispatch = Dispatch.borrow(player, outMsg);
origin.sellLock.unlock(); DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
} } catch (Exception e) {
Logger.error(e);
} finally {
city.transactionLock.writeLock().unlock();
} }
} }
@ -425,10 +435,10 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler {
if (targetItem.template.item_type.equals(ItemType.GOLD)) if (targetItem.template.item_type.equals(ItemType.GOLD))
return; return;
if (vendor.charItemManager.inventoryContains(targetItem) == false) if (!vendor.charItemManager.inventoryContains(targetItem))
return; return;
if (player.charItemManager.hasRoomInventory(targetItem.template.item_wt) == false) if (!player.charItemManager.hasRoomInventory(targetItem.template.item_wt))
return; return;
player.charItemManager.buyFromNPC(targetItem, vendor); player.charItemManager.buyFromNPC(targetItem, vendor);

Loading…
Cancel
Save