mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
add: server chat box
This commit is contained in:
@@ -116,8 +116,8 @@ public final class Server {
|
|||||||
information.players[1].name = opponent;
|
information.players[1].name = opponent;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TICTACTOE: new TicTacToeGame(information, myTurn, this::forfeitGame, this::exitGame); break;
|
case TICTACTOE: new TicTacToeGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage); break;
|
||||||
case REVERSI: new ReversiGame(information, myTurn, this::forfeitGame, this::exitGame); break;
|
case REVERSI: new ReversiGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).postEvent();
|
}).postEvent();
|
||||||
@@ -159,14 +159,18 @@ public final class Server {
|
|||||||
information.players[1].name = e.opponent();
|
information.players[1].name = e.opponent();
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TICTACTOE: new TicTacToeGame(information, myTurn, this::forfeitGame, this::exitGame); break;
|
case TICTACTOE: new TicTacToeGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage); break;
|
||||||
case REVERSI: new ReversiGame(information, myTurn, this::forfeitGame, this::exitGame); break;
|
case REVERSI: new ReversiGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendMessage(String message) {
|
||||||
|
new EventFlow().addPostEvent(new NetworkEvents.SendMessage(clientId, message)).postEvent();
|
||||||
|
}
|
||||||
|
|
||||||
private void disconnect() {
|
private void disconnect() {
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
||||||
ViewStack.push(new OnlineView());
|
ViewStack.push(new OnlineView());
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import javafx.scene.paint.Color;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class ReversiCanvas extends GameCanvas{
|
public final class ReversiCanvas extends GameCanvas {
|
||||||
public ReversiCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
|
public ReversiCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
|
||||||
super(color, width, height, 8, 8, 10, true, onCellClicked);
|
super(color, width, height, 8, 8, 10, true, onCellClicked);
|
||||||
drawStartingDots();
|
drawStartingDots();
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import javafx.scene.paint.Color;
|
|||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public final class ReversiGame {
|
public final class ReversiGame {
|
||||||
private final GameInformation information;
|
private final GameInformation information;
|
||||||
@@ -30,7 +31,7 @@ public final class ReversiGame {
|
|||||||
private final GameView view;
|
private final GameView view;
|
||||||
private final ReversiCanvas canvas;
|
private final ReversiCanvas canvas;
|
||||||
|
|
||||||
public ReversiGame(GameInformation information, int myTurn, Runnable onForfeit, Runnable onExit) {
|
public ReversiGame(GameInformation information, int myTurn, Runnable onForfeit, Runnable onExit, Consumer<String> onMessage) {
|
||||||
this.information = information;
|
this.information = information;
|
||||||
|
|
||||||
this.myTurn = myTurn;
|
this.myTurn = myTurn;
|
||||||
@@ -42,9 +43,9 @@ public final class ReversiGame {
|
|||||||
if (onForfeit == null || onExit == null) {
|
if (onForfeit == null || onExit == null) {
|
||||||
view = new GameView(null, () -> {
|
view = new GameView(null, () -> {
|
||||||
ViewStack.push(new LocalMultiplayerView(information));
|
ViewStack.push(new LocalMultiplayerView(information));
|
||||||
});
|
}, null);
|
||||||
} else {
|
} else {
|
||||||
view = new GameView(onForfeit, onExit);
|
view = new GameView(onForfeit, onExit, onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas = new ReversiCanvas(Color.GRAY,
|
canvas = new ReversiCanvas(Color.GRAY,
|
||||||
@@ -86,6 +87,10 @@ public final class ReversiGame {
|
|||||||
updateCanvas();
|
updateCanvas();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReversiGame(GameInformation information) {
|
||||||
|
this(information, 0, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
private void localGameThread() {
|
private void localGameThread() {
|
||||||
boolean isRunning = true;
|
boolean isRunning = true;
|
||||||
|
|
||||||
@@ -197,7 +202,7 @@ public final class ReversiGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onReceivedMessage(NetworkEvents.ReceivedMessage msg) {
|
private void onReceivedMessage(NetworkEvents.ReceivedMessage msg) {
|
||||||
view.updateChat("anon", msg.message());
|
view.updateChat(msg.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateCanvas() {
|
private void updateCanvas() {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import javafx.scene.paint.Color;
|
|||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public final class TicTacToeGame {
|
public final class TicTacToeGame {
|
||||||
private final GameInformation information;
|
private final GameInformation information;
|
||||||
@@ -30,7 +31,7 @@ public final class TicTacToeGame {
|
|||||||
private final GameView view;
|
private final GameView view;
|
||||||
private final TicTacToeCanvas canvas;
|
private final TicTacToeCanvas canvas;
|
||||||
|
|
||||||
public TicTacToeGame(GameInformation information, int myTurn, Runnable onForfeit, Runnable onExit) {
|
public TicTacToeGame(GameInformation information, int myTurn, Runnable onForfeit, Runnable onExit, Consumer<String> onMessage) {
|
||||||
this.information = information;
|
this.information = information;
|
||||||
|
|
||||||
this.myTurn = myTurn;
|
this.myTurn = myTurn;
|
||||||
@@ -42,9 +43,9 @@ public final class TicTacToeGame {
|
|||||||
if (onForfeit == null || onExit == null) {
|
if (onForfeit == null || onExit == null) {
|
||||||
view = new GameView(null, () -> {
|
view = new GameView(null, () -> {
|
||||||
ViewStack.push(new LocalMultiplayerView(information));
|
ViewStack.push(new LocalMultiplayerView(information));
|
||||||
});
|
}, null);
|
||||||
} else {
|
} else {
|
||||||
view = new GameView(onForfeit, onExit);
|
view = new GameView(onForfeit, onExit, onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas = new TicTacToeCanvas(Color.GRAY,
|
canvas = new TicTacToeCanvas(Color.GRAY,
|
||||||
@@ -84,6 +85,10 @@ public final class TicTacToeGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TicTacToeGame(GameInformation information) {
|
||||||
|
this(information, 0, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
private void localGameThread() {
|
private void localGameThread() {
|
||||||
boolean isRunning = true;
|
boolean isRunning = true;
|
||||||
|
|
||||||
@@ -205,7 +210,7 @@ public final class TicTacToeGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onReceivedMessage(NetworkEvents.ReceivedMessage msg) {
|
private void onReceivedMessage(NetworkEvents.ReceivedMessage msg) {
|
||||||
view.updateChat("anon", msg.message());
|
view.updateChat(msg.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setGameLabels(boolean isMe) {
|
private void setGameLabels(boolean isMe) {
|
||||||
|
|||||||
@@ -6,8 +6,12 @@ import org.toop.local.AppContext;
|
|||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.ListView;
|
||||||
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public final class GameView extends View {
|
public final class GameView extends View {
|
||||||
private static class GameOverView extends View {
|
private static class GameOverView extends View {
|
||||||
private final boolean iWon;
|
private final boolean iWon;
|
||||||
@@ -65,7 +69,10 @@ public final class GameView extends View {
|
|||||||
|
|
||||||
private final Text nextPlayerHeader;
|
private final Text nextPlayerHeader;
|
||||||
|
|
||||||
public GameView(Runnable onForfeit, Runnable onExit) {
|
private final ListView<Text> chatListView;
|
||||||
|
private final TextField chatInput;
|
||||||
|
|
||||||
|
public GameView(Runnable onForfeit, Runnable onExit, Consumer<String> onMessage) {
|
||||||
assert onExit != null;
|
assert onExit != null;
|
||||||
|
|
||||||
super(true, "bg-primary");
|
super(true, "bg-primary");
|
||||||
@@ -78,6 +85,16 @@ public final class GameView extends View {
|
|||||||
forfeitButton = null;
|
forfeitButton = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (onMessage != null) {
|
||||||
|
chatListView = new ListView<Text>();
|
||||||
|
|
||||||
|
chatInput = input();
|
||||||
|
chatInput.setOnAction(_ -> onMessage.accept(chatInput.getText()));
|
||||||
|
} else {
|
||||||
|
chatListView = null;
|
||||||
|
chatInput = null;
|
||||||
|
}
|
||||||
|
|
||||||
exitButton = button();
|
exitButton = button();
|
||||||
exitButton.setText(AppContext.getString("exit"));
|
exitButton.setText(AppContext.getString("exit"));
|
||||||
exitButton.setOnAction(_ -> onExit.run());
|
exitButton.setOnAction(_ -> onExit.run());
|
||||||
@@ -110,6 +127,15 @@ public final class GameView extends View {
|
|||||||
exitButton
|
exitButton
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (chatListView != null) {
|
||||||
|
add(Pos.BOTTOM_RIGHT,
|
||||||
|
fit(vboxFill(
|
||||||
|
chatListView,
|
||||||
|
chatInput
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer) {
|
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer) {
|
||||||
@@ -125,8 +151,15 @@ public final class GameView extends View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateChat(String player, String message) {
|
public void updateChat(String message) {
|
||||||
// Todo
|
if (chatListView == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Text messageText = text();
|
||||||
|
messageText.setText(message);
|
||||||
|
|
||||||
|
chatListView.getItems().add(messageText);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gameOver(boolean iWon, String winner) {
|
public void gameOver(boolean iWon, String winner) {
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ public final class LocalMultiplayerView extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (information.type) {
|
switch (information.type) {
|
||||||
case TICTACTOE: new TicTacToeGame(information, 0, null, null); break;
|
case TICTACTOE: new TicTacToeGame(information); break;
|
||||||
case REVERSI: new ReversiGame(information, 0, null, null); break;
|
case REVERSI: new ReversiGame(information); break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user