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

Restructured pictures to their own folders

Added some more functions for this page
parent fdab9bdd
No related branches found
No related tags found
1 merge request!33Resolve "As a QR-scanning connoisseur , I want to unlock stickers after scanning a QR code to feel a sense of reward."
Showing
with 107 additions and 161 deletions
...@@ -11,6 +11,7 @@ public class Pack extends Reward{ ...@@ -11,6 +11,7 @@ public class Pack extends Reward{
public Pack(int id, String name, String description) { public Pack(int id, String name, String description) {
super(id, name, description); super(id, name, description);
displayImg = super.findImagePath();
} }
public void setProgression(List<Sticker> packStickers, List<Sticker> userStickers){ public void setProgression(List<Sticker> packStickers, List<Sticker> userStickers){
......
...@@ -18,7 +18,6 @@ public abstract class Reward { ...@@ -18,7 +18,6 @@ public abstract class Reward {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.description = description; this.description = description;
displayImg = findImagePath();
} }
public abstract String getImgFolder(); public abstract String getImgFolder();
...@@ -32,7 +31,6 @@ public abstract class Reward { ...@@ -32,7 +31,6 @@ public abstract class Reward {
/* Finds the image in the Path folder, if image is not found assigns default image */ /* Finds the image in the Path folder, if image is not found assigns default image */
String imgPath = "images/rewards/" + getImgFolder() + "/" + id + ".png"; String imgPath = "images/rewards/" + getImgFolder() + "/" + id + ".png";
String notFoundPath = "images/rewards/" + getImgFolder() + "/" + getDefaultImg(); String notFoundPath = "images/rewards/" + getImgFolder() + "/" + getDefaultImg();
File imgFile = new File("src/main/resources/static/" + imgPath); File imgFile = new File("src/main/resources/static/" + imgPath);
return imgFile.exists() ? imgPath : notFoundPath; return imgFile.exists() ? imgPath : notFoundPath;
} }
......
package team5.smartTowns.rewards;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import team5.smartTowns.users.User;
import team5.smartTowns.users.UserRepository;
import java.util.List;
import java.util.Map;
@Controller
public class RewardsController {
@Autowired
RewardsRepository rewardsRepository;
@Autowired
UserRepository userRepository;
}
...@@ -7,12 +7,12 @@ import team5.smartTowns.rewards.Sticker; ...@@ -7,12 +7,12 @@ import team5.smartTowns.rewards.Sticker;
import java.util.List; import java.util.List;
public interface RewardsRepository { public interface RewardsRepository {
List<Badge> getAllBadges();
List<Sticker> getAllStickers(); List<Sticker> getAllStickers();
List<Sticker> getAllStickersFromPack(int packID); List<Sticker> getAllStickersFromPack(int packID);
List<Sticker> getAllStickersFromUser(int userID);
List<Pack> getAllPacks(); List<Pack> getAllPacks();
Pack findPackByID(int id); Pack findPackByID(int id);
......
...@@ -17,27 +17,17 @@ public class RewardsRepositoryJDBC implements RewardsRepository { ...@@ -17,27 +17,17 @@ public class RewardsRepositoryJDBC implements RewardsRepository {
public RewardsRepositoryJDBC(JdbcTemplate aJdbc) { public RewardsRepositoryJDBC(JdbcTemplate aJdbc) {
this.jdbc = aJdbc; this.jdbc = aJdbc;
setBadgeMapper();
setStickerMapper(); setStickerMapper();
setPackMapper(); setPackMapper();
} }
private void setBadgeMapper(){
badgeMapper = (rs, i) -> new Badge(
rs.getInt("badgeID"),
rs.getString("name"),
rs.getString("description"),
rs.getInt("difficulty")
);
}
private void setStickerMapper(){ private void setStickerMapper(){
stickerMapper = (rs, i) -> new Sticker( stickerMapper = (rs, i) -> new Sticker(
rs.getInt("id"), rs.getInt("packID"),
rs.getInt("stickerID"),
rs.getString("name"), rs.getString("name"),
rs.getString("description"), rs.getString("description"),
rs.getInt("rarity"), rs.getInt("rarity")
rs.getInt("packID")
); );
} }
private void setPackMapper(){ private void setPackMapper(){
...@@ -61,20 +51,24 @@ public class RewardsRepositoryJDBC implements RewardsRepository { ...@@ -61,20 +51,24 @@ public class RewardsRepositoryJDBC implements RewardsRepository {
} }
@Override @Override
public List<Badge> getAllBadges(){
String sql= "SELECT * FROM badges";
return jdbc.query(sql, badgeMapper);
}
public List<Sticker> getAllStickersFromPack(int packID){ public List<Sticker> getAllStickersFromPack(int packID){
String sql= "SELECT * FROM stickers WHERE packID="+packID; String sql= "SELECT * FROM stickers WHERE packID="+packID;
return jdbc.query(sql, stickerMapper); return jdbc.query(sql, stickerMapper);
} }
@Override
public List<Sticker> getAllStickersFromUser(int userID) {
/* FINDS ALL STICKERS UNLOCKED BY THE GIVEN USER */
String sql= "SELECT * FROM stickers LEFT JOIN stickerprogress " +
"ON (stickers.id, stickers.packID) = (stickerprogress.stickerID, stickerprogress.packID) " +
"WHERE stickerprogress.userID="+userID;
return jdbc.query(sql, stickerMapper);
}
@Override @Override
public Pack findPackByID(int id){ public Pack findPackByID(int id){
String sql= "SELECT * FROM packs WHERE id="+id; String sql= "SELECT * FROM packs WHERE id="+id;
List<Pack> result = jdbc.query(sql, packMapper); List<Pack> result = jdbc.query(sql, packMapper);
return result.get(0); return result.isEmpty() ? null : result.get(0);
} }
} }
...@@ -8,19 +8,22 @@ import lombok.Getter; ...@@ -8,19 +8,22 @@ import lombok.Getter;
public class Sticker extends Reward{ public class Sticker extends Reward{
/* Stickers are randomly earned rewards from a specific pack */ /* Stickers are randomly earned rewards from a specific pack */
int rarity; //1-5 final int rarity; //1-5
final int packID;
boolean hasSticker; boolean hasSticker;
int packID;
public Sticker(int id, String name, String description, int rarity, int packID) {
public Sticker(int packID, int id, String name, String description, int rarity) {
super(id, name, description); super(id, name, description);
this.rarity = rarity; this.rarity = rarity;
this.packID = packID; this.packID = packID;
displayImg = super.findImagePath();
} }
@Override @Override
public String getImgFolder() { public String getImgFolder() {
return "stickers"; return "stickers/" + getPackID();
} }
public boolean hasSticker(){ public boolean hasSticker(){
......
package team5.smartTowns.users; package team5.smartTowns.users;
import lombok.Getter;
import lombok.Setter;
import team5.smartTowns.rewards.Badge; import team5.smartTowns.rewards.Badge;
import team5.smartTowns.rewards.Sticker; import team5.smartTowns.rewards.Sticker;
import lombok.Data; import lombok.Data;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Getter
public class User { public class User {
int id; int id;
...@@ -16,9 +19,6 @@ public class User { ...@@ -16,9 +19,6 @@ public class User {
String name; String name;
String imgPath; String imgPath;
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) {
this.id = id; this.id = id;
this.email = email; this.email = email;
......
package team5.smartTowns.users; package team5.smartTowns.users;
import team5.smartTowns.rewards.Badge;
import team5.smartTowns.rewards.Pack; import team5.smartTowns.rewards.Pack;
import team5.smartTowns.rewards.RewardsRepository; import team5.smartTowns.rewards.RewardsRepository;
import team5.smartTowns.rewards.Sticker; import team5.smartTowns.rewards.Sticker;
...@@ -41,57 +40,51 @@ public class UserController { ...@@ -41,57 +40,51 @@ public class UserController {
@GetMapping("/user/{id}") @GetMapping("/user/{id}")
public ModelAndView getUserPage(@PathVariable int id) { public ModelAndView getUserPage(@PathVariable int id) {
ModelAndView mav = new ModelAndView("users/userProfile"); ModelAndView mav = new ModelAndView("users/userProfile");
List<Sticker> allStickers = rewardsRepository.getAllStickers();
List<Pack> allPacks = rewardsRepository.getAllPacks(); List<Pack> allPacks = rewardsRepository.getAllPacks();
mav.addObject("user", userRepository.getUserById(id));
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("packs", allPacks); mav.addObject("packs", allPacks);
mav.addAllObjects(getPackInfo(id, 1).getModelMap());
mav.addAllObjects(getPackInfo(id, 1).getModelMap());
return mav; return mav;
} }
@GetMapping("/packInfo/{userID}/{packID}") @GetMapping("/packInfo/{userID}/{packID}")
public ModelAndView getPackInfo(@PathVariable int userID, @PathVariable int packID) { public ModelAndView getPackInfo(@PathVariable int userID, @PathVariable int packID) {
/* Displays on page the stickers present in the pack and colour the ones the
* user has acquired */
ModelAndView mav = new ModelAndView("users/userFrags :: stickersBox"); ModelAndView mav = new ModelAndView("users/userFrags :: stickersBox");
List<Sticker> allStickers = rewardsRepository.getAllStickersFromPack(packID); List<Sticker> allStickers = rewardsRepository.getAllStickersFromPack(packID);
Map<Long, Boolean> userStickers = userRepository.getStickers(userID); List<Long> userStickers = userRepository.getUserStickersFromPack(userID, packID);
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(true));
}
mav.addObject("stickers", allStickers);
int progress = getPackProgress(allStickers); mav.addObject("stickers", setStickerVisibility(allStickers, userStickers));
mav.addObject("progress", progress); mav.addObject("progress", getPackProgress(allStickers));
mav.addObject("selectedPack", rewardsRepository.findPackByID(packID)); mav.addObject("selectedPack", rewardsRepository.findPackByID(packID));
return mav; return mav;
} }
public int getPackProgress(List<Sticker> allStickers){ public int getPackProgress(List<Sticker> userStickers){
/* GETS PROGRESS FOR GIVEN PACK*/ /* Returns the % of completion of given userStickers */
double progress = 0; double progress = 0;
try { if (!userStickers.isEmpty()) {
progress = ( progress = userStickers.stream().filter(Sticker::hasSticker).count();
(double) allStickers.stream().filter(Sticker::hasSticker).count() progress = progress / userStickers.size();
/ allStickers.size() ) progress = Math.round(progress * 100);
* 100; }
} catch (ArithmeticException e){ //allStickers is empty return (int) progress;
progress = 0; }
public List<Sticker> setStickerVisibility(List<Sticker> displayedStickers, List<Long> userStickers){
/* Makes displayedStickers which are present in userStickers visible */
for (Long stickerID : userStickers) {
displayedStickers.stream()
.filter(sticker -> sticker.getId()==stickerID) //Tries to find matching id from the two lists
.findFirst().ifPresent(sticker -> sticker.setVisibility(true));
} }
return (int)progress; return displayedStickers;
} }
} }
...@@ -6,7 +6,7 @@ import java.util.Map; ...@@ -6,7 +6,7 @@ import java.util.Map;
public interface UserRepository { public interface UserRepository {
List<User> getAllUsers(); List<User> getAllUsers();
// Map<Long, Integer> getBadgeProgress(int id); List<Long> getUserStickersFromPack(int userID, int packID);
Map<Long, Boolean> getStickers(int id); User getUserById(int userID);
User getUser(int id); boolean unlockSticker(int userID, int packID, int stickerID);
} }
...@@ -4,6 +4,7 @@ package team5.smartTowns.users; ...@@ -4,6 +4,7 @@ package team5.smartTowns.users;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import team5.smartTowns.rewards.Sticker;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -20,7 +21,6 @@ public class UserRepositoryJDBC implements UserRepository{ ...@@ -20,7 +21,6 @@ public class UserRepositoryJDBC implements UserRepository{
setUserMapper(); setUserMapper();
} }
private void setUserMapper(){ private void setUserMapper(){
userMapper = (rs, i) -> new User( userMapper = (rs, i) -> new User(
rs.getInt("id"), rs.getInt("id"),
...@@ -36,33 +36,23 @@ public class UserRepositoryJDBC implements UserRepository{ ...@@ -36,33 +36,23 @@ public class UserRepositoryJDBC implements UserRepository{
} }
@Override @Override
public User getUser(int id){ public User getUserById(int userID){
String sql= "SELECT * FROM users WHERE id="+id; String sql= "SELECT * FROM users WHERE id="+userID;
List<User> result = jdbc.query(sql, userMapper); List<User> result = jdbc.query(sql, userMapper);
return result.get(0); return result.isEmpty() ? null : result.get(0);
} }
@Override @Override
public Map<Long, Boolean> getStickers(int id){ public List<Long> getUserStickersFromPack(int userID, int packID) {
String sql = "SELECT stickerID, hasSticker FROM stickerprogress WHERE userID=" + id; String sql = "SELECT stickerID FROM stickerprogress WHERE (userID, packID)= (" + userID + "," + packID + ")";
List<Map<String, Object>> query = jdbc.queryForList(sql); return jdbc.queryForList(sql, Long.class);
Map<Long, Boolean> progress = new HashMap<>();
for (Map<String, Object> result : query) {
progress.put((Long)result.get("stickerID"), (boolean)result.get("hasSticker"));
}
System.out.println(progress);
return progress;
} }
@Override
// @Override public boolean unlockSticker(int userID, int packID, int stickerID){
// public Map<Long, Integer> getBadgeProgress(int id){ String sql = "INSERT INTO stickerprogress (userID, packID, stickerID) VALUES (" +
// String sql = "SELECT badgeID, progress FROM badgeprogress WHERE userID=" + id; userID + ", " + packID + "," + stickerID + ")";
// List<Map<String, Object>> query = jdbc.queryForList(sql); jdbc.update(sql);
// Map<Long, Integer> progress = new HashMap<>(); return true;
// for (Map<String, Object> result : query) { }
// progress.put((Long)result.get("badgeID"), (int)result.get("progress"));
// }
// return progress;
// }
} }
...@@ -38,41 +38,26 @@ delete from packs; ...@@ -38,41 +38,26 @@ delete from packs;
insert into packs (name, description) value ('Wales Football Team', 'Pack of Welsh Football Players in the National Team'); insert into packs (name, description) value ('Wales Football Team', 'Pack of Welsh Football Players in the National Team');
insert into packs (name, description) value ('Wales Rugby Team', 'Pack of Welsh Rugby Players in the National Team'); insert into packs (name, description) value ('Wales Rugby Team', 'Pack of Welsh Rugby Players in the National Team');
insert into packs (name, description) value ('Welsh Heritage', 'Pack About Welsh Heritage'); insert into packs (name, description) value ('Welsh Heritage', 'Pack About Welsh Heritage');
insert into packs (name, description) value ('Pack3', 'This is pack 2');
insert into packs (name, description) value ('Pack3', 'This is pack 2');
insert into packs (name, description) value ('Pack3', 'This is pack 2');
insert into packs (name, description) value ('Pack3', 'This is pack 2');
insert into packs (name, description) value ('Pack3', 'This is pack 2');
delete from stickers; delete from stickers;
insert into stickers (name, description, rarity, packID) value ('wayne_hennessey', 'Wales Football Team Player', '2', 1); insert into stickers (packID, stickerID, name, description, rarity) value (1, 1, 'wayne_hennessey', 'Wales Football Team Player', '2');
insert into stickers (name, description, rarity, packID) value ('neco_williams', 'Wales Football Team Player', '2', 1); insert into stickers (packID, stickerID, name, description, rarity) value (1, 2, 'neco_williams', 'Wales Football Team Player', '2');
insert into stickers (name, description, rarity, packID) value ('joe_morrell', 'Wales Football Team Player', '2', 1); insert into stickers (packID, stickerID, name, description, rarity) value (1, 3, 'joe_morrell', 'Wales Football Team Player', '2');
insert into stickers (name, description, rarity, packID) value ('ethan_ampadu', 'Wales Football Team Player', '2', 1); insert into stickers (packID, stickerID, name, description, rarity) value (1, 4, 'ethan_ampadu', 'Wales Football Team Player', '2');
insert into stickers (name, description, rarity, packID) value ('connor_roberts', 'Wales Football Team Player', '2', 1); insert into stickers (packID, stickerID, name, description, rarity) value (1, 5, 'connor_roberts', 'Wales Football Team Player', '2');
insert into stickers (name, description, rarity, packID) value ('Taine_Basham', 'Wales Rugby Team Player', '1', 2); insert into stickers (packID, stickerID, name, description, rarity) value (2, 1, 'Taine_Basham', 'Wales Rugby Team Player', '1');
insert into stickers (name, description, rarity, packID) value ('Adam Beard', 'Wales Rugby Team Player', '1', 2); insert into stickers (packID, stickerID, name, description, rarity) value (2, 2, 'Adam Beard', 'Wales Rugby Team Player', '1');
insert into stickers (name, description, rarity, packID) value ('Elliot Dee', 'Wales Rugby Team Player', '1', 2); insert into stickers (packID, stickerID, name, description, rarity) value (2, 3, 'Elliot Dee', 'Wales Rugby Team Player', '1');
insert into stickers (name, description, rarity, packID) value ('Corey Domachowski', 'Wales Rugby Team Player', '1', 2); insert into stickers (packID, stickerID, name, description, rarity) value (2, 4, 'Corey Domachowski', 'Wales Rugby Team Player', '1');
insert into stickers (name, description, rarity, packID) value ('Ryan Elias', 'Wales Rugby Team Player', '1', 2); insert into stickers (packID, stickerID, name, description, rarity) value (2, 5, 'Ryan Elias', 'Wales Rugby Team Player', '1');
insert into stickers (name, description, rarity, packID) value ('Welsh Lady', 'Welsh Heritage', '1', 3); insert into stickers (packID, stickerID, name, description, rarity) value (3, 1, 'Welsh Lady', 'Welsh Heritage', '1');
insert into stickers (name, description, rarity, packID) value ('Welsh Outline', 'Welsh Heritage', '1', 3); insert into stickers (packID, stickerID, name, description, rarity) value (3, 2, 'Welsh Outline', 'Welsh Heritage', '1');
insert into stickers (name, description, rarity, packID) value ('Welsh Spoon', 'Welsh Heritage', '1', 3); insert into stickers (packID, stickerID, name, description, rarity) value (3, 3, 'Welsh Spoon', 'Welsh Heritage', '1');
delete from stickerprogress; delete from stickerprogress;
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '1', true); insert into stickerprogress (userID, packID, stickerID) value (1, 1, 1);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '2', true); insert into stickerprogress (userID, packID, stickerID) value (1, 1, 2);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '3', true); insert into stickerprogress (userID, packID, stickerID) value (1, 1, 3);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '5', true); insert into stickerprogress (userID, packID, stickerID) value (1, 1, 5);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '7', true); insert into stickerprogress (userID, packID, stickerID) value (1, 2, 1);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '9', true); insert into stickerprogress (userID, packID, stickerID) value (1, 2, 3);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '12', true); \ No newline at end of file
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '13', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '2', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '4', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '6', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '7', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '8', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '9', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '10', true);
\ No newline at end of file
...@@ -41,18 +41,22 @@ create table if not exists packs ...@@ -41,18 +41,22 @@ create table if not exists packs
create table if not exists stickers create table if not exists stickers
( (
id bigint auto_increment primary key, id bigint auto_increment primary key,
name varchar(30),
description text,
rarity tinyint,
packID bigint, packID bigint,
FOREIGN KEY (packID) REFERENCES packs(id) FOREIGN KEY (packID) REFERENCES packs(id)
ON DELETE CASCADE ON DELETE CASCADE
ON UPDATE RESTRICT ON UPDATE RESTRICT,
stickerID bigint, /*STICKER ID NUMBER WITHIN ITS OWN PACK*/
name varchar(30),
description text,
rarity tinyint
) engine=InnoDB; ) engine=InnoDB;
create table if not exists stickerProgress create table if not exists stickerProgress
( (
id bigint auto_increment primary key,
userID bigint, userID bigint,
stickerID bigint, packID bigint,
hasSticker boolean /*Has sticker or not*/ stickerID bigint
) engine=InnoDB; ) engine=InnoDB;
...@@ -170,9 +170,9 @@ main { ...@@ -170,9 +170,9 @@ main {
transition: 0.5s ease-in-out 1ms; transition: 0.5s ease-in-out 1ms;
border-radius: 20%; border-radius: 20%;
} }
& .packImg:hover { }
transform: scale(1.5, 1.5) .packImg:hover {
} transform: scale(1.5, 1.5)
} }
.progressionContainer { .progressionContainer {
display: flex; display: flex;
...@@ -271,6 +271,7 @@ and (min-device-width: 1000px) { ...@@ -271,6 +271,7 @@ and (min-device-width: 1000px) {
height: 100%; height: 100%;
overflow: visible; overflow: visible;
border: 5px solid rgba(139, 0, 0, 0.5); border: 5px solid rgba(139, 0, 0, 0.5);
justify-content: space-evenly;
} }
.packImg { .packImg {
height: 100px; height: 100px;
...@@ -278,6 +279,9 @@ and (min-device-width: 1000px) { ...@@ -278,6 +279,9 @@ and (min-device-width: 1000px) {
padding-inline: 1em; padding-inline: 1em;
margin-inline: 3em; margin-inline: 3em;
} }
.packImg:hover{
transform: scale(2,2);
}
.packImg:hover ~ .packName{ .packImg:hover ~ .packName{
visibility: visible; visibility: visible;
opacity: 1; opacity: 1;
......
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