Skip to content

Commit

Permalink
Merge branch 'style_test' into 'master'
Browse files Browse the repository at this point in the history
- fixed some locks that weren't being released

See merge request nka73/cmpt371_project_ui!8
  • Loading branch information
nal9 committed Aug 5, 2023
2 parents f65f91a + fb10211 commit 835499a
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 154 deletions.
63 changes: 36 additions & 27 deletions src/main/java/com/wordhunter/client/logic/ClientListening.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ClientListening extends Thread {

private WordHunterController wordHunterController;
private boolean heartBeatSent = false;
private boolean gameOver = false;

/**
* ClientListening()
Expand All @@ -56,8 +57,6 @@ public ClientListening(Socket aSock, ClientMain aParent) {
messageToCallback.put("removeWord", ClientListening::handleCompletedWord);
messageToCallback.put("reserveWord", ClientListening::handleReserveWord);
messageToCallback.put("reopenWord", ClientListening::handleReopenWord);

SceneController.getInstance().toggleReconnectionOverlay(false); // TODO: move to reconnect handle when implemented
}

/**
Expand Down Expand Up @@ -88,12 +87,11 @@ public void run() {
handleServerMessage(input);
}
}
} catch (IOException e) // disconnect throws SocketException
} catch (IOException e) // disconnect
{
try {
if(!heartBeatSent)
{
System.out.println("sending heartbeat"); // TODO: REMOVE
heartBeatSent = true;
ClientMain.sendMsgToServer("");
}
Expand All @@ -104,26 +102,16 @@ public void run() {
System.exit(0);
}
} catch (IOException ex) {
System.out.println("server down");
System.exit(0);
if(!gameOver) {
System.out.println("server down");
System.exit(0);
}
return;
}
}
}
}

/**
* disconnect()
* disconnect with server and attempt reconnection if max retries not reached
*/
public void disconnect() throws IOException {
sock.close();
if (ClientMain.reconnectAttempts < ClientMain.reconnectMaxAttempt) {
SceneController.getInstance().toggleReconnectionOverlay(true);
System.out.println("reconnection attempt " + ClientMain.reconnectAttempts);
parent.connectServer(true);
}
}

/**
* handleServerMessage()
* call function depending on first keyword
Expand Down Expand Up @@ -155,6 +143,9 @@ public void newPlayerJoin(String input) {
// get own color id
if (parent.colorId.isEmpty()) {
parent.colorId = players.elementAt(players.size() - 1).getColor();
Platform.runLater(() -> {
SceneController.getInstance().setMainColor(parent.colorId);
});
}

Platform.runLater(() -> {
Expand Down Expand Up @@ -190,31 +181,50 @@ public void error(String input) {
}

public void displayGameScreen(String input) {
Platform.runLater(() -> this.wordHunterController = SceneController.getInstance().showGamePage());
Platform.runLater(() -> {
this.wordHunterController = SceneController.getInstance().showGamePage();
SceneController.getInstance().setMainColor(parent.colorId);
});
}

public void endGameScreen(String input) {
/**
* endGameScreen()
* close socket, display winner and everyones scores, exit after x seconds
* @param input playerlist
*/
public void endGameScreen(String input)
{
// close socket and set flag to prevent immediate exit
gameOver = true;
try {
sock.close();
}
catch (IOException ignored) { }

// get players + scores
String[] tokenList = input.split(ServerMain.messageDelimiter);
Vector<Player> players = PlayerConversion.toPlayers(tokenList[1]);

// show winner page
Platform.runLater(() -> {
SceneController.getInstance().showWinnerPage();
SceneController.getInstance().setMainColor(parent.colorId);
});

//TODO: have an end screen and put the winner here
// find and display the winner's username
Player winner = findWinner(players);
System.out.println("Winner is " + winner.getName() + " with score " + winner.getScore());
Platform.runLater(() -> {
parent.getWinnerPageController().updateScoreList(players);
parent.getWinnerPageController().updateWinner(winner);
});

// close program after 30s
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.sleep(30000);
}
Platform.runLater(() -> SceneController.getInstance().closeStage());
catch (InterruptedException ignored) { }

System.exit(0);
}

