Skip to content

Commit

Permalink
testing broadcast game
Browse files Browse the repository at this point in the history
  • Loading branch information
LToothbrush committed Apr 24, 2024
1 parent 783bfe0 commit f2703c4
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 215 deletions.
6 changes: 5 additions & 1 deletion html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,12 @@ <h1>LEADERBOARD</h1>
msg = evt.data;
let obj = JSON.parse(msg);

if('grid' in obj){
console.log(obj.grid);
}

console.log("Message received: " + msg);
console.log("NUM USERS: " + obj.length)
console.log("NUM USERS: " + obj.length);

obj.forEach(function(obj){
console.log("OBJECT: " + obj)
Expand Down
47 changes: 27 additions & 20 deletions src/main/java/com/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public App(int port, Draft_6455 draft){

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {

String filename = "words.txt";
//Read in file of words
ArrayList<String> wordList = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String line;
while((line = br.readLine()) != null)
{
wordList.add(line.trim());
}

}
catch (IOException e)
{
System.err.println("Error reading file:"+ e.getMessage());
}
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Game g = new Game(wordList);
String jsonString = gson.toJson(g);
System.out.println(jsonString);
broadcast(jsonString);

System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");
}

Expand All @@ -61,7 +85,7 @@ public void onMessage(WebSocket conn, String message) {
Gson gson = builder.create();
UserEvent U = gson.fromJson(message, UserEvent.class);
System.out.println(U.UserId + " sent request " + U.request);

if(U.request == 1){
User userRequest = new User(U.UserId);
ActiveUsers.add(userRequest);
Expand All @@ -83,6 +107,7 @@ public void onMessage(WebSocket conn, String message) {
String jsonString = gson.toJson(LobbyUsers);
broadcast(jsonString);
}


/*
// Get our Game Object
Expand Down Expand Up @@ -116,25 +141,7 @@ public void onStart() {
}

public static void main(String[] args) {
String filename = "words.txt";
//Read in file of words
ArrayList<String> wordList = new ArrayList<>();


try(BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String line;
while((line = br.readLine()) != null)
{
wordList.add(line.trim());
}
}
catch (IOException e)
{
System.err.println("Error reading file:"+ e.getMessage());
}
Game G = new Game(wordList);
/*
// Set up the http server
try{
String envPort = System.getenv("HTTP_PORT");
Expand Down Expand Up @@ -167,7 +174,7 @@ public static void main(String[] args) {
catch (NullPointerException e){ // Checks for environment variable
e.printStackTrace();
}
*/



}
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import java.util.Collections;

public class Game {
private ArrayList<User> users;
//public ArrayList<User> users;
public int GameId;
public char[][] grid;
public ArrayList<String> wordBank;

public Game(ArrayList<String> words)
{

grid = generateGrid(words,wordBank);
//users = new ArrayList<User>();
wordBank = new ArrayList<String>();
this.GameId = 0;
this.grid = generateGrid(words,wordBank);
}

public boolean checkVertical(ArrayList<String> selectedLetters,int startX, int startY, int length){
Expand Down Expand Up @@ -49,8 +51,7 @@ public char[][] generateGrid(ArrayList<String> words,ArrayList<String> wordbank)
double density = .67; // density of grid
int maxLength = 10;
int areaFactor = 1;
//Input words into the array
wordBank = new ArrayList<String>();
//Input words into the array
//Take random words from the word list to put inside a word bank
int index = 0;
while(((double)validWordsLetters / (length * width)) < density)
Expand Down Expand Up @@ -132,13 +133,11 @@ public char[][] generateGrid(ArrayList<String> words,ArrayList<String> wordbank)
{

grid[i][j] = alphabet.charAt(rand.nextInt(alphabet.length()));
System.out.printf(" |");
}
else
{

System.out.printf("" + grid[i][j] + "|");

}



}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/cse3310/UserEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ public class UserEvent {
int request;
int GameId;
String UserId;
//int Button[][];
}
Binary file removed target/classes/com/cse3310/App.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/Coordinate.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/Game.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/HttpServer$1.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/HttpServer.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/Lobby.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/Locations.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/ServerEvent.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/User.class
Binary file not shown.
Binary file removed target/classes/com/cse3310/UserEvent.class
Binary file not shown.
Binary file removed target/cse3310-wordsearch.jar
Binary file not shown.
9 changes: 0 additions & 9 deletions target/maven-archiver/pom.properties

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

122 changes: 0 additions & 122 deletions target/surefire-reports/TEST-com.cse3310.AppTest.xml

This file was deleted.

8 changes: 0 additions & 8 deletions target/surefire-reports/com.cse3310.AppTest.txt

This file was deleted.

Binary file removed target/test-classes/com/cse3310/AppTest.class
Binary file not shown.

0 comments on commit f2703c4

Please sign in to comment.