Merge remote-tracking branch 'origin/master'

This commit is contained in:
Alexander Sazonov
2022-04-28 13:30:05 +02:00
367 changed files with 969 additions and 95 deletions

View File

@@ -60,4 +60,5 @@ public class ClientVoteData {
LOGGER.warn("Position is:" + position);
}
}
}

View File

@@ -92,13 +92,13 @@ public class GameState {
}
/**
* Collects the current position of all ghosts and saves them in an array
* Collects the current position of all not kicked off ghosts and saves them in an array
* @return Boolean array, true if there is a ghost at that position
*/
public boolean[] getPositionOfGhosts(){
boolean[] ghosts = new boolean[passengerTrain.length];
for(int i = 0; i < passengerTrain.length; i++) {
if(passengerTrain[i].getIsGhost()) {
if(passengerTrain[i].getIsGhost() && !passengerTrain[i].getKickedOff()) {
ghosts[i] = true;
}
}

View File

@@ -0,0 +1,75 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* A class that handles all timed events in the game, such as vote times
*/
public class Timer {
public static final Logger LOGGER = LogManager.getLogger(Timer.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/**
* The maximum length of the ghost vote in the night, in seconds
*/
public static final int ghostVote = 30;
/**
* The maximum length of the human vote in the day, in seconds
*/
public static final int humanVote = 60;
/**
* The checking intervall in seconds
*/
public static final int intervall = 1;
/**
* The timer for the ghost vote. Checks every {@code intervall} seconds if every ghost has already voted.
* If all have voted or if the {@code ghostVote} value is reached, the timer ends
* @param game the game this Timer has been called in
*/
public static void ghostVoteTimer(Game game) {
int counter = 0;
while(counter < ghostVote) {
if(haveAllGhostsVoted(game)) { //if all ghost have voted
return;
}
try {
Thread.sleep(intervall*1000);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
}
counter = counter + (intervall*1000);
}
}
/**
* Checks if all ghosts in the game have already voted, returns true if so
* @param game the Game the ghosts live in
* @return true if all Ghosts have voted and false if at least 1 didn't
*/
public static boolean haveAllGhostsVoted(Game game) {
int nrOfGhosts = 0;
int j = 0; //counter
boolean[] positionOfGhosts = game.gameState.getPositionOfGhosts();
boolean[] whoHasVoted = game.getGameState().getClientVoteData().getHasVoted();
for (boolean positionOfGhost : positionOfGhosts) { //determines how many ghosts are in the game
if (positionOfGhost) {
nrOfGhosts++;
}
}
for(int i = 0; i < positionOfGhosts.length; i++) {
if (positionOfGhosts[i]) {
if(whoHasVoted[i]) {
j++;
}
}
}
return nrOfGhosts == j;
}
}

View File

@@ -50,11 +50,7 @@ public class VoteHandler {
}
}
try { // waits 30 seconds before votes get collected
Thread.sleep(10*1000);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
}
Timer.ghostVoteTimer(game);
int currentMax = ghostVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game);

View File

@@ -71,6 +71,7 @@ public class GhostNPC extends Ghost {
int randomPosition = (int) (Math.random() * humanPositions.length);
vote = humanPositions[randomPosition];
hasVoted = true;
game.getGameState().getClientVoteData().setHasVoted(position,hasVoted);
LOGGER.info("GhostNPC at Position: " + this.getPosition() + " has voted for: " + vote);
}
}

View File

@@ -67,6 +67,7 @@ public class HumanNPC extends Human {
int randomNr = (int) (Math.random() * inGamePositions.length);
vote = inGamePositions[randomNr];
hasVoted = true;
game.getGameState().getClientVoteData().setHasVoted(position,hasVoted);
LOGGER.info("HumanNPC at Position: " + this.getPosition() + " has voted for: " + vote);
}
}

View File

@@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class ClientListViewController {
}

View File

@@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class LobbyListView {
}

View File

@@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class LobbySceneViewController {
}

View File

@@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class ServerMessageViewController {
}

View File

@@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import javafx.scene.control.Button;
public class ChangeNameButton extends Button {
}

View File

@@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import java.awt.Button;
public class JoinGameButton extends Button {
}

View File

@@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import javafx.scene.control.Button;
public class LeaveServerButton extends Button {
}

View File

