diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieKahootController.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieKahootController.java index 529be47..cfc869a 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieKahootController.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieKahootController.java @@ -41,7 +41,9 @@ public class PartieKahootController { @RequestMapping(value = "/{codeInvitation}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 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) @@ -103,7 +105,6 @@ public class PartieKahootController { public PartieKahootStatusDTO demarrerPartie(@PathVariable String codeInvitation){ PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation); - if(partieKahoot.getStatus() == Status.Started){ throw new PartyAlreadyStartedException(); } @@ -117,9 +118,13 @@ public class PartieKahootController { @ResponseStatus(HttpStatus.OK) public PartieKahootQuestionDTO getQuestionActuel(@PathVariable String 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(); } + if(partieKahoot.getStatus() == Status.Ended){ + throw new PartyIsEndedException(); + } return this.modelMapper.map(partieKahoot, PartieKahootQuestionDTO.class); } @@ -127,6 +132,7 @@ public class PartieKahootController { @ResponseStatus(HttpStatus.OK) public ReponseValideDTO repondre(@PathVariable String codeInvitation, @RequestBody @Valid PartieReponse request){ PartieKahoot partieKahoot = this.partieKahootService.getPartieKahootByIdOrCodeInvitation(codeInvitation); + partieKahoot = this.partieKahootService.maintenirAJourQuestionActuel(partieKahoot); Joueur joueur = this.joueurService.findById(request.getIdJoueur()); if(partieKahoot.getStatus() != Status.Started){ @@ -137,9 +143,10 @@ public class PartieKahootController { throw new JoueurPasDansPartieException(); } + Question questionActuel = partieKahoot.getQuestionActuel(); if(partieKahoot.getReponses().stream() .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(); } diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/partie/PartyIsEndedException.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/partie/PartyIsEndedException.java new file mode 100644 index 0000000..858ddd1 --- /dev/null +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/partie/PartyIsEndedException.java @@ -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"); + } +} diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieKahootService.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieKahootService.java index 30b5c89..34c5cb3 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieKahootService.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieKahootService.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Service; import java.util.Calendar; import java.util.Date; +import java.util.Objects; import java.util.Optional; @AllArgsConstructor @@ -87,4 +88,18 @@ public class PartieKahootService { 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; + } + }