Skip to content
Snippets Groups Projects
Commit e51ddcd1 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch 'feature/addLevelSystemEnd' into 'main'

Feature/add level system end

See merge request idatt2106-v23-03/backend!102
parents eafe4566 fcd6e19c
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/main/resources/db/sql/dbschema.sql" dialect="H2" />
</component>
</project>
\ No newline at end of file
......@@ -67,9 +67,37 @@ public class GroupController {
* @return a ResponseEntity containing the level of the group if it exists, or a 404 if it doesn't
*/
@GetMapping("/group/{groupId}/level")
public ResponseEntity<Integer> getGroupLevel(@PathVariable("groupId") long groupId) {
public ResponseEntity<Long> getGroupLevel(@PathVariable("groupId") long groupId) {
return groupService.getGroupById(groupId)
.map(group -> ResponseEntity.ok(group.getLevel()))
.orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Sets the level of the group identified by the given ID to the level corresponding to the given experience points.
* Returns a ResponseEntity containing the new level of the group, or a 404 Not Found response if no Group with the given ID was found.
*
* @param groupId the ID of the group to update
* @param exp the new experience points of the group
* @return a ResponseEntity containing the new level of the group, or a 404 Not Found response if no Group with the given ID was found
*/
@PutMapping("/group/{groupId}/newLevel/{exp}")
public ResponseEntity<Long> setNewLevel(@PathVariable("groupId") long groupId, @PathVariable("exp") long exp) {
return groupService.setLevelByGroupId(groupId, exp)
.map(group -> ResponseEntity.ok(group.getLevel()))
.orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Returns the progress of the level for the group identified by the given ID.
* Returns a ResponseEntity containing the progress of the current level as a percentage, or a 404 Not Found response if no Group with the given ID was found.
*
* @param groupId the ID of the group to query
* @return a ResponseEntity containing the progress of the current level as a percentage, or a 404 Not Found response if no Group with the given ID was found
*/
@GetMapping("/group/{groupId}/progress")
public ResponseEntity<Integer> getProgressOfLevel(@PathVariable("groupId") long groupId) {
return groupService.getProgressOfLevel(groupId)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
}
......@@ -33,10 +33,10 @@ public class Group {
long groupId;
@Column(name = "level")
int level;
long level;
@Column(name = "points")
int points;
long points;
@Column(name = "group_name")
String groupName;
......
package ntnu.idatt2016.v233.SmartMat.repository.group;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;
......@@ -35,5 +39,12 @@ public interface GroupRepository extends JpaRepository<Group, Long> {
* @param id the id of the group
* @return the level of the group
*/
int getLevelByGroupId(long id);
Optional<Long> getLevelByGroupId(long id);
/**
* Finds a group by group id
* @param id the id of the group
* @return the group with the given id if it exists
*/
Optional<Group> findByGroupId(long id);
}
......@@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.service.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository;
import ntnu.idatt2016.v233.SmartMat.util.LevelUtil;
import org.springframework.stereotype.Service;
import java.util.Optional;
......@@ -60,7 +61,40 @@ public class GroupService {
* @param id the id of the group
* @return the level of the group
*/
public int getLevelByGroupId(long id) {
public Optional<Long> getLevelByGroupId(long id) {
return groupRepository.getLevelByGroupId(id);
}
/**
* Sets the level of the group identified by the given ID to the level corresponding to the given experience points.
*
* @param id the ID of the group to update
* @param exp the new experience points of the group
* @return an Optional containing the updated Group, or an empty Optional if no Group with the given ID was found
*/
public Optional<Group> setLevelByGroupId(long id, long exp) {
Optional<Group> answer = groupRepository.findByGroupId(id);
if (answer.isPresent()) {
Group realGroup = answer.get();
realGroup.setPoints(exp);
realGroup.setLevel(LevelUtil.getLevel(exp));
return Optional.of(groupRepository.save(realGroup));
}
return Optional.empty();
}
/**
* Returns the progress of the level for the group identified by the given ID.
*
* @param id the ID of the group to query
* @return an Optional containing the progress of the current level as a percentage, or an empty Optional if no Group with the given ID was found
*/
public Optional<Integer> getProgressOfLevel(long id) {
Optional<Group> answer = groupRepository.findByGroupId(id);
if (answer.isPresent()) {
Group realGroup = answer.get();
return Optional.of(LevelUtil.getProgressOfLevel(realGroup.getPoints()));
}
return Optional.empty();
}
}
......@@ -4,11 +4,11 @@ package ntnu.idatt2016.v233.SmartMat.util;
// Formula: 10/(e^(-x/3))
public class LevelUtil {
public static int getLevel(int exp){
public static int getLevel(long exp){
return (int) (-3.0 * Math.log(10.0/ ((double)exp)));
}
public static int getProgressOfLevel(int exp) {
public static int getProgressOfLevel(long exp) {
int currentLevel = getLevel(exp);
double expNextLevel = (10.0 / Math.pow(Math.E, (currentLevel+1) / -3.0));
double expCurrentLevel = (10.0 / Math.pow(Math.E, (currentLevel) / -3.0));
......
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