diff --git a/frontend/assets/config.properties b/frontend/assets/config.properties index 0289a246f57e8cac0d1d159b95ff7431c226c438..b523374e7bd341c92960970fc18031239184ca2d 100644 --- a/frontend/assets/config.properties +++ b/frontend/assets/config.properties @@ -6,3 +6,4 @@ backend.protocol=http backend.url=http://10.212.26.72 # local development: change backend-url to http://localhost:80 +#backend.url=http://localhost:80 diff --git a/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java b/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java index 643b8e1c5ebbd89eabac768ea803564933ae94ed..260f67065a99f1e157e80375a696aea86821fbe9 100644 --- a/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java +++ b/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Json; import com.game.tankwars.Callback; import com.game.tankwars.ConfigReader; +import com.game.tankwars.HTTPRequestHandler; import com.game.tankwars.ReceiverHandler; import com.game.tankwars.TankWarsGame; import com.game.tankwars.model.User; @@ -21,8 +22,6 @@ public class LeaderboardController { private final TankWarsGame tankWarsGame; private final Button backButton; private final LeaderboardScreen screen; - Array<User> leaderboardUsers; - private int retries = 0; public LeaderboardController(final TankWarsGame tankWarsGame, Button backButton, LeaderboardScreen screen) { this.tankWarsGame = tankWarsGame; @@ -47,40 +46,27 @@ public class LeaderboardController { } public void fetchLeaderboard() { - // Create a new instance of the Callback interface to handle the response - Callback callback = new Callback() { - // Method called if the response is successful - @Override - public void onResult(String result) { - // Create a new Json instance to parse the response body - Json json = new Json(); - // Convert the response body to an Array of User objects using the Json instance - leaderboardUsers = json.fromJson(Array.class, User.class, result); - } - // Method called if the response fails - @Override - public void onFailed(Throwable t){ - // Increment the number of retries - retries += 1; - } - }; - // Define the URL for the HTTP request - String url = ConfigReader.getProperty("backend.url") + "/highscores"; - // Create a new HttpRequest using the HttpRequestBuilder class - Net.HttpRequest httpRequest = new HttpRequestBuilder() - .newRequest() - .method(Net.HttpMethods.GET) - .url(url) - .build(); - // Send the HttpRequest and pass in the Callback instance to handle the response - Gdx.net.sendHttpRequest(httpRequest, new ReceiverHandler(callback)); - } + new HTTPRequestHandler( + new Callback() { + @Override + public void onResult(String result) { + Json json = new Json(); + // Convert the response body to an Array of User objects using the Json instance + Array<User> leaderboardUsers = json.fromJson(Array.class, User.class, result); + screen.setLeaderBoard(leaderboardUsers); + } - public Array<User> getLeaderboard() { - if (retries < 5 && leaderboardUsers == null) { - fetchLeaderboard(); - } - return leaderboardUsers; + @Override + public void onFailed(Throwable t) { + screen.setLeaderBoard(null); + } + }, + new HttpRequestBuilder() + .newRequest() + .url(String.format("%s/highscores", ConfigReader.getProperty("backend.url"))) + .method(Net.HttpMethods.GET) + .build()) + .sendRequest(); } } diff --git a/frontend/core/src/com/game/tankwars/controller/LoginController.java b/frontend/core/src/com/game/tankwars/controller/LoginController.java index b5ea7ddfaa92580b86741052ec84776c51e14a66..ad8e1e1e1642a8c2df0ce81a368b21f9cd80ca8d 100644 --- a/frontend/core/src/com/game/tankwars/controller/LoginController.java +++ b/frontend/core/src/com/game/tankwars/controller/LoginController.java @@ -14,10 +14,13 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Json; import com.game.tankwars.Callback; import com.game.tankwars.ConfigReader; +import com.game.tankwars.HTTPRequestHandler; import com.game.tankwars.ReceiverHandler; +import com.game.tankwars.ResourceManager; import com.game.tankwars.TankWarsGame; import com.game.tankwars.model.CurrentUser; import com.game.tankwars.model.User; +import com.game.tankwars.view.GameScreen; import com.game.tankwars.view.MainMenuScreen; @@ -32,9 +35,8 @@ public class LoginController { private final Stage stage; private final TextButton loginButton; private final TextField usernameField; - private int retries = 0; - private boolean failed = false; CurrentUser currentUser; + Runnable mainMenuScreenTransition; public LoginController(final TankWarsGame tankWarsGame, TextButton loginButton, TextField usernameField, Stage stage) { this.tankWarsGame = tankWarsGame; @@ -43,6 +45,13 @@ public class LoginController { this.stage = stage; currentUser = getCurrentUser(); setEventListeners(); + mainMenuScreenTransition = new Runnable() { + @Override + public void run() { + ResourceManager.getInstance().clear(); + tankWarsGame.setScreen(new MainMenuScreen(tankWarsGame)); + } + }; } public void setEventListeners() { @@ -91,41 +100,28 @@ public class LoginController { } public void handleInput(String username) { - retries = 0; - failed = false; fetchUser(username); - while (getCurrentUser().getUser() == null && !failed){ - } - if (!failed) - tankWarsGame.setScreen(new MainMenuScreen(tankWarsGame)); } public void fetchUser(final String username) { - Callback callback = new Callback() { + new HTTPRequestHandler(new Callback() { @Override public void onResult(String result) { Json json = new Json(); User user = json.fromJson(User.class, result); currentUser.setUser(user); + Gdx.app.postRunnable(mainMenuScreenTransition); } @Override public void onFailed(Throwable t) { - retries += 1; - if (retries < ReceiverHandler.MAX_RETRIES) { - fetchUser(username); - } else { - failed = true; - } + System.err.println("Login request failed:\n" + t); } - }; - - String url = ConfigReader.getProperty("backend.url") + "/user/create/" + username; - Net.HttpRequest httpRequest = new HttpRequestBuilder() + }, new HttpRequestBuilder() .newRequest() .method(Net.HttpMethods.POST) - .url(url) - .build(); - Gdx.net.sendHttpRequest(httpRequest, new ReceiverHandler(callback)); + .url(ConfigReader.getProperty("backend.url") + "/user/create/" + username) + .build() + ).sendRequest(); } }