ajout transition entre question

Springboot
Victor SOULIER 1 year ago
parent 2a2b84c55b
commit 70ff5a6fa2

@ -41,7 +41,9 @@ public class PartieKahootController {
@RequestMapping(value = "/{codeInvitation}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/{codeInvitation}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public PartieKahootDTO getPartie(@PathVariable String codeInvitation) { public PartieKahootDTO getPartie(@PathVariable String codeInvitation) {
return this.modelMapper.map(this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation), PartieKahootDTO.class); PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation);
partieKahoot = this.partieKahootService.maintenirAJourQuestionActuel(partieKahoot);
return this.modelMapper.map(partieKahoot, PartieKahootDTO.class);
} }
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ -103,7 +105,6 @@ public class PartieKahootController {
public PartieKahootStatusDTO demarrerPartie(@PathVariable String codeInvitation){ public PartieKahootStatusDTO demarrerPartie(@PathVariable String codeInvitation){
PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation); PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation);
if(partieKahoot.getStatus() == Status.Started){ if(partieKahoot.getStatus() == Status.Started){
throw new PartyAlreadyStartedException(); throw new PartyAlreadyStartedException();
} }
@ -117,9 +118,13 @@ public class PartieKahootController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public PartieKahootQuestionDTO getQuestionActuel(@PathVariable String codeInvitation){ public PartieKahootQuestionDTO getQuestionActuel(@PathVariable String codeInvitation){
PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation); PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation);
if(partieKahoot.getStatus() != Status.Started){ partieKahoot = this.partieKahootService.maintenirAJourQuestionActuel(partieKahoot);
if(partieKahoot.getStatus() == Status.Pending){
throw new PartyNotStartedException(); throw new PartyNotStartedException();
} }
if(partieKahoot.getStatus() == Status.Ended){
throw new PartyIsEndedException();
}
return this.modelMapper.map(partieKahoot, PartieKahootQuestionDTO.class); return this.modelMapper.map(partieKahoot, PartieKahootQuestionDTO.class);
} }
@ -127,6 +132,7 @@ public class PartieKahootController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public ReponseValideDTO repondre(@PathVariable String codeInvitation, @RequestBody @Valid PartieReponse request){ public ReponseValideDTO repondre(@PathVariable String codeInvitation, @RequestBody @Valid PartieReponse request){
PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation); PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation);
partieKahoot = this.partieKahootService.maintenirAJourQuestionActuel(partieKahoot);
Joueur joueur = this.joueurService.findById(request.getIdJoueur()); Joueur joueur = this.joueurService.findById(request.getIdJoueur());
if(partieKahoot.getStatus() != Status.Started){ if(partieKahoot.getStatus() != Status.Started){
@ -137,9 +143,10 @@ public class PartieKahootController {
throw new JoueurPasDansPartieException(); throw new JoueurPasDansPartieException();
} }
Question questionActuel = partieKahoot.getQuestionActuel();
if(partieKahoot.getReponses().stream() if(partieKahoot.getReponses().stream()
.filter(partieReponse -> Objects.equals(partieReponse.getJoueur().getId(), joueur.getId())) .filter(partieReponse -> Objects.equals(partieReponse.getJoueur().getId(), joueur.getId()))
.anyMatch(partieReponse -> Objects.equals(partieReponse.getQuestion().getId(), partieKahoot.getQuestionActuel().getId()))){ .anyMatch(partieReponse -> Objects.equals(partieReponse.getQuestion().getId(), questionActuel.getId()))){
throw new QuestionDejaReponduException(); throw new QuestionDejaReponduException();
} }

@ -0,0 +1,12 @@
package fr.iut.sciencequest.sae.exceptions.partie;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.FORBIDDEN)
public class PartyIsEndedException extends RuntimeException {
public PartyIsEndedException() {
super("Party is ended");
}
}

@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@AllArgsConstructor @AllArgsConstructor
@ -87,4 +88,18 @@ public class PartieKahootService {
return this.update(partieKahoot); return this.update(partieKahoot);
} }
public PartieKahoot maintenirAJourQuestionActuel(PartieKahoot partieKahoot){
if(partieKahoot.getStatus() == Status.Started){
Calendar actualDate = Calendar.getInstance();
actualDate.setTime(new Date());
Question questionActuel = partieKahoot.getQuestionActuel();
boolean toutLeMondeARepondu = partieKahoot.getJoueurs().size() == partieKahoot.getReponses().stream().filter(partieReponse -> Objects.equals(questionActuel.getId(), partieReponse.getQuestion().getId())).count();
if(actualDate.after(partieKahoot.getTempsLimiteReponse()) || toutLeMondeARepondu){
partieKahoot = this.questionSuivante(partieKahoot);
}
}
return partieKahoot;
}
} }

Loading…
Cancel
Save