@@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import javafx.scene.control.Button;
public class NewGameButton extends Button {
}

View File

@@ -84,7 +84,7 @@ public class ChatApp extends Application {
public void start(Stage primaryStage) throws Exception {
this.setcModel(clientModel);
URL resource = ChatApp.class.getResource(
"splitPaneChatView.fxml");
"ChatView.fxml");
if (resource == null) {
System.out.println("File wasnt found");
}
@@ -92,7 +92,7 @@ public class ChatApp extends Application {
try {
Parent root = FXMLLoader.load(
Objects.requireNonNull(ChatApp.class.getResource(
"splitPaneChatView.fxml")));
"ChatView.fxml")));
// TODO bin chatController.getChatPaneRoot() border to root border for rezising
Scene scene = new Scene(root);
scene.setRoot(root);

View File

@@ -4,6 +4,8 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import com.sun.javafx.scene.control.Properties;
import com.sun.javafx.scene.control.inputmap.KeyBinding;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
@@ -15,15 +17,22 @@ import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextFlow;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -33,7 +42,15 @@ public class ChatController implements Initializable {
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
@FXML
private SplitPane chatPaneRoot;
private Group vboxGroup;
@FXML
private GridPane vBoxGridPane;
@FXML
private ScrollPane ChatScrollPane;
@FXML
private VBox vBoxServerMessage;
@FXML
private Pane chatPaneRoot;
@FXML
private VBox vBoxChatMessages;
@FXML
@@ -41,7 +58,7 @@ public class ChatController implements Initializable {
@FXML
private TextField whisperTargetSelectField;
@FXML
private TextArea chatMsgField;
private TextField chatMsgField;
private static ClientModel client;
@@ -100,32 +117,23 @@ public class ChatController implements Initializable {
public void changed(ObservableValue<? extends Number> observable, Number oldValue,
Number newValue) {
vBoxChatMessages.setMaxHeight(newValue.doubleValue());
ChatScrollPane.setMaxHeight(newValue.doubleValue()*2);
}
});
/**
* Initialize what happens when the send button is pressed
*/
sendButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String msg = chatMsgField.getText().split("\\R")[0]; //cut off extra lines, if present.
if (!msg.isEmpty()) {
client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l;
if (cmd.startsWith(whisper)) {
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
}
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
} else {
LOGGER.debug("Trying to send an empty message.");
}
sendChatMsg();
}
});
chatMsgField.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
sendChatMsg();
}
});
@@ -155,6 +163,29 @@ public class ChatController implements Initializable {
});
}
private void sendChatMsg() {
String msg = chatMsgField.getText();//.split("\\R")[0]; //cut off extra lines, if present.
if (!msg.isEmpty()) {
client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l;
if (cmd.startsWith(whisper)) {
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
l.setScaleShape(true);
}
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
} else {
LOGGER.debug("Trying to send an empty message.");
}
}
/**
* @return the ClientModel whose chat controller this is
*/
@@ -171,7 +202,7 @@ public class ChatController implements Initializable {
this.client = client;
}
public SplitPane getChatPaneRoot() {
public Pane getChatPaneRoot() {
return chatPaneRoot;
}
@@ -182,6 +213,8 @@ public class ChatController implements Initializable {
*/
public void addChatMsgToView(String msg) {
Label l = new Label(msg);
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
if (msg.contains("whispers")) {
l.setBackground(Background.fill(Color.SLATEBLUE));
} else {

View File

@@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class ChangeNameButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class JoinGameButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class LeaveServerButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class NewGameButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@@ -0,0 +1,49 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextFlow;
public class GameController {
@FXML
private Group roomButtonGroupDay;
@FXML
private Button buttonRoom0;
@FXML
private Button buttonRoom1;
@FXML
private Button buttonRoom2;
@FXML
private Button buttonRoom3;
@FXML
private Button buttonRoom4;
@FXML
private Button buttonRoom5;
@FXML
private HBox roomLables;
@FXML
private TextField lableRoom0;
@FXML
private TextField lableRoom1;
@FXML
private TextField lableRoom2;
@FXML
private TextField lableRoom3;
@FXML
private TextField lableRoom4;
@FXML
private TextField lableRoom5;
@FXML
private Button noiseButton;
@FXML
private TextFlow notificationText;
}

View File

@@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
public class LoungeSceneViewController {
}