diff --git a/src/main/java/Team5/SmartTowns/rewards/Sticker.java b/src/main/java/Team5/SmartTowns/rewards/Sticker.java index 274091bdacbaf6199349956be041eb09e34074ff..4fa498c61e7d5748e7d6dcfc26c7c6442dfd8cc4 100644 --- a/src/main/java/Team5/SmartTowns/rewards/Sticker.java +++ b/src/main/java/Team5/SmartTowns/rewards/Sticker.java @@ -15,6 +15,7 @@ public class Sticker { String description; String imgPath; int rarity; //1-5 + boolean hasSticker; public Sticker(int id, String name, String description, int rarity) { this.id = id; @@ -33,6 +34,16 @@ public class Sticker { return imgFile.exists() ? imgPath : notFoundPath; } + public boolean hasSticker(){ + return hasSticker; + } + public void setVisibility(boolean hasSticker){ + this.hasSticker = hasSticker; + } + public String getVisibility(){ + return hasSticker? "" : "grayedOut"; + } + @Override public boolean equals(Object o) { diff --git a/src/main/java/Team5/SmartTowns/users/User.java b/src/main/java/Team5/SmartTowns/users/User.java index 667222393c481b9e2f573ca1773700a93cf7667d..84887664fbee090abb68281f28b5f2b0832af9ae 100644 --- a/src/main/java/Team5/SmartTowns/users/User.java +++ b/src/main/java/Team5/SmartTowns/users/User.java @@ -15,17 +15,20 @@ public class User { String email; //Validation would be done by email, since they will have that String name; String imgPath; + int dragonProgress; Map<Badge, Integer> badgeProgress = new HashMap<>(); // Demonstrates the progress towards a specific badge (0-100) Map<Sticker, Boolean> hasStickers = new HashMap<>(); // True if User has sticker (key) - public User(int id, String email, String name) { + public User(int id, String email, String name, int dragonProgress) { this.id = id; this.email = email; this.name = name; + this.dragonProgress = dragonProgress; imgPath = findImagePath(); } + private String findImagePath(){ /* Finds the image in the Path folder, if image is not found assigns default image */ String imgPath = "images/users/" + id + ".jpg"; diff --git a/src/main/java/Team5/SmartTowns/users/UserController.java b/src/main/java/Team5/SmartTowns/users/UserController.java index e9ba754370320a24c9414c961edca6904b3bc938..e4453bf07e5a26060d661bfa397f44726a9b89d6 100644 --- a/src/main/java/Team5/SmartTowns/users/UserController.java +++ b/src/main/java/Team5/SmartTowns/users/UserController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.servlet.ModelAndView; import java.util.List; +import java.util.Map; @Controller public class UserController { @@ -34,16 +35,21 @@ public class UserController { @GetMapping("/user/{id}") public ModelAndView getUserPage(@PathVariable int id) { - List<Badge> badges = badgesRepository.getAllBadges(); - List<Sticker> stickers = stickersRepository.getAllStickers(); - List<User> users = userRepository.getAllUsers(); ModelAndView mav = new ModelAndView("rewards/userProfile"); - users.stream() - .filter(user -> user.getId() == id) - .findFirst() //Convoluted way of finding the matching user to the id, probably easier to do a hashmap - .ifPresent(result -> mav.addObject("user", result)); - mav.addObject("badges", badges); - mav.addObject("stickers", stickers); +// List<Badge> badges = badgesRepository.getAllBadges(); DEPRECATED FOR THE MOMENT +// mav.addObject("badges", badges); + List<Sticker> allStickers = stickersRepository.getAllStickers(); + List<User> users = userRepository.getAllUsers(); + Map<Long, Boolean> userStickers = userRepository.getStickers(id); + + for (Long stickerID : userStickers.keySet()) { //Finds and updates visibility of stickers based on what the user has + allStickers.stream() + .filter(sticker -> sticker.getId()==stickerID) + .findFirst().ifPresent(sticker -> sticker.setVisibility(userStickers.get(stickerID))); + } + + mav.addObject("user", userRepository.getUser(id)); + mav.addObject("stickers", allStickers); return mav; } } diff --git a/src/main/java/Team5/SmartTowns/users/UserRepository.java b/src/main/java/Team5/SmartTowns/users/UserRepository.java index c994b3dc6a418757f57501a958cb571b542df5cf..9f4e828f17c231179df1a9fcd6a87cc5c8cf75b6 100644 --- a/src/main/java/Team5/SmartTowns/users/UserRepository.java +++ b/src/main/java/Team5/SmartTowns/users/UserRepository.java @@ -2,7 +2,11 @@ package Team5.SmartTowns.users; import java.util.List; +import java.util.Map; public interface UserRepository { List<User> getAllUsers(); +// Map<Long, Integer> getBadgeProgress(int id); + Map<Long, Boolean> getStickers(int id); + User getUser(int id); } diff --git a/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java index ee3a72fb30c5b58291d7caf1ec5242d0df2833b6..9b24461d06d300172d904272edf0cc20c51b99f0 100644 --- a/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java +++ b/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java @@ -5,7 +5,10 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Repository public class UserRepositoryJDBC implements UserRepository{ @@ -23,15 +26,44 @@ public class UserRepositoryJDBC implements UserRepository{ userMapper = (rs, i) -> new User( rs.getInt("userID"), rs.getString("email"), - rs.getString("name") + rs.getString("name"), + rs.getInt("dragonProgress") ); } - - @Override public List<User> getAllUsers(){ String sql= "SELECT * FROM users"; return jdbc.query(sql, userMapper); } + + @Override + public User getUser(int id){ + String sql= "SELECT * FROM users WHERE userID="+id; + List<User> result = jdbc.query(sql, userMapper); + return result.get(0); + } + + @Override + public Map<Long, Boolean> getStickers(int id){ + String sql = "SELECT stickerID, hasSticker FROM stickerprogress WHERE userID=" + id; + List<Map<String, Object>> query = jdbc.queryForList(sql); + Map<Long, Boolean> progress = new HashMap<>(); + for (Map<String, Object> result : query) { + progress.put((Long)result.get("stickerID"), (boolean)result.get("hasSticker")); + } + return progress; + } + + +// @Override +// public Map<Long, Integer> getBadgeProgress(int id){ +// String sql = "SELECT badgeID, progress FROM badgeprogress WHERE userID=" + id; +// List<Map<String, Object>> query = jdbc.queryForList(sql); +// Map<Long, Integer> progress = new HashMap<>(); +// for (Map<String, Object> result : query) { +// progress.put((Long)result.get("badgeID"), (int)result.get("progress")); +// } +// return progress; +// } } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 2c9ea94b36cf320299af1853a10472bd62f5bbc6..9f3110b076d8ca70e953adc1f96f4e3df43417b5 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,5 +1,5 @@ delete from users; -insert into users (userID, email, name) value ('1', 'hannah@gmail.com', 'Hannah'); +insert into users (userID, email, name, dragonProgress) value ('1', 'hannah@gmail.com', 'Hannah', '90'); insert into users (userID, email, name) value ('2', 'nigel@gmail.com', 'Nigel'); delete from trails; @@ -22,4 +22,14 @@ insert into stickers (name, description, rarity) value ('TownConnoisseur', 'You insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1'); insert into stickers (name, description, rarity) value ('TownMaster', 'You visited the town 7 days in a row!', '1'); insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1'); -insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1'); \ No newline at end of file +insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1'); + +delete from badgeprogress; +insert into badgeprogress (userID, badgeID, progress) value ('1', '1', '40'); +insert into badgeprogress (userID, badgeID, progress) value ('1', '2', '70'); +insert into badgeprogress (userID, badgeID, progress) value ('2', '2', '70'); + +delete from stickerprogress; +insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '1', true); +insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '3', true); +insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '2', true); \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index ab83b929297cb5930a4c127550f2b30422c83f7e..70a97a5f5538a50c076ed676413b0bd858f2cdb8 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -4,19 +4,23 @@ create table if not exists trails trailID bigint auto_increment primary key, name varchar(128) ) engine=InnoDB; + drop table if exists locations; create table if not exists locations ( locationID bigint auto_increment primary key, name varchar(128) ) engine=InnoDB; + drop table if exists users; create table if not exists users ( userID bigint auto_increment primary key, email varchar(128), - name varchar(128) + name varchar(128), + dragonProgress int ) engine=InnoDB; + drop table if exists badges; create table if not exists badges ( @@ -25,6 +29,7 @@ create table if not exists badges description varchar(128), difficulty bigint ) engine=InnoDB; + drop table if exists stickers; create table if not exists stickers ( @@ -32,4 +37,19 @@ create table if not exists stickers name varchar(128), description varchar(128), rarity bigint +) engine=InnoDB; + +drop table if exists badgeProgress; +create table if not exists badgeProgress +( + userID bigint, + badgeID bigint, + progress int /*0-100*/ +) engine=InnoDB; + +create table if not exists stickerProgress +( + userID bigint, + stickerID bigint, + hasSticker boolean /*Has sticker or not*/ ) engine=InnoDB; \ No newline at end of file diff --git a/src/main/resources/static/css/userProfile.css b/src/main/resources/static/css/userProfile.css index d59daa0689371b0adb2a204035b6a42d89dadab3..ae3282e5b69e2055d184c93cca4abb56e44fce7f 100644 --- a/src/main/resources/static/css/userProfile.css +++ b/src/main/resources/static/css/userProfile.css @@ -227,4 +227,8 @@ header .footerButton { } header .footerButton:hover { background-color: #36454F; +} + +.grayedOut { + filter: grayscale(1); } \ No newline at end of file diff --git a/src/main/resources/templates/rewards/userProfile.html b/src/main/resources/templates/rewards/userProfile.html index bd502cfc61f804533f7195de53ef812187d3f0ec..cecdb4067d75f9ff828af8bb08b2743764ba186a 100644 --- a/src/main/resources/templates/rewards/userProfile.html +++ b/src/main/resources/templates/rewards/userProfile.html @@ -29,17 +29,17 @@ <!--TODO add some progression info here?--> </div> <section class="rewards"> <!--Reward lists, badges on top, stickers (larger) on the bottom--> - <article id="badgesBar"> - <h2>Your Badges: </h2> <!--Shows first earned badges, followed by greyed out badges--> - <div id="allBadgesContainer" class="centerFlex"> - <img class="badgeImg" th:each="badge : ${badges}" th:src="@{'..' + ${badge.getImgPath()}}" - th:id="'img' + ${badge.getId()}" th:alt="${badge.getName()}" > - </div> - </article> +<!-- <article id="badgesBar">--> +<!-- <h2>Your Badges: </h2> <!–Shows first earned badges, followed by greyed out badges–>--> +<!-- <div id="allBadgesContainer" class="centerFlex">--> +<!-- <img class="badgeImg" th:each="badge : ${badges}" th:src="@{'..' + ${badge.getImgPath()}}"--> +<!-- th:id="'img' + ${badge.getId()}" th:alt="${badge.getName()}" >--> +<!-- </div>--> +<!-- </article>--> <article class="dragonProgression"> <h1>The Dragon Trail</h1> <div class="dragonContainer"> - <div class="dragonFill"> + <div class="dragonFill" th:style="'width:'+ ${user.getDragonProgress()} + '%;'"> <img th:src="@{/images/rewards/dragonFilled.png}" alt="Filled Dragon" id="FilledDragon" class="dragonImg"> </div> @@ -48,12 +48,12 @@ alt="Outline Dragon" id="OutlineDragon" class="dragonImg"> </div> </div> - <h2>40%</h2> + <h2 th:text="${user.getDragonProgress()} + '%'"></h2> </article> <article id="stickersBox"> <!--Need a controller to show earned stickers --> <h2> STICKERS! </h2> <div class="stickersContainer"> - <img class="stickerImg" th:each="sticker : ${stickers}" th:src="@{'../' + ${sticker.getImgPath()}}" + <img th:class="'stickerImg ' + ${sticker.getVisibility()}" th:each="sticker : ${stickers}" th:src="@{'../' + ${sticker.getImgPath()}}" th:id="'img' + ${sticker.getId()}" th:alt="${sticker.getName()}" > </div> </article> @@ -67,7 +67,8 @@ </footer> - +<script> +</script> </body> </html>