implement gamification badges system with UserBadge and Badge models, and BadgeService for badge management

# Conflicts:
#	.gitignore
#	src/main/java/SuMoC/backend/user/models/User.java
This commit is contained in:
2026-01-06 17:18:24 +01:00
parent bda244a2e8
commit 8c5fff5a3b
7 changed files with 206 additions and 4 deletions
@@ -0,0 +1,13 @@
package SuMoC.backend.gamification.badges;
import SuMoC.backend.gamification.badges.models.Badge;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface BadgeRepository extends JpaRepository<Badge, Long> {
Optional<Badge> findByName(String name);
}
@@ -0,0 +1,83 @@
package SuMoC.backend.gamification.badges;
import SuMoC.backend.gamification.badges.models.Badge;
import SuMoC.backend.gamification.badges.models.UserBadge;
import SuMoC.backend.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Service
public class BadgeService {
@Autowired
private BadgeRepository badgeRepository;
@Autowired
private UserBadgeRepository userBadgeRepository;
public Badge createBadge(String name, String description, Map<String, Integer> criteria) {
if (badgeRepository.findByName(name).isPresent()) {
throw new RuntimeException("Badge existiert bereits");
}
Badge badge = new Badge();
badge.setName(name);
badge.setDescription(description);
badge.setCriteria(criteria);
return badgeRepository.save(badge);
}
public void checkAndAwardBadges(User user) {
List<Badge> badges = badgeRepository.findAll();
for (Badge badge : badges) {
// Prüfe, ob Nutzer schon den Badge hat
boolean alreadyHas = user.getBadges().stream()
.anyMatch(ub -> ub.getBadge().equals(badge));
if (alreadyHas) continue;
// Prüfe Kriterien
boolean qualifies = badge.getCriteria().entrySet().stream()
.allMatch(entry ->
user.getStats().getOrDefault(entry.getKey(), 0) >= entry.getValue()
);
if (qualifies) {
UserBadge userBadge = new UserBadge();
userBadge.setUser(user);
userBadge.setBadge(badge);
userBadge.setAwardedAt(LocalDateTime.now());
userBadgeRepository.save(userBadge);
user.getBadges().add(userBadge);
// TODO: Remove in production
System.out.println("Badge " + badge.getName() + " vergeben an " + user.getPrivateName());
}
}
}
public Badge modifyBadge(Long badgeId, String name, String description, Map<String, Integer> criteria) {
Badge badge = badgeRepository.findById(badgeId)
.orElseThrow(() -> new RuntimeException("Badge nicht gefunden"));
badge.setName(name);
badge.setDescription(description);
badge.setCriteria(criteria);
return badgeRepository.save(badge);
}
public void deleteBadge(Long badgeId) {
Badge badge = badgeRepository.findById(badgeId)
.orElseThrow(() -> new RuntimeException("Badge nicht gefunden"));
badgeRepository.delete(badge);
}
}
@@ -0,0 +1,12 @@
package SuMoC.backend.gamification.badges;
import SuMoC.backend.gamification.badges.models.UserBadge;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserBadgeRepository extends JpaRepository<UserBadge, Integer> {
List<UserBadge> findByUserId(Integer userId);
}
@@ -0,0 +1,37 @@
package SuMoC.backend.gamification.badges.models;
import jakarta.persistence.*;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Badge {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@ElementCollection
@CollectionTable(name = "badge_criteria", joinColumns = @JoinColumn(name = "badge_id"))
@MapKeyColumn(name = "criteria_name")
@Column(name = "criteria_value")
private Map<String, Integer> criteria = new HashMap<>();
// Getter & Setter
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public Map<String, Integer> getCriteria() { return criteria; }
public void setCriteria(Map<String, Integer> criteria) { this.criteria = criteria; }
}
@@ -0,0 +1,36 @@
package SuMoC.backend.gamification.badges.models;
import SuMoC.backend.models.User;
import jakarta.persistence.*;
import java.time.LocalDateTime;
@Entity
public class UserBadge {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private User user;
@ManyToOne
private Badge badge;
private LocalDateTime awardedAt;
// Getter & Setter
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
public Badge getBadge() { return badge; }
public void setBadge(Badge badge) { this.badge = badge; }
public LocalDateTime getAwardedAt() { return awardedAt; }
public void setAwardedAt(LocalDateTime awardedAt) { this.awardedAt = awardedAt; }
}
@@ -1,12 +1,12 @@
package SuMoC.backend.user.models;
import SuMoC.backend.gamification.badges.models.UserBadge;
import SuMoC.backend.trip.models.Trip;
import SuMoC.backend.user.enums.RoleName;
import SuMoC.backend.user.enums.VisibilityPrivacy;
import jakarta.persistence.*;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
@Entity
@Table(name = "users")
@@ -52,8 +52,16 @@ public class User {
@Column(name = "role")
private Set<RoleName> roles;
public User() {
}
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<UserBadge> badges = new ArrayList<>();
@ElementCollection
@CollectionTable(name = "user_stats", joinColumns = @JoinColumn(name = "user_id"))
@MapKeyColumn(name = "stat_name")
@Column(name = "stat_value")
private Map<String, Integer> stats = new HashMap<>();
public User() {}
// public User(Long id, String publicName, String privateName, String email, String password, VisibilityPrivacy visibilityPrivacy, String friendCode, Set<Trip> trips, Set<Friendship> friendships, Set<RoleName> roles) {
// this.id = id;
@@ -77,6 +85,8 @@ public class User {
this.friendCode = java.util.UUID.randomUUID().toString(); // generate unique friend code
this.roles = new HashSet<>();
this.roles.add(RoleName.USER);
this.badges = new ArrayList<>();
this.stats = new HashMap<>();
}
public Long getId() {
@@ -158,4 +168,12 @@ public class User {
public void setRoles(Set<RoleName> roles) {
this.roles = roles;
}
public List<UserBadge> getBadges() { return badges; }
public void setBadges(List<UserBadge> badges) { this.badges = badges; }
public Map<String, Integer> getStats() { return stats;}
public void setStats(Map<String, Integer> stats) { this.stats = stats; }
}