Skip to content

Commit

Permalink
get volume level (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
katyaganina authored Sep 5, 2024
1 parent 231ac48 commit bb2646e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.unistuttgart.towercrushbackend.clients;

import de.unistuttgart.towercrushbackend.data.KeybindingDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
* This client sends a Request to the Overworld-Backend at the beginning of the game to retrieve
* keybinding statistics for a player.
*/
@FeignClient(name = "overworldClient", url = "${overworld.url}/players")
public interface OverworldClient {
/**
* Retrieves the keybinding statistics for a specific player.
*
* @param playerId the player id, whose keybinding statistics are to be retrieved
* @param binding the specific keybinding to retrieve statistics
* @param accessToken the users access token
*/
@GetMapping("/{playerId}/keybindings/{binding}")
KeybindingDTO getKeybindingStatistic(
@PathVariable("playerId") final String playerId,
@PathVariable("binding") final String binding,
@CookieValue("access_token") final String accessToken
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public ConfigurationDTO getConfiguration(
return configurationMapper.configurationToConfigurationDTO(configService.getConfiguration(id));
}

@GetMapping("/{id}/volume")
public ConfigurationDTO getAllConfiguration(
@CookieValue("access_token") final String accessToken,
@PathVariable final UUID id
) {
jwtValidatorService.validateTokenOrThrow(accessToken);
log.debug("get configuration {}", id);
return configurationMapper.configurationToConfigurationDTO(
configService.getAllConfigurations(id, accessToken)
);
}

@PostMapping("")
@ResponseStatus(HttpStatus.CREATED)
public ConfigurationDTO createConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class Configuration {

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
Set<Question> questions;
/**
* The volume level that is setted by the player.
*/
Integer volumeLevel;

public Configuration(final Set<Question> questions) {
this.questions = questions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public class ConfigurationDTO {
UUID id;

Set<QuestionDTO> questions;

/**
* The volume level that is setted by the player.
*/
Integer volumeLevel;
public ConfigurationDTO(final Set<QuestionDTO> questions) {
this.questions = questions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.unistuttgart.towercrushbackend.data;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

/**
* The KeybindingDTO.class contains all data that is retrieved from the overworld-backend
*/
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
@NoArgsConstructor
@AllArgsConstructor
public class KeybindingDTO {
private String binding;
private String key;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package de.unistuttgart.towercrushbackend.service;

import de.unistuttgart.towercrushbackend.data.Configuration;
import de.unistuttgart.towercrushbackend.data.ConfigurationDTO;
import de.unistuttgart.towercrushbackend.data.Question;
import de.unistuttgart.towercrushbackend.data.QuestionDTO;
import de.unistuttgart.gamifyit.authentificationvalidator.JWTValidatorService;
import de.unistuttgart.towercrushbackend.clients.OverworldClient;
import de.unistuttgart.towercrushbackend.data.*;
import de.unistuttgart.towercrushbackend.data.mapper.ConfigurationMapper;
import de.unistuttgart.towercrushbackend.data.mapper.QuestionMapper;
import de.unistuttgart.towercrushbackend.repositories.ConfigurationRepository;
Expand Down Expand Up @@ -37,6 +36,13 @@ public class ConfigService {
@Autowired
QuestionRepository questionRepository;

@Autowired
private OverworldClient overworldClient;

@Autowired
private JWTValidatorService jwtValidatorService;


/**
* Search a configuration by given id
*
Expand All @@ -59,6 +65,43 @@ public Configuration getConfiguration(final UUID id) {
);
}

/**
* Search a configuration by given id and get volume level from overworld-backend
*
* @param id the id of the configuration searching for
* @param accessToken the users access token
* @return the found configuration
* @throws ResponseStatusException when configuration by configurationName could not be found
* @throws IllegalArgumentException if at least one of the arguments is null
*/
public Configuration getAllConfigurations(final UUID id, final String accessToken) {
if (id == null) {
throw new IllegalArgumentException("id is null");
}
final String userId = jwtValidatorService.extractUserId(accessToken);

KeybindingDTO keyBindingVolumeLevel = overworldClient.getKeybindingStatistic(userId, "VOLUME_LEVEL", accessToken);
Integer volumeLevel = Integer.parseInt(keyBindingVolumeLevel.getKey());

Configuration config = configurationRepository
.findById(id)
.orElseThrow(() ->
new ResponseStatusException(
HttpStatus.NOT_FOUND,
String.format("There is no configuration with id %s.", id)
)
);
config.setVolumeLevel(volumeLevel);
return configurationRepository
.findById(id)
.orElseThrow(() ->
new ResponseStatusException(
HttpStatus.NOT_FOUND,
String.format("There is no configuration with id %s.", id)
)
);
}

/**
* Save a configuration
*
Expand Down

0 comments on commit bb2646e

Please sign in to comment.