Skip to content
Snippets Groups Projects
Commit 5fe761f2 authored by Snorre Skjellestad Kristiansen's avatar Snorre Skjellestad Kristiansen Committed by Sander Østrem Fagernes
Browse files

Fixed continuous getLeaderboardUsers method call in view

parent 9cae767e
No related branches found
No related tags found
1 merge request!36Fixed continuous getLeaderboardUsers method call in view
......@@ -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
......@@ -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();
}
}
......@@ -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();
}
}
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