Skip to content
Snippets Groups Projects
Commit 9a703ef4 authored by Sander Østrem Fagernes's avatar Sander Østrem Fagernes
Browse files

Merge branch '35-fix-highscore-frontend' into 'main'

Fixed continuous getLeaderboardUsers method call in view

Closes #35

See merge request !36
parents 9cae767e 5fe761f2
No related branches found
No related tags found
1 merge request!36Fixed continuous getLeaderboardUsers method call in view
Pipeline #214897 passed
...@@ -6,3 +6,4 @@ backend.protocol=http ...@@ -6,3 +6,4 @@ backend.protocol=http
backend.url=http://10.212.26.72 backend.url=http://10.212.26.72
# local development: change backend-url to http://localhost:80 # local development: change backend-url to http://localhost:80
#backend.url=http://localhost:80
...@@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.Array; ...@@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.Json;
import com.game.tankwars.Callback; import com.game.tankwars.Callback;
import com.game.tankwars.ConfigReader; import com.game.tankwars.ConfigReader;
import com.game.tankwars.HTTPRequestHandler;
import com.game.tankwars.ReceiverHandler; import com.game.tankwars.ReceiverHandler;
import com.game.tankwars.TankWarsGame; import com.game.tankwars.TankWarsGame;
import com.game.tankwars.model.User; import com.game.tankwars.model.User;
...@@ -21,8 +22,6 @@ public class LeaderboardController { ...@@ -21,8 +22,6 @@ public class LeaderboardController {
private final TankWarsGame tankWarsGame; private final TankWarsGame tankWarsGame;
private final Button backButton; private final Button backButton;
private final LeaderboardScreen screen; private final LeaderboardScreen screen;
Array<User> leaderboardUsers;
private int retries = 0;
public LeaderboardController(final TankWarsGame tankWarsGame, Button backButton, LeaderboardScreen screen) { public LeaderboardController(final TankWarsGame tankWarsGame, Button backButton, LeaderboardScreen screen) {
this.tankWarsGame = tankWarsGame; this.tankWarsGame = tankWarsGame;
...@@ -47,40 +46,27 @@ public class LeaderboardController { ...@@ -47,40 +46,27 @@ public class LeaderboardController {
} }
public void fetchLeaderboard() { 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 new HTTPRequestHandler(
String url = ConfigReader.getProperty("backend.url") + "/highscores"; new Callback() {
// Create a new HttpRequest using the HttpRequestBuilder class @Override
Net.HttpRequest httpRequest = new HttpRequestBuilder() public void onResult(String result) {
.newRequest() Json json = new Json();
.method(Net.HttpMethods.GET) // Convert the response body to an Array of User objects using the Json instance
.url(url) Array<User> leaderboardUsers = json.fromJson(Array.class, User.class, result);
.build(); screen.setLeaderBoard(leaderboardUsers);
// Send the HttpRequest and pass in the Callback instance to handle the response }
Gdx.net.sendHttpRequest(httpRequest, new ReceiverHandler(callback));
}
public Array<User> getLeaderboard() { @Override
if (retries < 5 && leaderboardUsers == null) { public void onFailed(Throwable t) {
fetchLeaderboard(); screen.setLeaderBoard(null);
} }
return leaderboardUsers; },
new HttpRequestBuilder()
.newRequest()
.url(String.format("%s/highscores", ConfigReader.getProperty("backend.url")))
.method(Net.HttpMethods.GET)
.build())
.sendRequest();
} }
} }
...@@ -14,10 +14,13 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; ...@@ -14,10 +14,13 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.Json;
import com.game.tankwars.Callback; import com.game.tankwars.Callback;
import com.game.tankwars.ConfigReader; import com.game.tankwars.ConfigReader;
import com.game.tankwars.HTTPRequestHandler;
import com.game.tankwars.ReceiverHandler; import com.game.tankwars.ReceiverHandler;
import com.game.tankwars.ResourceManager;
import com.game.tankwars.TankWarsGame; import com.game.tankwars.TankWarsGame;
import com.game.tankwars.model.CurrentUser; import com.game.tankwars.model.CurrentUser;
import com.game.tankwars.model.User; import com.game.tankwars.model.User;
import com.game.tankwars.view.GameScreen;
import com.game.tankwars.view.MainMenuScreen; import com.game.tankwars.view.MainMenuScreen;
...@@ -32,9 +35,8 @@ public class LoginController { ...@@ -32,9 +35,8 @@ public class LoginController {
private final Stage stage; private final Stage stage;
private final TextButton loginButton; private final TextButton loginButton;
private final TextField usernameField; private final TextField usernameField;
private int retries = 0;
private boolean failed = false;
CurrentUser currentUser; CurrentUser currentUser;
Runnable mainMenuScreenTransition;
public LoginController(final TankWarsGame tankWarsGame, TextButton loginButton, TextField usernameField, Stage stage) { public LoginController(final TankWarsGame tankWarsGame, TextButton loginButton, TextField usernameField, Stage stage) {
this.tankWarsGame = tankWarsGame; this.tankWarsGame = tankWarsGame;
...@@ -43,6 +45,13 @@ public class LoginController { ...@@ -43,6 +45,13 @@ public class LoginController {
this.stage = stage; this.stage = stage;
currentUser = getCurrentUser(); currentUser = getCurrentUser();
setEventListeners(); setEventListeners();
mainMenuScreenTransition = new Runnable() {
@Override
public void run() {
ResourceManager.getInstance().clear();
tankWarsGame.setScreen(new MainMenuScreen(tankWarsGame));
}
};
} }
public void setEventListeners() { public void setEventListeners() {
...@@ -91,41 +100,28 @@ public class LoginController { ...@@ -91,41 +100,28 @@ public class LoginController {
} }
public void handleInput(String username) { public void handleInput(String username) {
retries = 0;
failed = false;
fetchUser(username); fetchUser(username);
while (getCurrentUser().getUser() == null && !failed){
}
if (!failed)
tankWarsGame.setScreen(new MainMenuScreen(tankWarsGame));
} }
public void fetchUser(final String username) { public void fetchUser(final String username) {
Callback callback = new Callback() { new HTTPRequestHandler(new Callback() {
@Override @Override
public void onResult(String result) { public void onResult(String result) {
Json json = new Json(); Json json = new Json();
User user = json.fromJson(User.class, result); User user = json.fromJson(User.class, result);
currentUser.setUser(user); currentUser.setUser(user);
Gdx.app.postRunnable(mainMenuScreenTransition);
} }
@Override @Override
public void onFailed(Throwable t) { public void onFailed(Throwable t) {
retries += 1; System.err.println("Login request failed:\n" + t);
if (retries < ReceiverHandler.MAX_RETRIES) {
fetchUser(username);
} else {
failed = true;
}
} }
}; }, new HttpRequestBuilder()
String url = ConfigReader.getProperty("backend.url") + "/user/create/" + username;
Net.HttpRequest httpRequest = new HttpRequestBuilder()
.newRequest() .newRequest()
.method(Net.HttpMethods.POST) .method(Net.HttpMethods.POST)
.url(url) .url(ConfigReader.getProperty("backend.url") + "/user/create/" + username)
.build(); .build()
Gdx.net.sendHttpRequest(httpRequest, new ReceiverHandler(callback)); ).sendRequest();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment