Add leaderboard and fix friend bugs

This commit is contained in:
cugurlu
2019-03-31 00:47:21 +01:00
parent a41b697510
commit ed695f941d
4 changed files with 200 additions and 26 deletions

View File

@@ -28,12 +28,17 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import java.io.IOException; import java.io.IOException;
import java.util.List;
/** /**
* Class that controls the dashboard fxml file (the GUI Screen). * Class that controls the dashboard fxml file (the GUI Screen).
*/ */
@Controller @Controller
public class DashBoardController { public class DashBoardController {
public static ObservableList<Friend> data = FXCollections.observableArrayList();
public ObservableList<Friend> friendLeaderData = FXCollections.observableArrayList();
public ObservableList<Friend> globalLeaderData = FXCollections.observableArrayList();
@Autowired @Autowired
UserService userService; UserService userService;
@@ -76,6 +81,18 @@ public class DashBoardController {
@FXML @FXML
private TableColumn<Friend, Float> scoreColumn; private TableColumn<Friend, Float> scoreColumn;
@FXML @FXML
private TableView<Friend> globalLeaderboard;
@FXML
private TableColumn<Friend, String> globalUser;
@FXML
private TableColumn<Friend, Float> globalScore;
@FXML
private TableView<Friend> friendLeaderboard;
@FXML
private TableColumn<Friend, String> friendUser;
@FXML
private TableColumn<Friend, Float> friendScore;
@FXML
private PieChart pieChart; private PieChart pieChart;
@FXML @FXML
private Label usernameLabel; private Label usernameLabel;
@@ -83,7 +100,7 @@ public class DashBoardController {
/** /**
* Loads the the necessary things before anything else. * Loads the the necessary things before anything else.
*/ */
public void initialize() { public void initialize() throws InterruptedException {
//sets the text of the 'welcome back' text to include the username //sets the text of the 'welcome back' text to include the username
welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!"); welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!");
//adds the slide transition to the menu bar //adds the slide transition to the menu bar
@@ -95,8 +112,11 @@ public class DashBoardController {
friendsButton.setSkin(new MyButtonSkin(friendsButton)); friendsButton.setSkin(new MyButtonSkin(friendsButton));
friendsColumn.setCellValueFactory(new PropertyValueFactory<>("Friend")); friendsColumn.setCellValueFactory(new PropertyValueFactory<>("Friend"));
scoreColumn.setCellValueFactory(new PropertyValueFactory<>("Score")); scoreColumn.setCellValueFactory(new PropertyValueFactory<>("Score"));
friendsTable.setItems(FriendController.getData()); globalUser.setCellValueFactory(new PropertyValueFactory<>("Friend"));
if(pieChart != null) { globalScore.setCellValueFactory(new PropertyValueFactory<>("Score"));
friendUser.setCellValueFactory(new PropertyValueFactory<>("Friend"));
friendScore.setCellValueFactory(new PropertyValueFactory<>("Score"));
if (pieChart != null) {
ObservableList<PieChart.Data> pieChartData = ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList( FXCollections.observableArrayList(
new PieChart.Data("Vegan Meal", 100), new PieChart.Data("Vegan Meal", 100),
@@ -110,6 +130,37 @@ public class DashBoardController {
pieChart.setMaxSize(1000, 1000); pieChart.setMaxSize(1000, 1000);
pieChart.setData(pieChartData); pieChart.setData(pieChartData);
} }
List<String> friendList = userService.getFriendNames(userService.currentUser.getName());
for (int i = 0; i < friendList.size(); i++) {
Friend friend = new Friend(friendList.get(i),
userService.getFootprint(friendList.get(i)));
data.add(friend);
}
friendsTable.setItems(data);
updateLeaderboard();
}
/**
* Sorts the scores of users.
* @param users the list of users
*/
public void sortScores(List<String> users) throws InterruptedException {
for (int i = 0; i < users.size(); i++) {
for (int j = 0; j < users.size(); j++) {
Float firstScore = userService.getFootprint(users.get(i));
Float secondScore = userService.getFootprint(users.get(j));
if (i > j && firstScore < secondScore) {
String temp = users.get(i);
users.set(i, users.get(j));
users.set(j, temp);
}
if (i < j && firstScore > secondScore) {
String temp = users.get(i);
users.set(i, users.get(j));
users.set(j, temp);
}
}
}
} }
/** /**
@@ -117,7 +168,6 @@ public class DashBoardController {
* @param node the node on which the transition needs to act * @param node the node on which the transition needs to act
*/ */
public void addFadeTransition(Node node) { public void addFadeTransition(Node node) {
fadeTrans = new FadeTransition(Duration.millis(400), node); fadeTrans = new FadeTransition(Duration.millis(400), node);
fadeTrans.setFromValue(0); fadeTrans.setFromValue(0);
fadeTrans.setToValue(1.0); fadeTrans.setToValue(1.0);
@@ -129,14 +179,14 @@ public class DashBoardController {
* Displays the dashboard pane. * Displays the dashboard pane.
* @param event the event (clicking the button) * @param event the event (clicking the button)
*/ */
public void displayDashboard(ActionEvent event) { public void displayDashboard(ActionEvent event) throws InterruptedException {
addFadeTransition(dashboardPane); addFadeTransition(dashboardPane);
System.out.println("display dashboard"); System.out.println("display dashboard");
dashboardPane.setVisible(true); dashboardPane.setVisible(true);
userPane.setVisible(false); userPane.setVisible(false);
activitiesPane.setVisible(false); activitiesPane.setVisible(false);
friendsPane.setVisible(false); friendsPane.setVisible(false);
updateLeaderboard();
} }
/** /**
@@ -193,7 +243,7 @@ public class DashBoardController {
* Opens the calculator. * Opens the calculator.
* @throws IOException if the Application doesn't load. * @throws IOException if the Application doesn't load.
*/ */
public void openCalculator() throws IOException { public void openCalculator() throws IOException, InterruptedException {
Parent calc = Application.load(this.getClass().getClassLoader() Parent calc = Application.load(this.getClass().getClassLoader()
.getResource("fxml/calculator.fxml")); .getResource("fxml/calculator.fxml"));
Scene scene = new Scene(calc); Scene scene = new Scene(calc);
@@ -206,6 +256,10 @@ public class DashBoardController {
calcStage.show(); calcStage.show();
} }
/**
* method opend addFriend scene.
* @throws IOException when file is not found
*/
public void openAddFriend() throws IOException { public void openAddFriend() throws IOException {
Parent calc = Application.load(this.getClass().getClassLoader() Parent calc = Application.load(this.getClass().getClassLoader()
.getResource("fxml/AddFriend.fxml")); .getResource("fxml/AddFriend.fxml"));
@@ -216,6 +270,37 @@ public class DashBoardController {
calcStage.show(); calcStage.show();
} }
/**
* Leaderboard is updaating.
* @throws InterruptedException throws exception
*/
public void updateLeaderboard() throws InterruptedException {
friendLeaderboard.getItems().clear();
globalLeaderboard.getItems().clear();
//global leaderboard
globalLeaderData.removeAll();
List<String> userList = userService.getAllUsers();
sortScores(userList);
for (int j = 0; j < userList.size(); j++) {
Friend user = new Friend(userList.get(j), userService.getFootprint(userList.get(j)));
globalLeaderData.add(user);
}
globalLeaderboard.setItems(globalLeaderData);
// friend leaderboard
friendLeaderData.removeAll();
String name = userService.currentUser.getName();
List<String> friendList = userService.getFriendNames(name);
friendList.add(name);
sortScores(friendList);
for (int i = 0; i < friendList.size(); i++) {
Friend friend = new Friend(friendList.get(i),
userService.getFootprint(friendList.get(i)));
friendLeaderData.add(friend);
}
friendLeaderboard.setItems(friendLeaderData);
}
//class for the animations on the navigation buttons //class for the animations on the navigation buttons
public class MyButtonSkin extends ButtonSkin { public class MyButtonSkin extends ButtonSkin {
/** /**

View File

@@ -2,11 +2,11 @@ package greenify.client.controller;
import greenify.client.Friend; import greenify.client.Friend;
import greenify.client.rest.UserService; import greenify.client.rest.UserService;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.Window; import javafx.stage.Window;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +17,8 @@ public class FriendController {
@Autowired @Autowired
UserService userService; UserService userService;
public static ObservableList<Friend> data = FXCollections.observableArrayList(); @Autowired
DashBoardController controller;
@FXML @FXML
private Button addButton; private Button addButton;
@@ -29,7 +30,7 @@ public class FriendController {
* @param event the click of the sign up button * @param event the click of the sign up button
*/ */
@FXML @FXML
public void addFriend(ActionEvent event) { public void addFriend(ActionEvent event) throws InterruptedException {
//set the window to the current window (for displaying the alerts) //set the window to the current window (for displaying the alerts)
Window owner = addButton.getScene().getWindow(); Window owner = addButton.getScene().getWindow();
//check if the username field is empty //check if the username field is empty
@@ -38,19 +39,28 @@ public class FriendController {
UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!", UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!",
"Please enter a username!"); "Please enter a username!");
return; return;
} else if (userNameText.getText().equals(userService.currentUser.getName())) {
UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!",
"Cannot add yourself as a friend!");
return;
} else if (userService.getFriendNames(userService.currentUser.getName())
.contains(userNameText.getText())) {
UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!",
"Cannot add a friend twice!");
return;
} else if (!userService.getAllUsers().contains(userNameText.getText())) {
UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!",
"The user does not exist!");
return;
} }
//add friend to the current user //add friend to the current user
userService.addFriend(userService.currentUser.getName(), userNameText.getText()); userService.addFriend(userService.currentUser.getName(), userNameText.getText());
Friend friend = new Friend(userNameText.getText(), Friend friend = new Friend(userNameText.getText(),
userService.getFootprint(userNameText.getText())); userService.getFootprint(userNameText.getText()));
data.add(friend); DashBoardController.data.add(friend);
//close the register window after the user has entered all the credentials //close the register window after the user has entered all the credentials
Stage current = (Stage) owner; Stage current = (Stage) owner;
current.close(); current.close();
controller.updateLeaderboard();
} }
public static ObservableList<Friend> getData() {
return data;
}
} }

View File

@@ -12,6 +12,8 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import java.util.List;
@Service @Service
public class UserService { public class UserService {
public UserDto currentUser; public UserDto currentUser;
@@ -117,6 +119,42 @@ public class UserService {
return result; return result;
} }
/**
* Saves the footprint of the user.
* @param name name of the user
* @return returns the footprint score
*/
@SuppressWarnings("Duplicates")
public Float saveFootprint(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/saveFootprint")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
Float result = this.restTemplate.getForObject(builder
.build().encode().toUri(), Float.class);
return result;
}
/**
* Gets the friend list of the user.
* @param name name of the user
* @return returns the friend list
*/
@SuppressWarnings("Duplicates")
public List<String> getFriendNames(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFriends")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
List<String> result = this.restTemplate.getForObject(builder
.build().encode().toUri(), List.class);
return result;
}
/** /**
* Adds a friend to the user. * Adds a friend to the user.
* @param name the username of the current user. * @param name the username of the current user.
@@ -134,4 +172,19 @@ public class UserService {
ResponseEntity<String> authenticateResponse = this.restTemplate.getForEntity(builder.build() ResponseEntity<String> authenticateResponse = this.restTemplate.getForEntity(builder.build()
.encode().toUri(), String.class); .encode().toUri(), String.class);
} }
/**
* Gets the list of all users.
*/
@SuppressWarnings("Duplicates")
public List<String> getAllUsers() {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getAllUsers");
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
List<String> result = this.restTemplate.getForObject(builder
.build().encode().toUri(), List.class);
return result;
}
} }

View File

@@ -1,16 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.chart.PieChart?> <?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.Button?>
<?import javafx.scene.image.*?> <?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?> <?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Line?> <?import javafx.scene.shape.Line?>
<?import javafx.scene.text.*?> <?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="702.0" prefWidth="1032.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.DashBoardController"> <AnchorPane prefHeight="702.0" prefWidth="1032.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.DashBoardController">
<children> <children>
<AnchorPane fx:id="menuBar" prefHeight="703.0" prefWidth="216.0" style="-fx-background-color: #5a635c;"> <AnchorPane fx:id="menuBar" prefHeight="703.0" prefWidth="216.0" style="-fx-background-color: #5a635c;">
<children> <children>
@@ -258,6 +262,28 @@
</ImageView> </ImageView>
</graphic> </graphic>
</Button> </Button>
<TableView fx:id="globalLeaderboard" layoutX="56.0" layoutY="220.0" prefHeight="333.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="globalUser" prefWidth="75.0" text="User" />
<TableColumn fx:id="globalScore" prefWidth="75.0" text="Score" />
</columns>
</TableView>
<TableView fx:id="friendLeaderboard" layoutX="302.0" layoutY="220.0" prefHeight="333.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="friendUser" prefWidth="75.0" text="User" />
<TableColumn fx:id="friendScore" prefWidth="75.0" text="Score" />
</columns>
</TableView>
<Label layoutX="69.0" layoutY="177.0" prefHeight="46.0" prefWidth="187.0" text="Global Leaderboard" textAlignment="CENTER" textFill="#5f1616">
<font>
<Font size="20.0" />
</font>
</Label>
<Label layoutX="315.0" layoutY="177.0" prefHeight="46.0" prefWidth="187.0" text="Friends Leaderboard" textAlignment="CENTER" textFill="#5e1515">
<font>
<Font size="20.0" />
</font>
</Label>
</children> </children>
</AnchorPane> </AnchorPane>
<AnchorPane fx:id="friendsPane" layoutX="216.0" prefHeight="703.0" prefWidth="820.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="214.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <AnchorPane fx:id="friendsPane" layoutX="216.0" prefHeight="703.0" prefWidth="820.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="214.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">