Expand All @@ -241,7 +251,6 @@ public void processNewWord(String input) {
String[] tokenList = input.split(ServerMain.messageDelimiter);
Word newWord = WordConversion.toWord(tokenList[1]);

// ClientMain.wordsList.add(newWord);
ClientMain.wordsList.set(newWord.getWordID(), newWord);
if (wordHunterController != null) {
Platform.runLater(() -> {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/wordhunter/client/logic/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.wordhunter.client.ui.WinnerPageController;
import com.wordhunter.models.Word;
import com.wordhunter.models.Player;
import com.wordhunter.models.WordGenerator;
import com.wordhunter.models.WordList;
import com.wordhunter.server.ServerMain;
import com.wordhunter.server.ServerState;
Expand Down Expand Up @@ -94,7 +95,7 @@ public static ClientMain getInstance() {
* entry point -> choose start server or join as client
*/
public ClientMain() throws IOException, InterruptedException {
wordsList = new WordList(25);
wordsList = new WordList(WordGenerator.dimension);
}

public void setAddress(String address) {
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/com/wordhunter/client/ui/SceneController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wordhunter.client.ui;

import com.wordhunter.client.logic.ClientMain;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
Expand All @@ -15,6 +16,7 @@ public class SceneController {
// components
private Stage stage;
private Scene scene;
public Parent root;
private ReconnectionOverlay reconnectionOverlay;

// device screen size
Expand Down Expand Up @@ -48,7 +50,7 @@ private SceneController()
}

public void showStartPage() {
Parent root = null;
root = null;
try {
root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("StartPage.fxml")));
} catch (IOException e) {
Expand All @@ -62,8 +64,25 @@ public void showStartPage() {
stage.show();
}

public void setMainColor(String color)
{
// get lighter version
String newHex = color;
if(color.length() >= 7 && color.startsWith("#")) {
// make lighter by increasing value by 55
int r = Integer.parseInt(color.substring(1, 3), 16) + 55;
int g = Integer.parseInt(color.substring(3, 5), 16) + 55;
int b = Integer.parseInt(color.substring(5, 7), 16) + 55;
newHex = "#"
+ Integer.toHexString(r)
+ Integer.toHexString(g)
+ Integer.toHexString(b);
}
root.setStyle("-bg-main: " + newHex + ";-bg-main-dark: " + color + ";");
}

public void showWaitingPage() {
Parent root = null;
root = null;
try {
root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("ServerPage.fxml")));
} catch (IOException e) {
Expand All @@ -79,7 +98,7 @@ public void showWaitingPage() {
public WordHunterController showGamePage() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("WordHunter.fxml"));

Parent root = null;
root = null;
try {
root = fxmlLoader.load();
} catch (IOException e) {
Expand All @@ -97,7 +116,7 @@ public WordHunterController showGamePage() {
}

public void showWinnerPage() {
Parent root = null;
root = null;
try {
root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("WinnerPage.fxml")));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PlayerListCell extends Label
public PlayerListCell(Player player)
{
this.setText(player.getName());
this.setStyle("-fx-text-fill: " + player.getColor() + ";");
this.setStyle("-fx-background-radius: 10;-fx-text-fill: " + player.getColor() + ";");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class WinnerPageController {
@FXML
public ListView<ScoreListCell> scoreView;
private ObservableList<ScoreListCell> scoreViewList = FXCollections.observableList(new ArrayList<>());
private Vector<Player> playerList = new Vector<>();

@FXML
public void initialize() {
Expand All @@ -49,5 +48,6 @@ public void updateScoreList(Vector<Player> playersList) {

public void updateWinner(Player winner) {
winnerText.setText(winner.getName());
winnerText.setStyle("-fx-text-fill: " + winner.getColor());
}
}
10 changes: 2 additions & 8 deletions src/main/java/com/wordhunter/client/ui/WordHunterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@
import com.wordhunter.client.logic.ClientMain;
import com.wordhunter.conversion.WordConversion;
import com.wordhunter.models.Word;
import com.wordhunter.models.WordGenerator;
import com.wordhunter.models.WordList;
import com.wordhunter.models.WordState;
import com.wordhunter.server.ServerMain;
import javafx.animation.Transition;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Vector;

public class WordHunterController {
public ProgressBar healthBarBar;
@FXML
private GridPane grids;

Expand All @@ -43,7 +37,7 @@ public class WordHunterController {

public WordHunterController()
{
wordsList = new WordList(25);
wordsList = new WordList(WordGenerator.dimension);
}

public static WordHunterController getInstance() {
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/wordhunter/client/ui/WordPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class WordPane extends StackPane {
// Constant
// Word font
private static final Font FONT = new Font("Arial", 20);
public static final String BORDER = "-fx-border-color: #000000; -fx-border-width: 2px;";
public static final String BORDER = "-fx-border-color: -border-color; -fx-border-width: 2;";
public static final Color DEFAULT_COLOR = Color.WHITE;

// Member variables
Expand Down Expand Up @@ -62,11 +62,14 @@ public void initAnimation(long interval){
if (animation != null) {
closeAnimation();
}
animation = new FadeTransition(Duration.millis(interval), this.label);
animation.setFromValue(1);
animation.setToValue(0);
animation.setCycleCount(1);
animation.play();
if (interval > 0) {
animation = new FadeTransition(Duration.millis(interval), this.label);
double currentOpacity = this.label.getOpacity();
animation.setFromValue(currentOpacity);
animation.setToValue(0);
animation.setCycleCount(1);
animation.play();
}
}

public void closeAnimation() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/wordhunter/models/WordGenerator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wordhunter.models;

import com.wordhunter.conversion.WordConversion;
import com.wordhunter.server.ServerMain;

import java.io.File;
Expand Down
Loading

0 comments on commit 835499a

Please sign in to comment.