Skip to content

Commit

Permalink
Added nickname functionality to game screen
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanTrammell committed May 1, 2024
1 parent 66f1f8e commit b29f32c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
40 changes: 21 additions & 19 deletions html/entryScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ socket.onmessage = function(event) {
document.getElementById('lobby').style.display = "none";
document.getElementById('gameScreen').style.display = "block";
generateGrid(obj.wordBank.grid);
generateScoreboard(obj.scoreBoard.scores);
generateScoreboard(obj);
displayWords(obj.wordBank.wordsPlaced);
}
else
{
updateGrid(obj.pickedLetters);
updateScoreboard(obj.scoreBoard.scores);
updateScoreboard(obj);

if (obj.chatBox.messageSent == true) {
var messages = obj.chatBox.messages;
Expand Down Expand Up @@ -303,25 +303,27 @@ function generateGrid(grid) {
});
}

function generateScoreboard(scores) {
function generateScoreboard(obj) {
var scores = obj.scoreBoard.scores;
var scoreboard = document.getElementById('scoreBoard');
scoreboard.innerHTML = ''; // Clear previous scores
scores.forEach(function (score, index) {
var p = document.createElement('p');
p.textContent = "Player " + (index + 1) + ": 0";
scoreboard.appendChild(p);
});
}
scoreboard.innerHTML = ''; // Clear previous scores
scores.forEach(function (score, index) {
var p = document.createElement('p');
p.textContent = obj.playerNames[index] + ": 0";
scoreboard.appendChild(p);
});
}

function updateScoreboard(scores) {
var scoreboard = document.getElementById('scoreBoard');
scoreboard.innerHTML = ''; // Clear existing scores
scores.forEach(function (score, index) {
var p = document.createElement('p');
p.textContent = "Player " + (index + 1) + ": " + score;
scoreboard.appendChild(p);
});
}
function updateScoreboard(obj) {
var scores = obj.scoreBoard.scores;
var scoreboard = document.getElementById('scoreBoard');
scoreboard.innerHTML = ''; // Clear previous scores
scores.forEach(function (score, index) {
var p = document.createElement('p');
p.textContent = obj.playerNames[index] + ": " + score;
scoreboard.appendChild(p);
});
}

function displayWords(words) {
var listContainer = document.getElementById('wordList');
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,28 @@ public void onMessage(WebSocket conn, String message) {
// Add the first player
G.Players = uta.cse3310.PlayerType.PLAYER1;
G.playersJoined++;
G.playerNames[0] = nickname;
ActiveGames.add(G);
System.out.println(" creating a new Game");
} else if (G.Players == PlayerType.PLAYER1) {
// join an existing game
System.out.println(" not a new game");
G.Players = uta.cse3310.PlayerType.PLAYER2;
G.playersJoined++;
G.playerNames[1] = nickname;
} else if (G.Players == PlayerType.PLAYER2) {
// join an existing game
System.out.println(" not a new game");
G.Players = uta.cse3310.PlayerType.PLAYER3;
G.playersJoined++;
G.playerNames[2] = nickname;
}
else {
// join an existing game
System.out.println(" not a new game");
G.Players = uta.cse3310.PlayerType.PLAYER4;
G.playersJoined++;
G.playerNames[3] = nickname;
}

System.out.println("G.players is " + G.Players);
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/uta/cse3310/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public class GameScreen {
public PlayerType Players;
public String playerNames[];
public int GameId;
private ChatBox chatBox;
private WordBank wordBank;
Expand All @@ -27,6 +28,8 @@ public GameScreen()
// Created by Abubakar Kassim
this.players = new ArrayList<PlayerType>();

playerNames = new String[4];

wordBank = new WordBank();
List<String> wordList = new ArrayList<>();
wordBank.addWordsFromFile("words.txt", wordList);
Expand Down Expand Up @@ -90,6 +93,27 @@ else if (P == PlayerType.PLAYER4)
return retval;
}

public String PlayerToNickname(PlayerType P)
{
String retval = "UNKNOWN";
if (P == PlayerType.PLAYER1) {
retval = playerNames[0];
}
else if (P == PlayerType.PLAYER2)
{
retval = playerNames[1];
}
else if (P == PlayerType.PLAYER3)
{
retval = playerNames[2];
}
else if (P == PlayerType.PLAYER4)
{
retval = playerNames[3];
}
return retval;
}

public void Timer() {

Timer timer = new Timer();
Expand Down Expand Up @@ -183,7 +207,7 @@ else if(U.Button == 9)
{
if(!U.Msg.isEmpty())
{
chatBox.addMessage(U.PlayerIdx + ": " + U.Msg);
chatBox.addMessage(PlayerToNickname(U.PlayerIdx) + ": " + U.Msg);
chatBox.setMessageSent(true);
}
}
Expand Down

0 comments on commit b29f32c

Please sign in to comment.