Skip to content

Commit

Permalink
- added color to change text color according to colorId
Browse files Browse the repository at this point in the history
- code refactoring around lock handling
  • Loading branch information
NLucas0 committed Aug 2, 2023
1 parent 88f48ff commit 373e840
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 92 deletions.
136 changes: 83 additions & 53 deletions src/main/java/com/wordhunter/client/logic/ClientListening.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wordhunter.client.logic;

import com.wordhunter.client.ui.SceneController;
import com.wordhunter.client.ui.ServerPageController;
import com.wordhunter.client.ui.WordHunterController;
import com.wordhunter.conversion.PlayerConversion;
import com.wordhunter.conversion.WordConversion;
Expand Down Expand Up @@ -188,30 +187,27 @@ public void heartBeatAck(String input) {
}

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

public void endGameScreen(String input) {
Platform.runLater(() -> {
SceneController.getInstance().closeStage();
});
Platform.runLater(() -> SceneController.getInstance().closeStage());
}

/**
* Add or replace with a new Word object given the word index extracted from the broadcast message.
* @param input message from server
*/
public void processNewWord(String input) {
// get lock
if (!getWordsListLock())
{
return;
}

String[] tokenList = input.split(ServerMain.messageDelimiter);
Word newWord = WordConversion.toWord(tokenList[1]);

try {
ClientMain.clientWordsListLock.acquire();
} catch (InterruptedException e) {
System.out.println("unable to acquire the lock for client's wordsList");
}
ClientMain.wordsList.add(newWord);

if (wordHunterController != null) {
Expand All @@ -223,76 +219,110 @@ public void processNewWord(String input) {
ClientMain.clientWordsListLock.release();
}

/**
* getWordsListLock()
* try to get clientWordsListLock
* @return true if success, false if fail
*/
public boolean getWordsListLock()
{
try
{
ClientMain.clientWordsListLock.acquire();
return true;
}
catch (InterruptedException e)
{
System.out.println("unable to acquire the lock for client's wordsList");
return false;
}
}

public void handleCompletedWord(String input) {
// get lock
if (!getWordsListLock())
{
return;
}

String[] tokenList = input.split(ServerMain.messageDelimiter);
Word removedWord = WordConversion.toWord(tokenList[1]);

try {
ClientMain.clientWordsListLock.acquire();
} catch (InterruptedException e) {
System.out.println("unable to acquire the lock for client's wordsList");
}
ClientMain.wordsList.remove(removedWord);

Platform.runLater(() -> {
wordHunterController.clearWordPaneText(removedWord);
wordHunterController.stopAnimation(removedWord);
if (wordHunterController != null) {
wordHunterController.clearWordPaneText(removedWord);
wordHunterController.stopAnimation(removedWord);
}
});

ClientMain.clientWordsListLock.release();
}

public void handleReserveWord(String input) {
// get lock
if (!getWordsListLock())
{
return;
}

// get reserved word
String[] tokenList = input.split(ServerMain.messageDelimiter);
Word reservedWord = WordConversion.toWord(tokenList[1]);

try {
ClientMain.clientWordsListLock.acquire();
} catch (InterruptedException e) {
System.out.println("unable to acquire the lock for client's wordsList");
}
for (Word word : ClientMain.wordsList) {
if (reservedWord.equals(word)) {
word.setState(WordState.RESERVED);
word.setColor(reservedWord.getColor());
// if word in list
int index = ClientMain.wordsList.indexOf(reservedWord);
if(index != -1 )
{
Word word = ClientMain.wordsList.get(index);
word.setState(WordState.RESERVED);
word.setColor(reservedWord.getColor());

// set to player color
Platform.runLater(() -> {
if (wordHunterController != null) {
wordHunterController.setWordPaneTextColor(word);
wordHunterController.startAnimation(word);
}
});

// is current player
if (reservedWord.getColor().equals(ClientMain.colorId))
{
Platform.runLater(() -> {
wordHunterController.setWordPaneTextColor(reservedWord);
wordHunterController.startAnimation(reservedWord);
if (wordHunterController != null) {
wordHunterController.reservedWord = word;
}
});

if (reservedWord.getColor().equals(ClientMain.colorId)) {
Platform.runLater(() -> {
wordHunterController.reservedWord = reservedWord;
});
}
break;
}
}
ClientMain.clientWordsListLock.release();
}

public void handleReopenWord(String input) {
// get lock
if (!getWordsListLock())
{
return;
}

String[] tokenList = input.split(ServerMain.messageDelimiter);
Word reopenWord = WordConversion.toWord(tokenList[1]);

try {
ClientMain.clientWordsListLock.acquire();
} catch (InterruptedException e) {
System.out.println("unable to acquire the lock for client's wordsList");
}
for (Word word : ClientMain.wordsList) {
if (reopenWord.equals(word)) {
word.setState(WordState.OPEN);
word.setColor(reopenWord.getColor());

Platform.runLater(() -> {
wordHunterController.clearWordPaneColor(reopenWord);
wordHunterController.startAnimation(reopenWord);
});
int index = ClientMain.wordsList.indexOf(reopenWord);
if(index != -1 )
{
Word word = ClientMain.wordsList.get(index);
word.setState(WordState.OPEN);
word.setColor(reopenWord.getColor());

break;
}
Platform.runLater(() -> {
if (wordHunterController != null) {
wordHunterController.clearWordPaneColor(word);
wordHunterController.startAnimation(word);
}
});
}
ClientMain.clientWordsListLock.release();
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/wordhunter/client/logic/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public class ClientMain
public static Socket sock; // socket connected to server

// game variables
public String username; // temporary: entered by client later. move to player class?
public static String colorId = "#DC143C";
public String username;
public static String colorId = ""; // leave this empty
public static final Vector<Word> wordsList = new Vector<>();
public Vector<Player> players;

Expand All @@ -78,13 +78,11 @@ public class ClientMain
// Singleton
public static ClientMain getInstance() {
if (clientMainInstance == null) {
try {
try
{
clientMainInstance = new ClientMain();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
catch (IOException | InterruptedException ignored) {}
}
return clientMainInstance;
}
Expand Down
42 changes: 31 additions & 11 deletions src/main/java/com/wordhunter/client/ui/ServerPageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.util.Callback;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;


/**
* PlayerListCell
* used to display usernames with their assigned colors on waiting page
*/
class PlayerListCell extends Label
{
public PlayerListCell(Player player)
{
this.setText(player.getName());
this.setStyle("-fx-text-fill: " + player.getColor() + ";");
}
}

public class ServerPageController{
@FXML
public Label serverIPLabel;
Expand All @@ -26,12 +44,14 @@ public class ServerPageController{
public Label startTimer;

@FXML
public ListView<String> playerView;
private ObservableList<String> playerList = FXCollections.observableList(new ArrayList<>());
public ListView<PlayerListCell> playerView;
private ObservableList<PlayerListCell> playerViewList = FXCollections.observableList(new ArrayList<>());
private Vector<Player> playerList = new Vector<>();

@FXML
public void initialize(){
playerView.setItems(playerList);
public void initialize()
{
playerView.setItems(playerViewList);
ClientMain.getInstance().setServerPageController(this);
}

Expand Down Expand Up @@ -72,13 +92,13 @@ public void updateIPAddress(String address) {
}

public void updatePlayerList(Vector<Player> updatedPlayerList){
for (Player player : updatedPlayerList){
if (!playerList.contains(player.getName())){
Platform.runLater(() -> {
playerList.add(player.getName());
});
for (Player player : updatedPlayerList)
{
if (!playerList.contains(player))
{
playerList.add(player);
Platform.runLater(() -> playerViewList.add(new PlayerListCell(player)));
}

}
}
}
}
54 changes: 36 additions & 18 deletions src/main/java/com/wordhunter/server/PlayerThread.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wordhunter.server;

import com.wordhunter.client.logic.ClientMain;
import com.wordhunter.conversion.WordConversion;
import com.wordhunter.models.Word;
import com.wordhunter.models.WordGenerator;
Expand Down Expand Up @@ -147,15 +146,32 @@ public void handleHeartBeat(String input) {
ServerMain.sendMessageToClient(sock, "");
}

public void handleCompletedWord(String input) {
String[] tokenList = input.split(ServerMain.messageDelimiter);
Word target = WordConversion.toWord(tokenList[1]);

try {
public boolean getWordsListLock()
{
try
{
ServerMain.wordsListLock.acquire();
} catch (InterruptedException e) {
return true;
}
catch (InterruptedException e)
{
System.out.println("unable to lock the words list access");
return false;
}
}

public void handleCompletedWord(String input)
{
// get lock
if(!getWordsListLock())
{
return;
}

String[] tokenList = input.split(ServerMain.messageDelimiter);
Word target = WordConversion.toWord(tokenList[1]);


ServerMain.wordsList.remove(target);
System.out.println("Size of wordlist " + ServerMain.wordsList.size());

Expand All @@ -168,18 +184,20 @@ public void handleCompletedWord(String input) {
Word newWord = WordGenerator.generateNewWord();
ServerMain.wordsList.add(newWord);
ServerMain.broadcast("addNewWord" + ServerMain.messageDelimiter + WordConversion.fromWord(newWord));

ServerMain.wordsListLock.release();
}

public void handleReserveWord(String input) {
// get lock
if(!getWordsListLock())
{
return;
}

String[] tokenList = input.split(ServerMain.messageDelimiter);
Word target = WordConversion.toWord(tokenList[1]);

try {
ServerMain.wordsListLock.acquire();
} catch (InterruptedException e) {
System.out.println("unable to lock the words list access");
}
for (Word word : ServerMain.wordsList) {
if (target.equals(word) && word.getState() == WordState.OPEN) {
word.setState(WordState.RESERVED);
Expand All @@ -192,14 +210,15 @@ public void handleReserveWord(String input) {
}

public void handleReopenWord(String input) {
// get lock
if(!getWordsListLock())
{
return;
}

String[] tokenList = input.split(ServerMain.messageDelimiter);
Word target = WordConversion.toWord(tokenList[1]);

try {
ServerMain.wordsListLock.acquire();
} catch (InterruptedException e) {
System.out.println("unable to lock the words list access");
}
for (Word word : ServerMain.wordsList) {
if (target.equals(word) && word.getState() == WordState.RESERVED) {
word.setState(WordState.OPEN);
Expand All @@ -210,5 +229,4 @@ public void handleReopenWord(String input) {
}
ServerMain.wordsListLock.release();
}

}
Loading

0 comments on commit 373e840

Please sign in to comment.