rudimentary function for trade

This commit is contained in:
Johannes Schmelz 2024-11-29 03:07:17 +01:00
parent 333f27a016
commit 94ba9f0af2
10 changed files with 271 additions and 411 deletions

View File

@ -23,6 +23,7 @@ import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player;
import pp.monopoly.message.client.ViewAssetsRequest;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.notification.Sound;
@ -48,6 +49,7 @@ public class ChoosePartner extends Dialog {
super(app.getDialogManager());
this.app = app;
tradeHandler = new TradeHandler(app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()));
app.getGameLogic().send(new ViewAssetsRequest());
// Background Image
addBackgroundImage();
@ -140,7 +142,7 @@ public class ChoosePartner extends Dialog {
confirmButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
close();
new TradeMenu(app).open();
new TradeMenu(app, tradeHandler).open();
}));
lowerRightMenu.addChild(confirmButton);

View File

@ -13,15 +13,12 @@ import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.model.fields.Field;
import pp.monopoly.model.fields.FoodField;
import pp.monopoly.model.fields.GateField;
import pp.monopoly.model.fields.PropertyField;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -43,9 +40,6 @@ public class PropertyOverviewMenu extends Dialog {
super(app.getDialogManager());
this.app = app;
// Assign 10 random buildings to the player
//TODO Aufruf für Demo löschen
assignRandomPropertiesToPlayer();
// Make the menu fullscreen
Vector3f screenSize = new Vector3f(app.getCamera().getWidth(), app.getCamera().getHeight(), 0);
@ -102,7 +96,7 @@ public class PropertyOverviewMenu extends Dialog {
Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayers().get(0);
// Iterate through the player's properties
for (PropertyField property : currentPlayer.getProperties()) {
for (PropertyField property : currentPlayer.getPropertyFields()) {
if (property instanceof BuildingProperty) {
BuildingProperty building = (BuildingProperty) property;
cards.add(createBuildingCard(building));
@ -247,27 +241,4 @@ public class PropertyOverviewMenu extends Dialog {
app.getGuiNode().detachChild(mainContainer);
super.close();
}
//TODO Demo Funktion nach implementierung löschen
private void assignRandomPropertiesToPlayer() {
// Get the board from the BoardManager
BoardManager boardManager = app.getGameLogic().getBoardManager();
List<Field> fields = boardManager.getBoard();
// Filter for BuildingProperty, FoodField, and GateField
List<PropertyField> propertyFields = new ArrayList<>();
for (Field field : fields) {
if (field instanceof PropertyField) {
propertyFields.add((PropertyField) field);
}
}
// Shuffle and select up to 10 properties (mix of BuildingProperty, FoodField, GateField)
Collections.shuffle(propertyFields);
List<PropertyField> randomProperties = propertyFields.subList(0, Math.min(10, propertyFields.size()));
// Assign the properties to the current player
Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayers().get(0);
currentPlayer.getProperties().clear();
currentPlayer.getProperties().addAll(randomProperties);
}
}

View File

@ -1,7 +1,8 @@
package pp.monopoly.client.gui;
import static java.lang.Math.min;
import com.jme3.app.Application;
import com.jme3.app.state.BaseAppState;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
@ -12,55 +13,218 @@ import com.simsilica.lemur.*;
import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.component.SpringGridLayout;
import com.simsilica.lemur.core.VersionedList;
import com.simsilica.lemur.core.VersionedReference;
import com.simsilica.lemur.style.ElementId;
import com.simsilica.lemur.text.DocumentModel;
import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.model.fields.PropertyField;
import pp.monopoly.notification.Sound;
import java.util.Set;
public class TradeMenu extends Dialog {
private final MonopolyApp app;
private final TradeHandler tradeHandler;
private final Container mainContainer;
private Selector<String> leftBuildingSelector, leftCurrencySelector, leftSpecialCardSelector;
private Selector<String> rightBuildingSelector, rightCurrencySelector, rightSpecialCardSelector;
private final Button cancelButton = new Button("Abbrechen");
private final Button tradeButton = new Button("Handeln");
private Container lowerLeftMenu, lowerRightMenu;
private TextField leftSelectionsField;
private TextField rightSelectionsField;
private TextField leftCurrencyInput;
private TextField rightCurrencyInput;
private Selector<String> leftBuildingSelector, leftSpecialCardSelector;
private Selector<String> rightBuildingSelector, rightSpecialCardSelector;
private TextField leftSelectionsField, rightSelectionsField;
private TextField leftCurrencyInput, rightCurrencyInput;
QuadBackgroundComponent translucentWhiteBackground =
new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White));
private static final ColorRGBA TRANSLUCENT_WHITE = new ColorRGBA(1, 1, 1, 0.5f);
/**
* Constructs the TradeMenu dialog.
*
* @param app The Monopoly application instance.
*/
public TradeMenu(MonopolyApp app) {
public TradeMenu(MonopolyApp app, TradeHandler tradeHandler) {
super(app.getDialogManager());
this.app = app;
this.tradeHandler = tradeHandler;
// Background Image
addBackgroundImage();
// Main container for the UI components
mainContainer = new Container(new SpringGridLayout(Axis.Y, Axis.X));
mainContainer.setPreferredSize(new Vector3f(1200, 800, 0));
mainContainer.setBackground(translucentWhiteBackground);
// Add header container
mainContainer.addChild(createHeaderContainer());
// Add main content (three columns: left, middle, right)
mainContainer.addChild(createMainContent());
// Attach main container to GUI node
mainContainer = createMainContainer();
app.getGuiNode().attachChild(mainContainer);
positionMainContainer();
}
private Container createMainContainer() {
Container container = new Container(new SpringGridLayout(Axis.Y, Axis.X));
container.setPreferredSize(new Vector3f(1200, 800, 0));
container.setBackground(new QuadBackgroundComponent(TRANSLUCENT_WHITE));
container.addChild(createHeader());
container.addChild(createMainContent());
return container;
}
private Label createHeader() {
Label header = new Label("Handelsmenü", new ElementId("label-Bold"));
header.setFontSize(50);
header.setInsets(new Insets3f(10, 10, 10, 10));
header.setBackground(new QuadBackgroundComponent(TRANSLUCENT_WHITE));
return header;
}
private Container createMainContent() {
Container mainContent = new Container(new SpringGridLayout(Axis.X, Axis.Y));
mainContent.setPreferredSize(new Vector3f(1200, 700, 0));
mainContent.addChild(createTradeColumn("Wähle Handelsobjekt:", true));
mainContent.addChild(createMiddleSection());
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
Container buttons = mainContent.addChild(new Container(new SpringGridLayout()));
Button cancel = new Button("Abbrechen");
cancel.addClickCommands(s-> ifTopDialog( () -> {
close();
app.getGameLogic().playSound(Sound.BUTTON);
}));
Button trade = new Button("Handeln");
trade.addClickCommands(s-> ifTopDialog( () -> {
close();
app.getGameLogic().playSound(Sound.BUTTON);
}));
buttons.addChild(cancel);
buttons.addChild(trade);
return mainContent;
}
private Container createTradeColumn(String label, boolean isLeft) {
Container column = new Container(new SpringGridLayout(Axis.Y, Axis.X));
column.setBackground(new QuadBackgroundComponent(ColorRGBA.White));
Label columnLabel = column.addChild(new Label(label));
columnLabel.setFontSize(24);
columnLabel.setBackground(new QuadBackgroundComponent(TRANSLUCENT_WHITE));
column.addChild(new Label("Gebäude:"));
Selector<String> buildingSelector = createPropertySelector(isLeft);
column.addChild(buildingSelector);
column.addChild(new Label("Währung:"));
TextField currencyInput = createCurrencyInput();
column.addChild(currencyInput);
column.addChild(new Label("Sonderkarten:"));
Selector<String> specialCardSelector = createSpecialCardSelector();
styleSelector(specialCardSelector);
column.addChild(specialCardSelector);
assignSelectors(buildingSelector, specialCardSelector, currencyInput, isLeft);
return column;
}
private Selector<String> createPropertySelector(boolean isLeft) {
VersionedList<String> properties = new VersionedList<>();
for (PropertyField field : getPropertyFields(isLeft)) {
properties.add(field.getName());
}
Selector<String> selector = new Selector<>(properties, "glass");
styleSelector(selector);
return selector;
}
private Selector<String> createSpecialCardSelector() {
VersionedList<String> numbers = new VersionedList<>();
for (int i = 0; i <= app.getGameLogic().getPlayerHandler().getPlayerById(tradeHandler.getReceiver().getId()).getNumJailCard(); i++) {
numbers.add(i+"");
}
Selector<String> selector = new Selector<>(numbers, "glass");
styleSelector(selector);
return selector;
}
private Iterable<PropertyField> getPropertyFields(boolean isLeft) {
return app.getGameLogic()
.getBoardManager()
.getPropertyFields(app.getGameLogic()
.getPlayerHandler()
.getPlayerById(isLeft ? tradeHandler.getReceiver().getId() : tradeHandler.getSender().getId())
.getProperties());
}
private TextField createCurrencyInput() {
TextField currencyInput = new TextField("");
styleTextField(currencyInput);
return currencyInput;
}
/**
* Creates the middle section with selection fields and arrows.
*
* @return The middle section container.
*/
private Container createMiddleSection() {
Container middleSection = new Container(new SpringGridLayout(Axis.Y, Axis.X));
middleSection.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f)));
// Add combined label for the top
Label middleLabelTop = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabelTop.setFontSize(24);
middleLabelTop.setInsets(new Insets3f(5, 5, 5, 5));
// Add the left selection field (Quellobjekte)
leftSelectionsField = middleSection.addChild(new TextField(""));
leftSelectionsField.setPreferredSize(new Vector3f(600, 50, 0));
// Add arrows
Label arrows = middleSection.addChild(new Label(""));
arrows.setFontSize(40);
// Add the right selection field (Zielobjekte)
rightSelectionsField = middleSection.addChild(new TextField(""));
rightSelectionsField.setPreferredSize(new Vector3f(600, 50, 0));
// Add combined label for the bottom
Label middleLabelBottom = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabelBottom.setFontSize(24);
middleLabelBottom.setInsets(new Insets3f(5, 5, 5, 5));
// Spacer
Label spacer = middleSection.addChild(new Label(""));
spacer.setPreferredSize(new Vector3f(1, 50, 0));
return middleSection;
}
private TextField createSelectionsField(Container parent, boolean isLeft) {
TextField field = new TextField("");
field.setPreferredSize(new Vector3f(600, 50, 0));
parent.addChild(field);
return field;
}
private Label createArrowLabel() {
Label arrowLabel = new Label("");
arrowLabel.setFontSize(40);
return arrowLabel;
}
private void styleSelector(Selector<String> selector) {
selector.setInsets(new Insets3f(5, 10, 5, 10));
selector.setBackground(new QuadBackgroundComponent(ColorRGBA.Black));
}
private void styleTextField(TextField textField) {
textField.setInsets(new Insets3f(5, 10, 5, 10));
textField.setBackground(new QuadBackgroundComponent(ColorRGBA.Black));
}
private void assignSelectors(Selector<String> buildingSelector, Selector<String> specialCardSelector, TextField currencyInput, boolean isLeft) {
if (isLeft) {
leftBuildingSelector = buildingSelector;
leftSpecialCardSelector = specialCardSelector;
leftCurrencyInput = currencyInput;
} else {
rightBuildingSelector = buildingSelector;
rightSpecialCardSelector = specialCardSelector;
rightCurrencyInput = currencyInput;
}
}
private void positionMainContainer() {
mainContainer.setLocalTranslation(
(app.getCamera().getWidth() - mainContainer.getPreferredSize().x) / 2,
(app.getCamera().getHeight() + mainContainer.getPreferredSize().y) / 2,
@ -68,183 +232,6 @@ public class TradeMenu extends Dialog {
);
}
/**
* Creates a container for the header with a fixed size.
*
* @return The header container.
*/
private Container createHeaderContainer() {
// Create a container for the header
Container headerContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y));
headerContainer.setPreferredSize(new Vector3f(200, 100, 0)); // Set fixed width and height
// Add the header label
Label headerLabel = headerContainer.addChild(new Label("Handelsmenü", new ElementId("label-Bold")));
headerLabel.setFontSize(50); // Adjust font size as needed
headerLabel.setInsets(new Insets3f(10, 10, 10, 10)); // Add padding around the label
headerLabel.setBackground(new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f))); // Optional background
return headerContainer;
}
/**
* Creates the main content layout (left, middle, right columns).
*
* @return The main content container.
*/
private Container createMainContent() {
Container mainContent = new Container(new SpringGridLayout(Axis.X, Axis.Y));
mainContent.setPreferredSize(new Vector3f(1200, 700, 0));
// Left Column
mainContent.addChild(createTradeColumn("Wähle Handelsobjekt:", true));
// Middle Section
Container middleSection = mainContent.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X)));
middleSection.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f)));
// Add combined label
Label middleLabel = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabel.setFontSize(24); // Adjust font size as needed
middleLabel.setInsets(new Insets3f(5, 5, 5, 5)); // Add padding around the label
// Add the Quellobjekte TextField
leftSelectionsField = middleSection.addChild(new TextField(""));
leftSelectionsField.setPreferredSize(new Vector3f(600, 50, 0)); // Larger width to fit the split sections
addCustomSelectionListener(leftBuildingSelector, newSelection -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
addCustomSelectionListener(leftSpecialCardSelector, newSelection -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
// Add change listener for the currency input
monitorTextFieldChanges(leftCurrencyInput, () -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
Label arrows = middleSection.addChild(new Label(""));
arrows.setFontSize(40);
// Right Column
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
// Add combined label
middleLabel = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabel.setFontSize(24); // Adjust font size as needed
middleLabel.setInsets(new Insets3f(5, 5, 5, 5)); // Add padding around the label
// Add the Zielobjekte TextField
rightSelectionsField = middleSection.addChild(new TextField(""));
rightSelectionsField.setPreferredSize(new Vector3f(600, 50, 0));
addCustomSelectionListener(rightBuildingSelector, newSelection -> updateSelectionsField(rightSelectionsField, rightBuildingSelector, rightCurrencyInput, rightSpecialCardSelector));
addCustomSelectionListener(rightSpecialCardSelector, newSelection -> updateSelectionsField(rightSelectionsField, rightBuildingSelector, rightCurrencyInput, rightSpecialCardSelector));
// Add change listener for the currency input
monitorTextFieldChanges(rightCurrencyInput, () -> updateSelectionsField(rightSelectionsField, rightBuildingSelector, rightCurrencyInput, rightSpecialCardSelector));
// "Bestätigen" button
lowerRightMenu = new Container();
tradeButton.setPreferredSize(new Vector3f(200, 60, 0));
tradeButton.setFontSize(30);
tradeButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
close();
new TradeMenu(app).open();
}));
lowerRightMenu.addChild(tradeButton);
// Position the container near the bottom-right corner
lowerRightMenu.setLocalTranslation(new Vector3f(app.getCamera().getWidth() - 680, 100, 8)); // X: 220px from the right, Y: 50px above the bottom
app.getGuiNode().attachChild(lowerRightMenu);
Label spacer = middleSection.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 50, 0));
return mainContent;
}
/**
* Creates a column for trade objects (left or right side).
*
* @param label The label for the column.
* @param isLeft Whether this column is the left side.
* @return The column container.
*/
private Container createTradeColumn(String label, boolean isLeft) {
Container column = new Container(new SpringGridLayout(Axis.Y, Axis.X));
column.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White)));
Label columnLabel = column.addChild(new Label(label));
columnLabel.setFontSize(24);
columnLabel.setBackground(translucentWhiteBackground);
// Add dropdowns
column.addChild(new Label("Gebäude:"));
Selector<String> buildingSelector = column.addChild(new Selector<>(getSampleItems(),"glass"));
buildingSelector.setInsets(new Insets3f(5, 10, 5, 10));
buildingSelector.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
column.addChild(new Label("Währung:"));
TextField currencyInput = column.addChild(new TextField(""));
currencyInput.setInsets(new Insets3f(5, 10, 5, 10));
currencyInput.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
column.addChild(new Label("Sonderkarten:"));
Selector<String> specialCardSelector = column.addChild(new Selector<>(getSampleItems(),"glass"));
specialCardSelector.setInsets(new Insets3f(5, 10, 5, 10));
specialCardSelector.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
// Assign selectors to corresponding fields
if (isLeft) {
leftBuildingSelector = buildingSelector;
leftSpecialCardSelector = specialCardSelector;
leftCurrencyInput = currencyInput;
// "Abbrechen" button
lowerLeftMenu = new Container();
cancelButton.setPreferredSize(new Vector3f(200, 60, 0));
cancelButton.setFontSize(30);
cancelButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
this.close();
}));
lowerLeftMenu.addChild(cancelButton);
// Position the container near the bottom-left corner
lowerLeftMenu.setLocalTranslation(new Vector3f(50, 100, 8)); // Adjust X and Y to align with the bottom-left corner
app.getGuiNode().attachChild(lowerLeftMenu);
Label spacer = column.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 130, 0));
} else {
rightBuildingSelector = buildingSelector;
rightSpecialCardSelector = specialCardSelector;
rightCurrencyInput = currencyInput;
Label spacer = column.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 130, 0));
}
return column;
}
/**
* Provides sample dropdown items.
*/
private VersionedList<String> getSampleItems() {
VersionedList<String> items = new VersionedList<>();
items.add("Option 1");
items.add("Option 2");
items.add("Option 3");
return items;
}
/**
* Adds a background image to the dialog.
*/
private void addBackgroundImage() {
Texture backgroundImage = app.getAssetManager().loadTexture("Pictures/unibw-Bib2.png");
Quad quad = new Quad(app.getCamera().getWidth(), app.getCamera().getHeight());
@ -252,131 +239,18 @@ public class TradeMenu extends Dialog {
Material backgroundMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
backgroundMaterial.setTexture("ColorMap", backgroundImage);
background.setMaterial(backgroundMaterial);
background.setLocalTranslation(0, 0, 6); // Position behind other GUI elements
background.setLocalTranslation(0, 0, 6);
app.getGuiNode().attachChild(background);
}
private String truncateText(String text, int maxLength) {
return text.length() > maxLength ? text.substring(0, maxLength) + "..." : text;
}
private void updateSelectionsField(TextField selectionsField, Selector<String> buildingSelector, TextField currencyInput, Selector<String> specialCardSelector) {
// Get selections from the building selector
String buildingSelections = buildingSelector != null && buildingSelector.getSelectedItem() != null
? buildingSelector.getSelectedItem().trim()
: "";
// Get text from the currency input
String currencySelections = currencyInput != null
? currencyInput.getText().trim()
: "";
// Get selections from the special card selector
String specialCardSelections = specialCardSelector != null && specialCardSelector.getSelectedItem() != null
? specialCardSelector.getSelectedItem().trim()
: "";
// Build the combined text without adding unnecessary spaces
StringBuilder combinedText = new StringBuilder();
if (!buildingSelections.isEmpty()) {
combinedText.append(buildingSelections);
}
if (!currencySelections.isEmpty()) {
if (combinedText.length() > 0) {
combinedText.append(" | "); // Add a separator if there's already text
}
combinedText.append(currencySelections);
}
if (!specialCardSelections.isEmpty()) {
if (combinedText.length() > 0) {
combinedText.append(" | "); // Add a separator if there's already text
}
combinedText.append(specialCardSelections);
}
// Update the content of the TextField
selectionsField.setText(combinedText.toString());
}
/**
* Handles the escape action for the dialog.
*/
@Override
public void escape() {
new SettingsMenu(app).open();
}
/**
* Updates the dialog periodically, called by the dialog manager.
*
* @param delta The time elapsed since the last update.
*/
@Override
public void update(float delta) {
// Periodic updates (if needed) can be implemented here
public void close() {
app.getGuiNode().detachChild(mainContainer);
super.close();
}
/**
* Functional interface for a selection action listener.
*/
@FunctionalInterface
private interface SelectionActionListener<T> {
void onSelectionChanged(T selection);
}
/**
* Adds a custom action listener to the Selector.
*/
private void addCustomSelectionListener(Selector<String> selector, SelectionActionListener<String> listener) {
VersionedReference<Set<Integer>> selectionRef = selector.getSelectionModel().createReference();
app.getStateManager().attach(new BaseAppState() {
@Override
public void update(float tpf) {
if (selectionRef.update()) {
String selected = selector.getSelectedItem();
listener.onSelectionChanged(selected);
}
}
@Override
protected void initialize(Application app) {}
@Override
protected void cleanup(Application app) {}
@Override
protected void onEnable() {}
@Override
protected void onDisable() {}
});
}
private void monitorTextFieldChanges(TextField textField, Runnable onChange) {
VersionedReference<DocumentModel> ref = textField.getDocumentModel().createReference();
app.getStateManager().attach(new BaseAppState() {
@Override
public void update(float tpf) {
if (ref.update()) {
onChange.run();
}
}
@Override
protected void initialize(Application app) {}
@Override
protected void cleanup(Application app) {}
@Override
protected void onEnable() {}
@Override
protected void onDisable() {}
});
}
}

View File

@ -61,6 +61,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
private BoardManager boardManager = new BoardManager();
/**
* Constructs a ClientGameLogic with the specified sender object.
*
@ -290,7 +291,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(ViewAssetsResponse msg) {
boardManager = msg.getboard();
}
/**
@ -316,9 +317,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(TradeRequest msg) {
// playSound(Sound.TRADE_REQUEST); no sound effect
// notifyListeners();
}
/**

View File

@ -41,7 +41,7 @@ public class Player implements FieldVisitor<Void>{
private String name;
private int accountBalance = 15000;
private Figure figure;
private transient List<PropertyField> properties = new ArrayList<>();
private List<Integer> properties = new ArrayList<>();
private int getOutOfJailCard;
private int fieldID;
private DiceResult rollResult;
@ -193,7 +193,7 @@ public class Player implements FieldVisitor<Void>{
* Gets all the properties owned by this player
* @return List of all properties owned by this player
*/
public List<PropertyField> getProperties() {
public List<Integer> getProperties() {
return properties;
}
@ -204,7 +204,7 @@ public class Player implements FieldVisitor<Void>{
*/
public void buyProperty(PropertyField property) {
if (property.getOwner() == null && accountBalance >= property.getPrice()) {
properties.add(property);
properties.add(property.getId());
property.setOwner(this);
pay(property.getPrice());
}
@ -383,6 +383,16 @@ public class Player implements FieldVisitor<Void>{
return null;
}
public List<PropertyField> getPropertyFields() {
List<PropertyField> properties = new ArrayList<>();
for (Integer i : this.properties) {
properties.add((PropertyField)getHandler().getLogic().getBoardManager().getFieldAtIndex(i));
}
return properties;
}
/**
* Return the number of Properties of the speciefied fild type
* @param field the type of field to search for
@ -391,7 +401,8 @@ public class Player implements FieldVisitor<Void>{
public int getNumProp(PropertyField field) {
int count = 0;
if (properties.isEmpty()) return 0;
for (PropertyField propertyField : properties) {
for (PropertyField propertyField : getPropertyFields()) {
if (propertyField.getClass() == field.getClass()) {
count++;
}
@ -402,7 +413,7 @@ public class Player implements FieldVisitor<Void>{
public int getNumHouses() {
int total = 0;
for (PropertyField field : properties) {
for (PropertyField field : getPropertyFields()) {
if (field.getClass() == BuildingProperty.class) {
total += ((BuildingProperty) field).getHouses();
}
@ -412,7 +423,7 @@ public class Player implements FieldVisitor<Void>{
public int getNumHotels() {
int total = 0;
for (PropertyField field : properties) {
for (PropertyField field : getPropertyFields()) {
if (field.getClass() == BuildingProperty.class) {
total += ((BuildingProperty) field).getHotel();
}

View File

@ -19,7 +19,6 @@ import pp.monopoly.message.server.ServerMessage;
import pp.monopoly.message.server.TradeReply;
import pp.monopoly.message.server.TradeRequest;
import pp.monopoly.message.server.ViewAssetsResponse;
import pp.monopoly.model.Board;
import pp.monopoly.model.Figure;
import pp.monopoly.model.Rotation;
import pp.monopoly.model.card.DeckHelper;
@ -264,11 +263,12 @@ public class ServerGameLogic implements ClientInterpreter {
@Override
public void received(ViewAssetsRequest msg, int from) {
Player sender = playerHandler.getPlayerById(from);
Player player = msg.getPlayer();
if (sender != null && player != null) {
LOGGER.log(Level.DEBUG, "Processing ViewAssetsRequest for player {0}", sender.getName());
send(sender, new ViewAssetsResponse(boardManager, player.getProperties(), player.getAccountBalance(), player.getNumJailCard()));
if (sender != null) {
LOGGER.log(Level.DEBUG, "Processing ViewAssetsRequest for player {0}", sender.getName());
send(sender, new ViewAssetsResponse(boardManager));
for (Player player : playerHandler.getPlayers()) {
send(player, new PlayerStatusUpdate(playerHandler));
}
}
}

View File

@ -2,23 +2,13 @@ package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.Player;
/**
* Represents a request from a player to view their assets.
*/
@Serializable
public class ViewAssetsRequest extends ClientMessage{
private Player player;
/**
* Default constructor for serialization purposes.
*/
private ViewAssetsRequest() { /* empty */ }
public ViewAssetsRequest(Player player) {
this.player = player;
public ViewAssetsRequest() {
}
@Override
@ -26,8 +16,4 @@ public class ViewAssetsRequest extends ClientMessage{
interpreter.received(this, from);
}
public Player getPlayer() {
return player;
}
}

View File

@ -1,11 +1,9 @@
package pp.monopoly.message.server;
import java.util.List;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.PropertyField;
/**
* Represents a response containing the player's assets.
@ -13,10 +11,7 @@ import pp.monopoly.model.fields.PropertyField;
@Serializable
public class ViewAssetsResponse extends ServerMessage{
private List<PropertyField> properties;
private BoardManager board;
private int accountBalance;
private int jailCards;
/**
* Default constructor for serialization purposes.
@ -29,11 +24,8 @@ public class ViewAssetsResponse extends ServerMessage{
* @param properties a List of PropertyField objects representing the player's properties
* @param accountBalance the player's current account balance
*/
public ViewAssetsResponse(BoardManager board, List<PropertyField> properties, int accountBalance, int jailCards) {
public ViewAssetsResponse(BoardManager board) {
this.board = board;
this.properties = properties;
this.accountBalance = accountBalance;
this.jailCards = jailCards;
}
@Override
@ -47,18 +39,6 @@ public class ViewAssetsResponse extends ServerMessage{
throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'");
}
public List<PropertyField> getProperties() {
return properties;
}
public int getAccountBalance() {
return accountBalance;
}
public int getJailCards() {
return jailCards;
}
public BoardManager getboard() {
return board;
}

View File

@ -4,9 +4,12 @@ import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import com.jme3.network.serializing.Serializable;
/**
* Simple Manager class responsible for managing the GameBoard of Monopoly
*/
@Serializable
public class BoardManager {
private List<Field> board;
@ -92,4 +95,12 @@ public class BoardManager {
public List<Field> getBoard() {
return board;
}
public List<PropertyField> getPropertyFields(List<Integer> source) {
List<PropertyField> properties = new ArrayList<>();
for (Integer i : source) {
properties.add((PropertyField)getFieldAtIndex(i));
}
return properties;
}
}

View File

@ -44,9 +44,22 @@ import pp.monopoly.message.server.GameStart;
import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.PlayerStatusUpdate;
import pp.monopoly.message.server.ServerMessage;
import pp.monopoly.message.server.ViewAssetsResponse;
import pp.monopoly.model.Figure;
import pp.monopoly.model.IntPoint;
import pp.monopoly.model.LimitedLinkedList;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.model.fields.EventField;
import pp.monopoly.model.fields.Field;
import pp.monopoly.model.fields.FineField;
import pp.monopoly.model.fields.FoodField;
import pp.monopoly.model.fields.GateField;
import pp.monopoly.model.fields.GoField;
import pp.monopoly.model.fields.GulagField;
import pp.monopoly.model.fields.PropertyField;
import pp.monopoly.model.fields.TestStreckeField;
import pp.monopoly.model.fields.WacheField;
/**
* Server implementing the visitor pattern as MessageReceiver for ClientMessages
@ -129,6 +142,7 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
Serializer.registerClass(TradeOffer.class);
Serializer.registerClass(TradeResponse.class);
Serializer.registerClass(ViewAssetsRequest.class);
Serializer.registerClass(ViewAssetsResponse.class);
Serializer.registerClass(GameStart.class);
Serializer.registerClass(LimitedLinkedList.class);
Serializer.registerClass(NextPlayerTurn.class);
@ -138,6 +152,18 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
Serializer.registerClass(DiceResult.class);
Serializer.registerClass(EventDrawCard.class);
Serializer.registerClass(PlayerStatusUpdate.class);
Serializer.registerClass(BoardManager.class);
Serializer.registerClass(Field.class);
Serializer.registerClass(PropertyField.class);
Serializer.registerClass(BuildingProperty.class);
Serializer.registerClass(FoodField.class);
Serializer.registerClass(GateField.class);
Serializer.registerClass(WacheField.class);
Serializer.registerClass(GoField.class);
Serializer.registerClass(TestStreckeField.class);
Serializer.registerClass(EventField.class);
Serializer.registerClass(GulagField.class);
Serializer.registerClass(FineField.class);
}
private void registerListeners() {