fixed property trade

This commit is contained in:
Johannes Schmelz 2024-12-08 17:34:19 +01:00
parent 48913356e3
commit f8fe0b9877
2 changed files with 22 additions and 7 deletions

View File

@ -322,25 +322,29 @@ public class ServerGameLogic implements ClientInterpreter {
} }
/** /**
* Executes the trade by transferring requested properties from the receiver * Executes a trade between two players, transferring properties and money as specified.
* to the sender and offered properties from the sender to the receiver. *
* @param sender the player initiating the trade
* @param receiver the player receiving the trade offer
* @param requestedProperties the properties requested by the receiver
* @param offeredProperties the properties offered by the sender
*/ */
public void executeTrade(Player sender, Player receiver, Set<PropertyField> requestedProperties, Set<PropertyField> offeredProperties) { private void executeTrade(Player sender, Player receiver, Set<PropertyField> requestedProperties, Set<PropertyField> offeredProperties) {
// Transfer requested properties from receiver to sender // Transfer requested properties from receiver to sender
for (PropertyField field : requestedProperties) { for (PropertyField field : offeredProperties) {
receiver.removeProperty(field.getId()); receiver.removeProperty(field.getId());
sender.addProperty(field.getId()); sender.addProperty(field.getId());
field.setOwner(sender); // Update ownership field.setOwner(sender); // Update ownership
System.out.printf("Property %s transferred from %s to %s.\n", LOGGER.log(Level.DEBUG, "Property %s transferred from %s to %s.\n",
field.getName(), receiver.getName(), sender.getName()); field.getName(), receiver.getName(), sender.getName());
} }
// Transfer offered properties from sender to receiver // Transfer offered properties from sender to receiver
for (PropertyField field : offeredProperties) { for (PropertyField field : requestedProperties) {
sender.removeProperty(field.getId()); sender.removeProperty(field.getId());
receiver.addProperty(field.getId()); receiver.addProperty(field.getId());
field.setOwner(receiver); // Update ownership field.setOwner(receiver); // Update ownership
System.out.printf("Property %s transferred from %s to %s.\n", LOGGER.log(Level.DEBUG, "Property %s transferred from %s to %s.\n",
field.getName(), sender.getName(), receiver.getName()); field.getName(), sender.getName(), receiver.getName());
} }
} }

View File

@ -28,7 +28,18 @@ public class TradeResponse extends ClientMessage{
this.tradeHandler = tradeHandler; this.tradeHandler = tradeHandler;
} }
/**
* Returns whether the trade was accepted.
*
* @return {@code true} if the trade was accepted, {@code false} otherwise
*/
public boolean isAccepted() { return status; } public boolean isAccepted() { return status; }
/**
* Returns the TradeHandler corresponding to the Trade.
*
* @return the TradeHandler corresponding to the Trade
*/
public TradeHandler getTradeHandler() { public TradeHandler getTradeHandler() {
return tradeHandler; return tradeHandler;
} }