diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index d6ca92e..793c72e 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -44,12 +44,19 @@ import java.net.InetSocketAddress; import java.util.Vector; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; public abstract class App extends WebSocketServer { // Fields private Vector activeGames; - + private HashMap>> everyAttempt = new HashMap>>(); public App(int port) { super(new InetSocketAddress(port)); this.activeGames = new Vector<>(); @@ -69,8 +76,52 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) { @Override public void onMessage(WebSocket conn, String message) { // Logic for handling websocket message event + System.out.println(conn +": " +message); + + //Bring in data from the webpage + //Take in userEvents and make into an arrayList + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.create(); + UserEvent U = gson.fromJson(message,UserEvent.class); + insertInnerMap(U,everyAttempt); + + + + } + public ArrayList insertEvent(UserEvent U,HashMap> innerMap) + { + //declare list + ArrayList events = innerMap.get(U.player); + //if the arrayList of events is null create it, add data, put it in the innermap + if(events==null) + { + events = new ArrayList(); + events.add(U); + innerMap.put((U.player).getNick(),events); + } + else + { + events.add(U); + } + return events; + } + public void insertInnerMap(UserEvent U,HashMap>> everyAttempt) + { + //declare inner map + HashMap> innerMap =everyAttempt.get(U.gameId); + //if inner map is null create it and add the events,then put it in the outer map + if(innerMap==null) + { + innerMap=new HashMap>(); + innerMap.put(U.player.getNick(),insertEvent(U,innerMap)); + everyAttempt.put(U.gameId,innerMap); + } + else + { + innerMap.put(U.player.getNick(),insertEvent(U,innerMap)); + everyAttempt.put(U.gameId,innerMap); + } } - // Method to update the lobby public void updateLobby() { // Logic for updating the lobby