Skip to content

Commit

Permalink
lobby can now handle Invite messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bazola committed Oct 6, 2014
1 parent f565418 commit 5342deb
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import com.cardshifter.api.CardshifterConstants;
import com.cardshifter.api.both.ChatMessage;
import com.cardshifter.api.both.InviteRequest;
import com.cardshifter.api.both.InviteResponse;
import com.cardshifter.api.incoming.LoginMessage;
import com.cardshifter.api.incoming.ServerQueryMessage;
import com.cardshifter.api.incoming.ServerQueryMessage.Request;
Expand All @@ -49,6 +51,7 @@ public class GameClientLobby implements Initializable {
@FXML private TextField messageBox;
@FXML private Button sendMessageButton;
@FXML private AnchorPane inviteButton;
@FXML private AnchorPane inviteWindow;

private final ObjectMapper mapper = new ObjectMapper();
private final Set<GameClientController> gamesRunning = new HashSet<>();
Expand All @@ -62,6 +65,7 @@ public class GameClientLobby implements Initializable {
private Thread listenThread;
private final Map<String, Integer> usersOnlineList = new HashMap<>();
private String userForGameInvite;
private InviteRequest currentGameRequest;

public void acceptConnectionSettings(String ipAddress, int port, String userName) {
// this is passed into this object after it is automatically created by the FXML document
Expand Down Expand Up @@ -140,20 +144,18 @@ private void processMessageFromServer(Message message) {
}

if (message instanceof NewGameMessage) {
NewGameMessage msg = (NewGameMessage) message;
startNewGame(msg);
}

if (message instanceof UserStatusMessage) {
this.startNewGame((NewGameMessage)message);
} else if (message instanceof UserStatusMessage) {
this.processUserStatusMessage((UserStatusMessage)message);
}
if (message instanceof ChatMessage) {
} else if (message instanceof ChatMessage) {
ChatMessage msg = (ChatMessage) message;
chatOutput(msg.getFrom() + ": " + msg.getMessage());
}
if (message instanceof ServerErrorMessage) {
this.chatOutput(msg.getFrom() + ": " + msg.getMessage());
} else if (message instanceof ServerErrorMessage) {
ServerErrorMessage msg = (ServerErrorMessage) message;
chatOutput("SERVER ERROR: " + msg.getMessage());
this.chatOutput("SERVER ERROR: " + msg.getMessage());
} else if (message instanceof InviteRequest) {
this.currentGameRequest = (InviteRequest)message;
this.createInviteWindow((InviteRequest)message);
}
}

Expand Down Expand Up @@ -181,10 +183,6 @@ private void startNewGame(NewGameMessage message) {
}
}

private void chatOutput(String string) {
Platform.runLater(() -> this.chatMessages.getItems().add(string));

}
private void processUserStatusMessage(UserStatusMessage message) {
if (message.getStatus() == Status.ONLINE) {
this.usersOnlineList.put(message.getName(), message.getUserId());
Expand All @@ -196,6 +194,31 @@ private void processUserStatusMessage(UserStatusMessage message) {
this.usersOnline.getItems().addAll(this.usersOnlineList.keySet());
}


private void chatOutput(String string) {
Platform.runLater(() -> this.chatMessages.getItems().add(string));
}

private void createInviteWindow(InviteRequest message) {
this.inviteWindow.setVisible(true);
this.inviteWindow.getChildren().add(new InviteWindow(message, this).getRootPane());
}

public void acceptGameRequest(MouseEvent event) {
this.send(new InviteResponse(this.currentGameRequest.getId(), true));
this.closeInviteWindow();
}

public void declineGameRequest(MouseEvent event) {
this.send(new InviteResponse(this.currentGameRequest.getId(), false));
this.closeInviteWindow();
}

private void closeInviteWindow() {
this.inviteWindow.getChildren().clear();
this.inviteWindow.setVisible(false);
}

private void selectUserForGameInvite(MouseEvent event) {
String selected = this.usersOnline.getSelectionModel().getSelectedItem();
if (selected != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.cardshifter.client;

import com.cardshifter.api.both.InviteRequest;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

public class InviteWindow {

@FXML private AnchorPane rootPane;
@FXML private Label nameLabel;
@FXML private AnchorPane noButton;
@FXML private AnchorPane yesButton;

private final InviteRequest message;
private final GameClientLobby lobby;

public InviteWindow(InviteRequest message, GameClientLobby lobby) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("InviteWindowDocument.fxml"));
loader.setController(this);
loader.load();
} catch (Exception e) {
throw new RuntimeException(e);
}

this.message = message;
this.lobby = lobby;

this.nameLabel.setText(message.getName());
this.noButton.setOnMouseClicked(lobby::acceptGameRequest);
this.yesButton.setOnMouseClicked(lobby::declineGameRequest);
}

public AnchorPane getRootPane() {
return this.rootPane;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane fx:id="rootPane" prefHeight="240.0" prefWidth="400.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#0055a4" height="240.0" stroke="BLACK" strokeType="INSIDE" width="400.0" />
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#b1bdff" height="120.0" layoutX="15.0" layoutY="14.0" stroke="BLACK" strokeType="INSIDE" width="370.0" />
<Label layoutX="28.0" layoutY="22.0" text="Accept invite from">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
<Label fx:id="nameLabel" layoutX="166.0" layoutY="84.0" text="name" textAlignment="CENTER">
<font>
<Font name="System Bold" size="24.0" />
</font>
</Label>
<AnchorPane fx:id="yesButton" layoutX="15.0" layoutY="152.0" prefHeight="73.0" prefWidth="151.0">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#2bab35" height="73.0" stroke="BLACK" strokeType="INSIDE" width="151.0" />
<Label layoutX="42.0" layoutY="15.0" text="Yes">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane fx:id="noButton" layoutX="234.0" layoutY="152.0" prefHeight="73.0" prefWidth="151.0">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#a4300d" height="73.0" stroke="BLACK" strokeType="INSIDE" width="151.0" />
<Label layoutX="50.0" layoutY="15.0" text="No">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</AnchorPane>
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
</Label>
</children>
</AnchorPane>
<AnchorPane fx:id="inviteWindow" prefHeight="275.0" prefWidth="400.0" visible="false" AnchorPane.bottomAnchor="100.0" AnchorPane.leftAnchor="200.0" AnchorPane.rightAnchor="200.0" AnchorPane.topAnchor="100.0" />
</children>
</AnchorPane>

0 comments on commit 5342deb

Please sign in to comment.