Skip to content
Snippets Groups Projects
Commit 0ea6e994 authored by Gabriel Copat's avatar Gabriel Copat
Browse files

Dynamically update user page to show which stickers the user has acquired out of all available.

(Added required database entries and updated HTML/CSS accordingly)
parent 0bafd3f9
No related branches found
No related tags found
1 merge request!26Resolve "As a client I want gamification of the badges, so that users remain engaged."
......@@ -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) {
......
......@@ -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";
......
......@@ -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;
}
}
......@@ -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);
}
......@@ -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;
// }
}
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
......@@ -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
......@@ -227,4 +227,8 @@ header .footerButton {
}
header .footerButton:hover {
background-color: #36454F;
}
.grayedOut {
filter: grayscale(1);
}
\ No newline at end of file
......@@ -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> &lt;!&ndash;Shows first earned badges, followed by greyed out badges&ndash;&gt;-->
<!-- <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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment